bitwburak/First_agent_template
0
1from smolagents import CodeAgent, HfApiModel, load_tool, tool, DuckDuckGoSearchTool2import datetime3import pytz4import yaml5from tools.final_answer import FinalAnswerTool6from Gradio_UI import GradioUI7 8@tool9def get_stock_prices_via_search(ticker: str, days: int) -> list:10 """Fetches stock price information for the given ticker over the last {days} days using DuckDuckGo web search.11 12 Args:13 ticker: The stock ticker (e.g., 'AAPL').14 days: Number of days to look up historical prices.15 16 Returns:17 A list of search result snippets or URLs containing relevant stock price information.18 """19 # Construct a search query for historical stock prices.20 q = f"historical stock prices for {ticker} stock over last {days} days"21 # Instantiate the DuckDuckGoSearchTool and call it22 search_tool = DuckDuckGoSearchTool()23 results = search_tool(q)24 # Ensure the results are returned as a list25 if not isinstance(results, list):26 results = [results]27 return results28 29@tool30def get_current_time_in_timezone(timezone: str) -> str:31 """A tool that fetches the current local time in a specified timezone.32 33 Args:34 timezone: A string representing a valid timezone (e.g., 'America/New_York').35 """36 try:37 tz = pytz.timezone(timezone)38 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")39 return f"The current local time in {timezone} is: {local_time}"40 except Exception as e:41 return f"Error fetching time for timezone '{timezone}': {str(e)}"42 43final_answer = FinalAnswerTool()44 45model = HfApiModel(46 max_tokens=2096,47 temperature=0.5,48 model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',49 custom_role_conversions=None,50)51 52# Import image generation tool from Hub (if needed)53image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)54 55with open("prompts.yaml", 'r') as stream:56 prompt_templates = yaml.safe_load(stream)57 58# Initialize the agent with the stock price search tool and the current time tool.59agent = CodeAgent(60 model=model,61 tools=[get_stock_prices_via_search, final_answer, get_current_time_in_timezone],62 max_steps=6,63 verbosity_level=1,64 grammar=None,65 planning_interval=None,66 name=None,67 description=None,68 prompt_templates=prompt_templates69)70 71GradioUI(agent).launch()72 