Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -19,9 +19,11 @@ from langchain_core.runnables.history import RunnableWithMessageHistory
|
|
19 |
from langchain_core.chat_history import BaseChatMessageHistory
|
20 |
from langchain_community.chat_message_histories import ChatMessageHistory
|
21 |
from multiprocessing import Process
|
|
|
22 |
|
23 |
-
|
24 |
-
|
|
|
25 |
retriever = None
|
26 |
conversational_rag_chain = None
|
27 |
loader = DirectoryLoader('./database', glob="./*.txt", loader_cls=TextLoader)
|
@@ -31,13 +33,6 @@ documents = loader.load()
|
|
31 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
32 |
splits = text_splitter.split_documents(documents)
|
33 |
|
34 |
-
print()
|
35 |
-
print("-------")
|
36 |
-
print("TextSplitter, DirectoryLoader")
|
37 |
-
print("-------")
|
38 |
-
|
39 |
-
persist_directory = 'db'
|
40 |
-
|
41 |
model_name = "BAAI/bge-small-en-v1.5"
|
42 |
model_kwargs = {'device': 'cpu'}
|
43 |
encode_kwargs = {'normalize_embeddings': True}
|
@@ -56,7 +51,7 @@ def format_docs(docs):
|
|
56 |
retriever = vectorstore.as_retriever()
|
57 |
|
58 |
prompt = hub.pull("rlm/rag-prompt")
|
59 |
-
llm = HuggingFaceEndpoint(repo_id="
|
60 |
rag_chain = (
|
61 |
{"context": retriever | format_docs, "question": RunnablePassthrough()}
|
62 |
| prompt
|
@@ -65,9 +60,9 @@ rag_chain = (
|
|
65 |
)
|
66 |
|
67 |
### Contextualize question ###
|
68 |
-
contextualize_q_system_prompt = """Given a chat history and the latest user question
|
69 |
-
which might reference context in the chat history, formulate a standalone question
|
70 |
-
which can be understood without the chat history. Do NOT answer the question,
|
71 |
just reformulate it if needed and otherwise return it as is."""
|
72 |
contextualize_q_prompt = ChatPromptTemplate.from_messages(
|
73 |
[
|
@@ -82,12 +77,13 @@ history_aware_retriever = create_history_aware_retriever(
|
|
82 |
|
83 |
|
84 |
### Answer question ###
|
85 |
-
qa_system_prompt = """You are an assistant for question-answering tasks.
|
86 |
-
Use the following pieces of retrieved context to answer the question.
|
87 |
-
If you don't know the answer, just say that you don't know.
|
88 |
-
Use three sentences maximum and keep the answer concise
|
89 |
|
90 |
-
{context}
|
|
|
91 |
qa_prompt = ChatPromptTemplate.from_messages(
|
92 |
[
|
93 |
("system", qa_system_prompt),
|
|
|
19 |
from langchain_core.chat_history import BaseChatMessageHistory
|
20 |
from langchain_community.chat_message_histories import ChatMessageHistory
|
21 |
from multiprocessing import Process
|
22 |
+
from zipfile import ZipFile
|
23 |
|
24 |
+
with ZipFile("database.zip") as f:
|
25 |
+
f.extractall()
|
26 |
+
|
27 |
retriever = None
|
28 |
conversational_rag_chain = None
|
29 |
loader = DirectoryLoader('./database', glob="./*.txt", loader_cls=TextLoader)
|
|
|
33 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
34 |
splits = text_splitter.split_documents(documents)
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
model_name = "BAAI/bge-small-en-v1.5"
|
37 |
model_kwargs = {'device': 'cpu'}
|
38 |
encode_kwargs = {'normalize_embeddings': True}
|
|
|
51 |
retriever = vectorstore.as_retriever()
|
52 |
|
53 |
prompt = hub.pull("rlm/rag-prompt")
|
54 |
+
llm = HuggingFaceEndpoint(repo_id="google/gemma-2-2b-it")
|
55 |
rag_chain = (
|
56 |
{"context": retriever | format_docs, "question": RunnablePassthrough()}
|
57 |
| prompt
|
|
|
60 |
)
|
61 |
|
62 |
### Contextualize question ###
|
63 |
+
contextualize_q_system_prompt = """Given a chat history and the latest user question
|
64 |
+
which might reference context in the chat history, formulate a standalone question
|
65 |
+
which can be understood without the chat history. Do NOT answer the question,
|
66 |
just reformulate it if needed and otherwise return it as is."""
|
67 |
contextualize_q_prompt = ChatPromptTemplate.from_messages(
|
68 |
[
|
|
|
77 |
|
78 |
|
79 |
### Answer question ###
|
80 |
+
qa_system_prompt = """You are an assistant for question-answering tasks.
|
81 |
+
Use the following pieces of retrieved context to answer the question.
|
82 |
+
If you don't know the answer, just say that you don't know.
|
83 |
+
Use three sentences maximum and keep the answer concise. Do not repeat 'Assistant: ' or 'AI: '.
|
84 |
|
85 |
+
{context}
|
86 |
+
{}"""
|
87 |
qa_prompt = ChatPromptTemplate.from_messages(
|
88 |
[
|
89 |
("system", qa_system_prompt),
|