Spaces:
Sleeping
Sleeping
Vinh Nguyen
commited on
Commit
β’
c65178d
1
Parent(s):
2b35025
More proper code syntax
Browse files
app.py
CHANGED
@@ -1,8 +1,10 @@
|
|
1 |
import streamlit as st
|
2 |
-
from langchain.chains import ConversationalRetrievalChain
|
3 |
from langchain.memory import ConversationBufferMemory
|
4 |
-
from
|
5 |
-
|
|
|
|
|
6 |
|
7 |
from calback_handler import PrintRetrievalHandler, StreamHandler
|
8 |
from chat_profile import ChatProfileRoleEnum
|
@@ -75,35 +77,36 @@ with chat_tab:
|
|
75 |
if uploaded_files:
|
76 |
result_retriever = configure_retriever(uploaded_files)
|
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 |
if not openai_api_key:
|
109 |
st.caption("π Add your **OpenAI API key** on the `Settings` to continue.")
|
|
|
1 |
import streamlit as st
|
2 |
+
from langchain.chains.conversational_retrieval.base import ConversationalRetrievalChain
|
3 |
from langchain.memory import ConversationBufferMemory
|
4 |
+
from langchain_community.chat_message_histories.streamlit import (
|
5 |
+
StreamlitChatMessageHistory,
|
6 |
+
)
|
7 |
+
from langchain_community.chat_models.openai import ChatOpenAI
|
8 |
|
9 |
from calback_handler import PrintRetrievalHandler, StreamHandler
|
10 |
from chat_profile import ChatProfileRoleEnum
|
|
|
77 |
if uploaded_files:
|
78 |
result_retriever = configure_retriever(uploaded_files)
|
79 |
|
80 |
+
if result_retriever is not None:
|
81 |
+
memory = ConversationBufferMemory(
|
82 |
+
memory_key="chat_history",
|
83 |
+
chat_memory=msgs,
|
84 |
+
return_messages=True,
|
85 |
+
)
|
86 |
+
|
87 |
+
# Setup LLM and QA chain
|
88 |
+
llm = ChatOpenAI(
|
89 |
+
model=LLM_MODEL,
|
90 |
+
api_key=openai_api_key,
|
91 |
+
temperature=0,
|
92 |
+
streaming=True,
|
93 |
+
)
|
94 |
+
|
95 |
+
chain = ConversationalRetrievalChain.from_llm(
|
96 |
+
llm,
|
97 |
+
retriever=result_retriever,
|
98 |
+
memory=memory,
|
99 |
+
verbose=False,
|
100 |
+
max_tokens_limit=4000,
|
101 |
+
)
|
102 |
+
|
103 |
+
avatars = {
|
104 |
+
str(ChatProfileRoleEnum.HUMAN): "user",
|
105 |
+
str(ChatProfileRoleEnum.AI): "assistant",
|
106 |
+
}
|
107 |
+
|
108 |
+
for msg in msgs.messages:
|
109 |
+
st.chat_message(avatars[msg.type]).write(msg.content)
|
110 |
|
111 |
if not openai_api_key:
|
112 |
st.caption("π Add your **OpenAI API key** on the `Settings` to continue.")
|