tinaranathania's picture
Upload 3 files
31c9ea0
from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationChain
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
from langchain.prompts import (
SystemMessagePromptTemplate,
HumanMessagePromptTemplate,
ChatPromptTemplate,
MessagesPlaceholder
)
import streamlit as st
from streamlit_chat import message
import os
from dotenv import find_dotenv, load_dotenv
load_dotenv(find_dotenv())
# Get from utils.py
from utils import *
st.subheader("Chatbot with Langchain, ChatGPT, Pinecone, and Streamlit")
# Make sure to create a session -> responses and request
if 'responses' not in st.session_state:
st.session_state['responses'] = ["How can I assist you?"]
if 'requests' not in st.session_state:
st.session_state['requests'] = []
llm = ChatOpenAI(model_name="gpt-3.5-turbo", openai_api_key=os.environ["OPENAI_API_KEY"])
if 'buffer_memory' not in st.session_state:
#save the memory/chat history
# sometimes it can exceed the token limit -> can use conversation summary memory ->
# Conversation Buffer Window Memory: Maintain the conversation within a window (3 last interactions)
st.session_state.buffer_memory=ConversationBufferWindowMemory(k=3,return_messages=True)
# What the system should do
system_msg_template = SystemMessagePromptTemplate.from_template(template="""Answer the question as truthfully as possible using the provided context,
and if the answer is not contained within the text below, say 'I don't know'""")
# What to do with input variable
human_msg_template = HumanMessagePromptTemplate.from_template(template="{input}")
# We have to pass a variable name "history" ->
prompt_template = ChatPromptTemplate.from_messages([system_msg_template, MessagesPlaceholder(variable_name="history"), human_msg_template])
# Chain: Putting multiple components together
conversation = ConversationChain(memory=st.session_state.buffer_memory, prompt=prompt_template, llm=llm, verbose=True)
# STREAMLIT
# Container for chat history
response_container = st.container()
# Container for text box
textcontainer = st.container()
with textcontainer:
query = st.text_input("Query: ", key="input")
if query:
with st.spinner("typing..."):
conversation_string = get_conversation_string()
# st.code(conversation_string)
refined_query = query_refiner(conversation_string, query)
st.subheader("Refined Query:")
st.write(refined_query)
context = find_match(refined_query)
# print(context)
response = conversation.predict(input=f"Context:\n {context} \n\n Query:\n{query}")
# Adds response and requests to history (session)
st.session_state.requests.append(query)
st.session_state.responses.append(response)
with response_container:
if st.session_state['responses']:
for i in range(len(st.session_state['responses'])):
message(st.session_state['responses'][i],key=str(i))
if i < len(st.session_state['requests']):
message(st.session_state["requests"][i], is_user=True,key=str(i)+ '_user')