shreyasiv commited on
Commit
ad89062
·
1 Parent(s): e292f19

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.chat_models import ChatOpenAI
2
+ from langchain.chains import ConversationChain
3
+ from langchain.chains.conversation.memory import ConversationBufferWindowMemory
4
+ from langchain.prompts import (
5
+ SystemMessagePromptTemplate,
6
+ HumanMessagePromptTemplate,
7
+ ChatPromptTemplate,
8
+ MessagesPlaceholder
9
+ )
10
+ import streamlit as st
11
+ from streamlit_chat import message
12
+ from utils import *
13
+ import os
14
+ from dotenv import load_dotenv
15
+
16
+ # Load environment variables from the .env file
17
+ load_dotenv()
18
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
19
+ if OPENAI_API_KEY is None:
20
+ raise ValueError("OpenAI API key is not found in the .env file")
21
+ st.subheader("Insightly Chatbot")
22
+
23
+ if 'responses' not in st.session_state:
24
+ st.session_state['responses'] = ["How can I assist you?"]
25
+
26
+ if 'requests' not in st.session_state:
27
+ st.session_state['requests'] = []
28
+
29
+ llm = ChatOpenAI(model_name="gpt-3.5-turbo", openai_api_key=OPENAI_API_KEY)
30
+
31
+ if 'buffer_memory' not in st.session_state:
32
+ st.session_state.buffer_memory=ConversationBufferWindowMemory(k=3,return_messages=True)
33
+
34
+
35
+ system_msg_template = SystemMessagePromptTemplate.from_template(template="""Answer the question as truthfully as possible using the provided context,
36
+ and if the answer is not contained within the text below, say 'I don't know'""")
37
+
38
+
39
+ human_msg_template = HumanMessagePromptTemplate.from_template(template="{input}")
40
+
41
+ prompt_template = ChatPromptTemplate.from_messages([system_msg_template, MessagesPlaceholder(variable_name="history"), human_msg_template])
42
+
43
+ conversation = ConversationChain(memory=st.session_state.buffer_memory, prompt=prompt_template, llm=llm, verbose=True)
44
+
45
+
46
+
47
+
48
+ # container for chat history
49
+ response_container = st.container()
50
+ # container for text box
51
+ textcontainer = st.container()
52
+
53
+
54
+ with textcontainer:
55
+ query = st.text_input("Query: ", key="input")
56
+ if query:
57
+ with st.spinner("typing..."):
58
+ conversation_string = get_conversation_string()
59
+ # st.code(conversation_string)
60
+ refined_query = query_refiner(conversation_string, query)
61
+ st.subheader("Refined Query:")
62
+ st.write(refined_query)
63
+ context = find_match(refined_query)
64
+ # print(context)
65
+ response = conversation.predict(input=f"Context:\n {context} \n\n Query:\n{query}")
66
+ st.session_state.requests.append(query)
67
+ st.session_state.responses.append(response)
68
+ with response_container:
69
+ if st.session_state['responses']:
70
+
71
+ for i in range(len(st.session_state['responses'])):
72
+ message(st.session_state['responses'][i],key=str(i))
73
+ if i < len(st.session_state['requests']):
74
+ message(st.session_state["requests"][i], is_user=True,key=str(i)+ '_user')