Spaces:
Runtime error
Runtime error
Update app.py
Browse files18 unmodified lines1920212223242526273 unmodified lines3132333435363738394041424312 unmodified lines565758596061622 unmodified lines6566676869707172738 unmodified lines82838485868788899091929318 unmodified linesfrom langchain_core.chat_history import BaseChatMessageHistoryfrom langchain_community.chat_message_histories import ChatMessageHistoryfrom multiprocessing import Processif not os.path.isdir('database'):os.system("unzip database.zip")retriever = Noneconversational_rag_chain = Noneloader = DirectoryLoader('./database', glob="./*.txt", loader_cls=TextLoader)3 unmodified linestext_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)splits = text_splitter.split_documents(documents)print()print("-------")print("TextSplitter, DirectoryLoader")print("-------")persist_directory = 'db'model_name = "BAAI/bge-small-en-v1.5"model_kwargs = {'device': 'cpu'}encode_kwargs = {'normalize_embeddings': True}12 unmodified linesretriever = vectorstore.as_retriever()prompt = hub.pull("rlm/rag-prompt")llm = HuggingFaceEndpoint(repo_id="mistralai/Mistral-7B-Instruct-v0.3")rag_chain = ({"context": retriever | format_docs, "question": RunnablePassthrough()}| prompt2 unmodified lines)### Contextualize question ###contextualize_q_system_prompt = """Given a chat history and the latest user question \which might reference context in the chat history, formulate a standalone question \which can be understood without the chat history. Do NOT answer the question, \just reformulate it if needed and otherwise return it as is."""contextualize_q_prompt = ChatPromptTemplate.from_messages([8 unmodified lines### Answer question ###qa_system_prompt = """You are an assistant for question-answering tasks. \Use the following pieces of retrieved context to answer the question. \If you don't know the answer, just say that you don't know. \Use three sentences maximum and keep the answer concise.\{context}"""qa_prompt = ChatPromptTemplate.from_messages([("system", qa_system_prompt),18 unmodified lines19202122232425262728293 unmodified lines33343536373812 unmodified lines515253545556572 unmodified lines6061626364656667688 unmodified lines7778798081828384858687888918 unmodified linesfrom langchain_core.chat_history import BaseChatMessageHistoryfrom langchain_community.chat_message_histories import ChatMessageHistoryfrom multiprocessing import Processfrom zipfile import ZipFilewith ZipFile("database.zip") as f:f.extractall()retriever = Noneconversational_rag_chain = Noneloader = DirectoryLoader('./database', glob="./*.txt", loader_cls=TextLoader)3 unmodified linestext_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)splits = text_splitter.split_documents(documents)model_name = "BAAI/bge-small-en-v1.5"model_kwargs = {'device': 'cpu'}encode_kwargs = {'normalize_embeddings': True}12 unmodified linesretriever = vectorstore.as_retriever()prompt = hub.pull("rlm/rag-prompt")llm = HuggingFaceEndpoint(repo_id="google/gemma-2-2b-it")rag_chain = ({"context": retriever | format_docs, "question": RunnablePassthrough()}| prompt2 unmodified lines)### Contextualize question ###contextualize_q_system_prompt = """Given a chat history and the latest user questionwhich might reference context in the chat history, formulate a standalone questionwhich can be understood without the chat history. Do NOT answer the question,just reformulate it if needed and otherwise return it as is."""contextualize_q_prompt = ChatPromptTemplate.from_messages([8 unmodified lines### Answer question ###qa_system_prompt = """You are an assistant for question-answering tasks.Use the following pieces of retrieved context to answer the question.If you don't know the answer, just say that you don't know.Use three sentences maximum and keep the answer concise. Do not repeat 'Assistant: ' or 'AI: '.{context}{}"""qa_prompt = ChatPromptTemplate.from_messages([("system", qa_system_prompt),
app.py+14-18