Delete app.py
Browse files
app.py
DELETED
@@ -1,219 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
from typing import List
|
3 |
-
from chainlit.types import AskFileResponse
|
4 |
-
from aimakerspace.text_utils import CharacterTextSplitter, TextFileLoader, PDFLoader
|
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 chainlit import user_session
|
15 |
-
from chainlit.element import Text
|
16 |
-
|
17 |
-
system_template = """\
|
18 |
-
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."""
|
19 |
-
system_role_prompt = SystemRolePrompt(system_template)
|
20 |
-
|
21 |
-
user_prompt_template = """\
|
22 |
-
Context:
|
23 |
-
{context}
|
24 |
-
|
25 |
-
Question:
|
26 |
-
{question}
|
27 |
-
"""
|
28 |
-
user_role_prompt = UserRolePrompt(user_prompt_template)
|
29 |
-
|
30 |
-
@cl.on_chat_start
|
31 |
-
async def init_sidebar():
|
32 |
-
# μ¬μ΄λλ° ν€λ κΎΈλ―ΈκΈ°
|
33 |
-
await cl.Sidebar(
|
34 |
-
cl.Text(content="π **νμΌ μ
λ‘λ μΉμ
**", style="heading3"),
|
35 |
-
cl.FilePicker(
|
36 |
-
accept=[".pdf", ".txt"],
|
37 |
-
max_size_mb=2,
|
38 |
-
on_upload=handle_upload,
|
39 |
-
label="π€ PDF/TXT μ
λ‘λ",
|
40 |
-
description="μ΅λ 2MB νμΌλ§ μ
λ‘λ κ°λ₯ν©λλ€"
|
41 |
-
),
|
42 |
-
cl.Separator(),
|
43 |
-
cl.Text(content="π **λ¬Έμ λΆμ μν**", style="heading4"),
|
44 |
-
cl.ProgressRing(id="progress", visible=False),
|
45 |
-
cl.Text(id="status", content="λκΈ° μ€...", style="caption"),
|
46 |
-
title="π λ¬Έμ μ§μ μμ€ν
",
|
47 |
-
persistent=True # π μ¬μ΄λλ° κ³ μ μ€μ
|
48 |
-
).send()
|
49 |
-
|
50 |
-
|
51 |
-
async def handle_upload(file: AskFileResponse):
|
52 |
-
# μ§ν μν μ
λ°μ΄νΈ
|
53 |
-
status = user_session.get("status")
|
54 |
-
progress = user_session.get("progress")
|
55 |
-
|
56 |
-
await status.update(content=f"π {file.name} λΆμ μ€...")
|
57 |
-
await progress.update(visible=True)
|
58 |
-
|
59 |
-
try:
|
60 |
-
# νμΌ μ²λ¦¬ λ‘μ§
|
61 |
-
texts = process_file(file)
|
62 |
-
|
63 |
-
# λ²‘ν° DB ꡬμΆ
|
64 |
-
vector_db = VectorDatabase()
|
65 |
-
vector_db = await vector_db.abuild_from_list(texts)
|
66 |
-
|
67 |
-
# μΈμ
μ μ μ₯
|
68 |
-
user_session.set("vector_db", vector_db)
|
69 |
-
|
70 |
-
# μν μ
λ°μ΄νΈ
|
71 |
-
await status.update(content=f"β
{len(texts)}κ° μ²ν¬ μ²λ¦¬ μλ£!")
|
72 |
-
await progress.update(visible=False)
|
73 |
-
|
74 |
-
# νμΌ μ 보 μμ½ νμ
|
75 |
-
await cl.Accordion(
|
76 |
-
title="π μ
λ‘λ λ¬Έμ μ 보",
|
77 |
-
content=[
|
78 |
-
cl.Text(f"νμΌλͺ
: {file.name}"),
|
79 |
-
cl.Text(f"ν¬κΈ°: {file.size/1024:.1f}KB"),
|
80 |
-
cl.Text(f"λΆμ μκ°: {datetime.now().strftime('%H:%M:%S')}")
|
81 |
-
],
|
82 |
-
expanded=False
|
83 |
-
).send()
|
84 |
-
|
85 |
-
except Exception as e:
|
86 |
-
await cl.Error(
|
87 |
-
title="νμΌ μ²λ¦¬ μ€λ₯",
|
88 |
-
content=f"{str(e)}"
|
89 |
-
).send()
|
90 |
-
|
91 |
-
class RetrievalAugmentedQAPipeline:
|
92 |
-
def __init__(self, llm: ChatOpenAI(), vector_db_retriever: VectorDatabase) -> None:
|
93 |
-
self.llm = llm
|
94 |
-
self.vector_db_retriever = vector_db_retriever
|
95 |
-
|
96 |
-
async def arun_pipeline(self, user_query: str):
|
97 |
-
context_list = self.vector_db_retriever.search_by_text(user_query, k=4)
|
98 |
-
|
99 |
-
context_prompt = ""
|
100 |
-
for context in context_list:
|
101 |
-
context_prompt += context[0] + "\n"
|
102 |
-
|
103 |
-
formatted_system_prompt = system_role_prompt.create_message()
|
104 |
-
|
105 |
-
formatted_user_prompt = user_role_prompt.create_message(question=user_query, context=context_prompt)
|
106 |
-
|
107 |
-
async def generate_response():
|
108 |
-
async for chunk in self.llm.astream([formatted_system_prompt, formatted_user_prompt]):
|
109 |
-
yield chunk
|
110 |
-
|
111 |
-
return {"response": generate_response(), "context": context_list}
|
112 |
-
|
113 |
-
text_splitter = CharacterTextSplitter()
|
114 |
-
|
115 |
-
|
116 |
-
def process_file(file: AskFileResponse):
|
117 |
-
import tempfile
|
118 |
-
import shutil
|
119 |
-
|
120 |
-
print(f"Processing file: {file.name}")
|
121 |
-
|
122 |
-
# Create a temporary file with the correct extension
|
123 |
-
suffix = f".{file.name.split('.')[-1]}"
|
124 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file:
|
125 |
-
# Copy the uploaded file content to the temporary file
|
126 |
-
shutil.copyfile(file.path, temp_file.name)
|
127 |
-
print(f"Created temporary file at: {temp_file.name}")
|
128 |
-
|
129 |
-
# Create appropriate loader
|
130 |
-
if file.name.lower().endswith('.pdf'):
|
131 |
-
loader = PDFLoader(temp_file.name)
|
132 |
-
else:
|
133 |
-
loader = TextFileLoader(temp_file.name)
|
134 |
-
|
135 |
-
try:
|
136 |
-
# Load and process the documents
|
137 |
-
documents = loader.load_documents()
|
138 |
-
texts = text_splitter.split_texts(documents)
|
139 |
-
return texts
|
140 |
-
finally:
|
141 |
-
# Clean up the temporary file
|
142 |
-
try:
|
143 |
-
os.unlink(temp_file.name)
|
144 |
-
except Exception as e:
|
145 |
-
print(f"Error cleaning up temporary file: {e}")
|
146 |
-
|
147 |
-
|
148 |
-
@cl.on_chat_start
|
149 |
-
async def on_chat_start():
|
150 |
-
files = None
|
151 |
-
|
152 |
-
# Wait for the user to upload a file
|
153 |
-
while files == None:
|
154 |
-
files = await cl.AskFileMessage(
|
155 |
-
content="Please upload a Text or PDF file to begin!",
|
156 |
-
accept=["text/plain", "application/pdf"],
|
157 |
-
max_size_mb=2,
|
158 |
-
timeout=180,
|
159 |
-
).send()
|
160 |
-
|
161 |
-
file = files[0]
|
162 |
-
|
163 |
-
msg = cl.Message(
|
164 |
-
content=f"Processing `{file.name}`..."
|
165 |
-
)
|
166 |
-
await msg.send()
|
167 |
-
|
168 |
-
# load the file
|
169 |
-
texts = process_file(file)
|
170 |
-
|
171 |
-
print(f"Processing {len(texts)} text chunks")
|
172 |
-
|
173 |
-
# Create a dict vector store
|
174 |
-
vector_db = VectorDatabase()
|
175 |
-
vector_db = await vector_db.abuild_from_list(texts)
|
176 |
-
|
177 |
-
chat_openai = ChatOpenAI()
|
178 |
-
|
179 |
-
# Create a chain
|
180 |
-
retrieval_augmented_qa_pipeline = RetrievalAugmentedQAPipeline(
|
181 |
-
vector_db_retriever=vector_db,
|
182 |
-
llm=chat_openai
|
183 |
-
)
|
184 |
-
|
185 |
-
# Let the user know that the system is ready
|
186 |
-
msg.content = f"Processing `{file.name}` done. You can now ask questions!"
|
187 |
-
await msg.update()
|
188 |
-
|
189 |
-
cl.user_session.set("chain", retrieval_augmented_qa_pipeline)
|
190 |
-
|
191 |
-
|
192 |
-
@cl.on_message
|
193 |
-
async def main(message):
|
194 |
-
chain = cl.user_session.get("chain")
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
# μλ΅ μ€νμΌ κ°μ
|
200 |
-
msg = cl.Message(
|
201 |
-
content="",
|
202 |
-
actions=[
|
203 |
-
cl.Action(name="source", value="π μμ€ λ³΄κΈ°"),
|
204 |
-
cl.Action(name="feedback", value="π¬ νΌλλ°± λ¨κΈ°κΈ°")
|
205 |
-
]
|
206 |
-
)
|
207 |
-
|
208 |
-
async for token in result["response"]:
|
209 |
-
await msg.stream_token(token, is_final=False)
|
210 |
-
|
211 |
-
# μ΅μ’
λ©μμ§ ν¬λ§·ν
|
212 |
-
final_content = f"""
|
213 |
-
π§ **AI λΆμ κ²°κ³Ό**
|
214 |
-
{msg.content}
|
215 |
-
|
216 |
-
π μ°Έμ‘° λ¬Έμ₯:
|
217 |
-
{chr(10).join([f'- {ctx[0][:50]}...' for ctx in result['context']])}
|
218 |
-
"""
|
219 |
-
await msg.update(content=final_content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|