Spaces:
Sleeping
Sleeping
Update generate_answer.py
Browse files- generate_answer.py +5 -3
generate_answer.py
CHANGED
@@ -2,10 +2,12 @@ import os
|
|
2 |
from glob import glob
|
3 |
import openai
|
4 |
from dotenv import load_dotenv
|
|
|
5 |
from langchain.embeddings import OpenAIEmbeddings
|
6 |
from langchain.vectorstores import Chroma
|
7 |
from langchain.document_loaders import PyPDFLoader
|
8 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
|
9 |
from langchain_community.chat_models import ChatOpenAI
|
10 |
from langchain.chains import RetrievalQA
|
11 |
from langchain.memory import ConversationBufferMemory
|
@@ -61,7 +63,7 @@ class VectorDB:
|
|
61 |
chunks = text_splitter.split_documents(pdf_docs)
|
62 |
|
63 |
return Chroma.from_documents(chunks, OpenAIEmbeddings())
|
64 |
-
|
65 |
class ConversationalRetrievalChain:
|
66 |
"""Class to manage the QA chain setup."""
|
67 |
|
@@ -83,11 +85,11 @@ class ConversationalRetrievalChain:
|
|
83 |
retriever=retriever,
|
84 |
memory=memory,
|
85 |
)
|
86 |
-
|
87 |
def with_pdf_chatbot(messages):
|
88 |
query = messages[-1]['content'].strip()
|
89 |
qa_chain = ConversationalRetrievalChain().create_chain()
|
90 |
result = qa_chain({"query": query})
|
91 |
if not is_response_complete(result['result']):
|
92 |
result['result'] += " This is the end of the response. Let me know if you need further clarification."
|
93 |
-
return result['result']
|
|
|
2 |
from glob import glob
|
3 |
import openai
|
4 |
from dotenv import load_dotenv
|
5 |
+
|
6 |
from langchain.embeddings import OpenAIEmbeddings
|
7 |
from langchain.vectorstores import Chroma
|
8 |
from langchain.document_loaders import PyPDFLoader
|
9 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
10 |
+
|
11 |
from langchain_community.chat_models import ChatOpenAI
|
12 |
from langchain.chains import RetrievalQA
|
13 |
from langchain.memory import ConversationBufferMemory
|
|
|
63 |
chunks = text_splitter.split_documents(pdf_docs)
|
64 |
|
65 |
return Chroma.from_documents(chunks, OpenAIEmbeddings())
|
66 |
+
|
67 |
class ConversationalRetrievalChain:
|
68 |
"""Class to manage the QA chain setup."""
|
69 |
|
|
|
85 |
retriever=retriever,
|
86 |
memory=memory,
|
87 |
)
|
88 |
+
|
89 |
def with_pdf_chatbot(messages):
|
90 |
query = messages[-1]['content'].strip()
|
91 |
qa_chain = ConversationalRetrievalChain().create_chain()
|
92 |
result = qa_chain({"query": query})
|
93 |
if not is_response_complete(result['result']):
|
94 |
result['result'] += " This is the end of the response. Let me know if you need further clarification."
|
95 |
+
return result['result']
|