1+ # API imports
12from fastapi import FastAPI
23import groq
3- from agno .agent import Agent
4- from agno .models .groq import Groq
5- from agno .tools .yfinance import YFinanceTools
6- from agno .tools .duckduckgo import DuckDuckGoTools
7- from agno .agent import Agent , RunResponse
84import os
95from dotenv import load_dotenv
10- # Load API key from .env file
116from fastapi .middleware .cors import CORSMiddleware
127from topStocks import get_top_stocks
8+ from agents import multi_ai
9+ from agno .agent import RunResponse
10+
11+
1312load_dotenv (dotenv_path = ".env" )
1413GROQ_API_KEY = os .getenv ("api_key" )
1514
2827 allow_headers = ["*" ],
2928)
3029
31- web_search_agent = Agent (
32- name = "web_agent" ,
33- role = "search the web for information based on the user given input" ,
34- model = Groq (id = "llama-3.3-70b-specdec" ,api_key = GROQ_API_KEY ),
35- tools = [
36- DuckDuckGoTools (search = True , news = True ),
37-
38- ],
39- instructions = [
40- "You are a very professional web search AI agent" ,
41- "your job is to search the web for information based on the user given input" ,
42- "provide exact information to the user available on the web" ,
43- ],
44- structured_outputs = False ,
45- markdown = True ,
46- )
47- financial_agent = Agent (
48- name = "financial_agent" ,
49- role = "get financial information" ,
50- model = Groq (id = "llama-3.3-70b-specdec" ,api_key = GROQ_API_KEY ),
51- tools = [
52- YFinanceTools (stock_price = True ,
53- analyst_recommendations = True ,
54- stock_fundamentals = True ,
55- company_info = True ,
56- technical_indicators = True ,
57- historical_prices = True ,
58- key_financial_ratios = True ,
59- income_statements = True ,
60- ),
61- ],
62- instructions = [
63- "You are a very professional financial advisor AI agent" ,
64- "your job is to provide financial information to users" ,
65- "you can provide stock price, analyst recommendations, and stock fundamentals" ,
66- "you can also provide information about companies, industries, and financial terms" ,
67- ],
68- structured_outputs = False ,
69- markdown = True ,
70- )
71-
72- multi_ai = Agent (
73- team = [web_search_agent , financial_agent ],
74- model = Groq (id = "llama-3.3-70b-specdec" ,api_key = GROQ_API_KEY ),
75- markdown = True ,
76- )
77-
7830@app .get ("/top-stocks" )
7931async def read_top_stocks ():
8032 top_stocks = ['AAPL' , 'GOOGL' , 'MSFT' , 'AMZN' , 'TSLA' ]
8133 stock_info = get_top_stocks (top_stocks )
8234 return stock_info
8335
36+
37+ @app .get ("/chat" )
38+ def chat (query : str ):
39+ """
40+ API endpoint to handle user investment-related questions and return AI-generated insights.
41+ """
42+ if not query :
43+ return {"error" : "Query parameter is required" }
44+
45+ try :
46+ response = groq_client .chat .completions .create (
47+ model = "llama-3.3-70b-versatile" ,
48+ messages = [{"role" : "system" , "content" : "You are an AI investment assistant." },
49+ {"role" : "user" , "content" : query }]
50+ )
51+
52+ answer = response .choices [0 ].message .content
53+ return {"question" : query , "answer" : answer }
54+
55+ except Exception as e :
56+ return {"error" : str (e )}
57+
58+
8459@app .get ("/ask" )
8560def ask (query : str ):
8661 """
@@ -90,7 +65,6 @@ def ask(query: str):
9065 return {"error" : "Query parameter is required" }
9166
9267 try :
93- # response = financial_agent.print_response(query)
9468 response : RunResponse = multi_ai .run (query )
9569 answer = response .content
9670
0 commit comments