Spaces:
Sleeping
Sleeping
Dan Foley
commited on
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,213 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
|
| 3 |
-
from typing import List
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 8 |
-
|
| 9 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 10 |
-
|
| 11 |
-
from langchain.vectorstores import Chroma
|
| 12 |
-
|
| 13 |
-
from langchain.chains import (
|
| 14 |
-
|
| 15 |
-
ConversationalRetrievalChain,
|
| 16 |
-
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
from langchain.chat_models import ChatOpenAI
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
from langchain.docstore.document import Document
|
| 24 |
-
|
| 25 |
-
from langchain.memory import ChatMessageHistory, ConversationBufferMemory
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
import chainlit as cl
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
@cl.on_chat_start
|
| 44 |
-
|
| 45 |
-
async def on_chat_start():
|
| 46 |
-
|
| 47 |
-
files = None
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
# Wait for the user to upload a file
|
| 52 |
-
|
| 53 |
-
while files == None:
|
| 54 |
-
|
| 55 |
-
files = await cl.AskFileMessage(
|
| 56 |
-
|
| 57 |
-
content="Please upload a text file to begin!",
|
| 58 |
-
|
| 59 |
-
accept=["text/plain"],
|
| 60 |
-
|
| 61 |
-
max_size_mb=20,
|
| 62 |
-
|
| 63 |
-
timeout=180,
|
| 64 |
-
|
| 65 |
-
).send()
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
file = files[0]
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
msg = cl.Message(content=f"Processing `{file.name}`...")
|
| 74 |
-
|
| 75 |
-
await msg.send()
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
with open(file.path, "r", encoding="utf-8") as f:
|
| 80 |
-
|
| 81 |
-
text = f.read()
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
# Split the text into chunks
|
| 86 |
-
|
| 87 |
-
texts = text_splitter.split_text(text)
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
# Create a metadata for each chunk
|
| 92 |
-
|
| 93 |
-
metadatas = [{"source": f"{i}-pl"} for i in range(len(texts))]
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
# Create a Chroma vector store
|
| 98 |
-
|
| 99 |
-
embeddings = OpenAIEmbeddings()
|
| 100 |
-
|
| 101 |
-
docsearch = await cl.make_async(Chroma.from_texts)(
|
| 102 |
-
|
| 103 |
-
texts, embeddings, metadatas=metadatas
|
| 104 |
-
|
| 105 |
-
)
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
message_history = ChatMessageHistory()
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
memory = ConversationBufferMemory(
|
| 114 |
-
|
| 115 |
-
memory_key="chat_history",
|
| 116 |
-
|
| 117 |
-
output_key="answer",
|
| 118 |
-
|
| 119 |
-
chat_memory=message_history,
|
| 120 |
-
|
| 121 |
-
return_messages=True,
|
| 122 |
-
|
| 123 |
-
)
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
# Create a chain that uses the Chroma vector store
|
| 128 |
-
|
| 129 |
-
chain = ConversationalRetrievalChain.from_llm(
|
| 130 |
-
|
| 131 |
-
ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0, streaming=True),
|
| 132 |
-
|
| 133 |
-
chain_type="stuff",
|
| 134 |
-
|
| 135 |
-
retriever=docsearch.as_retriever(),
|
| 136 |
-
|
| 137 |
-
memory=memory,
|
| 138 |
-
|
| 139 |
-
return_source_documents=True,
|
| 140 |
-
|
| 141 |
-
)
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
# Let the user know that the system is ready
|
| 146 |
-
|
| 147 |
-
msg.content = f"Processing `{file.name}` done. You can now ask questions!"
|
| 148 |
-
|
| 149 |
-
await msg.update()
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
cl.user_session.set("chain", chain)
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
@cl.on_message
|
| 160 |
-
|
| 161 |
-
async def main(message: cl.Message):
|
| 162 |
-
|
| 163 |
-
chain = cl.user_session.get("chain") # type: ConversationalRetrievalChain
|
| 164 |
-
|
| 165 |
-
cb = cl.AsyncLangchainCallbackHandler()
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
res = await chain.acall(message.content, callbacks=[cb])
|
| 170 |
-
|
| 171 |
-
answer = res["answer"]
|
| 172 |
-
|
| 173 |
-
source_documents = res["source_documents"] # type: List[Document]
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
text_elements = [] # type: List[cl.Text]
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
if source_documents:
|
| 182 |
-
|
| 183 |
-
for source_idx, source_doc in enumerate(source_documents):
|
| 184 |
-
|
| 185 |
-
source_name = f"source_{source_idx}"
|
| 186 |
-
|
| 187 |
-
# Create the text element referenced in the message
|
| 188 |
-
|
| 189 |
-
text_elements.append(
|
| 190 |
-
|
| 191 |
-
cl.Text(content=source_doc.page_content, name=source_name, display="side")
|
| 192 |
-
|
| 193 |
-
)
|
| 194 |
-
|
| 195 |
-
source_names = [text_el.name for text_el in text_elements]
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
if source_names:
|
| 200 |
-
|
| 201 |
-
answer += f"\nSources: {', '.join(source_names)}"
|
| 202 |
-
|
| 203 |
-
else:
|
| 204 |
-
|
| 205 |
-
answer += "\nNo sources found"
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
await cl.Message(content=answer, elements=text_elements).send()
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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