mohantest commited on
Commit
9c3ffd4
·
verified ·
1 Parent(s): 531137b

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +29 -6
agent.py CHANGED
@@ -78,6 +78,25 @@ def web_search(query: str) -> str:
78
  except Exception as e:
79
  return f"Search error: {e}"
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  # ---------- Custom Model using OpenAI ----------
82
  class OpenAIModel(Model):
83
  def __init__(self, model="gpt-3.5-turbo", **kwargs):
@@ -119,20 +138,24 @@ class OpenAIModel(Model):
119
  stop=stop_sequences,
120
  **kwargs
121
  )
122
- # Log response type for debugging
123
  logger.info(f"OpenAI response type: {type(response)}")
124
 
125
- # Extract content and token usage
126
  if hasattr(response, 'choices') and len(response.choices) > 0:
127
  choice = response.choices[0]
128
  if hasattr(choice, 'message'):
129
  message = choice.message
130
  content = message.content if hasattr(message, 'content') else str(message)
131
- # Extract token usage if available
 
132
  token_usage = None
133
  if hasattr(response, 'usage'):
134
- token_usage = response.usage
135
- # Return a proper ChatMessage object
 
 
 
 
 
136
  return ChatMessage(
137
  role="assistant",
138
  content=content,
@@ -149,7 +172,7 @@ class OpenAIModel(Model):
149
  raise Exception(f"OpenAI model {self.model} inference failed: {e}")
150
 
151
  # ---------- Assemble the Agent ----------
152
- tools = [calculator]
153
  try:
154
  import duckduckgo_search
155
  tools.append(web_search)
 
78
  except Exception as e:
79
  return f"Search error: {e}"
80
 
81
+ # New tool to handle document-based questions (placeholder)
82
+ @tool
83
+ def document_qa(document: str, question: str) -> str:
84
+ """
85
+ Answer a question based on a provided document description or URL.
86
+ This is a placeholder – in a real implementation, you would fetch the document
87
+ and use an LLM to answer the question. For now, it returns a default message.
88
+
89
+ Args:
90
+ document: Description or URL of the document.
91
+ question: The question to answer based on the document.
92
+
93
+ Returns:
94
+ An answer string.
95
+ """
96
+ # If the document looks like a URL, we could fetch it and use the LLM to answer.
97
+ # For simplicity, we return a placeholder.
98
+ return f"Document QA is not fully implemented. The question was: '{question}' based on document: {document[:100]}..."
99
+
100
  # ---------- Custom Model using OpenAI ----------
101
  class OpenAIModel(Model):
102
  def __init__(self, model="gpt-3.5-turbo", **kwargs):
 
138
  stop=stop_sequences,
139
  **kwargs
140
  )
 
141
  logger.info(f"OpenAI response type: {type(response)}")
142
 
 
143
  if hasattr(response, 'choices') and len(response.choices) > 0:
144
  choice = response.choices[0]
145
  if hasattr(choice, 'message'):
146
  message = choice.message
147
  content = message.content if hasattr(message, 'content') else str(message)
148
+
149
+ # Extract token usage and convert to smolagents expected format
150
  token_usage = None
151
  if hasattr(response, 'usage'):
152
+ usage = response.usage
153
+ # Map OpenAI's prompt_tokens/completion_tokens to input_tokens/output_tokens
154
+ token_usage = type('TokenUsage', (), {
155
+ 'input_tokens': usage.prompt_tokens if hasattr(usage, 'prompt_tokens') else 0,
156
+ 'output_tokens': usage.completion_tokens if hasattr(usage, 'completion_tokens') else 0
157
+ })()
158
+
159
  return ChatMessage(
160
  role="assistant",
161
  content=content,
 
172
  raise Exception(f"OpenAI model {self.model} inference failed: {e}")
173
 
174
  # ---------- Assemble the Agent ----------
175
+ tools = [calculator, document_qa] # add document_qa to available tools
176
  try:
177
  import duckduckgo_search
178
  tools.append(web_search)