Mjlehtim commited on
Commit
ba5121a
·
verified ·
1 Parent(s): c5bfc9b

LLM revisions

Browse files
Files changed (1) hide show
  1. app.py +34 -96
app.py CHANGED
@@ -32,8 +32,6 @@ from langchain.memory import ConversationBufferMemory
32
  from langchain.prompts import PromptTemplate
33
  import joblib
34
  import nltk
35
- import json
36
- nltk.download('averaged_perceptron_tagger')
37
 
38
  import nest_asyncio # noqa: E402
39
  nest_asyncio.apply()
@@ -106,55 +104,15 @@ css = """
106
  """
107
 
108
  st.write(css, unsafe_allow_html=True)
109
- #--------------
110
- def load_credentials(filepath):
111
- with open(filepath, 'r') as file:
112
- return json.load(file)
113
-
114
- # Load credentials from 'credentials.json'
115
- credentials = load_credentials('chroma_db_LT1/credentials.json')
116
-
117
- # Initialize session state if not already done
118
- if 'logged_in' not in st.session_state:
119
- st.session_state.logged_in = False
120
- st.session_state.username = ''
121
-
122
- # Function to handle login
123
- def login(username, password):
124
- if username in credentials and credentials[username] == password:
125
- st.session_state.logged_in = True
126
- st.session_state.username = username
127
- st.rerun() # Rerun to reflect login state
128
- else:
129
- st.session_state.logged_in = False
130
- st.session_state.username = ''
131
- st.error("Invalid username or password.")
132
-
133
- # Function to handle logout
134
- def logout():
135
- st.session_state.logged_in = False
136
- st.session_state.username = ''
137
- st.rerun() # Rerun to reflect logout state
138
-
139
- # If not logged in, show login form
140
- if not st.session_state.logged_in:
141
- st.sidebar.write("Login")
142
- username = st.sidebar.text_input('Username')
143
- password = st.sidebar.text_input('Password', type='password')
144
- if st.sidebar.button('Login'):
145
- login(username, password)
146
- # Stop the script here if the user is not logged in
147
- st.stop()
148
-
149
- # If logged in, show logout button and main content
150
- if st.session_state.logged_in:
151
- st.sidebar.write(f"Welcome, {st.session_state.username}!")
152
- if st.sidebar.button('Logout'):
153
- logout()
154
-
155
  #-------------
156
  llm=ChatGroq(groq_api_key=groq_api_key,
157
- model_name="llama-3.1-70b-Versatile", temperature = 0.0, streaming=True)
 
 
 
 
 
158
  #--------------
159
  doc_retriever_ESG = None
160
  doc_retriever_financials = None
@@ -166,7 +124,7 @@ def load_or_parse_data_ESG():
166
 
167
  parsingInstructionUber10k = """The provided document contain detailed information about the company's environmental, social and governance matters.
168
  It contains several tables, figures and statistical information about CO2 emissions and energy consumption.
169
- Give only precide CO2 and energy consumotion levels inly from the context documents.
170
  You must never provide false numeric or statistical data that is not included in the context document.
171
  Include tables and numeric data always when possible. Only refer to other sources if the context document refers to them or if necessary to provide additional understanding to company's own data."""
172
 
@@ -340,15 +298,15 @@ def ESG_strategy():
340
  )
341
 
342
  ESG_answer_1 = qa.invoke("Give a summary what specific ESG measures the company has taken recently and compare these to the best practices.")
343
- ESG_answer_2 = qa.invoke("Does the company's main business fall under the European Union's taxonomy regulation? Is the company taxonomy compliant under European Union Taxonomy Regulation?")
344
- ESG_answer_3 = qa.invoke("Explain what items of ESG information the company publishes. Describe what ESG transparency commitments the company has given. Does the company follow the Paris Treaty's obligation to limit globabl warming to 1.5 celcius degrees?")
345
- ESG_answer_4 = qa.invoke("Does the company have carbon emissions reduction plan and has the company reached its carbod dioxide reduction objectives? Set out in a table the company's carbon footprint by location and its development from the context. Set out carbon dioxide emissions in relation to turnover.")
346
- ESG_answer_5 = qa.invoke("Describe and set out in a table the following carbon emissions figures: (i) Scope 1 CO2 emissions, (ii) Scope 2 CO2, and (iii) Scope 3 CO2 emissions. Set out the material changes relating to these figures.")
347
- ESG_answer_6 = qa.invoke("Set out in a table the company's energy and renewable energy usage for each material activity coverning the available years. Explain the energy efficiency measures taken by the company.")
348
- ESG_answer_7 = qa.invoke("Does the company follow UN Guiding Principles on Business and Human Rights, ILO Declaration on Fundamental Principles and Rights at Work or OECD Guidelines for Multinational Enterprises that involve affected communities? Set out the measures taken to have the gender balance on the upper management of the company.")
349
  ESG_answer_8 = qa.invoke("List the environmental permits and certifications held by the company. Set out and explain any environmental procedures and investigations and decisions taken against the company. Answer whether the company's locations or operations are connected to areas sensitive in relation to biodiversity.")
350
- ESG_answer_9 = qa.invoke("Set out waste produces by the company and possible waste into the soil by real estate. Describe if the company's real estates have hazardous waste.")
351
- ESG_answer_10 = qa.invoke("What percentage of women are represented in the (i) board, (ii) executive directors and (iii) upper management?")
352
  ESG_answer_11 = qa.invoke("What policies has the company implemented to counter money laundering and corruption?")
353
 
354
  ESG_output = f"**__Summary of ESG reporting and obligations:__** {ESG_answer_1} \n\n **__Compliance with taxonomy:__** \n\n {ESG_answer_2} \n\n **__Disclosure transparency:__** \n\n {ESG_answer_3} \n\n **__Carbon footprint:__** \n\n {ESG_answer_4} \n\n **__Carbon dioxide emissions:__** \n\n {ESG_answer_5} \n\n **__Renewable energy:__** \n\n {ESG_answer_6} \n\n **__Human rights compliance:__** \n\n {ESG_answer_7} \n\n **__Management and gender balance:__** \n\n {ESG_answer_8} \n\n **__Waste and other emissions:__** {ESG_answer_9} \n\n **__Gender equality:__** {ESG_answer_10} \n\n **__Anti-money laundering:__** {ESG_answer_11}"
@@ -561,7 +519,7 @@ with strategies_container:
561
  Conversation history:
562
  {chat_history}
563
 
564
- Based on the context: answer the following question: {question}.
565
  """
566
  )
567
 
@@ -574,7 +532,7 @@ with strategies_container:
574
  "question": RunnablePassthrough(),
575
  }
576
  | prompt_financials
577
- | llm
578
  | StrOutputParser()
579
  )
580
 
@@ -585,59 +543,39 @@ with strategies_container:
585
  "question": RunnablePassthrough(),
586
  }
587
  | prompt_ESG
588
- | llm
589
  | StrOutputParser()
590
  )
591
 
592
  # Define the tools with LCEL expressions
593
  # Define the vector query engine tool
594
  vector_query_tool_ESG = Tool(
595
- name="Vector Query Engine ESG",
596
- func=lambda query: query_engine.query(query), # Use query_engine to query the vector database
597
- description="Useful for answering questions that require ESG figures, data and statistics.",
598
  )
599
 
600
  vector_query_tool_financials = Tool(
601
- name="Vector Query Engine Financials",
602
- func=lambda query: query_engine_financials.query(query), # Use query_engine to query the vector database
603
- description="Useful for answering questions that require financial figures, data and statistics.",
604
  )
605
 
606
- # Create a function to validate responses
607
- def validate_esg_response(query):
608
- esg_response = vector_query_tool_ESG.func(query)
609
- esg_validation = ESG_chain.invoke({
610
- "context": doc_retriever_ESG,
611
- "chat_history": format_chat_history(memory.load_memory_variables({})["chat_history"]),
612
- "question": esg_response
613
- })
614
- return esg_validation
615
-
616
- def validate_financials_response(query):
617
- financials_response = vector_query_tool_financials.func(query)
618
- financials_validation = financials_chain.invoke({
619
- "context": doc_retriever_financials,
620
- "chat_history": format_chat_history(memory.load_memory_variables({})["chat_history"]),
621
- "question": financials_response
622
- })
623
- return financials_validation
624
-
625
- # Update the tools list to include validation
626
  tools = [
627
  Tool(
628
- name="Search Tool",
629
- func=search.run,
630
- description="Useful when other tools do not provide the answer.",
631
  ),
632
  Tool(
633
- name="Validate ESG Response",
634
- func=validate_esg_response,
635
- description="Validates the response of the Vector Query Engine ESG tool.",
636
  ),
637
  Tool(
638
- name="Validate Financials Response",
639
- func=validate_financials_response,
640
- description="Validates the response of the Vector Query Engine Financials tool.",
641
  ),
642
  vector_query_tool_ESG,
643
  vector_query_tool_financials,
 
32
  from langchain.prompts import PromptTemplate
33
  import joblib
34
  import nltk
 
 
35
 
36
  import nest_asyncio # noqa: E402
37
  nest_asyncio.apply()
 
104
  """
105
 
106
  st.write(css, unsafe_allow_html=True)
107
+ st.sidebar.image('StratXcel.png', width=150)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  #-------------
109
  llm=ChatGroq(groq_api_key=groq_api_key,
110
+ model_name="Llama-3.1-70b-Versatile", temperature = 0.0, streaming=True)
111
+ #model_name="Llama-3.1-70b-Versatile", temperature = 0.0, streaming=True)
112
+
113
+ llm_tool=ChatGroq(groq_api_key=groq_api_key,
114
+ model_name="llama3-groq-70b-8192-tool-use-preview", temperature = 0.0, streaming=True)
115
+ #model_name="Llama-3.1-70b-Versatile", temperature = 0.0, streaming=True)
116
  #--------------
117
  doc_retriever_ESG = None
118
  doc_retriever_financials = None
 
124
 
125
  parsingInstructionUber10k = """The provided document contain detailed information about the company's environmental, social and governance matters.
126
  It contains several tables, figures and statistical information about CO2 emissions and energy consumption.
127
+ Give only precide CO2 and energy consumption levels inly from the context documents.
128
  You must never provide false numeric or statistical data that is not included in the context document.
129
  Include tables and numeric data always when possible. Only refer to other sources if the context document refers to them or if necessary to provide additional understanding to company's own data."""
130
 
 
298
  )
299
 
300
  ESG_answer_1 = qa.invoke("Give a summary what specific ESG measures the company has taken recently and compare these to the best practices.")
301
+ ESG_answer_2 = qa.invoke("Does the company's main business fall under the European Union's taxonomy regulation? Answer whether the company is taxonomy compliant under European Union Taxonomy Regulation?")
302
+ ESG_answer_3 = qa.invoke("Describe what specific ESG transparency commitments the company has given. Give details how the company has followed the Paris Treaty's obligation to limit globabl warming to 1.5 celcius degrees.")
303
+ ESG_answer_4 = qa.invoke("Does the company have carbon emissions reduction plan? Has the company reached its carbod dioxide reduction objectives? Set out in a table the company's carbon footprint by location and its development or equivalent figures. List carbon dioxide emissions in relation to turnover.")
304
+ ESG_answer_5 = qa.invoke("Describe and set out in a table the following specific information: (i) Scope 1 CO2 emissions, (ii) Scope 2 CO2 emissions, and (iii) Scope 3 CO2 emissions of the company for 2021, 2022 and 2023. List the material changes relating to these figures.")
305
+ ESG_answer_6 = qa.invoke("List in a table the company's energy and renewable energy usage for each material activity. Explain the main energy efficiency measures taken by the company.")
306
+ ESG_answer_7 = qa.invoke("Does the company follow UN Guiding Principles on Business and Human Rights, ILO Declaration on Fundamental Principles and Rights at Work or OECD Guidelines for Multinational Enterprises that involve affected communities?")
307
  ESG_answer_8 = qa.invoke("List the environmental permits and certifications held by the company. Set out and explain any environmental procedures and investigations and decisions taken against the company. Answer whether the company's locations or operations are connected to areas sensitive in relation to biodiversity.")
308
+ ESG_answer_9 = qa.invoke("Set out waste management produces by the company and possible waste into the soil. Describe if the company's real estates have hazardous waste.")
309
+ ESG_answer_10 = qa.invoke("What percentage of women are represented in the (i) board, (ii) executive directors and (iii) upper management? Set out the measures taken to have the gender balance on the upper management of the company.")
310
  ESG_answer_11 = qa.invoke("What policies has the company implemented to counter money laundering and corruption?")
311
 
312
  ESG_output = f"**__Summary of ESG reporting and obligations:__** {ESG_answer_1} \n\n **__Compliance with taxonomy:__** \n\n {ESG_answer_2} \n\n **__Disclosure transparency:__** \n\n {ESG_answer_3} \n\n **__Carbon footprint:__** \n\n {ESG_answer_4} \n\n **__Carbon dioxide emissions:__** \n\n {ESG_answer_5} \n\n **__Renewable energy:__** \n\n {ESG_answer_6} \n\n **__Human rights compliance:__** \n\n {ESG_answer_7} \n\n **__Management and gender balance:__** \n\n {ESG_answer_8} \n\n **__Waste and other emissions:__** {ESG_answer_9} \n\n **__Gender equality:__** {ESG_answer_10} \n\n **__Anti-money laundering:__** {ESG_answer_11}"
 
519
  Conversation history:
520
  {chat_history}
521
 
522
+ Based on the context: {context}, answer the following question: {question}.
523
  """
524
  )
525
 
 
532
  "question": RunnablePassthrough(),
533
  }
534
  | prompt_financials
535
+ | llm_tool
536
  | StrOutputParser()
537
  )
538
 
 
543
  "question": RunnablePassthrough(),
544
  }
545
  | prompt_ESG
546
+ | llm_tool
547
  | StrOutputParser()
548
  )
549
 
550
  # Define the tools with LCEL expressions
551
  # Define the vector query engine tool
552
  vector_query_tool_ESG = Tool(
553
+ name="Vector Query Engine ESG",
554
+ func=lambda query: query_engine.query(query), # Use query_engine to query the vector database
555
+ description="Useful for answering questions about specific ESG figures, data and statistics.",
556
  )
557
 
558
  vector_query_tool_financials = Tool(
559
+ name="Vector Query Engine Financials",
560
+ func=lambda query: query_engine_financials.query(query), # Use query_engine to query the vector database
561
+ description="Useful for answering questions about specific financial figures, data and statistics.",
562
  )
563
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
564
  tools = [
565
  Tool(
566
+ name="ESG QA System",
567
+ func=ESG_chain.invoke,
568
+ description="Useful for answering general questions about environmental, social, and governance (ESG) matters related to the company. ",
569
  ),
570
  Tool(
571
+ name="Financials QA System",
572
+ func=financials_chain.invoke,
573
+ description="Useful for answering general questions about financial or operational information concerning the company.",
574
  ),
575
  Tool(
576
+ name="Search Tool",
577
+ func=search.run,
578
+ description="Useful when other tools do not provide the answer.",
579
  ),
580
  vector_query_tool_ESG,
581
  vector_query_tool_financials,