MarianaUser/Hybrid_Retrieval_Agent
0
1#from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool2#import datetime3import requests4#import pytz5#import yaml6 7from smolagents import CodeAgent, InferenceClientModel8import json9from tools.web_search import DuckDuckGoSearchTool10from tools.visit_webpage import VisitWebpageTool11from tools.final_answer import FinalAnswerTool12 13from Gradio_UI import GradioUI14 15'''16# Below is an example of a tool that does nothing. Amaze us with your creativity !17@tool18def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type19 #Keep this format for the description / args / args description but feel free to modify the tool20 """A tool that does nothing yet 21 Args:22 arg1: the first argument23 arg2: the second argument24 """25 return "What magic will you build ?"26 27@tool28def get_current_time_in_timezone(timezone: str) -> str:29 """A tool that fetches the current local time in a specified timezone.30 Args:31 timezone: A string representing a valid timezone (e.g., 'America/New_York').32 """33 try:34 # Create timezone object35 tz = pytz.timezone(timezone)36 # Get current time in that timezone37 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")38 return f"The current local time in {timezone} is: {local_time}"39 except Exception as e:40 return f"Error fetching time for timezone '{timezone}': {str(e)}"41 42@tool43def get_weather(city: str) -> str:44 """Get current weather for a city.45 Args:46 city: Name of the city47 """48 url = f"https://wttr.in/{city}?format=3"49 return requests.get(url).text 50 51'''52 53# Load JSON54with open("agent.json") as f:55 config = json.load(f)56 57# Load model from config58model_data = config["model"]["data"]59model = InferenceClientModel(**model_data)60 61# Map tool names to actual functions62tool_map = { "web_search": DuckDuckGoSearchTool(),63 "visit_webpage": VisitWebpageTool(),64 "final_answer": FinalAnswerTool() }65 66tools = [tool_map[name] for name in config["tools"]]67 68# Load prompt templates69prompt_templates = config["prompt_templates"]70 71'''72final_answer = FinalAnswerTool()73 74# 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:75# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 76 77model = HfApiModel(78max_tokens=2096,79temperature=0.5,80model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded81custom_role_conversions=None,82)83 84 85# Import tool from Hub86image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)87 88with open("prompts.yaml", 'r') as stream:89 prompt_templates = yaml.safe_load(stream)90 91'''92# Create agent 93agent = CodeAgent(94 model=model,95 tools=tools,96 max_steps=config["max_steps"],97 verbosity_level=config["verbosity_level"],98 planning_interval=config["planning_interval"],99 name=config["name"],100 description=config["description"],101 additional_authorized_imports=config["authorized_imports"],102 prompt_templates=prompt_templates103)104 105 106GradioUI(agent).launch()