Aravindram commited on
Commit
611f301
·
verified ·
1 Parent(s): 81917a3

Create agent.py

Browse files
Files changed (1) hide show
  1. agent.py +37 -0
agent.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents import ToolCallingAgent
2
+ from smolagents.models import HfApiModel
3
+ from smolagents.tools import tool
4
+ import requests
5
+
6
+ # Tool: Stock price lookup via Yahoo Finance
7
+ @tool
8
+ def get_stock_price(ticker: str) -> str:
9
+ """Returns the current stock price for the given ticker symbol (e.g., AAPL)."""
10
+ try:
11
+ url = f"https://query1.finance.yahoo.com/v7/finance/quote?symbols={ticker}"
12
+ response = requests.get(url)
13
+ data = response.json()
14
+ result = data['quoteResponse']['result']
15
+ if not result:
16
+ return f"No stock data found for '{ticker}'."
17
+ price = result[0]['regularMarketPrice']
18
+ name = result[0]['shortName']
19
+ return f"{name} ({ticker.upper()}) is currently trading at ${price:.2f}"
20
+ except Exception as e:
21
+ return f"Error fetching stock data: {e}"
22
+
23
+ # Model
24
+ model = HfApiModel(model_id="mistralai/Mistral-7B-Instruct-v0.2")
25
+
26
+ # Agent
27
+ agent = ToolCallingAgent(
28
+ tools=[get_stock_price],
29
+ model=model,
30
+ max_steps=3,
31
+ name="stock_price_agent",
32
+ description="Answers questions about stock prices using Yahoo Finance."
33
+ )
34
+
35
+ # Interface function
36
+ def answer_question(question: str) -> str:
37
+ return agent.run(question).strip()