Mjlehtim commited on
Commit
ec70fb1
·
verified ·
1 Parent(s): e3db1ce

Tools renewed with if financials exist clauses

Browse files
Files changed (1) hide show
  1. app.py +39 -25
app.py CHANGED
@@ -506,7 +506,15 @@ with strategies_container:
506
  if "ESG_analysis_button_key" in st.session_state.results and st.session_state.results["ESG_analysis_button_key"]:
507
 
508
  doc_retriever_ESG, query_engine = create_vector_database_ESG()
509
- doc_retriever_financials, query_engine_financials = create_vector_database_financials()
 
 
 
 
 
 
 
 
510
  memory = ConversationBufferMemory(memory_key="chat_history", k=3, return_messages=True)
511
  search = SerpAPIWrapper()
512
 
@@ -519,7 +527,7 @@ with strategies_container:
519
  prompt_financials = PromptTemplate.from_template(
520
  template="""
521
  You are a seasoned corporate finance specialist.
522
- Use figures, numerical, and statistical data when possible. Never give false information, numbers or data.
523
 
524
  Conversation history:
525
  {chat_history}
@@ -531,7 +539,7 @@ with strategies_container:
531
  prompt_ESG = PromptTemplate.from_template(
532
  template="""
533
  You are a seasoned finance specialist and a specialist in environmental, social, and governance matters.
534
- Use figures, numerical, and statistical data when possible. Never give false information, numbers or data.
535
 
536
  Conversation history:
537
  {chat_history}
@@ -541,17 +549,18 @@ with strategies_container:
541
  )
542
 
543
  # LCEL Chains with memory integration
544
- financials_chain = (
545
- {
546
- "context": doc_retriever_financials,
547
- # Lambda function now accepts one argument (even if unused)
548
- "chat_history": lambda _: format_chat_history(memory.load_memory_variables({})["chat_history"]),
549
- "question": RunnablePassthrough(),
550
- }
551
- | prompt_financials
552
- | llm_tool
553
- | StrOutputParser()
554
- )
 
555
 
556
  ESG_chain = (
557
  {
@@ -572,11 +581,12 @@ with strategies_container:
572
  description="Useful for answering questions about specific ESG figures, data and statistics.",
573
  )
574
 
575
- vector_query_tool_financials = Tool(
576
- name="Vector Query Engine Financials",
577
- func=lambda query: query_engine_financials.query(query), # Use query_engine to query the vector database
578
- description="Useful for answering questions about specific financial figures, data and statistics.",
579
- )
 
580
 
581
  tools = [
582
  Tool(
@@ -584,20 +594,24 @@ with strategies_container:
584
  func=ESG_chain.invoke,
585
  description="Useful for answering general questions about environmental, social, and governance (ESG) matters related to the company. ",
586
  ),
587
- Tool(
588
- name="Financials QA System",
589
- func=financials_chain.invoke,
590
- description="Useful for answering general questions about financial or operational information concerning the company.",
591
- ),
592
  Tool(
593
  name="Search Tool",
594
  func=search.run,
595
  description="Useful when other tools do not provide the answer.",
596
  ),
597
  vector_query_tool_ESG,
598
- vector_query_tool_financials,
599
  ]
600
 
 
 
 
 
 
 
 
 
 
 
601
  # Initialize the agent with LCEL tools and memory
602
  agent = initialize_agent(
603
  tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True, memory=memory, handle_parsing_errors=True)
 
506
  if "ESG_analysis_button_key" in st.session_state.results and st.session_state.results["ESG_analysis_button_key"]:
507
 
508
  doc_retriever_ESG, query_engine = create_vector_database_ESG()
509
+ # Define the file path
510
+ file_path = os.path.join("data", "parsed_data_financials.pkl")
511
+
512
+ # Check if the file exists before running the function
513
+ if os.path.exists(file_path):
514
+ doc_retriever_financials, query_engine_financials = create_vector_database_financials()
515
+ else:
516
+ print(f"The file {file_path} does not exist. Skipping vector database creation.")
517
+
518
  memory = ConversationBufferMemory(memory_key="chat_history", k=3, return_messages=True)
519
  search = SerpAPIWrapper()
520
 
 
527
  prompt_financials = PromptTemplate.from_template(
528
  template="""
529
  You are a seasoned corporate finance specialist.
530
+ Use figures, and numerical, and statistical data when possible. Never give false information, numbers, or data.
531
 
532
  Conversation history:
533
  {chat_history}
 
539
  prompt_ESG = PromptTemplate.from_template(
540
  template="""
541
  You are a seasoned finance specialist and a specialist in environmental, social, and governance matters.
542
+ Use figures, and numerical, and statistical data when possible. Never give false information, numbers or data.
543
 
544
  Conversation history:
545
  {chat_history}
 
549
  )
550
 
551
  # LCEL Chains with memory integration
552
+ if os.path.exists(file_path)
553
+ financials_chain = (
554
+ {
555
+ "context": doc_retriever_financials,
556
+ # Lambda function now accepts one argument (even if unused)
557
+ "chat_history": lambda _: format_chat_history(memory.load_memory_variables({})["chat_history"]),
558
+ "question": RunnablePassthrough(),
559
+ }
560
+ | prompt_financials
561
+ | llm_tool
562
+ | StrOutputParser()
563
+ )
564
 
565
  ESG_chain = (
566
  {
 
581
  description="Useful for answering questions about specific ESG figures, data and statistics.",
582
  )
583
 
584
+ if os.path.exists(file_path)
585
+ vector_query_tool_financials = Tool(
586
+ name="Vector Query Engine Financials",
587
+ func=lambda query: query_engine_financials.query(query), # Use query_engine to query the vector database
588
+ description="Useful for answering questions about specific financial figures, data and statistics.",
589
+ )
590
 
591
  tools = [
592
  Tool(
 
594
  func=ESG_chain.invoke,
595
  description="Useful for answering general questions about environmental, social, and governance (ESG) matters related to the company. ",
596
  ),
 
 
 
 
 
597
  Tool(
598
  name="Search Tool",
599
  func=search.run,
600
  description="Useful when other tools do not provide the answer.",
601
  ),
602
  vector_query_tool_ESG,
 
603
  ]
604
 
605
+ if os.path.exists(file_path)
606
+ tools.append(
607
+ Tool(
608
+ name="Financials QA System",
609
+ func=financials_chain.invoke,
610
+ description="Useful for answering general questions about financial or operational information concerning the company.",
611
+ vector_query_tool_financials,
612
+ )
613
+ )
614
+
615
  # Initialize the agent with LCEL tools and memory
616
  agent = initialize_agent(
617
  tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True, memory=memory, handle_parsing_errors=True)