Spaces:
Running
Running
Update chatbot.py
Browse files- chatbot.py +99 -102
chatbot.py
CHANGED
@@ -1,102 +1,99 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
-
from
|
4 |
-
from langchain_community.
|
5 |
-
from langchain_community.
|
6 |
-
from
|
7 |
-
from
|
8 |
-
from langchain.chains
|
9 |
-
from
|
10 |
-
from langchain_community.
|
11 |
-
from
|
12 |
-
from langchain_core.
|
13 |
-
from langchain_core.prompts import
|
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 |
-
return answer
|
101 |
-
else:
|
102 |
-
return "Please load a document first."
|
|
|
1 |
+
|
2 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
3 |
+
from langchain_community.llms import HuggingFaceEndpoint
|
4 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
5 |
+
from langchain_community.vectorstores import Chroma
|
6 |
+
from langchain_core.prompts import ChatPromptTemplate
|
7 |
+
from langchain.chains.combine_documents import create_stuff_documents_chain
|
8 |
+
from langchain.chains import create_retrieval_chain, create_history_aware_retriever
|
9 |
+
from langchain_community.document_loaders import PyMuPDFLoader
|
10 |
+
from langchain_community.llms import Ollama
|
11 |
+
from langchain_core.messages import HumanMessage, AIMessage
|
12 |
+
from langchain_core.prompts import ChatPromptTemplate
|
13 |
+
from langchain_core.prompts import MessagesPlaceholder
|
14 |
+
|
15 |
+
|
16 |
+
|
17 |
+
class AdjustedHuggingFaceEmbeddings(HuggingFaceEmbeddings):
|
18 |
+
def __call__(self, input):
|
19 |
+
return super().__call__(input)
|
20 |
+
|
21 |
+
|
22 |
+
def create_chain(chains, pdf_doc, use_local_model=True):
|
23 |
+
if pdf_doc is None:
|
24 |
+
return 'You must convert or upload a pdf first'
|
25 |
+
db = create_vector_db(pdf_doc)
|
26 |
+
llm = create_model(use_local_model)
|
27 |
+
prompt_search_query = ChatPromptTemplate.from_messages([
|
28 |
+
MessagesPlaceholder(
|
29 |
+
variable_name="chat_history"),
|
30 |
+
("user", "{input}"),
|
31 |
+
("user",
|
32 |
+
"Given the above conversation, generate a search query to look up to get information relevant to the conversation")
|
33 |
+
])
|
34 |
+
retriever_chain = create_history_aware_retriever(llm, db.as_retriever(), prompt_search_query)
|
35 |
+
prompt_get_answer = ChatPromptTemplate.from_messages([
|
36 |
+
("system", "Answer the user's questions based on the below context:\\n\\n{context}"),
|
37 |
+
MessagesPlaceholder(variable_name="chat_history"),
|
38 |
+
("user", "{input}"),
|
39 |
+
])
|
40 |
+
combine_docs_chain = create_stuff_documents_chain(llm=llm, prompt=prompt_get_answer)
|
41 |
+
chains[0] = create_retrieval_chain(retriever_chain, combine_docs_chain)
|
42 |
+
return 'Document has successfully been loaded'
|
43 |
+
|
44 |
+
|
45 |
+
def create_model(local: bool):
|
46 |
+
if local:
|
47 |
+
llm = Ollama(model='phi')
|
48 |
+
else:
|
49 |
+
llm = HuggingFaceEndpoint(
|
50 |
+
repo_id="OpenAssistant/oasst-sft-1-pythia-12b",
|
51 |
+
model_kwargs={"max_length": 256},
|
52 |
+
temperature=1.0
|
53 |
+
)
|
54 |
+
return llm
|
55 |
+
|
56 |
+
|
57 |
+
def create_vector_db(doc):
|
58 |
+
document = load_document(doc)
|
59 |
+
text = split_document(document)
|
60 |
+
embedding = AdjustedHuggingFaceEmbeddings()
|
61 |
+
db = Chroma.from_documents(text, embedding)
|
62 |
+
return db
|
63 |
+
|
64 |
+
|
65 |
+
def load_document(doc):
|
66 |
+
loader = PyMuPDFLoader(doc.name)
|
67 |
+
document = loader.load()
|
68 |
+
return document
|
69 |
+
|
70 |
+
|
71 |
+
def split_document(doc):
|
72 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
73 |
+
text = text_splitter.split_documents(doc)
|
74 |
+
return text
|
75 |
+
|
76 |
+
|
77 |
+
def save_history(history):
|
78 |
+
with open('history.txt', 'w') as file:
|
79 |
+
for s in history:
|
80 |
+
file.write(f'- {s.content}\n')
|
81 |
+
|
82 |
+
|
83 |
+
def answer_query(chain, query: str, chat_history=None) -> str:
|
84 |
+
if chain:
|
85 |
+
# run the given chain with the given query and history
|
86 |
+
chat_history.append(HumanMessage(content=query))
|
87 |
+
response = chain.invoke({
|
88 |
+
'chat_history': chat_history,
|
89 |
+
'input': query
|
90 |
+
})
|
91 |
+
answer = response['answer']
|
92 |
+
print('RESPONSE: ', answer, '\n\n')
|
93 |
+
# add the current question and answer to history
|
94 |
+
chat_history.append(AIMessage(content=answer))
|
95 |
+
# save chat history to text file
|
96 |
+
save_history(chat_history)
|
97 |
+
return answer
|
98 |
+
else:
|
99 |
+
return "Please load a document first."
|
|
|
|
|
|