add vector store
Browse files- agents/rag_analyzer.py +68 -19
- app.py +13 -6
agents/rag_analyzer.py
CHANGED
@@ -11,7 +11,11 @@ import os
|
|
11 |
embeddings = HuggingFaceEmbeddings(model_name="Snowflake/snowflake-arctic-embed-l")
|
12 |
|
13 |
# Initialize Vector DB
|
14 |
-
vector_db = Chroma(
|
|
|
|
|
|
|
|
|
15 |
|
16 |
# Define RAG prompt template for technical analysis interpretation
|
17 |
TECHNICAL_RAG_PROMPT = """
|
@@ -264,29 +268,74 @@ def rag_analyzer(state: AgentState) -> AgentState:
|
|
264 |
"investment_goals": investment_goals
|
265 |
})
|
266 |
|
267 |
-
#
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
275 |
if current_ticker is not None and current_ticker in technical_analysis:
|
276 |
technical_analysis[current_ticker]["rag_interpretation"] = '\n'.join(current_analysis).strip()
|
277 |
-
current_analysis = []
|
278 |
-
|
279 |
-
# Start new ticker
|
280 |
-
current_ticker = line.split(':')[0].strip()
|
281 |
-
current_analysis.append(line.split(':', 1)[1].strip())
|
282 |
-
elif current_ticker is not None:
|
283 |
-
current_analysis.append(line)
|
284 |
|
285 |
-
|
286 |
-
|
287 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
288 |
except Exception as e:
|
289 |
# Fallback to a simpler approach if batch processing fails
|
|
|
|
|
|
|
290 |
for ticker, data in technical_analysis.items():
|
291 |
if "error" not in data:
|
292 |
technical_analysis[ticker]["rag_interpretation"] = f"Analysis unavailable due to processing error: {str(e)}"
|
|
|
11 |
embeddings = HuggingFaceEmbeddings(model_name="Snowflake/snowflake-arctic-embed-l")
|
12 |
|
13 |
# Initialize Vector DB
|
14 |
+
vector_db = Chroma(
|
15 |
+
embedding_function=embeddings,
|
16 |
+
persist_directory="./chroma_db",
|
17 |
+
collection_name="Security_Analysis" # Explicitly use the Security_Analysis collection
|
18 |
+
)
|
19 |
|
20 |
# Define RAG prompt template for technical analysis interpretation
|
21 |
TECHNICAL_RAG_PROMPT = """
|
|
|
268 |
"investment_goals": investment_goals
|
269 |
})
|
270 |
|
271 |
+
# Try to parse as JSON first
|
272 |
+
try:
|
273 |
+
import json
|
274 |
+
import re
|
275 |
+
|
276 |
+
# Try to find JSON-like content in the response using regex
|
277 |
+
json_match = re.search(r'\{[\s\S]*\}', batch_analysis_result)
|
278 |
+
if json_match:
|
279 |
+
json_str = json_match.group(0)
|
280 |
+
analysis_data = json.loads(json_str)
|
281 |
+
|
282 |
+
# Update technical_analysis with the parsed JSON
|
283 |
+
for ticker, analysis in analysis_data.items():
|
284 |
+
if ticker in technical_analysis:
|
285 |
+
technical_analysis[ticker]["rag_interpretation"] = analysis
|
286 |
+
else:
|
287 |
+
# Fallback to the original text parsing approach
|
288 |
+
current_ticker = None
|
289 |
+
current_analysis = []
|
290 |
+
|
291 |
+
for line in batch_analysis_result.split('\n'):
|
292 |
+
if ':' in line and line.split(':')[0].strip() in technical_analysis:
|
293 |
+
# If we have a previous ticker, save its analysis
|
294 |
+
if current_ticker is not None and current_ticker in technical_analysis:
|
295 |
+
technical_analysis[current_ticker]["rag_interpretation"] = '\n'.join(current_analysis).strip()
|
296 |
+
current_analysis = []
|
297 |
+
|
298 |
+
# Start new ticker
|
299 |
+
current_ticker = line.split(':')[0].strip()
|
300 |
+
current_analysis.append(line.split(':', 1)[1].strip())
|
301 |
+
elif current_ticker is not None:
|
302 |
+
current_analysis.append(line)
|
303 |
+
|
304 |
+
# Add the last ticker's analysis
|
305 |
if current_ticker is not None and current_ticker in technical_analysis:
|
306 |
technical_analysis[current_ticker]["rag_interpretation"] = '\n'.join(current_analysis).strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
307 |
|
308 |
+
except (json.JSONDecodeError, Exception) as json_err:
|
309 |
+
print(f"Error parsing JSON response: {str(json_err)}")
|
310 |
+
# Log the raw response for debugging
|
311 |
+
print(f"Raw response: {batch_analysis_result}")
|
312 |
+
|
313 |
+
# Fallback to old approach if JSON parsing fails
|
314 |
+
current_ticker = None
|
315 |
+
current_analysis = []
|
316 |
+
|
317 |
+
for line in batch_analysis_result.split('\n'):
|
318 |
+
if ':' in line and line.split(':')[0].strip() in technical_analysis:
|
319 |
+
# If we have a previous ticker, save its analysis
|
320 |
+
if current_ticker is not None and current_ticker in technical_analysis:
|
321 |
+
technical_analysis[current_ticker]["rag_interpretation"] = '\n'.join(current_analysis).strip()
|
322 |
+
current_analysis = []
|
323 |
+
|
324 |
+
# Start new ticker
|
325 |
+
current_ticker = line.split(':')[0].strip()
|
326 |
+
current_analysis.append(line.split(':', 1)[1].strip())
|
327 |
+
elif current_ticker is not None:
|
328 |
+
current_analysis.append(line)
|
329 |
+
|
330 |
+
# Add the last ticker's analysis
|
331 |
+
if current_ticker is not None and current_ticker in technical_analysis:
|
332 |
+
technical_analysis[current_ticker]["rag_interpretation"] = '\n'.join(current_analysis).strip()
|
333 |
+
|
334 |
except Exception as e:
|
335 |
# Fallback to a simpler approach if batch processing fails
|
336 |
+
import traceback
|
337 |
+
print(f"Error in batch analysis: {str(e)}")
|
338 |
+
print(traceback.format_exc())
|
339 |
for ticker, data in technical_analysis.items():
|
340 |
if "error" not in data:
|
341 |
technical_analysis[ticker]["rag_interpretation"] = f"Analysis unavailable due to processing error: {str(e)}"
|
app.py
CHANGED
@@ -284,6 +284,11 @@ if generate_button or st.session_state.portfolio_analyzed:
|
|
284 |
progress_placeholder.progress(75, "RAG Analysis")
|
285 |
status_placeholder.info("Value investing analysis complete. Generating recommendations...")
|
286 |
|
|
|
|
|
|
|
|
|
|
|
287 |
### SHOW NEWS
|
288 |
# Display relevant news with links if available
|
289 |
#if "news_analysis" in final_state and final_state["news_analysis"]:
|
@@ -416,12 +421,6 @@ if generate_button or st.session_state.portfolio_analyzed:
|
|
416 |
if url:
|
417 |
st.write(f"[Read more]({url})")
|
418 |
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
elif "news_analysis" in state and state["news_analysis"]:
|
423 |
-
progress_placeholder.progress(60, "News Analysis")
|
424 |
-
status_placeholder.info("News analysis complete. Applying value investing principles...")
|
425 |
elif "portfolio_analysis" in state and state["portfolio_analysis"]:
|
426 |
progress_placeholder.progress(40, "Portfolio Analysis")
|
427 |
status_placeholder.info("Portfolio analysis complete. Gathering financial news...")
|
@@ -710,6 +709,12 @@ if generate_button or st.session_state.portfolio_analyzed:
|
|
710 |
|
711 |
# Display portfolio strengths and weaknesses
|
712 |
col1, col2 = st.columns(2)
|
|
|
|
|
|
|
|
|
|
|
|
|
713 |
|
714 |
with col1:
|
715 |
st.subheader("Portfolio strengths")
|
@@ -882,6 +887,8 @@ if generate_button or st.session_state.portfolio_analyzed:
|
|
882 |
new_investment_summary = new_inv_final_state.get("new_investment_summary", "")
|
883 |
if new_investment_summary:
|
884 |
st.write(new_investment_summary)
|
|
|
|
|
885 |
|
886 |
# Display the new investment recommendations
|
887 |
new_investments = new_inv_final_state.get("new_investments", [])
|
|
|
284 |
progress_placeholder.progress(75, "RAG Analysis")
|
285 |
status_placeholder.info("Value investing analysis complete. Generating recommendations...")
|
286 |
|
287 |
+
|
288 |
+
elif "news_analysis" in state and state["news_analysis"]:
|
289 |
+
progress_placeholder.progress(60, "News Analysis")
|
290 |
+
status_placeholder.info("News analysis complete. Applying value investing principles...")
|
291 |
+
|
292 |
### SHOW NEWS
|
293 |
# Display relevant news with links if available
|
294 |
#if "news_analysis" in final_state and final_state["news_analysis"]:
|
|
|
421 |
if url:
|
422 |
st.write(f"[Read more]({url})")
|
423 |
|
|
|
|
|
|
|
|
|
|
|
|
|
424 |
elif "portfolio_analysis" in state and state["portfolio_analysis"]:
|
425 |
progress_placeholder.progress(40, "Portfolio Analysis")
|
426 |
status_placeholder.info("Portfolio analysis complete. Gathering financial news...")
|
|
|
709 |
|
710 |
# Display portfolio strengths and weaknesses
|
711 |
col1, col2 = st.columns(2)
|
712 |
+
print("yoyoyo")
|
713 |
+
print(final_state.get("technical_analysis"));
|
714 |
+
print(final_state.get("rag_context"));
|
715 |
+
print(final_state.get("fundamental_analysis"));
|
716 |
+
|
717 |
+
|
718 |
|
719 |
with col1:
|
720 |
st.subheader("Portfolio strengths")
|
|
|
887 |
new_investment_summary = new_inv_final_state.get("new_investment_summary", "")
|
888 |
if new_investment_summary:
|
889 |
st.write(new_investment_summary)
|
890 |
+
|
891 |
+
print(new_inv_final_state.get("new_stock_analysis"))
|
892 |
|
893 |
# Display the new investment recommendations
|
894 |
new_investments = new_inv_final_state.get("new_investments", [])
|