Commit ·
fb8728b
1
Parent(s): 4a8d3f6
updated agent with more tools and added a new tool
Browse files- agent.py +33 -6
- src/tools/reverse_question.py +15 -0
agent.py
CHANGED
|
@@ -3,9 +3,16 @@ import os
|
|
| 3 |
import PIL.Image
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
from loguru import logger
|
| 6 |
-
from smolagents import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
from src.file_handler.parse import parse_file
|
|
|
|
| 9 |
|
| 10 |
load_dotenv()
|
| 11 |
|
|
@@ -18,12 +25,28 @@ class Agent:
|
|
| 18 |
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
|
| 19 |
api_version=os.getenv("OPENAI_API_VERSION"),
|
| 20 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
self.agent = CodeAgent(
|
| 22 |
-
tools=
|
| 23 |
model=model,
|
| 24 |
-
add_base_tools=True, # Add any additional base tools
|
| 25 |
-
# planning_interval=3,
|
| 26 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
logger.info("BasicAgent initialized.")
|
| 28 |
|
| 29 |
def __call__(
|
|
@@ -33,6 +56,7 @@ class Agent:
|
|
| 33 |
f"Agent received question (first 50 chars): {question[:50]}..."
|
| 34 |
)
|
| 35 |
images = None
|
|
|
|
| 36 |
|
| 37 |
if file_name:
|
| 38 |
content = parse_file(task_id, file_name, api_url)
|
|
@@ -42,10 +66,13 @@ class Agent:
|
|
| 42 |
): # Parse content as image
|
| 43 |
images = [content]
|
| 44 |
else: # Append content to question
|
| 45 |
-
|
| 46 |
logger.info(f"Question with content: {question}")
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
answer = self.agent.run(
|
|
|
|
| 49 |
logger.info(f"Agent returning answer: {answer}")
|
| 50 |
return answer
|
| 51 |
|
|
|
|
| 3 |
import PIL.Image
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
from loguru import logger
|
| 6 |
+
from smolagents import (
|
| 7 |
+
AzureOpenAIServerModel,
|
| 8 |
+
CodeAgent,
|
| 9 |
+
GoogleSearchTool,
|
| 10 |
+
PythonInterpreterTool,
|
| 11 |
+
VisitWebpageTool,
|
| 12 |
+
)
|
| 13 |
|
| 14 |
from src.file_handler.parse import parse_file
|
| 15 |
+
from src.tools import reverse_question
|
| 16 |
|
| 17 |
load_dotenv()
|
| 18 |
|
|
|
|
| 25 |
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
|
| 26 |
api_version=os.getenv("OPENAI_API_VERSION"),
|
| 27 |
)
|
| 28 |
+
tools = [
|
| 29 |
+
GoogleSearchTool(provider="serper"),
|
| 30 |
+
VisitWebpageTool(),
|
| 31 |
+
PythonInterpreterTool(),
|
| 32 |
+
reverse_question,
|
| 33 |
+
]
|
| 34 |
self.agent = CodeAgent(
|
| 35 |
+
tools=tools,
|
| 36 |
model=model,
|
|
|
|
|
|
|
| 37 |
)
|
| 38 |
+
self.user_prompt = """
|
| 39 |
+
I will ask you a question.
|
| 40 |
+
Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 41 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
|
| 42 |
+
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.
|
| 43 |
+
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.
|
| 44 |
+
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.
|
| 45 |
+
|
| 46 |
+
Question: {question}
|
| 47 |
+
|
| 48 |
+
Attached content: {content}
|
| 49 |
+
"""
|
| 50 |
logger.info("BasicAgent initialized.")
|
| 51 |
|
| 52 |
def __call__(
|
|
|
|
| 56 |
f"Agent received question (first 50 chars): {question[:50]}..."
|
| 57 |
)
|
| 58 |
images = None
|
| 59 |
+
prompt = self.user_prompt.format(question=question)
|
| 60 |
|
| 61 |
if file_name:
|
| 62 |
content = parse_file(task_id, file_name, api_url)
|
|
|
|
| 66 |
): # Parse content as image
|
| 67 |
images = [content]
|
| 68 |
else: # Append content to question
|
| 69 |
+
prompt = prompt.format(content=content)
|
| 70 |
logger.info(f"Question with content: {question}")
|
| 71 |
+
else:
|
| 72 |
+
prompt = prompt.format(content="")
|
| 73 |
|
| 74 |
+
answer = self.agent.run(prompt, images=images)
|
| 75 |
+
answer = answer.replace("FINAL ANSWER:", "").strip()
|
| 76 |
logger.info(f"Agent returning answer: {answer}")
|
| 77 |
return answer
|
| 78 |
|
src/tools/reverse_question.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import tool
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
@tool
|
| 5 |
+
def reverse_question(question: str) -> str:
|
| 6 |
+
"""
|
| 7 |
+
Reverse the given question. Often useful if the question doesn't make sense.
|
| 8 |
+
|
| 9 |
+
Args:
|
| 10 |
+
question: The question to be reversed.
|
| 11 |
+
|
| 12 |
+
Returns:
|
| 13 |
+
The reversed question.
|
| 14 |
+
"""
|
| 15 |
+
return question[::-1]
|