Update rag.py
Browse files
rag.py
CHANGED
|
@@ -14,22 +14,18 @@ from langchain_community.embeddings import HuggingFaceEmbeddings
|
|
| 14 |
from langchain_community.vectorstores import FAISS
|
| 15 |
from langchain_community.retrievers import BM25Retriever
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
pad_token_id=tokenizer.eos_token_id,
|
| 29 |
-
)
|
| 30 |
|
| 31 |
-
# Wrap in LangChain LLM
|
| 32 |
-
llm = HuggingFacePipeline(pipeline=hf_pipeline)
|
| 33 |
|
| 34 |
# Define your RAG response function
|
| 35 |
|
|
@@ -80,21 +76,25 @@ def respond_rag_huggingface(message: str):
|
|
| 80 |
docs = ensemble_retriever(message)
|
| 81 |
context = "\n\n".join(doc.page_content for doc in docs)
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
- Keep answers under 5 sentences
|
| 92 |
-
- Include book/season references when possible""")
|
| 93 |
-
])
|
| 94 |
|
| 95 |
-
chain = prompt_template | llm
|
| 96 |
-
response = chain.invoke({"context": context, "question": message})
|
| 97 |
-
return response.content
|
| 98 |
|
| 99 |
|
| 100 |
__all__ = ["respond_rag_huggingface"]
|
|
|
|
| 14 |
from langchain_community.vectorstores import FAISS
|
| 15 |
from langchain_community.retrievers import BM25Retriever
|
| 16 |
|
| 17 |
+
import google.generativeai as genai
|
| 18 |
+
import os
|
| 19 |
+
import google.generativeai as genai
|
| 20 |
+
|
| 21 |
+
# Initialize Gemini
|
| 22 |
+
genai.configure(api_key=os.environ.get("GEMINI_API_KEY")) # Replace this with actual key or environment-safe config
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 27 |
+
|
|
|
|
|
|
|
| 28 |
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Define your RAG response function
|
| 31 |
|
|
|
|
| 76 |
docs = ensemble_retriever(message)
|
| 77 |
context = "\n\n".join(doc.page_content for doc in docs)
|
| 78 |
|
| 79 |
+
system_message = os.environ.get("SYSTEM_MESSAGE",
|
| 80 |
+
"You are a Game of Thrones measter. Answer the given question strictly based on the provided context. If you don't know, say 'I don't know'. Do not guess.")
|
| 81 |
+
|
| 82 |
+
prompt = f"""{system_message}
|
| 83 |
+
|
| 84 |
+
Context:
|
| 85 |
+
{context}
|
| 86 |
+
|
| 87 |
+
Question:
|
| 88 |
+
{message}
|
| 89 |
|
| 90 |
+
Rules:
|
| 91 |
+
- If the answer isn't in the context, respond with "I don't know"
|
| 92 |
+
- Keep answers under 5 sentences
|
| 93 |
+
- Include book/season references when possible"""
|
| 94 |
|
| 95 |
+
response = model.generate_content(prompt)
|
| 96 |
+
return response.text
|
|
|
|
|
|
|
|
|
|
| 97 |
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
|
| 100 |
__all__ = ["respond_rag_huggingface"]
|