Team Ai
Apppublic

LukaszBergiel/Final_Assignment_Template_OpenAPI

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
agent.py74 linesDownload Raw Back to root
1from smolagents import OpenAIServerModel2from tools.browser import fetch_webpage3from tools.image import analyze_image4from smolagents import ToolCallingAgent5from smolagents import CodeAgent6from smolagents import DuckDuckGoSearchTool, PythonInterpreterTool7 8class BasicAgent:9    def __init__(self, api_key: str = None):10        if not api_key:11            raise ValueError("OpenAI API key is required")12    13        print("BasicAgent initializing with OpenAIServerModel.") 14    15        self.model = OpenAIServerModel(16            model_id="gpt-4o",17            api_key=api_key18        )19        20        self.web_agent = CodeAgent(21            model=self.model,22            tools=[23                DuckDuckGoSearchTool(),24                fetch_webpage,              25            ],26            name="web_agent",27            description="Use search engine to find webpages related to a subject and get the page content",28            additional_authorized_imports=["pandas", "numpy","bs4"],29            verbosity_level=1,    30            max_steps=7,31        )32 33        self.visual_agent = CodeAgent(34            model=self.model,35            tools=[36                analyze_image,37            ],38            name="visual_agent",39            description="An agent for analyzing images",40        )41 42        self.manager_agent = CodeAgent(43            model=self.model,  # Changed from openai_41mini_model to self.model44            tools=[PythonInterpreterTool()],45            managed_agents=[self.web_agent, self.visual_agent],  # Changed audiovideo_agent to visual_agent    46            additional_authorized_imports=["pandas", "numpy","bs4"],47            planning_interval=5,48            verbosity_level=2,49            max_steps=15,50            name="manager_agent",51            description="A manager agent that coordinates the work of other agents to answer questions.",52        )53 54        print("BasicAgent initialized with OpenAIServerModel.")55 56    def __call__(self, question: str) -> str:57        print(f"Agent received question (first 50 chars): {question[:50]}...")58        try:59            prefix = """You are top agent in a multi agent system. You are given a question and you need to answer it. 60            you can access web_agent to get the information from the web. You can use your own knowledge to answer the question.61            You can access visual_agent to get the information from the image.62            You need to respect the fromat that is given to you.63            Finding the correct answer to the question need reasoning and plannig, read the question carrefully, think step by step and do not skip any steps.64            The resposnse should be written as a string. If any iniger is provided parse it to string65                """66            question = prefix + "\nTHE QUESTION:\n" + question + '\n'67 68            response = self.manager_agent.run(question)69 70            print(f"Agent returning answer: {response[:100]}...")71            return response72        except Exception as e:73            print(f"Error getting response from model: {e}")74            return f"Error: {str(e)}"