Mehrdad Esmaeili
commited on
Commit
•
164769e
1
Parent(s):
c386109
Update app.py
Browse files
app.py
CHANGED
@@ -13,7 +13,16 @@ from langchain.vectorstores import Chroma
|
|
13 |
import os
|
14 |
from tqdm import tqdm
|
15 |
import gradio as gr
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
documents=[]
|
18 |
path='./bios/'
|
19 |
# path='./augBios/'
|
@@ -24,15 +33,56 @@ for file in os.listdir(path):
|
|
24 |
documents += loader.load()
|
25 |
# print(documents)
|
26 |
print(len(documents))
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
texts = text_splitter.split_documents(documents)
|
29 |
|
30 |
# embeddings = OpenAIEmbeddings()
|
31 |
embeddings = CohereEmbeddings(model='embed-english-v3.0')
|
32 |
docsearch = Chroma.from_documents(texts, embeddings)
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
36 |
|
37 |
def predict(message, history):
|
38 |
# history_langchain_format = []
|
@@ -42,7 +92,17 @@ def predict(message, history):
|
|
42 |
# history_langchain_format.append(HumanMessage(content=message))
|
43 |
# gpt_response = llm(history_langchain_format)
|
44 |
# return gpt_response.content
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
result = qa({"query": message})
|
47 |
# r1=docsearch.similarity_search_with_score(query=q,k=3)
|
48 |
# print([(item[-2].metadata,item[-1]) for item in r1],\
|
@@ -51,10 +111,8 @@ def predict(message, history):
|
|
51 |
return result['result']+f'\n---\nAmazon Kindle ebook description is:\n {result["source_documents"][0].page_content}'+\
|
52 |
f'\nfrom this file: {result["source_documents"][0].metadata}'
|
53 |
else:
|
54 |
-
return result['result']
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
gr.ChatInterface(predict,
|
59 |
chatbot=gr.Chatbot(height='auto'),
|
60 |
textbox=gr.Textbox(placeholder="Recommend a book on someone who..."),
|
|
|
13 |
import os
|
14 |
from tqdm import tqdm
|
15 |
import gradio as gr
|
16 |
+
from langchain.memory import ConversationSummaryMemory
|
17 |
+
from langchain.chains import ConversationalRetrievalChain
|
18 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
19 |
+
from langchain.chains import LLMChain
|
20 |
+
from langchain.prompts import (
|
21 |
+
ChatPromptTemplate,
|
22 |
+
HumanMessagePromptTemplate,
|
23 |
+
MessagesPlaceholder,
|
24 |
+
SystemMessagePromptTemplate,
|
25 |
+
)
|
26 |
documents=[]
|
27 |
path='./bios/'
|
28 |
# path='./augBios/'
|
|
|
33 |
documents += loader.load()
|
34 |
# print(documents)
|
35 |
print(len(documents))
|
36 |
+
'''This is the code used for without memory chat'''
|
37 |
+
# text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=0)
|
38 |
+
# texts = text_splitter.split_documents(documents)
|
39 |
+
|
40 |
+
# # embeddings = OpenAIEmbeddings()
|
41 |
+
# embeddings = CohereEmbeddings(model='embed-english-v3.0')
|
42 |
+
# docsearch = Chroma.from_documents(texts, embeddings)
|
43 |
+
|
44 |
+
# qa = RetrievalQA.from_chain_type(llm=Cohere(model='command'), chain_type="stuff", \
|
45 |
+
# retriever=docsearch.as_retriever(search_kwargs={'k':1}),return_source_documents=True)
|
46 |
+
|
47 |
+
# def predict(message, history):
|
48 |
+
# # history_langchain_format = []
|
49 |
+
# # for human, ai in history:
|
50 |
+
# # history_langchain_format.append(HumanMessage(content=human))
|
51 |
+
# # history_langchain_format.append(AIMessage(content=ai))
|
52 |
+
# # history_langchain_format.append(HumanMessage(content=message))
|
53 |
+
# # gpt_response = llm(history_langchain_format)
|
54 |
+
# # return gpt_response.content
|
55 |
+
|
56 |
+
# message=message+'? just the book title-Author'
|
57 |
+
# #---------
|
58 |
+
# for human, ai in history:
|
59 |
+
# history_langchain_format.append(HumanMessage(content=human))
|
60 |
+
# history_langchain_format.append(AIMessage(content=ai))
|
61 |
+
# history_langchain_format.append(HumanMessage(content=message))
|
62 |
+
# gpt_response = llm(history_langchain_format)
|
63 |
+
# #------------
|
64 |
+
# return gpt_response.content
|
65 |
+
# result = qa({"query": message})
|
66 |
+
# # r1=docsearch.similarity_search_with_score(query=q,k=3)
|
67 |
+
# # print([(item[-2].metadata,item[-1]) for item in r1],\
|
68 |
+
# # '\n\n',result['result'],f'|| {result["source_documents"][0].metadata}','\n*****\n')
|
69 |
+
# if result['result'] not in ["I don't know","I don't know."]:
|
70 |
+
# return result['result']+f'\n---\nAmazon Kindle ebook description is:\n {result["source_documents"][0].page_content}'+\
|
71 |
+
# f'\nfrom this file: {result["source_documents"][0].metadata}'
|
72 |
+
# else:
|
73 |
+
# return result['result']
|
74 |
+
'''This is code used for with memory chat'''
|
75 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=0)
|
76 |
texts = text_splitter.split_documents(documents)
|
77 |
|
78 |
# embeddings = OpenAIEmbeddings()
|
79 |
embeddings = CohereEmbeddings(model='embed-english-v3.0')
|
80 |
docsearch = Chroma.from_documents(texts, embeddings)
|
81 |
+
memory=ConversationSummaryMemory(
|
82 |
+
llm=Cohere(model='command'), memory_key="chat_history", return_messages=True
|
83 |
+
)
|
84 |
+
qa = ConversationalRetrievalChain.from_llm(llm=Cohere(model='command'), \
|
85 |
+
retriever=docsearch.as_retriever(),return_source_documents=True,memory=memory)
|
86 |
|
87 |
def predict(message, history):
|
88 |
# history_langchain_format = []
|
|
|
92 |
# history_langchain_format.append(HumanMessage(content=message))
|
93 |
# gpt_response = llm(history_langchain_format)
|
94 |
# return gpt_response.content
|
95 |
+
|
96 |
+
# message=message+'? just the book title-Autho'
|
97 |
+
#---------
|
98 |
+
# for human, ai in history:
|
99 |
+
# history_langchain_format.append(HumanMessage(content=human))
|
100 |
+
# history_langchain_format.append(AIMessage(content=ai))
|
101 |
+
# history_langchain_format.append(HumanMessage(content=message))
|
102 |
+
# gpt_response = llm(history_langchain_format)
|
103 |
+
# return gpt_response.content
|
104 |
+
#------------
|
105 |
+
|
106 |
result = qa({"query": message})
|
107 |
# r1=docsearch.similarity_search_with_score(query=q,k=3)
|
108 |
# print([(item[-2].metadata,item[-1]) for item in r1],\
|
|
|
111 |
return result['result']+f'\n---\nAmazon Kindle ebook description is:\n {result["source_documents"][0].page_content}'+\
|
112 |
f'\nfrom this file: {result["source_documents"][0].metadata}'
|
113 |
else:
|
114 |
+
return result['result']
|
115 |
+
'''------'''
|
|
|
|
|
116 |
gr.ChatInterface(predict,
|
117 |
chatbot=gr.Chatbot(height='auto'),
|
118 |
textbox=gr.Textbox(placeholder="Recommend a book on someone who..."),
|