Update app.py
Browse files
app.py
CHANGED
@@ -80,7 +80,7 @@ async def chat(request: ChatRequest):
|
|
80 |
if request.model_choice == "google":
|
81 |
client = genai.Client(api_key=google_api_key)
|
82 |
|
83 |
-
# summarize chat history
|
84 |
summary_thresh = 10
|
85 |
if len(request.chat_history) > summary_thresh:
|
86 |
summarize_prompt = f"""Please summarize the following chat history concisely, focusing on the key points and main topics discussed. Avoid
|
@@ -98,12 +98,42 @@ async def chat(request: ChatRequest):
|
|
98 |
request.chat_history.insert(1,
|
99 |
{"role": "user",
|
100 |
"parts": [{"text": f"Here is a summary of this conversation so far: {summary_response.text}"}]})
|
101 |
-
|
102 |
|
103 |
-
#
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
docs = "\n\n".join(docs)
|
106 |
|
|
|
107 |
rag_prompt = f"""Use the following information to answer the user's query. You do not have to use all the information, just the pieces that directly
|
108 |
help answer the query most accurately. Start directly with information, NOT with a question, and NOT restating the subject matter of the user query in
|
109 |
any way, or you will be penalized. Respond in a conversational manner.
|
@@ -126,7 +156,7 @@ async def chat(request: ChatRequest):
|
|
126 |
|
127 |
Using the information above, answer the user's query as accurately as possible in the tone and style of the Good Response:
|
128 |
|
129 |
-
User Query: {
|
130 |
"""
|
131 |
|
132 |
# remove the unfformatted user message
|
@@ -149,7 +179,7 @@ async def chat(request: ChatRequest):
|
|
149 |
del request.chat_history[-1]
|
150 |
request.chat_history.append({"role": "user", "parts": [{"text": request.message}]})
|
151 |
|
152 |
-
return {"response": response.text, "dataset_str": text_string, "docs": docs, "filtered_docs_and_scores": filtered_docs_and_scores, "history": request.chat_history, "RAG_prompt": rag_prompt, "chunks": chunks}
|
153 |
|
154 |
if request.model_choice == "HF":
|
155 |
if hf_token:
|
|
|
80 |
if request.model_choice == "google":
|
81 |
client = genai.Client(api_key=google_api_key)
|
82 |
|
83 |
+
# ------------ summarize chat history ------------
|
84 |
summary_thresh = 10
|
85 |
if len(request.chat_history) > summary_thresh:
|
86 |
summarize_prompt = f"""Please summarize the following chat history concisely, focusing on the key points and main topics discussed. Avoid
|
|
|
98 |
request.chat_history.insert(1,
|
99 |
{"role": "user",
|
100 |
"parts": [{"text": f"Here is a summary of this conversation so far: {summary_response.text}"}]})
|
|
|
101 |
|
102 |
+
# ------------ rephrase user question ------------
|
103 |
+
rephrase_prompt = f"""You are an AI that enhances user questions for better retrieval in a Retrieval-Augmented Generation (RAG) system. Given a vague user question, rewrite it as a rich, context-filled question that provides specificity, improves retrieval accuracy, and ensures detailed responses.
|
104 |
+
|
105 |
+
Expand on key concepts related to the original question.
|
106 |
+
|
107 |
+
Add relevant context, such as location, conditions, behavior patterns, or influencing factors.
|
108 |
+
|
109 |
+
Keep the core intent of the user's question but refine it for depth and clarity.
|
110 |
+
|
111 |
+
Example Inputs & Outputs:
|
112 |
+
|
113 |
+
User Input: "Where do bucks bed?"
|
114 |
+
Enhanced Question: "What types of terrain and cover do bucks prefer for bedding, and how do factors like pressure, wind, and thermals influence their bedding locations?"
|
115 |
+
|
116 |
+
User Input: "How do I get success early season?"
|
117 |
+
Enhanced Question: "What key factors contribute to early-season hunting success, including buck bedding habits, food sources, and weather patterns, and how can hunters adjust their strategy to capitalize on predictable movement?"
|
118 |
+
|
119 |
+
Now, transform the following user question into a rich, context-filled question:
|
120 |
+
{request.user_message}"""
|
121 |
+
|
122 |
+
rephrase_response = client.models.generate_content(
|
123 |
+
model="gemini-2.0-flash",
|
124 |
+
contents=rephrase_prompt,
|
125 |
+
config=GenerateContentConfig(
|
126 |
+
system_instruction=["You are a helpful assistant who is an expert at summarization."],
|
127 |
+
max_output_tokens=250,
|
128 |
+
temperature=0.5
|
129 |
+
),
|
130 |
+
)
|
131 |
+
|
132 |
+
# ------------ Retrieve relevant text ------------
|
133 |
+
docs, filtered_docs_and_scores = retrieve(rephrase_response, vectorstore, top_k=8)
|
134 |
docs = "\n\n".join(docs)
|
135 |
|
136 |
+
# ------------ Retrievel Augmented Generation ------------
|
137 |
rag_prompt = f"""Use the following information to answer the user's query. You do not have to use all the information, just the pieces that directly
|
138 |
help answer the query most accurately. Start directly with information, NOT with a question, and NOT restating the subject matter of the user query in
|
139 |
any way, or you will be penalized. Respond in a conversational manner.
|
|
|
156 |
|
157 |
Using the information above, answer the user's query as accurately as possible in the tone and style of the Good Response:
|
158 |
|
159 |
+
User Query: {rephrase_response}
|
160 |
"""
|
161 |
|
162 |
# remove the unfformatted user message
|
|
|
179 |
del request.chat_history[-1]
|
180 |
request.chat_history.append({"role": "user", "parts": [{"text": request.message}]})
|
181 |
|
182 |
+
return {"response": response.text, "dataset_str": text_string, "rephrase_response", rephrase_response, "docs": docs, "filtered_docs_and_scores": filtered_docs_and_scores, "history": request.chat_history, "RAG_prompt": rag_prompt, "chunks": chunks}
|
183 |
|
184 |
if request.model_choice == "HF":
|
185 |
if hf_token:
|