Spaces:
Sleeping
Sleeping
Update src/RAGSample.py
Browse files- src/RAGSample.py +43 -7
src/RAGSample.py
CHANGED
@@ -317,6 +317,32 @@ def setup_retriever(use_kaggle_data: bool = False, kaggle_dataset: Optional[str]
|
|
317 |
print("Retriever setup complete.")
|
318 |
return vectorstore.as_retriever(k=4)
|
319 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
320 |
def setup_rag_chain() -> Runnable:
|
321 |
"""Sets up the RAG chain with a prompt template and an LLM."""
|
322 |
# Define the prompt template for the LLM
|
@@ -332,17 +358,27 @@ Answer:
|
|
332 |
input_variables=["question", "documents"],
|
333 |
)
|
334 |
|
335 |
-
#
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
340 |
)
|
341 |
-
|
|
|
|
|
|
|
342 |
# Create a chain combining the prompt template and LLM
|
343 |
return prompt | llm | StrOutputParser()
|
344 |
|
345 |
-
|
346 |
# Define the RAG application class
|
347 |
class RAGApplication:
|
348 |
def __init__(self, retriever: BaseRetriever, rag_chain: Runnable):
|
|
|
317 |
print("Retriever setup complete.")
|
318 |
return vectorstore.as_retriever(k=4)
|
319 |
|
320 |
+
# def setup_rag_chain() -> Runnable:
|
321 |
+
# """Sets up the RAG chain with a prompt template and an LLM."""
|
322 |
+
# # Define the prompt template for the LLM
|
323 |
+
# prompt = PromptTemplate(
|
324 |
+
# template="""You are an assistant for question-answering tasks.
|
325 |
+
# Use the following documents to answer the question.
|
326 |
+
# If you don't know the answer, just say that you don't know.
|
327 |
+
# Use three sentences maximum and keep the answer concise:
|
328 |
+
# Question: {question}
|
329 |
+
# Documents: {documents}
|
330 |
+
# Answer:
|
331 |
+
# """,
|
332 |
+
# input_variables=["question", "documents"],
|
333 |
+
# )
|
334 |
+
|
335 |
+
# # Initialize the LLM with dolphin-llama3:8b model
|
336 |
+
# # Note: This requires the Ollama server to be running with the specified model
|
337 |
+
# llm = ChatOllama(
|
338 |
+
# model="deepseek-ai/DeepSeek-R1-0528-Qwen3-8B",
|
339 |
+
# temperature=0,
|
340 |
+
# )
|
341 |
+
|
342 |
+
# # Create a chain combining the prompt template and LLM
|
343 |
+
# return prompt | llm | StrOutputParser()
|
344 |
+
|
345 |
+
|
346 |
def setup_rag_chain() -> Runnable:
|
347 |
"""Sets up the RAG chain with a prompt template and an LLM."""
|
348 |
# Define the prompt template for the LLM
|
|
|
358 |
input_variables=["question", "documents"],
|
359 |
)
|
360 |
|
361 |
+
# OPTION 1: Use Hugging Face Pipeline (Recommended for HF Spaces)
|
362 |
+
from transformers import pipeline
|
363 |
+
from langchain.llms import HuggingFacePipeline
|
364 |
+
|
365 |
+
# Initialize a local Hugging Face model
|
366 |
+
hf_pipeline = pipeline(
|
367 |
+
"text-generation",
|
368 |
+
model="microsoft/DialoGPT-medium", # Good for Q&A tasks
|
369 |
+
tokenizer="microsoft/DialoGPT-medium",
|
370 |
+
max_length=512,
|
371 |
+
temperature=0.1,
|
372 |
+
device=0 if torch.cuda.is_available() else -1,
|
373 |
+
return_full_text=False
|
374 |
)
|
375 |
+
|
376 |
+
# Wrap it in LangChain
|
377 |
+
llm = HuggingFacePipeline(pipeline=hf_pipeline)
|
378 |
+
|
379 |
# Create a chain combining the prompt template and LLM
|
380 |
return prompt | llm | StrOutputParser()
|
381 |
|
|
|
382 |
# Define the RAG application class
|
383 |
class RAGApplication:
|
384 |
def __init__(self, retriever: BaseRetriever, rag_chain: Runnable):
|