gemechisw/First_agent_template
0
1from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool2import datetime3import requests4import pytz5import yaml6from tools.final_answer import FinalAnswerTool7 8from Gradio_UI import GradioUI9 10# Below is an example of a tool that does nothing. Amaze us with your creativity !11@tool12def addTwoNumbers(arg1:int, arg2:int)-> int: #it's import to specify the return type13 #Keep this format for the description / args / args description but feel free to modify the tool14 """A tool that adds two integers and returns it 15 Args:16 arg1: the first number17 arg2: the second number18 """19 return arg1 + arg220 21@tool22def getLinkedinProfileInfo(username: str) -> str:23 """A tool that calls an API to fetch linkedin profile info and returns it.24 Args:25 username: a linkedin username of a profile in question26 Output: returns a JSON object containing the information from a given username's linkedin profile27 """28 import requests29 30 url = "https://linkedin-data-api.p.rapidapi.com/about-this-profile"31 32 querystring = {"username": username}33 34 headers = {35 "x-rapidapi-key": "929182774fmsh988ee3ad62ddef0p10b601jsnd75c6f4742e4",36 "x-rapidapi-host": "linkedin-data-api.p.rapidapi.com"37 }38 39 response = requests.get(url, headers=headers, params=querystring)40 return response41 42@tool43def get_current_time_in_timezone(timezone: str) -> str:44 """A tool that fetches the current local time in a specified timezone.45 Args:46 timezone: A string representing a valid timezone (e.g., 'America/New_York').47 """48 try:49 # Create timezone object50 tz = pytz.timezone(timezone)51 # Get current time in that timezone52 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")53 return f"The current local time in {timezone} is: {local_time}"54 except Exception as e:55 return f"Error fetching time for timezone '{timezone}': {str(e)}"56 57 58final_answer = FinalAnswerTool()59 60# 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:61# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 62 63model = HfApiModel(64max_tokens=2096,65temperature=0.5,66model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded67custom_role_conversions=None,68)69 70 71# Import tool from Hub72image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)73 74with open("prompts.yaml", 'r') as stream:75 prompt_templates = yaml.safe_load(stream)76 77agent = CodeAgent(78 model=model,79 tools=[final_answer, addTwoNumbers, get_current_time_in_timezone, image_generation_tool, getLinkedinProfileInfo], ## add your tools here (don't remove final answer)80 max_steps=6,81 verbosity_level=1,82 grammar=None,83 planning_interval=None,84 name=None,85 description=None,86 prompt_templates=prompt_templates87)88 89 90GradioUI(agent).launch()