varunbhogayta28/First_agent_template
0
1from smolagents import CodeAgent,GoogleSearchTool, HfApiModel,load_tool,tool2import datetime3import requests4import pytz5import yaml6from tools.final_answer import FinalAnswerTool7import json8 9from Gradio_UI import GradioUI10 11# Below is an example of a tool that does nothing. Amaze us with your creativity !12@tool13def search(query: str) -> str:14 """15 A tool to perform web search.16 Args:17 query: search query string18 """19 web_search_tool = GoogleSearchTool(provider="serpapi", max_results=5)20 21 # Correct call — NOT web_search_tool(query), NOT .run()22 results_markdown = web_search_tool.forward(query)23 24 # results_markdown is already a string (Markdown)25 # smolagents wants string → return it directly26 return results_markdown27 28 29@tool30def get_current_time_in_timezone(timezone: str) -> str:31 """A tool that fetches the current local time in a specified timezone.32 Args:33 timezone: A string representing a valid timezone (e.g., 'America/New_York').34 """35 try:36 # Create timezone object37 tz = pytz.timezone(timezone)38 # Get current time in that timezone39 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")40 return f"The current local time in {timezone} is: {local_time}"41 except Exception as e:42 return f"Error fetching time for timezone '{timezone}': {str(e)}"43 44 45final_answer = FinalAnswerTool()46 47# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:48# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 49 50model = HfApiModel(51max_tokens=2096,52temperature=0.5,53model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded54custom_role_conversions=None,55)56 57 58# Import tool from Hub59image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)60 61with open("prompts.yaml", 'r') as stream:62 prompt_templates = yaml.safe_load(stream)63 64agent = CodeAgent(65 model=model,66 tools=[final_answer,search,get_current_time_in_timezone], ## add your tools here (don't remove final answer)67 max_steps=6,68 verbosity_level=1,69 grammar=None,70 planning_interval=None,71 name=None,72 description=None,73 prompt_templates=prompt_templates74)75 76 77GradioUI(agent).launch()