-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge-05.py
187 lines (146 loc) · 5.17 KB
/
challenge-05.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import streamlit as st
from langchain.callbacks.base import BaseCallbackHandler
from langchain.embeddings import CacheBackedEmbeddings
from langchain.prompts import ChatPromptTemplate
from langchain.schema.runnable import RunnableLambda, RunnablePassthrough
from langchain.storage import LocalFileStore
from langchain.text_splitter import CharacterTextSplitter
from langchain_community.document_loaders import UnstructuredFileLoader
from langchain_community.vectorstores import FAISS
from langchain_openai import ChatOpenAI
from langchain_openai import OpenAIEmbeddings
from pathlib import Path
st.set_page_config(
page_title="::: Document GPT :::",
page_icon="📜",
)
st.title("Document GPT")
st.markdown(
"""
Use this chatbot to ask questions about your document.
1. Input your OpenAI API Key on the sidebar.
2. Choose an AI model (gpt-4o-mini, ...).
3. Upload a document file (txt | doc | pdf).
4. Ask questions related to the document.
"""
)
st.divider()
with st.sidebar:
# Input LLM API Key
openai_api_key = st.text_input("Input your OpenAI API Key", type="password")
# Select AI Model
selected_model = st.selectbox(
"Choose your AI Model",
("gpt-3.5-turbo", "gpt-4o-mini"),
)
# Upload Document File
file = st.file_uploader(
"Upload a txt, pdf or docx file",
type=["docx", "pdf", "txt"],
)
# Link to Github Repo
st.markdown("---")
github_link = (
"https://github.com/toweringcloud/fullstack-gpt-v2/blob/main/challenge-05.py"
)
badge_link = "https://badgen.net/badge/icon/GitHub?icon=github&label"
st.write(f"[]({github_link})")
class ChatCallbackHandler(BaseCallbackHandler):
message = ""
def on_llm_start(self, *args, **kwargs):
self.message_box = st.empty()
def on_llm_end(self, *args, **kwargs):
save_message(self.message, "ai")
def on_llm_new_token(self, token, *args, **kwargs):
self.message += token
self.message_box.markdown(self.message)
if "messages" not in st.session_state:
st.session_state["messages"] = []
@st.cache_resource(show_spinner="Embedding file...")
def embed_file(file):
file_content = file.read()
file_path = f"./.files/{file.name}"
Path("./.files").mkdir(parents=True, exist_ok=True)
with open(file_path, "wb+") as f:
f.write(file_content)
cache_path = "./.cache"
embedding_path = f"{cache_path}/challenge-05"
Path(embedding_path).mkdir(parents=True, exist_ok=True)
embedding_cache_dir = LocalFileStore(embedding_path)
splitter = CharacterTextSplitter.from_tiktoken_encoder(
separator="\n",
chunk_size=600,
chunk_overlap=100,
)
loader = UnstructuredFileLoader(file_path)
docs = loader.load_and_split(text_splitter=splitter)
embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
cached_embeddings = CacheBackedEmbeddings.from_bytes_store(
embeddings, embedding_cache_dir
)
vectorstore = FAISS.from_documents(docs, cached_embeddings)
retriever = vectorstore.as_retriever()
return retriever
def save_message(message, role):
st.session_state["messages"].append({"message": message, "role": role})
def send_message(message, role, save=True):
with st.chat_message(role):
st.markdown(message)
if save:
save_message(message, role)
def paint_history():
for message in st.session_state["messages"]:
send_message(message["message"], message["role"], save=False)
def format_docs(docs):
return "\n\n".join(document.page_content for document in docs)
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"""
Answer the question using ONLY the following context.
If you don't know the answer just say you don't know.
DON't make anything up.
Context: {context}
""",
),
("human", "{question}"),
]
)
def main():
if not openai_api_key:
return
llm = ChatOpenAI(
openai_api_key=openai_api_key,
model=selected_model,
temperature=0.1,
streaming=True,
callbacks=[
ChatCallbackHandler(),
],
)
if file:
retriever = embed_file(file)
send_message("I'm ready! Ask away!", "ai", save=False)
paint_history()
message = st.chat_input("Ask anything about your file.....")
if message:
send_message(message, "human")
chain = (
{
"context": retriever | RunnableLambda(format_docs),
"question": RunnablePassthrough(),
}
| prompt
| llm
)
with st.chat_message("ai"):
chain.invoke(message)
else:
st.session_state["messages"] = []
return
try:
main()
except Exception as e:
st.error("Check your OpenAI API Key or File")
st.write(e)