Spaces:
Runtime error
Runtime error
Update my_agent.py
Browse files- my_agent.py +40 -15
my_agent.py
CHANGED
|
@@ -1,18 +1,41 @@
|
|
|
|
|
|
|
|
| 1 |
import yaml
|
| 2 |
-
from smolagents import OpenAIServerModel, VisitWebpageTool
|
| 3 |
-
from smolagents import CodeAgent, FinalAnswerTool, WikipediaSearchTool, Tool
|
| 4 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 5 |
|
| 6 |
import os
|
| 7 |
|
| 8 |
# Load environment variables from .env file
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
|
|
|
|
|
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
model = OpenAIServerModel(
|
| 18 |
model_id="CLAUDE_3_SONNET_35", # Replace with the desired model ID
|
|
@@ -23,13 +46,13 @@ model = OpenAIServerModel(
|
|
| 23 |
client_kwargs={
|
| 24 |
"default_headers": {"Subscription-Key": os.getenv("SUBSCRIPTION_KEY")}, # Additional headers,
|
| 25 |
},
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
)
|
| 31 |
|
| 32 |
-
#
|
| 33 |
with open("prompts.yaml", 'r') as stream:
|
| 34 |
prompt_templates = yaml.safe_load(stream)
|
| 35 |
|
|
@@ -38,18 +61,20 @@ final_answer = FinalAnswerTool()
|
|
| 38 |
ddg_tool = Tool.from_langchain(DuckDuckGoSearchRun())
|
| 39 |
|
| 40 |
agent = CodeAgent(
|
| 41 |
-
tools=[final_answer, VisitWebpageTool(), WikipediaSearchTool(), ddg_tool],
|
| 42 |
add_base_tools=True,
|
| 43 |
-
max_steps=
|
| 44 |
verbosity_level=1,
|
| 45 |
grammar=None,
|
| 46 |
planning_interval=None,
|
| 47 |
name=None,
|
| 48 |
description=None,
|
| 49 |
prompt_templates=prompt_templates,
|
| 50 |
-
additional_authorized_imports=['pandas', 'numpy', 'openpyxl', 'xlrd', 'os'],
|
| 51 |
model=model)
|
| 52 |
|
|
|
|
|
|
|
| 53 |
|
| 54 |
|
| 55 |
#
|
|
|
|
| 1 |
+
from typing import BinaryIO
|
| 2 |
+
|
| 3 |
import yaml
|
| 4 |
+
from smolagents import OpenAIServerModel, VisitWebpageTool, PythonInterpreterTool, SpeechToTextTool
|
| 5 |
+
from smolagents import CodeAgent, FinalAnswerTool, WikipediaSearchTool, Tool, tool
|
| 6 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 7 |
|
| 8 |
import os
|
| 9 |
|
| 10 |
# Load environment variables from .env file
|
| 11 |
+
with open('.env') as f:
|
| 12 |
+
for line in f:
|
| 13 |
+
if line.strip() and not line.startswith('#'):
|
| 14 |
+
key, value = line.strip().split('=', 1)
|
| 15 |
+
os.environ[key] = value
|
| 16 |
+
|
| 17 |
+
@tool
|
| 18 |
+
def get_file_from_documents(file_name: str) -> BinaryIO:
|
| 19 |
+
"""
|
| 20 |
+
opens and returns a file as a BinaryIO object from the 'documents' directory provided the file name.
|
| 21 |
+
|
| 22 |
+
Args:
|
| 23 |
+
file_name (str): The name of the file to read.
|
| 24 |
|
| 25 |
+
Returns:
|
| 26 |
+
BinaryIO: A file-like object containing the content of the file.
|
| 27 |
|
| 28 |
+
Raises:
|
| 29 |
+
FileNotFoundError: If the file does not exist in the 'documents' directory.
|
| 30 |
+
"""
|
| 31 |
+
documents_dir = os.path.join(os.getcwd(), "documents")
|
| 32 |
+
file_path = os.path.join(documents_dir, file_name)
|
| 33 |
+
|
| 34 |
+
if not os.path.exists(file_path):
|
| 35 |
+
raise FileNotFoundError(f"The file '{file_name}' does not exist in the 'documents' directory.")
|
| 36 |
+
|
| 37 |
+
file = open(file_path, "rb")
|
| 38 |
+
return file
|
| 39 |
|
| 40 |
model = OpenAIServerModel(
|
| 41 |
model_id="CLAUDE_3_SONNET_35", # Replace with the desired model ID
|
|
|
|
| 46 |
client_kwargs={
|
| 47 |
"default_headers": {"Subscription-Key": os.getenv("SUBSCRIPTION_KEY")}, # Additional headers,
|
| 48 |
},
|
| 49 |
+
web_search_options={
|
| 50 |
+
"enabled": True,
|
| 51 |
+
"dynamic_threshold": 0.5
|
| 52 |
+
}
|
| 53 |
)
|
| 54 |
|
| 55 |
+
#Load system prompt from prompt.yaml file
|
| 56 |
with open("prompts.yaml", 'r') as stream:
|
| 57 |
prompt_templates = yaml.safe_load(stream)
|
| 58 |
|
|
|
|
| 61 |
ddg_tool = Tool.from_langchain(DuckDuckGoSearchRun())
|
| 62 |
|
| 63 |
agent = CodeAgent(
|
| 64 |
+
tools=[final_answer, VisitWebpageTool(), WikipediaSearchTool(), ddg_tool, get_file_from_documents, PythonInterpreterTool(), SpeechToTextTool()],
|
| 65 |
add_base_tools=True,
|
| 66 |
+
max_steps=6,
|
| 67 |
verbosity_level=1,
|
| 68 |
grammar=None,
|
| 69 |
planning_interval=None,
|
| 70 |
name=None,
|
| 71 |
description=None,
|
| 72 |
prompt_templates=prompt_templates,
|
| 73 |
+
additional_authorized_imports=['pandas', 'numpy', 'openpyxl', 'xlrd', 'os', ],
|
| 74 |
model=model)
|
| 75 |
|
| 76 |
+
#agent.run("""What is the final numeric output from the attached Python code? (File Name: f918266a-b3e0-4914-865d-4faa564f1aef.py) """)
|
| 77 |
+
|
| 78 |
|
| 79 |
|
| 80 |
#
|