hash-map commited on
Commit
2c9a851
·
verified ·
1 Parent(s): 80268c3

Update rag.py

Browse files
Files changed (1) hide show
  1. rag.py +27 -27
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
- # Load Zephyr model
18
- tokenizer = AutoTokenizer.from_pretrained("HuggingFaceH4/zephyr-7b-beta")
19
- model = AutoModelForCausalLM.from_pretrained("HuggingFaceH4/zephyr-7b-beta")
20
-
21
- # Create HF pipeline
22
- hf_pipeline = pipeline(
23
- "text-generation",
24
- model=model,
25
- tokenizer=tokenizer,
26
- max_new_tokens=128,
27
- temperature=0.8,
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
- prompt_template = ChatPromptTemplate.from_messages([
84
- ("system", "you are game of thrones measter answer the given question strictly based on the context provived.if u donot know the answer reply i dont know donot give gibberish answers"),
85
- ("human", """Context: {context}
 
 
 
 
 
 
 
86
 
87
- Question: {question}
 
 
 
88
 
89
- Rules:
90
- - If the answer isn't in the context, respond with "I don't know"
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"]