Spaces:
Sleeping
Sleeping
too verbose reverted and removed char limit
Browse files- nova_agent.py +19 -49
nova_agent.py
CHANGED
|
@@ -7,7 +7,7 @@ load_dotenv()
|
|
| 7 |
|
| 8 |
class NovaProAgent:
|
| 9 |
def __init__(self):
|
| 10 |
-
print("
|
| 11 |
|
| 12 |
# Get AWS credentials from environment variables
|
| 13 |
aws_access_key_id = os.getenv('AWS_ACCESS_KEY_ID')
|
|
@@ -31,31 +31,17 @@ class NovaProAgent:
|
|
| 31 |
self.accept = "application/json"
|
| 32 |
|
| 33 |
async def __call__(self, question: str) -> str:
|
| 34 |
-
print(f"
|
| 35 |
|
| 36 |
try:
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
-
'calculate', 'how many', 'what is the', 'find', 'determine', 'solve',
|
| 40 |
-
'table', 'given', 'prove', 'counter-example'
|
| 41 |
-
])
|
| 42 |
-
|
| 43 |
-
if needs_reasoning:
|
| 44 |
-
prompt = f"""You are an expert problem solver. Think step by step to solve this question accurately.
|
| 45 |
-
|
| 46 |
-
Question: {question}
|
| 47 |
-
|
| 48 |
-
Think through this step by step, then provide your final answer:"""
|
| 49 |
-
max_tokens = 300
|
| 50 |
-
else:
|
| 51 |
-
prompt = f"""Answer this question directly and concisely. Provide only the essential information requested.
|
| 52 |
|
| 53 |
Question: {question}
|
| 54 |
|
| 55 |
Answer:"""
|
| 56 |
-
max_tokens = 150
|
| 57 |
|
| 58 |
-
# Prepare the request payload for Nova
|
| 59 |
payload = {
|
| 60 |
"messages": [
|
| 61 |
{
|
|
@@ -66,8 +52,8 @@ Answer:"""
|
|
| 66 |
}
|
| 67 |
],
|
| 68 |
"inferenceConfig": {
|
| 69 |
-
"max_new_tokens":
|
| 70 |
-
"temperature": 0.
|
| 71 |
}
|
| 72 |
}
|
| 73 |
|
|
@@ -83,46 +69,30 @@ Answer:"""
|
|
| 83 |
response_body = json.loads(response['body'].read())
|
| 84 |
answer = response_body['output']['message']['content'][0]['text']
|
| 85 |
|
| 86 |
-
# Clean up the answer
|
| 87 |
answer = answer.strip()
|
| 88 |
|
| 89 |
-
#
|
| 90 |
-
if any(phrase in question.lower() for phrase in [
|
| 91 |
-
'attached', 'video', 'image', 'audio', 'file', 'excel', '.mp3', '.jpg', '.png'
|
| 92 |
-
]):
|
| 93 |
-
if 'video' in question.lower() or 'audio' in question.lower():
|
| 94 |
-
return "I cannot access external media files."
|
| 95 |
-
elif 'image' in question.lower():
|
| 96 |
-
return "I cannot view images."
|
| 97 |
-
elif 'excel' in question.lower() or 'file' in question.lower():
|
| 98 |
-
return "I cannot access attached files."
|
| 99 |
-
|
| 100 |
-
# Extract final answer if reasoning was used
|
| 101 |
-
if needs_reasoning and 'final answer' in answer.lower():
|
| 102 |
-
lines = answer.split('\n')
|
| 103 |
-
for line in reversed(lines):
|
| 104 |
-
if 'final answer' in line.lower() or 'answer:' in line.lower():
|
| 105 |
-
# Extract the part after the colon
|
| 106 |
-
if ':' in line:
|
| 107 |
-
answer = line.split(':', 1)[1].strip()
|
| 108 |
-
break
|
| 109 |
-
|
| 110 |
-
# Remove verbose beginnings
|
| 111 |
verbose_starts = [
|
| 112 |
-
"To answer this question",
|
| 113 |
-
"
|
|
|
|
|
|
|
|
|
|
| 114 |
]
|
| 115 |
|
| 116 |
for start in verbose_starts:
|
| 117 |
if answer.lower().startswith(start.lower()):
|
| 118 |
sentences = answer.split('. ')
|
| 119 |
for sentence in sentences[1:]:
|
| 120 |
-
if len(sentence.strip()) >
|
| 121 |
answer = sentence.strip()
|
| 122 |
break
|
| 123 |
|
| 124 |
-
#
|
| 125 |
-
|
|
|
|
|
|
|
| 126 |
|
| 127 |
return answer
|
| 128 |
|
|
|
|
| 7 |
|
| 8 |
class NovaProAgent:
|
| 9 |
def __init__(self):
|
| 10 |
+
print("NovaLiteAgent initialized.")
|
| 11 |
|
| 12 |
# Get AWS credentials from environment variables
|
| 13 |
aws_access_key_id = os.getenv('AWS_ACCESS_KEY_ID')
|
|
|
|
| 31 |
self.accept = "application/json"
|
| 32 |
|
| 33 |
async def __call__(self, question: str) -> str:
|
| 34 |
+
print(f"NovaLiteAgent received question (first 50 chars): {question}...")
|
| 35 |
|
| 36 |
try:
|
| 37 |
+
# Create a more focused prompt for concise answers
|
| 38 |
+
prompt = f"""Answer this question directly and concisely. Provide only the essential information requested, not explanations or step-by-step reasoning unless specifically asked.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
Question: {question}
|
| 41 |
|
| 42 |
Answer:"""
|
|
|
|
| 43 |
|
| 44 |
+
# Prepare the request payload for Nova Lite
|
| 45 |
payload = {
|
| 46 |
"messages": [
|
| 47 |
{
|
|
|
|
| 52 |
}
|
| 53 |
],
|
| 54 |
"inferenceConfig": {
|
| 55 |
+
"max_new_tokens": 250,
|
| 56 |
+
"temperature": 0.0
|
| 57 |
}
|
| 58 |
}
|
| 59 |
|
|
|
|
| 69 |
response_body = json.loads(response['body'].read())
|
| 70 |
answer = response_body['output']['message']['content'][0]['text']
|
| 71 |
|
| 72 |
+
# Clean up the answer - remove verbose patterns
|
| 73 |
answer = answer.strip()
|
| 74 |
|
| 75 |
+
# Remove common verbose beginnings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
verbose_starts = [
|
| 77 |
+
"To answer this question",
|
| 78 |
+
"Based on the information",
|
| 79 |
+
"According to",
|
| 80 |
+
"The answer is",
|
| 81 |
+
"Looking at"
|
| 82 |
]
|
| 83 |
|
| 84 |
for start in verbose_starts:
|
| 85 |
if answer.lower().startswith(start.lower()):
|
| 86 |
sentences = answer.split('. ')
|
| 87 |
for sentence in sentences[1:]:
|
| 88 |
+
if len(sentence.strip()) > 10:
|
| 89 |
answer = sentence.strip()
|
| 90 |
break
|
| 91 |
|
| 92 |
+
# Limit length
|
| 93 |
+
if len(answer) > 200:
|
| 94 |
+
sentences = answer.split('. ')
|
| 95 |
+
answer = sentences[0] + '.'
|
| 96 |
|
| 97 |
return answer
|
| 98 |
|