Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -78,6 +78,12 @@ 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):
|
|
@@ -121,22 +127,25 @@ class OpenAIModel(Model):
|
|
| 121 |
)
|
| 122 |
# Log response type for debugging
|
| 123 |
logger.info(f"OpenAI response type: {type(response)}")
|
| 124 |
-
|
|
|
|
| 125 |
if hasattr(response, 'choices') and len(response.choices) > 0:
|
| 126 |
choice = response.choices[0]
|
| 127 |
if hasattr(choice, 'message'):
|
| 128 |
message = choice.message
|
| 129 |
if hasattr(message, 'content'):
|
| 130 |
-
|
|
|
|
|
|
|
| 131 |
else:
|
| 132 |
logger.error(f"Message has no 'content' attribute: {message}")
|
| 133 |
-
return str(message)
|
| 134 |
else:
|
| 135 |
logger.error(f"Choice has no 'message' attribute: {choice}")
|
| 136 |
-
return str(choice)
|
| 137 |
else:
|
| 138 |
logger.error(f"Unexpected response structure: {response}")
|
| 139 |
-
return str(response)
|
| 140 |
except Exception as e:
|
| 141 |
logger.error(f"OpenAI API error: {e}")
|
| 142 |
raise Exception(f"OpenAI model {self.model} inference failed: {e}")
|
|
|
|
| 78 |
except Exception as e:
|
| 79 |
return f"Search error: {e}"
|
| 80 |
|
| 81 |
+
# ---------- A simple class that mimics smolagents' expected return type ----------
|
| 82 |
+
class AssistantMessage:
|
| 83 |
+
def __init__(self, content: str):
|
| 84 |
+
self.content = content
|
| 85 |
+
self.role = "assistant" # optional, but good to have
|
| 86 |
+
|
| 87 |
# ---------- Custom Model using OpenAI ----------
|
| 88 |
class OpenAIModel(Model):
|
| 89 |
def __init__(self, model="gpt-3.5-turbo", **kwargs):
|
|
|
|
| 127 |
)
|
| 128 |
# Log response type for debugging
|
| 129 |
logger.info(f"OpenAI response type: {type(response)}")
|
| 130 |
+
|
| 131 |
+
# Extract content safely
|
| 132 |
if hasattr(response, 'choices') and len(response.choices) > 0:
|
| 133 |
choice = response.choices[0]
|
| 134 |
if hasattr(choice, 'message'):
|
| 135 |
message = choice.message
|
| 136 |
if hasattr(message, 'content'):
|
| 137 |
+
content = message.content
|
| 138 |
+
# Return an object with a .content attribute
|
| 139 |
+
return AssistantMessage(content)
|
| 140 |
else:
|
| 141 |
logger.error(f"Message has no 'content' attribute: {message}")
|
| 142 |
+
return AssistantMessage(str(message))
|
| 143 |
else:
|
| 144 |
logger.error(f"Choice has no 'message' attribute: {choice}")
|
| 145 |
+
return AssistantMessage(str(choice))
|
| 146 |
else:
|
| 147 |
logger.error(f"Unexpected response structure: {response}")
|
| 148 |
+
return AssistantMessage(str(response))
|
| 149 |
except Exception as e:
|
| 150 |
logger.error(f"OpenAI API error: {e}")
|
| 151 |
raise Exception(f"OpenAI model {self.model} inference failed: {e}")
|