|
|
|
|
|
|
|
import os |
|
from openai import AsyncOpenAI |
|
import chainlit as cl |
|
from chainlit.prompt import Prompt, PromptMessage |
|
|
|
from aimakerspace.openai_utils.chatmodel import ChatOpenAI |
|
from dotenv import load_dotenv |
|
from chainlit.types import AskFileResponse |
|
|
|
from aimakerspace.text_utils import TextFileLoader, CharacterTextSplitter |
|
from aimakerspace.vectordatabase import VectorDatabase |
|
import asyncio |
|
from aimakerspace.rag_utils.raqa import RetrievalAugmentedQAPipeline |
|
|
|
load_dotenv() |
|
|
|
|
|
|
|
|
|
|
|
system_template = """You are a helpful assistant who always speaks in a pleasant tone! |
|
""" |
|
|
|
user_template = """{input} |
|
Think through your response step by step. |
|
""" |
|
|
|
def transform_file(file: AskFileResponse): |
|
|
|
import tempfile |
|
|
|
with tempfile.NamedTemporaryFile(mode="w", delete=False) as tempfile: |
|
with open(tempfile.name, "wb") as f: |
|
f.write(file.content) |
|
|
|
|
|
print(tempfile.name) |
|
text_loader_pdf = TextFileLoader(tempfile.name) |
|
documents = text_loader_pdf.load_documents() |
|
|
|
text_splitter = CharacterTextSplitter() |
|
split_documents = text_splitter.split_texts(documents) |
|
|
|
return split_documents |
|
|
|
|
|
@cl.on_chat_start |
|
async def start_chat(): |
|
files = None |
|
|
|
while files == None: |
|
files = await cl.AskFileMessage( |
|
content="Please upload a PDF file to begin!", |
|
accept=["application/pdf"], |
|
max_size_mb=20, |
|
timeout=180, |
|
).send() |
|
|
|
file = files[0] |
|
|
|
msg = cl.Message( |
|
content=f"Processing `{file.name}`...", disable_human_feedback=True |
|
) |
|
await msg.send() |
|
|
|
|
|
documents = transform_file(file) |
|
|
|
vector_uefa_db = VectorDatabase() |
|
vector_uefa_db = asyncio.run(vector_uefa_db.abuild_from_list(documents)) |
|
|
|
retrieval_augmented_qa_pipeline = RetrievalAugmentedQAPipeline( |
|
vector_db_retriever=vector_uefa_db, |
|
llm=ChatOpenAI() |
|
) |
|
|
|
|
|
|
|
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: cl.Message): |
|
chain = cl.user_session.get("chain") |
|
|
|
resp = chain.run_pipeline(message.content) |
|
|
|
msg = cl.Message(content=resp["response"]) |
|
|
|
await msg.send() |
|
|