OnurKerimoglu commited on
Commit
fecb358
·
1 Parent(s): 2a02c57

stock_analysis_agent: use response format; tuned prompt

Browse files
Files changed (1) hide show
  1. src/stock_analysis_agent.py +33 -26
src/stock_analysis_agent.py CHANGED
@@ -9,6 +9,8 @@ from langchain_core.tools import tool
9
  from langgraph.prebuilt import ToolNode, tools_condition
10
  from langgraph.graph.message import add_messages
11
  import logging
 
 
12
  from src.technical_analysis import TechnicalAnalysis
13
  from src.fundamental_analysis import FundamentalAnalysis
14
 
@@ -53,6 +55,14 @@ def get_financial_metrics(
53
  else:
54
  return f"Error fetching financial metrics for ticker: {ticker}"
55
 
 
 
 
 
 
 
 
 
56
 
57
  class StockAnalyst():
58
  def __init__(
@@ -97,43 +107,37 @@ class StockAnalyst():
97
  stock_analyst_prompt= """
98
  You are a stock analyst specializing in evaluating the performance of a given company (whose symbol is {company})
99
  based on recent price data and technical indicators as well as financial metrics.
100
- Your task is to provide a comprehensive summary of the technical and fundamental analysis for a given stock,
101
- and based on the analysis, choose an action among the following options:
102
- - strong sell: there are overwhelmingly bad signals for the stock, sell large quantities
103
- - sell: there are some bad signals for the stock, sell medium quantities
104
- - hold: there are either neutral signals, or good signals mixed with bad signals for the stock, hold
105
- - buy: there are some good signals for the stock, buy medium quantities
106
- - strong buy: there are overwhelmingly good signals for the stock, buy large quantities
107
 
108
  You have access to the following tools:
109
  1. **get_stock_prices**: Retrieves the latest stock price, historical price data and technical Indicators like VWAP, RSI, Stochastic Oscillator and MACD metrics.
110
  2. **get_financial_metrics**: Retrieves key financial metrics, such as revenue, earnings per share (EPS), price-to-earnings ratio (P/E), and debt-to-equity ratio.
111
 
112
- ### Your Task:
113
- 1. **Input Stock Symbol**: Use the provided stock symbol to query the tools and gather the relevant information.
114
- 2. **Analyze Data**: Evaluate the results from the tools and identify potential resistance, key trends, strengths, or concerns.
115
- 3. **Provide Summary**: Write a concise, well-structured summary that highlights:
116
- - Recent stock price movements, trends and potential resistance.
117
- - Key insights from technical indicators (e.g., whether the stock is overbought or oversold).
118
- - Financial health and performance based on financial metrics.
119
-
 
 
 
 
 
 
 
 
120
  ### Constraints:
121
  - Use only the data provided by the tools.
122
  - If any tool fails to provide data, clearly state that in your summary.
123
  - Avoid speculative language; focus on observable data and trends.
124
  - Ensure that your response is objective, concise, and actionable.
125
-
126
- ### Output Format:
127
- Respond in the following format:
128
- "Stock": "<Stock symbol>",
129
- "Price analysis": "<Detailed analysis of stock price trends>",
130
- "Technical analysis": "<Detailed analysis of technical indicators>",
131
- "Fundamental analysis": "<Detailed analysis of financial metrics>",
132
- "Final summary": "<Conclusive summary of the analyses above>"
133
- "Recommended action": "<Suggested action based on the above analyses among options: [strong sell, sell, hold, buy, strong buy]>"
134
  """
135
  return stock_analyst_prompt.format(company=company)
136
-
137
  def build_graph(
138
  self
139
  ) -> StateGraph:
@@ -156,7 +160,10 @@ class StockAnalyst():
156
 
157
  tools = [get_stock_prices, get_financial_metrics]
158
  llm = ChatOpenAI(model='gpt-4o-mini')
159
- llm_with_tool = llm.bind_tools(tools)
 
 
 
160
 
161
  def stock_analyst(state: State):
162
 
 
9
  from langgraph.prebuilt import ToolNode, tools_condition
10
  from langgraph.graph.message import add_messages
11
  import logging
12
+ from pydantic import BaseModel, Field
13
+
14
  from src.technical_analysis import TechnicalAnalysis
15
  from src.fundamental_analysis import FundamentalAnalysis
16
 
 
55
  else:
56
  return f"Error fetching financial metrics for ticker: {ticker}"
57
 
58
+ class StockAnalysisResponse(BaseModel):
59
+ """Stock Analysis Response Schema"""
60
+ stock: str = Field(description="Stock symbol")
61
+ price_analysis: str = Field(description="Detailed analysis of stock price trends")
62
+ technical_analysis: str = Field(description="Detailed analysis of technical indicators")
63
+ technical_analysis: str = Field(description="Detailed analysis of financial metrics")
64
+ final_summary: str = Field(description="Conclusive summary of the analyses above")
65
+ recommended_action: str = Field(description="Suggested action based on the above analyses among options: [strong sell, sell, hold, buy, strong buy]")
66
 
67
  class StockAnalyst():
68
  def __init__(
 
107
  stock_analyst_prompt= """
108
  You are a stock analyst specializing in evaluating the performance of a given company (whose symbol is {company})
109
  based on recent price data and technical indicators as well as financial metrics.
110
+ Your task is to provide a comprehensive summaries of price movements, technical and fundamental analysis for a given stock,
111
+ and based on the analysis, provide receommended action (see below) for details.
 
 
 
 
 
112
 
113
  You have access to the following tools:
114
  1. **get_stock_prices**: Retrieves the latest stock price, historical price data and technical Indicators like VWAP, RSI, Stochastic Oscillator and MACD metrics.
115
  2. **get_financial_metrics**: Retrieves key financial metrics, such as revenue, earnings per share (EPS), price-to-earnings ratio (P/E), and debt-to-equity ratio.
116
 
117
+ ### Your Tasks:
118
+ 1. **Input Stock Symbol**: use the provided stock symbol to query the tools and gather the relevant information.
119
+ 2. **Analyze Data**: evaluate the results from the tools
120
+ 3. **Summarize and Synthesize**: in particular, we need:
121
+ a) A summary of recent stock price movements, highlighting final available closing prices.
122
+ b) A summary of trends and potential resistance.
123
+ c) A summary of technical indicators (e.g., whether the stock is overbought or oversold).
124
+ d) A summary of Financial health and performance based on financial metrics.
125
+ e) A final, conclusive synthesis that highlights key concerns and strenghts
126
+ f) Recommended action among following options:
127
+ - strong sell: if there are overwhelmingly bad signals
128
+ - sell: if there are some bad signals
129
+ - hold: there are either neutral signals, or good signals mixed with bad signals
130
+ - buy: there are some good signals
131
+ - strong buy: there are overwhelmingly good signals
132
+
133
  ### Constraints:
134
  - Use only the data provided by the tools.
135
  - If any tool fails to provide data, clearly state that in your summary.
136
  - Avoid speculative language; focus on observable data and trends.
137
  - Ensure that your response is objective, concise, and actionable.
 
 
 
 
 
 
 
 
 
138
  """
139
  return stock_analyst_prompt.format(company=company)
140
+
141
  def build_graph(
142
  self
143
  ) -> StateGraph:
 
160
 
161
  tools = [get_stock_prices, get_financial_metrics]
162
  llm = ChatOpenAI(model='gpt-4o-mini')
163
+ llm_with_tool = llm.bind_tools(
164
+ tools,
165
+ strict=True,
166
+ response_format=StockAnalysisResponse)
167
 
168
  def stock_analyst(state: State):
169