-
Notifications
You must be signed in to change notification settings - Fork 0
/
RAG103_agent_memory.py
170 lines (141 loc) · 5.07 KB
/
RAG103_agent_memory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# it has memory
from openai import OpenAI
import streamlit as st
from langchain_openai import OpenAIEmbeddings
from langchain_pinecone import PineconeVectorStore
from langchain_openai import ChatOpenAI
from langchain.chains import RetrievalQA
from langchain.chains import ConversationalRetrievalChain
from langchain.memory import ConversationBufferMemory
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
from langchain.agents import initialize_agent
from langchain.agents import Tool
PINECONE_API = '<>'
OPENAI_API = '<>'
# initialize embedding model
model_name = 'text-embedding-ada-002'
embed = OpenAIEmbeddings(model=model_name,openai_api_key=OPENAI_API)
# initialize vectorstore
text_field = 'text'
index_name = 'test101'
vectorstore = PineconeVectorStore(index_name= index_name, embedding = embed, pinecone_api_key = PINECONE_API,text_key=text_field)
# initialize llm
llm = ChatOpenAI(openai_api_key=OPENAI_API,model_name="gpt-3.5-turbo", temperature=0.0) #,streaming=True,callbacks=[StreamingStdOutCallbackHandler()]
# memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
conversational_memory = ConversationBufferWindowMemory(memory_key='chat_history',k=5,return_messages=True)
# conversational_memory.chat_memory.add_message('test-test-test')
# initialize QA chain
qa = RetrievalQA.from_chain_type(llm=llm,chain_type="stuff",retriever=vectorstore.as_retriever())
tools = [Tool(name='Knowledge Base',
func=qa.invoke,
description=('use this tool when answering general questions to get more information about the topic'))]
agent = initialize_agent(agent='chat-conversational-react-description',
tools=tools,
llm=llm,
verbose=False,
max_iterations=3,
early_stopping_method='generate',
memory = conversational_memory)
# print(agent("hi, how are you? my name is jack"))
# print(agent("what is my name?"))
# qa = ConversationalRetrievalChain.from_llm(
# llm=llm,
# retriever=vectorstore.as_retriever(),
# chain_type="stuff",
# memory=memory,
# )
# query = "my name is jack"
# # result = qa.invoke(query)['result']
# result = qa.invoke(query)
#
# print(result)
#
#
# query = "what is my name?"
# # result = qa.invoke(query)['result']
# result = qa.invoke(query)
#
# print(result)
#
# # Streamlit application setup
# st.title("ChatGPT-like clone")
#
# if "messages" not in st.session_state:
# st.session_state.messages = []
#
# # Chat interface
# for message in st.session_state.messages:
# with st.chat_message(message["role"]):
# st.text(message["content"])
#
# # Input from the user
# user_input = st.text_input("You:", "")
#
# if st.button("Send"):
# st.session_state.messages.append({"role": "user", "content": user_input})
#
# # Get response from the model
# response = qa.invoke(user_input)['result']
#
# st.session_state.messages.append({"role": "assistant", "content": response})
#
#
#
#
st.title("Drilling Assistant")
client = OpenAI(api_key='<>')
if "openai_model" not in st.session_state:
st.session_state["openai_model"] = "gpt-3.5-turbo"
if "messages" not in st.session_state:
st.session_state.messages = []
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
#
# if prompt := st.chat_input("what is up?"):
# print('debug001......')
# st.session_state.messages.append({"role":"user","content":prompt})
# print('debug002......')
#
# with st.chat_message("user"):
# print('debug003......')
#
# st.markdown(prompt)
#
# with st.chat_message("assistant"):
# print('debug004......')
#
# # stream = client.chat.completions.create(
# # model = st.session_state["openai_model"],
# # messages = [
# # {"role": m["role"], "content": m["content"]}
# # for m in st.session_state.messages
# # ],
# # stream = True,
# # )
# print('>>>',type(prompt), prompt)
# result = qa.invoke(prompt)['result']
# # print('response:',response)
#
# # response = st.write_stream(stream)
# response = st.write(result)
#
#
# st.session_state.messages.append({"role":"assistant","content":response})
# print('append...........')
#
if prompt := st.chat_input("You:"):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
with st.spinner('Thinking...'):
last_five = st.session_state.messages[-5:]
print('last_five:',last_five)
conversational_memory.chat_memory.add_message(str(last_five))
result = agent(prompt)
print('result:',result)
response = result['output']
st.session_state.messages.append({"role": "assistant", "content": response})
with st.chat_message("assistant"):
st.write(response)