Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool,
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
|
| 5 |
# Load environment variables (API keys)
|
|
@@ -14,13 +14,20 @@ class CustomAgent:
|
|
| 14 |
# Using OpenAI gpt-4o-mini as requested/seen in logs
|
| 15 |
self.model = OpenAIModel(model_id="gpt-4o-mini", api_key=api_key)
|
| 16 |
else:
|
| 17 |
-
#
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
self.search_tool = DuckDuckGoSearchTool()
|
| 21 |
|
| 22 |
# Initialize the agent
|
| 23 |
-
# CodeAgent is the best choice for the final project
|
| 24 |
self.agent = CodeAgent(
|
| 25 |
tools=[self.search_tool],
|
| 26 |
model=self.model,
|
|
@@ -42,7 +49,6 @@ class CustomAgent:
|
|
| 42 |
result = self.agent.run(question)
|
| 43 |
|
| 44 |
# CRITICAL: Always return a string
|
| 45 |
-
# If the result is a list (which matches your error), this converts it
|
| 46 |
if isinstance(result, list):
|
| 47 |
return " ".join(map(str, result))
|
| 48 |
|
|
|
|
| 1 |
import os
|
| 2 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIModel
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
|
| 5 |
# Load environment variables (API keys)
|
|
|
|
| 14 |
# Using OpenAI gpt-4o-mini as requested/seen in logs
|
| 15 |
self.model = OpenAIModel(model_id="gpt-4o-mini", api_key=api_key)
|
| 16 |
else:
|
| 17 |
+
# If no OpenAI key, we can use a Hugging Face model
|
| 18 |
+
# Note: HfApiModel has been renamed to LiteLLMModel or InferenceClientModel in some versions.
|
| 19 |
+
# However, since you are using OpenAI gpt-4o-mini, let's stick to that.
|
| 20 |
+
# If we need a HF fallback, we import InferenceClientModel inside
|
| 21 |
+
try:
|
| 22 |
+
from smolagents import LiteLLMModel
|
| 23 |
+
self.model = LiteLLMModel(model_id="huggingface/Qwen/Qwen2.5-Coder-32B-Instruct")
|
| 24 |
+
except ImportError:
|
| 25 |
+
# If LiteLLMModel is also not found, use whatever is available
|
| 26 |
+
raise Exception("OPENAI_API_KEY not found and fallback model could not be initialized.")
|
| 27 |
|
| 28 |
self.search_tool = DuckDuckGoSearchTool()
|
| 29 |
|
| 30 |
# Initialize the agent
|
|
|
|
| 31 |
self.agent = CodeAgent(
|
| 32 |
tools=[self.search_tool],
|
| 33 |
model=self.model,
|
|
|
|
| 49 |
result = self.agent.run(question)
|
| 50 |
|
| 51 |
# CRITICAL: Always return a string
|
|
|
|
| 52 |
if isinstance(result, list):
|
| 53 |
return " ".join(map(str, result))
|
| 54 |
|