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