Spaces:
Sleeping
Sleeping
Delete app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139import osfrom typing import Listfrom chainlit.types import AskFileResponsefrom aimakerspace.text_utils import CharacterTextSplitter, TextFileLoader, PDFLoaderfrom aimakerspace.openai_utils.prompts import ( UserRolePrompt, SystemRolePrompt, AssistantRolePrompt,)from aimakerspace.openai_utils.embedding import EmbeddingModelfrom aimakerspace.vectordatabase import VectorDatabasefrom aimakerspace.openai_utils.chatmodel import ChatOpenAIimport chainlit as cl
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)
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_startasync 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_messageasync def main(message): chain = cl.user_session.get("chain")
msg = cl.Message(content="") result = await chain.arun_pipeline(message.content)
async for stream_resp in result["response"]: await msg.stream_token(stream_resp)
await msg.send()app.pyDeleted-139