from langchain.tools import DuckDuckGoSearchRun from langchain.agents.tools import tool from langchain import OpenAI from langchain.agents import Tool, load_tools, initialize_agent, AgentType from langchain.prompts import PromptTemplate from langchain.chains import LLMChain # Streamlit UI Callback from langchain.callbacks import StreamlitCallbackHandler from langchain.chains import LLMMathChain from langchain.memory import ConversationBufferMemory from langchain_openai import ChatOpenAI import streamlit as st from dotenv import load_dotenv from sqlalchemy import create_engine, text, URL from htmlTemplates import css, bot_template, user_template from openai import OpenAI import os import time from be import config, get_db_chain, outreach_sms_message, get_store_address, data_visualization from PIL import Image from langchain_core.messages import HumanMessage, AIMessage import matplotlib.pyplot as plt import re def conversation_agent(question): llm = ChatOpenAI(temperature=0.5, model="gpt-3.5-turbo") # create LLM # search = DuckDuckGoSearchRun() llm_math_chain = LLMMathChain(llm=llm, verbose=True) tools = [ Tool( name="Calculator", func=llm_math_chain.run, description="useful when you need to answer questions with math" ) ] ######### CREATING ALL THE TOOLS FOR THE AGENT TO USE ##################### # Create the final SMS message outreach_sms_message_tool = Tool( name="Outreach SMS Message", func=outreach_sms_message, description="Create an outreach SMS message for the customer. Pass both user input and the Store Address as ONE SINGLE INPUT STRING. Use this Tool only to create an outreach SMS or Text message. At the end always include the Store Address for appointment confirmation messages" ) #tools.append(outreach_sms_message_tool) # Creating a Query Tool - to generate SQL statements and query the database get_db_chain_tool = Tool( name='Query Generation Tool', func=get_db_chain, description="ONLY use this tool for query generation and to fetch any information. Use this to Create MYSQL Query with the ORIGINAL User Input question to pull customer, store, product information. MySQL database connections are established within this tool. Use this tool first" "During SQL Query Generation, make sure the SELECT list of columns are in GROUP BY clause" "Use this to get the store address from the database" ) # create the tool for finding the store details get_store_address_tool = Tool( name="Get Store Address", func=get_store_address, description="Use this tool with store number to get the store address. INPUT to this tool is Store number. Do not use this tool if you don't have Store number as input" ) # create the tool for finding the store details data_visualization_tool = Tool( name="Data Visualization Tool", func=data_visualization, description="ONLY USE THIS TOOL for data visualization, do not create on your own. This tool is used to create python code to generate charts for data visualization with user question, column names, and pandas dataframe. DO NOT create sample dataframe, always use the provided dataframe in df. Always make sure the query generation tool is executed and the response is shown to the user before executing this tool" ) #tools.append(get_db_chain_tool) # List all the tools for the agent to use tools = [get_db_chain_tool, get_store_address_tool, outreach_sms_message_tool, data_visualization_tool] conversational_agent = initialize_agent( agent="conversational-react-description", tools=tools, llm=llm, verbose=True, max_iterations=10, return_intermediate_steps=True, memory=st.session_state.memory, handle_parsing_errors=True ) #print(conversational_agent.agent.llm_chain.prompt.template) #response = conversational_agent.invoke(question) response = conversational_agent({"input": question}) return response client = OpenAI() def main(): st.set_option('deprecation.showPyplotGlobalUse', False) img = Image.open('assist_logo.jpg') # user_avatar = Image.open('renga_profile.jpg') # ai_avatar = Image.open('Designer.png') load_dotenv() # load env parameters st.set_page_config(page_title="Assist", page_icon=img) st.write(css, unsafe_allow_html=True) # Logo and image next to each other with a space column separating them out for rendering in small devices st.title(':blue[Assist] Store Associates') with st.sidebar: st.image('assist_logo.jpg', width=120) st.sidebar.header("Assist App for Store Associates") st.write("Assist store associates to get information on Customers, Stores, Product, Sales Analytics, Inventory Management and help with customer outreach") st.write(" ") st.write("Tasks I can help with:") st.write("a. Extract Data/info") st.write("b. Outreach message ") st.write("c. Send Text to Customers") st.write("d. Search websites and look up Product prices & other info") st.write("e. Generate charts for greater visualization") if "chat_history" not in st.session_state: st.session_state.memory = ConversationBufferMemory(memory_key="chat_history") st.session_state.chat_history = [] # ini chat history if "messages" not in st.session_state: st.session_state.messages = [] for message in st.session_state.chat_history: if isinstance(message, HumanMessage): with st.chat_message("Human"): st.markdown(message.content) else: with st.chat_message("AI"): st.markdown(message.content) user_question = st.chat_input("Type your question") if user_question is not None: #Print the last question from user with st.chat_message("Human"): st.markdown(user_question) with st.spinner("Processing"): st.session_state.chat_history.append(HumanMessage(user_question)) # Print the last answer from user with st.chat_message("AI"): assistant_response = conversation_agent(user_question) assistant_response_output = assistant_response["output"] st.markdown(assistant_response_output) st.session_state.chat_history.append(AIMessage(assistant_response_output)) if __name__== '__main__': main() #if st.button: # question = "Top 3 sales in store 4057" # output = data_visualization(question)