Spaces:
Sleeping
Sleeping
mohammadreza pakzadian commited on
Commit ·
89b8666
1
Parent(s): afee9d0
use gpt model and update prompt
Browse files
app.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
from smolagents import CodeAgent,
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
| 8 |
from tools.visit_webpage import VisitWebpageTool
|
| 9 |
from tools.web_search import DuckDuckGoSearchTool
|
|
@@ -11,7 +10,6 @@ from tools.web_search import DuckDuckGoSearchTool
|
|
| 11 |
# (Keep Constants as is)
|
| 12 |
# --- Constants ---
|
| 13 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 14 |
-
agent_description = "You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."
|
| 15 |
|
| 16 |
# --- Basic Agent Definition ---
|
| 17 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
@@ -21,25 +19,34 @@ class BasicAgent:
|
|
| 21 |
final_answer = FinalAnswerTool()
|
| 22 |
visit_webpage = VisitWebpageTool()
|
| 23 |
web_search = DuckDuckGoSearchTool()
|
| 24 |
-
model =
|
| 25 |
-
max_tokens=2096,
|
| 26 |
-
temperature=0.5,
|
| 27 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', custom_role_conversions=None,
|
| 28 |
-
)
|
| 29 |
self.agent = CodeAgent(
|
| 30 |
model=model,
|
| 31 |
tools=[visit_webpage, web_search, final_answer],
|
| 32 |
-
max_steps=
|
| 33 |
verbosity_level=1,
|
| 34 |
-
grammar=None,
|
| 35 |
-
planning_interval=None,
|
| 36 |
-
name=None,
|
| 37 |
-
description=agent_description,
|
| 38 |
add_base_tools=True
|
| 39 |
)
|
| 40 |
def __call__(self, question: str) -> str:
|
| 41 |
print(f"Agent received question (first 50 chars): {question}")
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
print(f"Agent returning answer: {answer}")
|
| 44 |
return answer
|
| 45 |
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
+
from smolagents import CodeAgent, OpenAIServerModel
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
from tools.visit_webpage import VisitWebpageTool
|
| 8 |
from tools.web_search import DuckDuckGoSearchTool
|
|
|
|
| 10 |
# (Keep Constants as is)
|
| 11 |
# --- Constants ---
|
| 12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 13 |
|
| 14 |
# --- Basic Agent Definition ---
|
| 15 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
|
| 19 |
final_answer = FinalAnswerTool()
|
| 20 |
visit_webpage = VisitWebpageTool()
|
| 21 |
web_search = DuckDuckGoSearchTool()
|
| 22 |
+
model = OpenAIServerModel(model_id="gpt-4.1", api_key=os.getenv("OPENAI_API_KEY"))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
self.agent = CodeAgent(
|
| 24 |
model=model,
|
| 25 |
tools=[visit_webpage, web_search, final_answer],
|
| 26 |
+
max_steps=3,
|
| 27 |
verbosity_level=1,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
add_base_tools=True
|
| 29 |
)
|
| 30 |
def __call__(self, question: str) -> str:
|
| 31 |
print(f"Agent received question (first 50 chars): {question}")
|
| 32 |
+
prompt = (f"""
|
| 33 |
+
You are a general AI assistant. I will ask you a question.
|
| 34 |
+
Report your thoughts, and finish your answer with the following template: [YOUR FINAL ANSWER]
|
| 35 |
+
[YOUR FINAL ANSWER] should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
|
| 36 |
+
If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise.
|
| 37 |
+
If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
|
| 38 |
+
If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
| 39 |
+
IMPORTANT NOTES TO LIMIT COSTS AND PREVENT ERRORS:
|
| 40 |
+
- Use web search sparingly and only when absolutely necessary.
|
| 41 |
+
- Limit to 1-2 web searches per question.
|
| 42 |
+
- If the search fails due to rate limiting, add a 3-5 second delay using time.sleep() before retrying with a different search term.
|
| 43 |
+
- Do not import libraries that aren't available - stick to basic Python and the tools provided.
|
| 44 |
+
- Focus on answering directly with what you already know when possible.
|
| 45 |
+
- If you have made more than 3 attempts to solve a problem, prioritize providing your best guess.
|
| 46 |
+
- Always add a delay of 2-3 seconds between web searches using time.sleep() to avoid rate limiting.
|
| 47 |
+
Remember to structure your response in Python code format using the final_answer() function.
|
| 48 |
+
The question is: {question}""")
|
| 49 |
+
answer = self.agent.run(prompt)
|
| 50 |
print(f"Agent returning answer: {answer}")
|
| 51 |
return answer
|
| 52 |
|