Skip to content

Commit

Permalink
Using ChatOpenAI instead of OpenAI
Browse files Browse the repository at this point in the history
  • Loading branch information
taherfattahi committed Jul 11, 2023
1 parent ccee200 commit a128889
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
6 changes: 4 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from langchain.vectorstores import Chroma
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.document_loaders import UnstructuredPDFLoader
from langchain.llms import OpenAI
# from langchain.llms import OpenAI
from langchain.chat_models import ChatOpenAI
from langchain.chains.question_answering import load_qa_chain

# Replace book.pdf with any pdf of your choice
Expand All @@ -14,6 +15,7 @@
# Choose any query of your choice
query = "Who is Rich Dad?"
docs = docsearch.get_relevant_documents(query)
chain = load_qa_chain(OpenAI(temperature=0), chain_type="stuff")
# chain = load_qa_chain(OpenAI(temperature=0), chain_type="stuff")
chain = load_qa_chain(ChatOpenAI(temperature=0), chain_type="stuff")
output = chain.run(input_documents=docs, question=query)
print(output)
9 changes: 6 additions & 3 deletions pdfquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
from langchain.vectorstores import Chroma
from langchain.document_loaders import PyPDFium2Loader
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI
# from langchain.llms import OpenAI
from langchain.chat_models import ChatOpenAI


class PDFQuery:
def __init__(self, openai_api_key = None) -> None:
self.embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
os.environ["OPENAI_API_KEY"] = openai_api_key
self.text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
self.llm = OpenAI(temperature=0, openai_api_key=openai_api_key)
# self.llm = OpenAI(temperature=0, openai_api_key=openai_api_key)
self.llm = ChatOpenAI(temperature=0, openai_api_key=openai_api_key)
self.chain = None
self.db = None

Expand All @@ -29,7 +31,8 @@ def ingest(self, file_path: os.PathLike) -> None:
documents = loader.load()
splitted_documents = self.text_splitter.split_documents(documents)
self.db = Chroma.from_documents(splitted_documents, self.embeddings).as_retriever()
self.chain = load_qa_chain(OpenAI(temperature=0), chain_type="stuff")
# self.chain = load_qa_chain(OpenAI(temperature=0), chain_type="stuff")
self.chain = load_qa_chain(ChatOpenAI(temperature=0), chain_type="stuff")

def forget(self) -> None:
self.db = None
Expand Down

0 comments on commit a128889

Please sign in to comment.