import boto3
from botocore.exceptions import ClientError
import json
import langchain
from importlib import reload
from langchain.agents.structured_chat import output_parser
from typing import List
import logging
import os
import sqlalchemy
from sqlalchemy import create_engine
from langchain.docstore.document import Document
from langchain import PromptTemplate,SQLDatabase, LLMChain
from langchain_experimental.sql.base import SQLDatabaseChain
from langchain.prompts.prompt import PromptTemplate
import streamlit as st
import pandas as pd
import datetime
from langchain.tools import tool
from typing import List, Optional
import json
from langchain.prompts import (
    ChatPromptTemplate,
    PromptTemplate,
    SystemMessagePromptTemplate,
    AIMessagePromptTemplate,
    HumanMessagePromptTemplate,
)
from langchain.llms.bedrock import Bedrock
from langchain_experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner
from langchain.agents.tools import Tool
import time
import uuid
from utility import get_cfn_details,custom_logga, upload_amz_file
from langchain.tools.python.tool import PythonREPLTool
from langchain.memory import ConversationBufferMemory
from langchain.memory.chat_message_histories import DynamoDBChatMessageHistory
from streamlit.web.server.websocket_headers import _get_websocket_headers
import sys 

st.set_page_config(layout="wide")
# logger = logging.getLogger('sagemaker')
# logger.setLevel(logging.DEBUG)
# logger.addHandler(logging.StreamHandler())

sys.stdout = custom_logga.Logger()


#Session states to hold sateful variables
if 'generated' not in st.session_state:
    st.session_state['generated'] = []
if 'past' not in st.session_state:
    st.session_state['past'] = []
if 'messages' not in st.session_state:
    st.session_state['messages'] = []
if 'ant_key' not in st.session_state:
    st.session_state['ant_key'] = ''
if 'chat_id' not in st.session_state:
    st.session_state['chat_id'] = 1
if 'client_id' not in st.session_state:
    st.session_state['client_id'] = ''
if 'prompt' not in st.session_state:
    st.session_state['prompt'] = ''
if 'memory' not in st.session_state:
    st.session_state['memory'] = ""
    
# Global Variables
STACK_NAME="mmfsi" #change to the name of the cloudformation stack
REGION='us-east-1' #change to the name of the region you are working in

    
if len(st.session_state['messages'])<1:  
    ## browser client info
    headers = _get_websocket_headers()
    st.session_state['client_id'] = str(headers.get("Sec-Websocket-Key"))
    #print(f"Client KEY {st.session_state['client_id']}")
    

    #st.session_state['ant_key']= get_secret()(REGION, "Secrete Name") ## PASS the AWS SECRETES secrete name 
    st.session_state['chat_id']= st.session_state['chat_id']+1
    #print(f"Session Chat ID {st.session_state['chat_id']}")
    
    # get cfn parameters      
    glue_db_name,kendra_index_id,audio_transcripts_source_bucket,textract_source_bucket,query_staging_bucket,multimodal_output_bucket=get_cfn_details.stack_info(STACK_NAME,REGION)
    param={}
    param['db']=glue_db_name
    param['query_bucket']=query_staging_bucket
    param['region']=REGION
    param['kendra_id']=kendra_index_id#'45739a4f-c80f-4201-b183-20389d0febc7'
    
    #Store parameters in json file
    with open('param.json', 'w', encoding='utf-8') as f:
        json.dump(param, f, ensure_ascii=False, indent=4)
        
    
    # upload files to s3
    #from utility.upload_amz_file import upload_file_amz
    upload_amz_file.upload_file_amz('files/Amazon-10K-2022-EarningsReport.pdf', textract_source_bucket)
    upload_amz_file.upload_file_amz('files/Amazon-10Q-Q1-2023-QuaterlyEarningsReport.pdf', textract_source_bucket)
    upload_amz_file.upload_file_amz('files/Amazon-Quarterly-Earnings-Report-Q1-2023-Full-Call-v1.mp3', audio_transcripts_source_bucket)   

    
    #Athena connection config
    connathena=f"athena.{REGION}.amazonaws.com" 
    portathena='443' #Update, if port is different
    schemaathena=glue_db_name #from user defined params
    s3stagingathena=f's3://{query_staging_bucket}/athenaresults/'#from cfn params
    wkgrpathena='primary'#Update, if workgroup is different

    ##  Create the athena connection string
    connection_string = f"awsathena+rest://@{connathena}:{portathena}/{schemaathena}?s3_staging_dir={s3stagingathena}&work_group={wkgrpathena}"

    ##  Create the athena  SQLAlchemy engine
    engine_athena = create_engine(connection_string, echo=False)
    dbathena = SQLDatabase(engine_athena)
       

from botocore.config import Config

config = Config(
    retries = dict(
        max_attempts = 10
    )
)

from utility import stock_query_mm, kendra_tool_mm, aws_tools, portfolio_tool
inference_modifier = {
    'max_tokens_to_sample':512,                  
                      "temperature":0.01,
                      "stop_sequences":["\n\nQuestion:","\n\nHuman:","\nHuman:"]#"\n\nAssistant:","\nAssistant:"]#,"\nHuman:"]#,"\n\nAssistant:","\nAssistant:"],
                    #  "top_k": 50,
                    # "top_p": 1,
  
                     }
llm = llm = Bedrock(model_id='anthropic.claude-v2',model_kwargs =inference_modifier )
table = 'stock_prices'
session_id=st.session_state['client_id']
chat_id= st.session_state['chat_id']

#persist dynamodb table id for chat history for each session and browser client
@st.cache_data
def db_table_id(session_id, chat_id):
    chat_sess_id=str(uuid.uuid4())
    return chat_sess_id

chat_session_id=db_table_id(session_id, chat_id)
#print(f"Chat SESSION ID {chat_session_id}")

def run_query(query):

    PROMPT_sql = PromptTemplate(
        input_variables=["input", "table_info", "dialect"], template=_DEFAULT_TEMPLATE
    )
    
    db_chain = SQLDatabaseChain.from_llm(llm, dbathena, prompt=PROMPT_sql, verbose=True, return_intermediate_steps=False)
    response=db_chain.run(query)
    
    return response

def SentimentAnalysis(inputString):
    print(inputString)
    lambda_client = boto3.client('lambda', region_name=REGION)
    lambda_payload = {"inputString:"+inputString}
    response=lambda_client.invoke(FunctionName='FSI-SentimentDetecttion',
                        InvocationType='RequestResponse',
                     Payload=json.dumps(inputString))
    #print(response['Payload'].read())
    output=json.loads(response['Payload'].read().decode())
    return output['body']

def DetectKeyPhrases(inputString):
    #print(inputString)
    lambda_client = boto3.client('lambda', region_name=REGION)
    lambda_payload = {"inputString:"+inputString}
    response=lambda_client.invoke(FunctionName='FSI-KeyPhrasesDetection',
                        InvocationType='RequestResponse',
                     Payload=json.dumps(inputString))
    #print(response['Payload'].read())
    output=json.loads(response['Payload'].read().decode())
    return output['body']


tools = [
    Tool(
        name="Stock Querying Tool",
        func=stock_query_mm.run_query,
        description="""
        Useful for when you need to answer questions about stocks. It only has information about stocks.
        """
    ),
    portfolio_tool.OptimizePortfolio(),
    Tool(
        name="Financial Information Lookup Tool",
        func=kendra_tool_mm.run_chain,
        description="""
        Useful for when you need to look up financial information like revenues, sales, loss, risks etc. 
        """
    ),
    PythonREPLTool(),
    Tool(
        name="Sentiment Analysis Tool",
        func=SentimentAnalysis,
        description="""
        Useful for when you need to analyze the sentiment of an excerpt from a financial report.
        """
    ),
     Tool(
        name="Detect Phrases Tool",
        func=DetectKeyPhrases,
        description="""
        Useful for when you need to detect key phrases in financial reports.
        """
    ),
     Tool(
        name="Text Extraction Tool",
        func=aws_tools.IntiateTextExtractProcessing,
        description="""
        Useful for when you need to trigger conversion of  pdf version of quaterly reports to text files using amazon textextract
        """
    ),
     Tool(
        name="Transcribe Audio Tool",
        func=aws_tools.TranscribeAudio,
        description="""
        Useful for when you need to convert audio recordings of earnings calls from audio to text format using Amazon Transcribe
        """
    )
]

combo_template = """\n\nHuman:
You are a Minimization Solutionist with a set of tools at your disposal.

You would be presented with a problem. First understand the problem and devise a plan to solve the problem. 
Please output the plan starting with the header 'Plan:' and then followed by a numbered list of steps.        
Ensure the plan has the minimum amount of steps needed to solve the problem. Do not include unnecessary steps.

<instructions>
These are guidance on when to use a tool to solve a task, follow them strictly:
1. For the tool that specifically focuses on stock price data, use "Stock Query Tool".    
2. For financial information lookup that covers various financial data like company's finance, performance or any other information pertaining a company beyond stocks, use the "Financial Data Explorer Tool". Ask specific questions using this tool as it is your knowledge database. Refrain from asking question like "look up 10K filings" instead a more specific question like "what is the revenue for this company". 
3. When you need to find key phrases in a report, use the "Detect Phrases Tool" to get the information about all key phrases and respond with key phrases relavent to the question. 
4. When you need to provide an optimized stock portfolio based on stock names, use Portfolio Optimization Tool. The output is the percent of fund you should spend on each stock. This tool only takes stock ticker as input and not stock prices, for example ["EWR","JHT"].
5. Please use the PythonREPLTool exclusively for calculations, refrain from utilizing 'print' statements for output. Use this too only when needed, most times its unnecessary.
6. When you need to analyze sentiment of a topic, use "Sentiment Analysis Tool".
</instructions>\n\nAssistant:"""

combo_template=combo_template if st.session_state['prompt']=="" else st.session_state['prompt']
chat_history_table = 'DYNAMODB table name' ### SPECIFY THE DYNAMODB TABLE
chat_history_memory = DynamoDBChatMessageHistory(table_name=chat_history_table, session_id=chat_session_id)
model = llm
planner = load_chat_planner(model)

system_message_prompt = SystemMessagePromptTemplate.from_template(combo_template)
human_message_prompt = planner.llm_chain.prompt.messages[1]
planner.llm_chain.prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])

executor = load_agent_executor(model, tools, verbose=True)
if st.session_state['memory']:
    
    
    memory = ConversationBufferMemory(memory_key="chat_history", chat_memory=chat_history_memory, return_messages=True)
    agent = PlanAndExecute(planner=planner, executor=executor, verbose=True, max_iterations=2, memory=memory)
else:
    agent = PlanAndExecute(planner=planner, executor=executor, verbose=True, max_iterations=2)#, memory=memory)


def query(request, agent, chat_history_memory):
    output=agent(request)
    chat_history_memory.add_ai_message(str(output))
    try:
        return output['output']
    except:
        return output



    
def action_doc(agent, chat_history_memory):
    st.title('Multi-Modal Agent to assist Financial Analyst')

    # Display chat messages from history on app rerun
    for message in st.session_state.messages:
        if "role" in message.keys():
            with st.chat_message(message["role"]):            
                st.markdown(message['content'].replace("$","USD ").replace("%", " percent"))
                
        else:
            with st.expander(label="**Intermediate Steps**"):
                st.write(message["steps"])
            
    if prompt := st.chat_input("Hello?"):
        st.session_state.messages.append({"role": "user", "content": prompt})
        with st.chat_message("user"):
            st.markdown(prompt)
            
            
        with st.chat_message("assistant"):
            message_placeholder = st.empty()
            output_answer=query(prompt, agent, chat_history_memory) 
            message_placeholder.markdown(output_answer.replace("$","USD ").replace("%", " percent"))
        st.session_state.messages.append({"role": "assistant", "content": output_answer})
        
        # Saving the intermediate steps in a logf file to be shown in the UI. This is a hack due to the inability to capture these steps with the agent planner and executor library being used
        with st.expander(label="**Intermediate Steps**"): 
            with open('logfile.txt','r')as f:
                steps=f.readlines()
                st.write(steps)
            os.remove('logfile.txt')
        st.session_state.messages.append({"steps": steps})
      
            
        
                    
            
def app_sidebar():
    with st.sidebar:
        st.write('## How to use:')
        description = """This app lets you query multi-modal documents and get relevant answers. 
                        Documents inculde DB Tables, audio files and pdf files.
                        Type your query in the chat box to get appropiate answers.
                        If you need to refresh session, click on the `Clear Session` button.
                        Happy QnA :)
                        """
        st.markdown(description)
        st.write('---')
        st.write('## Sample Questions')
        st.markdown("""
                    - What are the closing prices of stocks AAAA, WWW, DDD in year 2018? Can you build an optimized portfolio using these three stocks? Please provide answers to both questions.
                    - What is the net sales for Amazon in 2021 and 2022? What is the percent difference?
                    - What are the biggest risks facing Amazon Inc?                                  
                    """)
        st.markdown("""
                    **Datasets**
                    
                    - [Quterly Earnings recordings](https://github.com/revdotcom/speech-datasets)
                    - [Annual Reports (FinTabNet)](https://developer.ibm.com/exchanges/data/all/fintabnet/)
                    - [S&P 500 stock data](https://www.kaggle.com/camnugent/sandp500)
                    """)
        st.write('---')
        #st.write('Pass your custom prompt')
        user_input = st.text_area("Custom prompt goes here", "")
        if user_input:
            st.session_state['prompt']=user_input
        print(user_input)
        
        use_memory=''
        mem = st.checkbox('Conversation Memory')
        if mem:
            use_memory='yes' 
        st.session_state['memory']=use_memory
            
        if st.button('Clear Session'):
            '''
                The Clear context helps to refresh the UI and also create a new session for the chat. This creates a new Dynamo DB table to                   hold the chat history.
            ''' 
            # Delete all the items in Session state
            for key in st.session_state.keys():
                del st.session_state[key] 
            # create new session state items
            if 'generated' not in st.session_state:
                st.session_state['generated'] = []
            if 'past' not in st.session_state:
                st.session_state['past'] = []
            if 'messages' not in st.session_state:
                st.session_state['messages'] = []
            if 'ant_key' not in st.session_state:
                st.session_state['ant_key'] = ''
            if 'chat_id' not in st.session_state:
                st.session_state['chat_id'] = 1
            if 'client_id' not in st.session_state:
                st.session_state['client_id'] = ''
            if 'prompt' not in st.session_state:
                st.session_state['prompt'] = ""
            if 'memory' not in st.session_state:
                st.session_state['memory'] = ""

def main(agent,chat_history_memory):
    params=app_sidebar()
    action_doc(agent, chat_history_memory)


if __name__ == '__main__':
    main(agent, chat_history_memory)

langchain.memory.ConversationBufferMemory langchain.prompts.ChatPromptTemplate.from_messages langchain.tools.python.tool.PythonREPLTool langchain.llms.bedrock.Bedrock langchain.prompts.PromptTemplate langchain_experimental.plan_and_execute.load_chat_planner langchain_experimental.plan_and_execute.PlanAndExecute langchain.memory.chat_message_histories.DynamoDBChatMessageHistory langchain.SQLDatabase langchain.agents.tools.Tool langchain.prompts.SystemMessagePromptTemplate.from_template langchain_experimental.plan_and_execute.load_agent_executor langchain_experimental.sql.base.SQLDatabaseChain.from_llm
[((1364, 1397), 'streamlit.set_page_config', 'st.set_page_config', ([], {'layout': '"""wide"""'}), "(layout='wide')\n", (1382, 1397), True, 'import streamlit as st\n'), ((1532, 1553), 'utility.custom_logga.Logger', 'custom_logga.Logger', ([], {}), '()\n', (1551, 1553), False, 'from utility import get_cfn_details, custom_logga, upload_amz_file\n'), ((5121, 5193), 'langchain.llms.bedrock.Bedrock', 'Bedrock', ([], {'model_id': '"""anthropic.claude-v2"""', 'model_kwargs': 'inference_modifier'}), "(model_id='anthropic.claude-v2', model_kwargs=inference_modifier)\n", (5128, 5193), False, 'from langchain.llms.bedrock import Bedrock\n'), ((10370, 10460), 'langchain.memory.chat_message_histories.DynamoDBChatMessageHistory', 'DynamoDBChatMessageHistory', ([], {'table_name': 'chat_history_table', 'session_id': 'chat_session_id'}), '(table_name=chat_history_table, session_id=\n    chat_session_id)\n', (10396, 10460), False, 'from langchain.memory.chat_message_histories import DynamoDBChatMessageHistory\n'), ((10478, 10502), 'langchain_experimental.plan_and_execute.load_chat_planner', 'load_chat_planner', (['model'], {}), '(model)\n', (10495, 10502), False, 'from langchain_experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner\n'), ((10528, 10585), 'langchain.prompts.SystemMessagePromptTemplate.from_template', 'SystemMessagePromptTemplate.from_template', (['combo_template'], {}), '(combo_template)\n', (10569, 10585), False, 'from langchain.prompts import ChatPromptTemplate, PromptTemplate, SystemMessagePromptTemplate, AIMessagePromptTemplate, HumanMessagePromptTemplate\n'), ((10673, 10752), 'langchain.prompts.ChatPromptTemplate.from_messages', 'ChatPromptTemplate.from_messages', (['[system_message_prompt, human_message_prompt]'], {}), '([system_message_prompt, human_message_prompt])\n', (10705, 10752), False, 'from langchain.prompts import ChatPromptTemplate, PromptTemplate, SystemMessagePromptTemplate, AIMessagePromptTemplate, HumanMessagePromptTemplate\n'), ((10765, 10812), 'langchain_experimental.plan_and_execute.load_agent_executor', 'load_agent_executor', (['model', 'tools'], {'verbose': '(True)'}), '(model, tools, verbose=True)\n', (10784, 10812), False, 'from langchain_experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner\n'), ((2449, 2473), 'streamlit.web.server.websocket_headers._get_websocket_headers', '_get_websocket_headers', ([], {}), '()\n', (2471, 2473), False, 'from streamlit.web.server.websocket_headers import _get_websocket_headers\n'), ((3016, 3062), 'utility.get_cfn_details.stack_info', 'get_cfn_details.stack_info', (['STACK_NAME', 'REGION'], {}), '(STACK_NAME, REGION)\n', (3042, 3062), False, 'from utility import get_cfn_details, custom_logga, upload_amz_file\n'), ((3511, 3614), 'utility.upload_amz_file.upload_file_amz', 'upload_amz_file.upload_file_amz', (['"""files/Amazon-10K-2022-EarningsReport.pdf"""', 'textract_source_bucket'], {}), "('files/Amazon-10K-2022-EarningsReport.pdf',\n    textract_source_bucket)\n", (3542, 3614), False, 'from utility import get_cfn_details, custom_logga, upload_amz_file\n'), ((3615, 3734), 'utility.upload_amz_file.upload_file_amz', 'upload_amz_file.upload_file_amz', (['"""files/Amazon-10Q-Q1-2023-QuaterlyEarningsReport.pdf"""', 'textract_source_bucket'], {}), "(\n    'files/Amazon-10Q-Q1-2023-QuaterlyEarningsReport.pdf',\n    textract_source_bucket)\n", (3646, 3734), False, 'from utility import get_cfn_details, custom_logga, upload_amz_file\n'), ((3730, 3870), 'utility.upload_amz_file.upload_file_amz', 'upload_amz_file.upload_file_amz', (['"""files/Amazon-Quarterly-Earnings-Report-Q1-2023-Full-Call-v1.mp3"""', 'audio_transcripts_source_bucket'], {}), "(\n    'files/Amazon-Quarterly-Earnings-Report-Q1-2023-Full-Call-v1.mp3',\n    audio_transcripts_source_bucket)\n", (3761, 3870), False, 'from utility import get_cfn_details, custom_logga, upload_amz_file\n'), ((4454, 4498), 'sqlalchemy.create_engine', 'create_engine', (['connection_string'], {'echo': '(False)'}), '(connection_string, echo=False)\n', (4467, 4498), False, 'from sqlalchemy import create_engine\n'), ((4514, 4540), 'langchain.SQLDatabase', 'SQLDatabase', (['engine_athena'], {}), '(engine_athena)\n', (4525, 4540), False, 'from langchain import PromptTemplate, SQLDatabase, LLMChain\n'), ((5625, 5724), 'langchain.prompts.PromptTemplate', 'PromptTemplate', ([], {'input_variables': "['input', 'table_info', 'dialect']", 'template': '_DEFAULT_TEMPLATE'}), "(input_variables=['input', 'table_info', 'dialect'], template\n    =_DEFAULT_TEMPLATE)\n", (5639, 5724), False, 'from langchain.prompts import ChatPromptTemplate, PromptTemplate, SystemMessagePromptTemplate, AIMessagePromptTemplate, HumanMessagePromptTemplate\n'), ((5754, 5864), 'langchain_experimental.sql.base.SQLDatabaseChain.from_llm', 'SQLDatabaseChain.from_llm', (['llm', 'dbathena'], {'prompt': 'PROMPT_sql', 'verbose': '(True)', 'return_intermediate_steps': '(False)'}), '(llm, dbathena, prompt=PROMPT_sql, verbose=True,\n    return_intermediate_steps=False)\n', (5779, 5864), False, 'from langchain_experimental.sql.base import SQLDatabaseChain\n'), ((5999, 6041), 'boto3.client', 'boto3.client', (['"""lambda"""'], {'region_name': 'REGION'}), "('lambda', region_name=REGION)\n", (6011, 6041), False, 'import boto3\n'), ((6482, 6524), 'boto3.client', 'boto3.client', (['"""lambda"""'], {'region_name': 'REGION'}), "('lambda', region_name=REGION)\n", (6494, 6524), False, 'import boto3\n'), ((6901, 7113), 'langchain.agents.tools.Tool', 'Tool', ([], {'name': '"""Stock Querying Tool"""', 'func': 'stock_query_mm.run_query', 'description': '"""\n        Useful for when you need to answer questions about stocks. It only has information about stocks.\n        """'}), '(name=\'Stock Querying Tool\', func=stock_query_mm.run_query, description\n    =\n    """\n        Useful for when you need to answer questions about stocks. It only has information about stocks.\n        """\n    )\n', (6905, 7113), False, 'from langchain.agents.tools import Tool\n'), ((7134, 7168), 'utility.portfolio_tool.OptimizePortfolio', 'portfolio_tool.OptimizePortfolio', ([], {}), '()\n', (7166, 7168), False, 'from utility import stock_query_mm, kendra_tool_mm, aws_tools, portfolio_tool\n'), ((7174, 7401), 'langchain.agents.tools.Tool', 'Tool', ([], {'name': '"""Financial Information Lookup Tool"""', 'func': 'kendra_tool_mm.run_chain', 'description': '"""\n        Useful for when you need to look up financial information like revenues, sales, loss, risks etc. \n        """'}), '(name=\'Financial Information Lookup Tool\', func=kendra_tool_mm.\n    run_chain, description=\n    """\n        Useful for when you need to look up financial information like revenues, sales, loss, risks etc. \n        """\n    )\n', (7178, 7401), False, 'from langchain.agents.tools import Tool\n'), ((7422, 7438), 'langchain.tools.python.tool.PythonREPLTool', 'PythonREPLTool', ([], {}), '()\n', (7436, 7438), False, 'from langchain.tools.python.tool import PythonREPLTool\n'), ((7444, 7640), 'langchain.agents.tools.Tool', 'Tool', ([], {'name': '"""Sentiment Analysis Tool"""', 'func': 'SentimentAnalysis', 'description': '"""\n        Useful for when you need to analyze the sentiment of an excerpt from a financial report.\n        """'}), '(name=\'Sentiment Analysis Tool\', func=SentimentAnalysis, description=\n    """\n        Useful for when you need to analyze the sentiment of an excerpt from a financial report.\n        """\n    )\n', (7448, 7640), False, 'from langchain.agents.tools import Tool\n'), ((7667, 7838), 'langchain.agents.tools.Tool', 'Tool', ([], {'name': '"""Detect Phrases Tool"""', 'func': 'DetectKeyPhrases', 'description': '"""\n        Useful for when you need to detect key phrases in financial reports.\n        """'}), '(name=\'Detect Phrases Tool\', func=DetectKeyPhrases, description=\n    """\n        Useful for when you need to detect key phrases in financial reports.\n        """\n    )\n', (7671, 7838), False, 'from langchain.agents.tools import Tool\n'), ((7865, 8117), 'langchain.agents.tools.Tool', 'Tool', ([], {'name': '"""Text Extraction Tool"""', 'func': 'aws_tools.IntiateTextExtractProcessing', 'description': '"""\n        Useful for when you need to trigger conversion of  pdf version of quaterly reports to text files using amazon textextract\n        """'}), '(name=\'Text Extraction Tool\', func=aws_tools.\n    IntiateTextExtractProcessing, description=\n    """\n        Useful for when you need to trigger conversion of  pdf version of quaterly reports to text files using amazon textextract\n        """\n    )\n', (7869, 8117), False, 'from langchain.agents.tools import Tool\n'), ((8139, 8377), 'langchain.agents.tools.Tool', 'Tool', ([], {'name': '"""Transcribe Audio Tool"""', 'func': 'aws_tools.TranscribeAudio', 'description': '"""\n        Useful for when you need to convert audio recordings of earnings calls from audio to text format using Amazon Transcribe\n        """'}), '(name=\'Transcribe Audio Tool\', func=aws_tools.TranscribeAudio,\n    description=\n    """\n        Useful for when you need to convert audio recordings of earnings calls from audio to text format using Amazon Transcribe\n        """\n    )\n', (8143, 8377), False, 'from langchain.agents.tools import Tool\n'), ((10867, 10978), 'langchain.memory.ConversationBufferMemory', 'ConversationBufferMemory', ([], {'memory_key': '"""chat_history"""', 'chat_memory': 'chat_history_memory', 'return_messages': '(True)'}), "(memory_key='chat_history', chat_memory=\n    chat_history_memory, return_messages=True)\n", (10891, 10978), False, 'from langchain.memory import ConversationBufferMemory\n'), ((10986, 11087), 'langchain_experimental.plan_and_execute.PlanAndExecute', 'PlanAndExecute', ([], {'planner': 'planner', 'executor': 'executor', 'verbose': '(True)', 'max_iterations': '(2)', 'memory': 'memory'}), '(planner=planner, executor=executor, verbose=True,\n    max_iterations=2, memory=memory)\n', (11000, 11087), False, 'from langchain_experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner\n'), ((11102, 11188), 'langchain_experimental.plan_and_execute.PlanAndExecute', 'PlanAndExecute', ([], {'planner': 'planner', 'executor': 'executor', 'verbose': '(True)', 'max_iterations': '(2)'}), '(planner=planner, executor=executor, verbose=True,\n    max_iterations=2)\n', (11116, 11188), False, 'from langchain_experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner\n'), ((11461, 11518), 'streamlit.title', 'st.title', (['"""Multi-Modal Agent to assist Financial Analyst"""'], {}), "('Multi-Modal Agent to assist Financial Analyst')\n", (11469, 11518), True, 'import streamlit as st\n'), ((3361, 3410), 'json.dump', 'json.dump', (['param', 'f'], {'ensure_ascii': '(False)', 'indent': '(4)'}), '(param, f, ensure_ascii=False, indent=4)\n', (3370, 3410), False, 'import json\n'), ((5451, 5463), 'uuid.uuid4', 'uuid.uuid4', ([], {}), '()\n', (5461, 5463), False, 'import uuid\n'), ((11979, 12002), 'streamlit.chat_input', 'st.chat_input', (['"""Hello?"""'], {}), "('Hello?')\n", (11992, 12002), True, 'import streamlit as st\n'), ((12012, 12081), 'streamlit.session_state.messages.append', 'st.session_state.messages.append', (["{'role': 'user', 'content': prompt}"], {}), "({'role': 'user', 'content': prompt})\n", (12044, 12081), True, 'import streamlit as st\n'), ((12444, 12529), 'streamlit.session_state.messages.append', 'st.session_state.messages.append', (["{'role': 'assistant', 'content': output_answer}"], {}), "({'role': 'assistant', 'content':\n    output_answer})\n", (12476, 12529), True, 'import streamlit as st\n'), ((12945, 12995), 'streamlit.session_state.messages.append', 'st.session_state.messages.append', (["{'steps': steps}"], {}), "({'steps': steps})\n", (12977, 12995), True, 'import streamlit as st\n'), ((13107, 13133), 'streamlit.write', 'st.write', (['"""## How to use:"""'], {}), "('## How to use:')\n", (13115, 13133), True, 'import streamlit as st\n'), ((13561, 13585), 'streamlit.markdown', 'st.markdown', (['description'], {}), '(description)\n', (13572, 13585), True, 'import streamlit as st\n'), ((13594, 13609), 'streamlit.write', 'st.write', (['"""---"""'], {}), "('---')\n", (13602, 13609), True, 'import streamlit as st\n'), ((13618, 13649), 'streamlit.write', 'st.write', (['"""## Sample Questions"""'], {}), "('## Sample Questions')\n", (13626, 13649), True, 'import streamlit as st\n'), ((13658, 14109), 'streamlit.markdown', 'st.markdown', (['"""\n                    - What are the closing prices of stocks AAAA, WWW, DDD in year 2018? Can you build an optimized portfolio using these three stocks? Please provide answers to both questions.\n                    - What is the net sales for Amazon in 2021 and 2022? What is the percent difference?\n                    - What are the biggest risks facing Amazon Inc?                                  \n                    """'], {}), '(\n    """\n                    - What are the closing prices of stocks AAAA, WWW, DDD in year 2018? Can you build an optimized portfolio using these three stocks? Please provide answers to both questions.\n                    - What is the net sales for Amazon in 2021 and 2022? What is the percent difference?\n                    - What are the biggest risks facing Amazon Inc?                                  \n                    """\n    )\n', (13669, 14109), True, 'import streamlit as st\n'), ((14108, 14504), 'streamlit.markdown', 'st.markdown', (['"""\n                    **Datasets**\n                    \n                    - [Quterly Earnings recordings](https://github.com/revdotcom/speech-datasets)\n                    - [Annual Reports (FinTabNet)](https://developer.ibm.com/exchanges/data/all/fintabnet/)\n                    - [S&P 500 stock data](https://www.kaggle.com/camnugent/sandp500)\n                    """'], {}), '(\n    """\n                    **Datasets**\n                    \n                    - [Quterly Earnings recordings](https://github.com/revdotcom/speech-datasets)\n                    - [Annual Reports (FinTabNet)](https://developer.ibm.com/exchanges/data/all/fintabnet/)\n                    - [S&P 500 stock data](https://www.kaggle.com/camnugent/sandp500)\n                    """\n    )\n', (14119, 14504), True, 'import streamlit as st\n'), ((14503, 14518), 'streamlit.write', 'st.write', (['"""---"""'], {}), "('---')\n", (14511, 14518), True, 'import streamlit as st\n'), ((14585, 14628), 'streamlit.text_area', 'st.text_area', (['"""Custom prompt goes here"""', '""""""'], {}), "('Custom prompt goes here', '')\n", (14597, 14628), True, 'import streamlit as st\n'), ((14773, 14807), 'streamlit.checkbox', 'st.checkbox', (['"""Conversation Memory"""'], {}), "('Conversation Memory')\n", (14784, 14807), True, 'import streamlit as st\n'), ((14924, 14950), 'streamlit.button', 'st.button', (['"""Clear Session"""'], {}), "('Clear Session')\n", (14933, 14950), True, 'import streamlit as st\n'), ((6253, 6276), 'json.dumps', 'json.dumps', (['inputString'], {}), '(inputString)\n', (6263, 6276), False, 'import json\n'), ((6736, 6759), 'json.dumps', 'json.dumps', (['inputString'], {}), '(inputString)\n', (6746, 6759), False, 'import json\n'), ((12095, 12118), 'streamlit.chat_message', 'st.chat_message', (['"""user"""'], {}), "('user')\n", (12110, 12118), True, 'import streamlit as st\n'), ((12132, 12151), 'streamlit.markdown', 'st.markdown', (['prompt'], {}), '(prompt)\n', (12143, 12151), True, 'import streamlit as st\n'), ((12191, 12219), 'streamlit.chat_message', 'st.chat_message', (['"""assistant"""'], {}), "('assistant')\n", (12206, 12219), True, 'import streamlit as st\n'), ((12255, 12265), 'streamlit.empty', 'st.empty', ([], {}), '()\n', (12263, 12265), True, 'import streamlit as st\n'), ((12740, 12783), 'streamlit.expander', 'st.expander', ([], {'label': '"""**Intermediate Steps**"""'}), "(label='**Intermediate Steps**')\n", (12751, 12783), True, 'import streamlit as st\n'), ((12912, 12936), 'os.remove', 'os.remove', (['"""logfile.txt"""'], {}), "('logfile.txt')\n", (12921, 12936), False, 'import os\n'), ((15241, 15264), 'streamlit.session_state.keys', 'st.session_state.keys', ([], {}), '()\n', (15262, 15264), True, 'import streamlit as st\n'), ((11674, 11706), 'streamlit.chat_message', 'st.chat_message', (["message['role']"], {}), "(message['role'])\n", (11689, 11706), True, 'import streamlit as st\n'), ((11861, 11904), 'streamlit.expander', 'st.expander', ([], {'label': '"""**Intermediate Steps**"""'}), "(label='**Intermediate Steps**')\n", (11872, 11904), True, 'import streamlit as st\n'), ((11922, 11948), 'streamlit.write', 'st.write', (["message['steps']"], {}), "(message['steps'])\n", (11930, 11948), True, 'import streamlit as st\n'), ((12884, 12899), 'streamlit.write', 'st.write', (['steps'], {}), '(steps)\n', (12892, 12899), True, 'import streamlit as st\n')]