Spaces:
Sleeping
Sleeping
add some smarts
Browse files- nova_agent.py +38 -10
nova_agent.py
CHANGED
|
@@ -98,10 +98,22 @@ If you cannot access the video content, try to do a search for a video with this
|
|
| 98 |
response_body = json.loads(response['body'].read())
|
| 99 |
answer = response_body['output']['message']['content'][0]['text'].strip()
|
| 100 |
|
| 101 |
-
#
|
| 102 |
-
if
|
| 103 |
-
#
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
return answer
|
| 107 |
|
|
@@ -178,14 +190,31 @@ If you cannot access the video content, try to do a search for a video with this
|
|
| 178 |
return "Unable to analyze Excel data. Please provide the file directly."
|
| 179 |
|
| 180 |
async def _handle_text_question(self, question: str) -> str:
|
| 181 |
-
"""Handle regular text-based questions"""
|
| 182 |
-
#
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
Question: {question}
|
| 186 |
-
|
| 187 |
Answer:"""
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
# Prepare the request payload for Nova Pro
|
| 190 |
payload = {
|
| 191 |
"messages": [
|
|
@@ -201,7 +230,6 @@ Answer:"""
|
|
| 201 |
"temperature": 0.0
|
| 202 |
}
|
| 203 |
}
|
| 204 |
-
|
| 205 |
# Call Nova Pro model
|
| 206 |
response = self.bedrock_client.invoke_model(
|
| 207 |
modelId=self.model_id,
|
|
|
|
| 98 |
response_body = json.loads(response['body'].read())
|
| 99 |
answer = response_body['output']['message']['content'][0]['text'].strip()
|
| 100 |
|
| 101 |
+
# Clean up video responses to be more concise
|
| 102 |
+
if len(answer) > 100:
|
| 103 |
+
# Extract key information
|
| 104 |
+
if '"' in answer:
|
| 105 |
+
# Extract quoted text
|
| 106 |
+
quotes = re.findall(r'"([^"]+)"', answer)
|
| 107 |
+
if quotes:
|
| 108 |
+
return quotes[0]
|
| 109 |
+
# Extract numbers if it's a counting question
|
| 110 |
+
if 'how many' in question.lower() or 'number' in question.lower():
|
| 111 |
+
numbers = re.findall(r'\b\d+\b', answer)
|
| 112 |
+
if numbers:
|
| 113 |
+
return numbers[0]
|
| 114 |
+
# Take first sentence
|
| 115 |
+
sentences = answer.split('. ')
|
| 116 |
+
answer = sentences[0]
|
| 117 |
|
| 118 |
return answer
|
| 119 |
|
|
|
|
| 190 |
return "Unable to analyze Excel data. Please provide the file directly."
|
| 191 |
|
| 192 |
async def _handle_text_question(self, question: str) -> str:
|
| 193 |
+
"""Handle regular text-based questions, with improved handling for fact-based and domain-specific queries"""
|
| 194 |
+
# Check for chemistry LibreText/CK-12 style questions
|
| 195 |
+
if (
|
| 196 |
+
'libretext' in question.lower() or 'ck-12' in question.lower() or 'chemistry' in question.lower() or 'surname' in question.lower()
|
| 197 |
+
):
|
| 198 |
+
# Retrieval-augmented prompt (placeholder for retrieval step)
|
| 199 |
+
retrieval_context = "[RETRIEVED PASSAGES FROM CHEMISTRY MATERIALS WOULD GO HERE]"
|
| 200 |
+
prompt = f"""Use the following context to answer the question. If the answer is not in the context, say 'Not found in provided materials.'
|
| 201 |
+
Context:
|
| 202 |
+
{retrieval_context}
|
| 203 |
|
| 204 |
Question: {question}
|
|
|
|
| 205 |
Answer:"""
|
| 206 |
+
# Check for botanical categorization questions
|
| 207 |
+
elif (
|
| 208 |
+
'vegetable' in question.lower() and 'botany' in question.lower()
|
| 209 |
+
):
|
| 210 |
+
botanical_definition = (
|
| 211 |
+
"In botany, a vegetable is any edible part of a plant that is not a fruit or seed. "
|
| 212 |
+
"Fruits contain seeds and develop from the ovary of a flower. Please use this definition."
|
| 213 |
+
)
|
| 214 |
+
prompt = f"{botanical_definition}\n\n{question}\n\nList only the vegetables, alphabetized, comma separated."
|
| 215 |
+
else:
|
| 216 |
+
# Default concise answer prompt
|
| 217 |
+
prompt = f"""Answer this question directly and concisely. Provide only the essential information requested, not explanations or step-by-step reasoning unless specifically asked.\n\nQuestion: {question}\n\nAnswer:"""
|
| 218 |
# Prepare the request payload for Nova Pro
|
| 219 |
payload = {
|
| 220 |
"messages": [
|
|
|
|
| 230 |
"temperature": 0.0
|
| 231 |
}
|
| 232 |
}
|
|
|
|
| 233 |
# Call Nova Pro model
|
| 234 |
response = self.bedrock_client.invoke_model(
|
| 235 |
modelId=self.model_id,
|