Spaces:
Sleeping
Sleeping
Using langchain_openai to stream message
Browse files- app.py +64 -44
- requirements.txt +2 -3
app.py
CHANGED
@@ -1,18 +1,33 @@
|
|
1 |
import os
|
2 |
|
3 |
-
__import__("pysqlite3")
|
4 |
-
import sys
|
5 |
-
|
6 |
import streamlit as st
|
|
|
7 |
from langchain.chains import ConversationalRetrievalChain
|
|
|
8 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
9 |
-
from langchain_community.chat_models import ChatOpenAI
|
10 |
from langchain_community.document_loaders import Docx2txtLoader, PyPDFLoader, TextLoader
|
11 |
from langchain_community.embeddings.openai import OpenAIEmbeddings
|
12 |
from langchain_community.vectorstores.chroma import Chroma
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
|
18 |
def load_and_process_file(file_data, openai_api_key):
|
@@ -70,7 +85,6 @@ def main():
|
|
70 |
The main function that runs the Streamlit app.
|
71 |
"""
|
72 |
|
73 |
-
st.set_page_config(page_title="InkChatGPT", page_icon="π")
|
74 |
st.title("π InkChatGPT")
|
75 |
st.write("Upload a document and ask questions related to its content.")
|
76 |
|
@@ -78,48 +92,39 @@ def main():
|
|
78 |
"Select a file", type=["pdf", "docx", "txt"], key="file_uploader"
|
79 |
)
|
80 |
|
81 |
-
openai_api_key = st.text_input(
|
82 |
-
"OpenAI API Key", type="password", disabled=not (uploaded_file)
|
83 |
-
)
|
84 |
-
|
85 |
if uploaded_file and openai_api_key.startswith("sk-"):
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
openai_api_key,
|
97 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
-
|
100 |
-
crc = initialize_chat_model(
|
101 |
-
vector_store,
|
102 |
-
openai_api_key=openai_api_key,
|
103 |
-
)
|
104 |
-
st.session_state.crc = crc
|
105 |
-
st.success("File processed successfully!")
|
106 |
-
|
107 |
-
st.markdown("## Ask a Question")
|
108 |
-
question = st.text_area(
|
109 |
-
"Enter your question",
|
110 |
-
height=93,
|
111 |
-
key="question_input",
|
112 |
-
)
|
113 |
-
|
114 |
-
submit_button = st.button("Submit", key="submit_button")
|
115 |
-
|
116 |
-
if submit_button and "crc" in st.session_state:
|
117 |
-
handle_question(question)
|
118 |
-
|
119 |
-
display_chat_history()
|
120 |
|
121 |
|
122 |
-
def handle_question(question):
|
123 |
"""
|
124 |
Handles the user's question by generating a response and updating the chat history.
|
125 |
"""
|
@@ -136,13 +141,28 @@ def handle_question(question):
|
|
136 |
)
|
137 |
|
138 |
st.session_state["history"].append((question, response))
|
139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
|
141 |
|
142 |
def display_chat_history():
|
143 |
"""
|
144 |
Displays the chat history in the Streamlit app.
|
145 |
"""
|
|
|
146 |
if "history" in st.session_state:
|
147 |
st.markdown("## Chat History")
|
148 |
for q, a in st.session_state["history"]:
|
|
|
1 |
import os
|
2 |
|
|
|
|
|
|
|
3 |
import streamlit as st
|
4 |
+
from langchain.callbacks.base import BaseCallbackHandler
|
5 |
from langchain.chains import ConversationalRetrievalChain
|
6 |
+
from langchain.schema import ChatMessage
|
7 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
|
8 |
from langchain_community.document_loaders import Docx2txtLoader, PyPDFLoader, TextLoader
|
9 |
from langchain_community.embeddings.openai import OpenAIEmbeddings
|
10 |
from langchain_community.vectorstores.chroma import Chroma
|
11 |
+
from langchain_openai import ChatOpenAI
|
12 |
+
|
13 |
+
|
14 |
+
st.set_page_config(page_title="InkChatGPT", page_icon="π")
|
15 |
+
|
16 |
+
with st.sidebar:
|
17 |
+
openai_api_key = st.text_input("OpenAI API Key", type="password")
|
18 |
+
|
19 |
+
if not openai_api_key:
|
20 |
+
st.info("Please add your OpenAI API key to continue.")
|
21 |
|
22 |
|
23 |
+
class StreamHandler(BaseCallbackHandler):
|
24 |
+
def __init__(self, container, initial_text=""):
|
25 |
+
self.container = container
|
26 |
+
self.text = initial_text
|
27 |
+
|
28 |
+
def on_llm_new_token(self, token: str, **kwargs) -> None:
|
29 |
+
self.text += token
|
30 |
+
self.container.markdown(self.text)
|
31 |
|
32 |
|
33 |
def load_and_process_file(file_data, openai_api_key):
|
|
|
85 |
The main function that runs the Streamlit app.
|
86 |
"""
|
87 |
|
|
|
88 |
st.title("π InkChatGPT")
|
89 |
st.write("Upload a document and ask questions related to its content.")
|
90 |
|
|
|
92 |
"Select a file", type=["pdf", "docx", "txt"], key="file_uploader"
|
93 |
)
|
94 |
|
|
|
|
|
|
|
|
|
95 |
if uploaded_file and openai_api_key.startswith("sk-"):
|
96 |
+
with st.spinner("π Thinking..."):
|
97 |
+
vector_store = load_and_process_file(
|
98 |
+
uploaded_file,
|
99 |
+
openai_api_key,
|
100 |
+
)
|
101 |
+
|
102 |
+
if vector_store:
|
103 |
+
crc = initialize_chat_model(
|
104 |
+
vector_store,
|
105 |
+
openai_api_key=openai_api_key,
|
|
|
106 |
)
|
107 |
+
st.session_state.crc = crc
|
108 |
+
st.success("File processed successfully!")
|
109 |
+
|
110 |
+
if "crc" in st.session_state:
|
111 |
+
st.session_state["messages"] = [
|
112 |
+
ChatMessage(role="assistant", content="How can I help you?")
|
113 |
+
]
|
114 |
+
|
115 |
+
if prompt := st.chat_input():
|
116 |
+
st.session_state.messages.append(
|
117 |
+
ChatMessage(
|
118 |
+
role="user",
|
119 |
+
content=prompt,
|
120 |
+
)
|
121 |
+
)
|
122 |
+
st.chat_message("user").write(prompt)
|
123 |
|
124 |
+
handle_question(prompt, openai_api_key=openai_api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
|
126 |
|
127 |
+
def handle_question(question, openai_api_key):
|
128 |
"""
|
129 |
Handles the user's question by generating a response and updating the chat history.
|
130 |
"""
|
|
|
141 |
)
|
142 |
|
143 |
st.session_state["history"].append((question, response))
|
144 |
+
|
145 |
+
for msg in st.session_state.messages:
|
146 |
+
st.chat_message(msg.role).write(msg.content)
|
147 |
+
|
148 |
+
with st.chat_message("assistant"):
|
149 |
+
stream_handler = StreamHandler(st.empty())
|
150 |
+
llm = ChatOpenAI(
|
151 |
+
openai_api_key=openai_api_key,
|
152 |
+
streaming=True,
|
153 |
+
callbacks=[stream_handler],
|
154 |
+
)
|
155 |
+
response = llm.invoke(st.session_state.messages)
|
156 |
+
st.session_state.messages.append(
|
157 |
+
ChatMessage(role="assistant", content=response.content)
|
158 |
+
)
|
159 |
|
160 |
|
161 |
def display_chat_history():
|
162 |
"""
|
163 |
Displays the chat history in the Streamlit app.
|
164 |
"""
|
165 |
+
|
166 |
if "history" in st.session_state:
|
167 |
st.markdown("## Chat History")
|
168 |
for q, a in st.session_state["history"]:
|
requirements.txt
CHANGED
@@ -1,10 +1,9 @@
|
|
1 |
langchain
|
|
|
2 |
streamlit
|
3 |
streamlit_chat
|
4 |
chromadb
|
5 |
openai
|
6 |
tiktoken
|
7 |
pypdf
|
8 |
-
|
9 |
-
docx2txt
|
10 |
-
pysqlite3-binary
|
|
|
1 |
langchain
|
2 |
+
langchain_openai
|
3 |
streamlit
|
4 |
streamlit_chat
|
5 |
chromadb
|
6 |
openai
|
7 |
tiktoken
|
8 |
pypdf
|
9 |
+
docx2txt
|
|
|
|