Spaces:
Sleeping
Sleeping
multiple files option
Browse files
app.py
CHANGED
@@ -1,137 +1,134 @@
|
|
1 |
-
import os
|
2 |
-
from typing import List
|
3 |
-
from chainlit.types import AskFileResponse
|
4 |
-
from aimakerspace.text_utils import CharacterTextSplitter, TextFileLoader
|
5 |
-
from aimakerspace.openai_utils.prompts import (
|
6 |
-
UserRolePrompt,
|
7 |
-
SystemRolePrompt,
|
8 |
-
AssistantRolePrompt,
|
9 |
-
)
|
10 |
-
from aimakerspace.openai_utils.embedding import EmbeddingModel
|
11 |
-
from aimakerspace.vectordatabase import VectorDatabase
|
12 |
-
from aimakerspace.openai_utils.chatmodel import ChatOpenAI
|
13 |
-
import chainlit as cl
|
14 |
-
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
15 |
-
# from langchain_experimental.text_splitter import SemanticChunker
|
16 |
-
# from langchain_openai.embeddings import OpenAIEmbeddings
|
17 |
-
|
18 |
-
system_template = """\
|
19 |
-
Use the following context to answer a users question. If you cannot find the answer in the context, say you don't know the answer."""
|
20 |
-
system_role_prompt = SystemRolePrompt(system_template)
|
21 |
-
|
22 |
-
user_prompt_template = """\
|
23 |
-
Context:
|
24 |
-
{context}
|
25 |
-
Question:
|
26 |
-
{question}
|
27 |
-
"""
|
28 |
-
user_role_prompt = UserRolePrompt(user_prompt_template)
|
29 |
-
|
30 |
-
class RetrievalAugmentedQAPipeline:
|
31 |
-
def __init__(self, llm: ChatOpenAI(), vector_db_retriever: VectorDatabase) -> None:
|
32 |
-
self.llm = llm
|
33 |
-
self.vector_db_retriever = vector_db_retriever
|
34 |
-
|
35 |
-
async def arun_pipeline(self, user_query: str):
|
36 |
-
context_list = self.vector_db_retriever.search_by_text(user_query, k=4)
|
37 |
-
|
38 |
-
context_prompt = ""
|
39 |
-
for context in context_list:
|
40 |
-
context_prompt += context[0] + "\n"
|
41 |
-
|
42 |
-
formatted_system_prompt = system_role_prompt.create_message()
|
43 |
-
|
44 |
-
formatted_user_prompt = user_role_prompt.create_message(question=user_query, context=context_prompt)
|
45 |
-
|
46 |
-
async def generate_response():
|
47 |
-
async for chunk in self.llm.astream([formatted_system_prompt, formatted_user_prompt]):
|
48 |
-
yield chunk
|
49 |
-
|
50 |
-
return {"response": generate_response(), "context": context_list}
|
51 |
-
|
52 |
-
text_splitter = RecursiveCharacterTextSplitter()
|
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 |
-
msg
|
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 |
-
async for stream_resp in result["response"]:
|
135 |
-
await msg.stream_token(stream_resp)
|
136 |
-
|
137 |
await msg.send()
|
|
|
1 |
+
import os
|
2 |
+
from typing import List
|
3 |
+
from chainlit.types import AskFileResponse
|
4 |
+
from aimakerspace.text_utils import CharacterTextSplitter, TextFileLoader
|
5 |
+
from aimakerspace.openai_utils.prompts import (
|
6 |
+
UserRolePrompt,
|
7 |
+
SystemRolePrompt,
|
8 |
+
AssistantRolePrompt,
|
9 |
+
)
|
10 |
+
from aimakerspace.openai_utils.embedding import EmbeddingModel
|
11 |
+
from aimakerspace.vectordatabase import VectorDatabase
|
12 |
+
from aimakerspace.openai_utils.chatmodel import ChatOpenAI
|
13 |
+
import chainlit as cl
|
14 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
15 |
+
# from langchain_experimental.text_splitter import SemanticChunker
|
16 |
+
# from langchain_openai.embeddings import OpenAIEmbeddings
|
17 |
+
|
18 |
+
system_template = """\
|
19 |
+
Use the following context to answer a users question. If you cannot find the answer in the context, say you don't know the answer."""
|
20 |
+
system_role_prompt = SystemRolePrompt(system_template)
|
21 |
+
|
22 |
+
user_prompt_template = """\
|
23 |
+
Context:
|
24 |
+
{context}
|
25 |
+
Question:
|
26 |
+
{question}
|
27 |
+
"""
|
28 |
+
user_role_prompt = UserRolePrompt(user_prompt_template)
|
29 |
+
|
30 |
+
class RetrievalAugmentedQAPipeline:
|
31 |
+
def __init__(self, llm: ChatOpenAI(), vector_db_retriever: VectorDatabase) -> None:
|
32 |
+
self.llm = llm
|
33 |
+
self.vector_db_retriever = vector_db_retriever
|
34 |
+
|
35 |
+
async def arun_pipeline(self, user_query: str):
|
36 |
+
context_list = self.vector_db_retriever.search_by_text(user_query, k=4)
|
37 |
+
|
38 |
+
context_prompt = ""
|
39 |
+
for context in context_list:
|
40 |
+
context_prompt += context[0] + "\n"
|
41 |
+
|
42 |
+
formatted_system_prompt = system_role_prompt.create_message()
|
43 |
+
|
44 |
+
formatted_user_prompt = user_role_prompt.create_message(question=user_query, context=context_prompt)
|
45 |
+
|
46 |
+
async def generate_response():
|
47 |
+
async for chunk in self.llm.astream([formatted_system_prompt, formatted_user_prompt]):
|
48 |
+
yield chunk
|
49 |
+
|
50 |
+
return {"response": generate_response(), "context": context_list}
|
51 |
+
|
52 |
+
text_splitter = RecursiveCharacterTextSplitter()
|
53 |
+
|
54 |
+
|
55 |
+
def process_text_file(file: AskFileResponse):
|
56 |
+
import tempfile
|
57 |
+
from langchain_community.document_loaders.pdf import PyPDFLoader
|
58 |
+
|
59 |
+
with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=file.name) as temp_file:
|
60 |
+
temp_file_path = temp_file.name
|
61 |
+
|
62 |
+
with open(temp_file_path, "wb") as f:
|
63 |
+
f.write(file.content)
|
64 |
+
|
65 |
+
if file.type == 'text/plain':
|
66 |
+
text_loader = TextFileLoader(temp_file_path)
|
67 |
+
documents = text_loader.load_documents()
|
68 |
+
elif file.type == 'application/pdf':
|
69 |
+
pdf_loader = PyPDFLoader(temp_file_path)
|
70 |
+
documents = pdf_loader.load()
|
71 |
+
else:
|
72 |
+
raise ValueError("Provide a .txt or .pdf file")
|
73 |
+
texts = [x.page_content for x in text_splitter.transform_documents(documents)]
|
74 |
+
# texts = [x.page_content for x in text_splitter.split_documents(documents)]
|
75 |
+
return texts
|
76 |
+
|
77 |
+
|
78 |
+
|
79 |
+
@cl.on_chat_start
|
80 |
+
async def on_chat_start():
|
81 |
+
files = None
|
82 |
+
|
83 |
+
# Wait for the user to upload a file
|
84 |
+
while files == None:
|
85 |
+
files = await cl.AskFileMessage(
|
86 |
+
content="Please upload a Text file or a PDF to begin!",
|
87 |
+
accept=["text/plain", "application/pdf"],
|
88 |
+
max_size_mb=12,
|
89 |
+
timeout=180,
|
90 |
+
max_files=10
|
91 |
+
).send()
|
92 |
+
|
93 |
+
file = files[0]
|
94 |
+
|
95 |
+
msg = cl.Message(
|
96 |
+
content=f"Processing `{file.name}`...", disable_human_feedback=True
|
97 |
+
)
|
98 |
+
await msg.send()
|
99 |
+
|
100 |
+
# load the file
|
101 |
+
texts = process_text_file(file)
|
102 |
+
|
103 |
+
print(f"Processing {len(texts)} text chunks")
|
104 |
+
|
105 |
+
# Create a dict vector store
|
106 |
+
vector_db = VectorDatabase()
|
107 |
+
vector_db = await vector_db.abuild_from_list(texts)
|
108 |
+
|
109 |
+
chat_openai = ChatOpenAI()
|
110 |
+
|
111 |
+
# Create a chain
|
112 |
+
retrieval_augmented_qa_pipeline = RetrievalAugmentedQAPipeline(
|
113 |
+
vector_db_retriever=vector_db,
|
114 |
+
llm=chat_openai
|
115 |
+
)
|
116 |
+
|
117 |
+
# Let the user know that the system is ready
|
118 |
+
msg.content = f"Processing `{file.name}` done. You can now ask questions!"
|
119 |
+
await msg.update()
|
120 |
+
|
121 |
+
cl.user_session.set("chain", retrieval_augmented_qa_pipeline)
|
122 |
+
|
123 |
+
|
124 |
+
@cl.on_message
|
125 |
+
async def main(message):
|
126 |
+
chain = cl.user_session.get("chain")
|
127 |
+
|
128 |
+
msg = cl.Message(content="")
|
129 |
+
result = await chain.arun_pipeline(message.content)
|
130 |
+
|
131 |
+
async for stream_resp in result["response"]:
|
132 |
+
await msg.stream_token(stream_resp)
|
133 |
+
|
|
|
|
|
|
|
134 |
await msg.send()
|