rodo61/api-test
0
1from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel, load_tool, tool, OpenAIServerModel2import os3import datetime4import requests5import pytz6import yaml7from tools.final_answer import FinalAnswerTool8from Gradio_UI import GradioUI9 10 11# Below is an example of a tool that does nothing. Amaze us with your creativity !12@tool13def get_mood()-> str: #it's import to specify the return type14 #Keep this format for the description / args / args description but feel free to modify the tool15 """A tool that returns a description of the user's current mood16 Args: 17 """18 return "Excellent mood"19 20@tool21def get_current_time_in_timezone(timezone: str) -> str:22 """A tool that fetches the current local time in a specified timezone.23 Args:24 timezone: A string representing a valid timezone (e.g., 'America/New_York').25 """26 try:27 # Create timezone object28 tz = pytz.timezone(timezone)29 # Get current time in that timezone30 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")31 return f"The current local time in {timezone} is: {local_time}"32 except Exception as e:33 return f"Error fetching time for timezone '{timezone}': {str(e)}"34 35@tool36def get_answer_wo_specific_tool(history: str) -> str:37 """A tool that can should be used if no other tools seem to useful to answer the user question.38 Args:39 history: the conversation history including the current user message40 """41 try:42 mock_answer = "Here would come the answer"43 return f"The answer ro the latest user message is: {mock_answer}"44 except Exception as e:45 return f"Error fetching answer '{mock_answer}': {str(e)}"46 47@tool48def introduction(history: str) -> str:49 """A tool that provides the final answer if the current user message is the first messsage.50 Args:51 history: the conversation history including the current user message52 """53 try:54 mock_answer = "Ask the user for his name!"55 return f"{mock_answer}"56 except Exception as e:57 return f"Error fetching answer '{mock_answer}': {str(e)}"58 59final_answer = FinalAnswerTool()60 61model = OpenAIServerModel(62 model_id="gpt-4o",63 api_base="https://api.openai.com/v1",64 api_key=os.environ["OPENAI_API_KEY"],65)66 67 68with open("prompts.yaml", 'r') as stream:69 prompt_templates = yaml.safe_load(stream)70 71agent = CodeAgent(72 model=model,73 tools=[final_answer, introduction, get_mood, get_answer_wo_specific_tool],74 add_base_tools=True,75 max_steps=6,76 verbosity_level=1,77 grammar=None,78 planning_interval=None,79 name=None,80 description=None,81 prompt_templates=prompt_templates82)83 84 85GradioUI(agent).launch(show_error=True)