-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinancial_agent.py
More file actions
107 lines (90 loc) · 3.02 KB
/
financial_agent.py
File metadata and controls
107 lines (90 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import os
from phi.agent import Agent
from phi.model.groq import Groq
from phi.tools.yfinance import YFinanceTools
from phi.tools.duckduckgo import DuckDuckGo
import logging
def setup_groq_agent(groq_api_key):
"""
Set up a Groq-based agent with financial and web search capabilities
Args:
groq_api_key: Your Groq API key
"""
try:
# Configure the Groq model
groq_model = Groq(
api_key=groq_api_key, id="llama-3.3-70b-versatile", temperature=0.7
)
# Create the web search agent
web_agent = Agent(
name="web_agent",
model=groq_model,
tools=[DuckDuckGo()],
instructions=[
"Search for relevant news and information",
"Focus on reliable sources",
"Provide source attribution",
],
show_tool_calls=True,
markdown=True,
)
# Create the financial analysis agent
finance_agent = Agent(
name="finance_agent",
model=groq_model,
tools=[
YFinanceTools(
stock_price=True,
analyst_recommendations=True,
stock_fundamentals=True,
)
],
instructions=[
"Analyze financial metrics and market data",
"Present data in table format when possible",
"Include key performance indicators",
],
show_tool_calls=True,
markdown=True,
)
# Create the multi-agent system
multi_agent = Agent(
team=[web_agent, finance_agent],
model=groq_model,
instructions=[
"Combine financial data with news analysis",
"Present information in a clear, organized format",
"Highlight key insights and trends",
],
show_tool_calls=True,
markdown=True,
)
return multi_agent
except Exception as e:
logging.error(f"Failed to create agent: {str(e)}")
raise
def analyze_stock(agent, symbol):
"""
Analyze a stock using the multi-agent system
Args:
agent: Configured multi-agent
symbol: Stock symbol to analyze
"""
prompt = f"""
Provide a comprehensive analysis of {symbol} including:
1. Current analyst recommendations and price targets
Present the information in a clear, organized format using tables where appropriate.
"""
return agent.print_response(prompt, stream=True)
# Usage example
if __name__ == "__main__":
# Configure logging
logging.basicConfig(level=logging.INFO)
try:
# Replace with your actual Groq API key
GROQ_API_KEY = "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"
# Set up and run the analysis
agent = setup_groq_agent(GROQ_API_KEY)
analyze_stock(agent, "TSLA")
except Exception as e:
print(f"Error setting up or running agent: {str(e)}")