Kackle commited on
Commit
e96e18c
·
verified ·
1 Parent(s): 58e55fe

Update nova_agent.py

Browse files
Files changed (1) hide show
  1. nova_agent.py +50 -20
nova_agent.py CHANGED
@@ -5,9 +5,9 @@ from dotenv import load_dotenv
5
 
6
  load_dotenv()
7
 
8
- class NovaLiteAgent:
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,17 +31,31 @@ class NovaLiteAgent:
31
  self.accept = "application/json"
32
 
33
  async def __call__(self, question: str) -> str:
34
- print(f"NovaLiteAgent received question (first 50 chars): {question[:50]}...")
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,8 +66,8 @@ Answer:"""
52
  }
53
  ],
54
  "inferenceConfig": {
55
- "max_new_tokens": 150,
56
- "temperature": 0.0
57
  }
58
  }
59
 
@@ -69,30 +83,46 @@ Answer:"""
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
 
 
5
 
6
  load_dotenv()
7
 
8
+ class NovaProAgent:
9
  def __init__(self):
10
+ print("NovaProAgent 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"NovaProAgent received question (first 50 chars): {question[:50]}...")
35
 
36
  try:
37
+ # Detect question type and adjust approach
38
+ needs_reasoning = any(keyword in question.lower() for keyword in [
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 Pro
59
  payload = {
60
  "messages": [
61
  {
 
66
  }
67
  ],
68
  "inferenceConfig": {
69
+ "max_new_tokens": max_tokens,
70
+ "temperature": 0.1 if needs_reasoning else 0.0
71
  }
72
  }
73
 
 
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
+ # Handle questions requiring external resources
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", "Based on the information", "According to",
113
+ "The answer is", "Looking at", "Step by step", "Let me think"
 
 
 
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()) > 5:
121
  answer = sentence.strip()
122
  break
123
 
124
+ # Clean up common patterns
125
+ answer = answer.replace('**', '').replace('*', '')
 
 
126
 
127
  return answer
128