File size: 6,815 Bytes
234eac0
 
 
ba43ba6
234eac0
 
 
 
 
 
 
 
 
87dacef
 
234eac0
 
 
 
 
 
 
 
 
 
 
 
 
 
87dacef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234eac0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba43ba6
234eac0
6130712
 
 
 
 
 
 
 
 
 
ba43ba6
6130712
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234eac0
 
 
 
 
 
 
 
 
ba43ba6
 
1aaad7e
234eac0
 
 
 
 
 
b791c4a
234eac0
 
 
 
ba43ba6
234eac0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87dacef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import os
from typing import List
from chainlit.types import AskFileResponse
from aimakerspace.text_utils import CharacterTextSplitter, TextFileLoader, PDFLoader
from aimakerspace.openai_utils.prompts import (
    UserRolePrompt,
    SystemRolePrompt,
    AssistantRolePrompt,
)
from aimakerspace.openai_utils.embedding import EmbeddingModel
from aimakerspace.vectordatabase import VectorDatabase
from aimakerspace.openai_utils.chatmodel import ChatOpenAI
import chainlit as cl
from chainlit import user_session
from chainlit.element import Text

system_template = """\
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."""
system_role_prompt = SystemRolePrompt(system_template)

user_prompt_template = """\
Context:
{context}

Question:
{question}
"""
user_role_prompt = UserRolePrompt(user_prompt_template)

@cl.on_chat_start
async def init_sidebar():
    # μ‚¬μ΄λ“œλ°” 헀더 κΎΈλ―ΈκΈ°
    await cl.Sidebar(
        cl.Text(content="πŸ“ **파일 μ—…λ‘œλ“œ μ„Ήμ…˜**", style="heading3"),
        cl.FilePicker(
            accept=[".pdf", ".txt"],
            max_size_mb=2,
            on_upload=handle_upload,
            label="πŸ“€ PDF/TXT μ—…λ‘œλ“œ",
            description="μ΅œλŒ€ 2MB 파일만 μ—…λ‘œλ“œ κ°€λŠ₯ν•©λ‹ˆλ‹€"
        ),
        cl.Separator(),
        cl.Text(content="πŸ” **λ¬Έμ„œ 뢄석 μƒνƒœ**", style="heading4"),
        cl.ProgressRing(id="progress", visible=False),
        cl.Text(id="status", content="λŒ€κΈ° 쀑...", style="caption"),
        title="πŸ“š λ¬Έμ„œ 질의 μ‹œμŠ€ν…œ",
        persistent=True  # πŸ‘ˆ μ‚¬μ΄λ“œλ°” κ³ μ • μ„€μ •
    ).send()


async def handle_upload(file: AskFileResponse):
    # μ§„ν–‰ μƒνƒœ μ—…λ°μ΄νŠΈ
    status = user_session.get("status")
    progress = user_session.get("progress")
    
    await status.update(content=f"πŸ” {file.name} 뢄석 쀑...")
    await progress.update(visible=True)
    
    try:
        # 파일 처리 둜직
        texts = process_file(file)
        
        # 벑터 DB ꡬ좕
        vector_db = VectorDatabase()
        vector_db = await vector_db.abuild_from_list(texts)
        
        # μ„Έμ…˜μ— μ €μž₯
        user_session.set("vector_db", vector_db)
        
        # μƒνƒœ μ—…λ°μ΄νŠΈ
        await status.update(content=f"βœ… {len(texts)}개 청크 처리 μ™„λ£Œ!")
        await progress.update(visible=False)
        
        # 파일 정보 μš”μ•½ ν‘œμ‹œ
        await cl.Accordion(
            title="πŸ“„ μ—…λ‘œλ“œ λ¬Έμ„œ 정보",
            content=[
                cl.Text(f"파일λͺ…: {file.name}"),
                cl.Text(f"크기: {file.size/1024:.1f}KB"),
                cl.Text(f"뢄석 μ‹œκ°„: {datetime.now().strftime('%H:%M:%S')}")
            ],
            expanded=False
        ).send()
        
    except Exception as e:
        await cl.Error(
            title="파일 처리 였λ₯˜",
            content=f"{str(e)}"
        ).send()

class RetrievalAugmentedQAPipeline:
    def __init__(self, llm: ChatOpenAI(), vector_db_retriever: VectorDatabase) -> None:
        self.llm = llm
        self.vector_db_retriever = vector_db_retriever

    async def arun_pipeline(self, user_query: str):
        context_list = self.vector_db_retriever.search_by_text(user_query, k=4)

        context_prompt = ""
        for context in context_list:
            context_prompt += context[0] + "\n"

        formatted_system_prompt = system_role_prompt.create_message()

        formatted_user_prompt = user_role_prompt.create_message(question=user_query, context=context_prompt)

        async def generate_response():
            async for chunk in self.llm.astream([formatted_system_prompt, formatted_user_prompt]):
                yield chunk

        return {"response": generate_response(), "context": context_list}

text_splitter = CharacterTextSplitter()


def process_file(file: AskFileResponse):
    import tempfile
    import shutil
    
    print(f"Processing file: {file.name}")
    
    # Create a temporary file with the correct extension
    suffix = f".{file.name.split('.')[-1]}"
    with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as temp_file:
        # Copy the uploaded file content to the temporary file
        shutil.copyfile(file.path, temp_file.name)
        print(f"Created temporary file at: {temp_file.name}")
        
        # Create appropriate loader
        if file.name.lower().endswith('.pdf'):
            loader = PDFLoader(temp_file.name)
        else:
            loader = TextFileLoader(temp_file.name)
            
        try:
            # Load and process the documents
            documents = loader.load_documents()
            texts = text_splitter.split_texts(documents)
            return texts
        finally:
            # Clean up the temporary file
            try:
                os.unlink(temp_file.name)
            except Exception as e:
                print(f"Error cleaning up temporary file: {e}")


@cl.on_chat_start
async def on_chat_start():
    files = None

    # Wait for the user to upload a file
    while files == None:
        files = await cl.AskFileMessage(
            content="Please upload a Text or PDF file to begin!",
            accept=["text/plain", "application/pdf"],
            max_size_mb=2,
            timeout=180,
        ).send()

    file = files[0]

    msg = cl.Message(
        content=f"Processing `{file.name}`..."
    )
    await msg.send()

    # load the file
    texts = process_file(file)

    print(f"Processing {len(texts)} text chunks")

    # Create a dict vector store
    vector_db = VectorDatabase()
    vector_db = await vector_db.abuild_from_list(texts)
    
    chat_openai = ChatOpenAI()

    # Create a chain
    retrieval_augmented_qa_pipeline = RetrievalAugmentedQAPipeline(
        vector_db_retriever=vector_db,
        llm=chat_openai
    )
    
    # Let the user know that the system is ready
    msg.content = f"Processing `{file.name}` done. You can now ask questions!"
    await msg.update()

    cl.user_session.set("chain", retrieval_augmented_qa_pipeline)


@cl.on_message
async def main(message):
    chain = cl.user_session.get("chain")




    # 응닡 μŠ€νƒ€μΌ κ°œμ„ 
    msg = cl.Message(
        content="",
        actions=[
            cl.Action(name="source", value="πŸ“‘ μ†ŒμŠ€ 보기"),
            cl.Action(name="feedback", value="πŸ’¬ ν”Όλ“œλ°± 남기기")
        ]
    )
    
    async for token in result["response"]:
        await msg.stream_token(token, is_final=False)
    
    # μ΅œμ’… λ©”μ‹œμ§€ ν¬λ§·νŒ…
    final_content = f"""
    🧠 **AI 뢄석 κ²°κ³Ό**  
    {msg.content}
    
    πŸ“Œ μ°Έμ‘° λ¬Έμž₯:  
    {chr(10).join([f'- {ctx[0][:50]}...' for ctx in result['context']])}
    """
    await msg.update(content=final_content)