Hi everyone,
I am using Langchain RetrievalQA chain to QA over a JSON document. The document is related to the organization’s portfolio. I tried it for one-on-one module, the chatbot results are good for that but when I try it on a complete portfolio it does not return correct answer.
import os
from langchain.vectorstores import Chroma
from langchain.embeddings import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.llms import OpenAI
from langchain.chains import RetrievalQA
from langchain.document_loaders import JSONLoader
os.environ['OPENAI_API_KEY'] = 'sk-my-openai-api-key'
document_path = '/path/to/document/portfolio.json'
def load_docs(directory):
loader = JSONLoader(document_path, jq_schema=".", text_content=False)
documents = loader.load()
return documents
documents = load_docs(document_path)
def split_docs(documents, chunk_size=500, chunk_overlap=10):
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=chunk_size,
chunk_overlap=chunk_overlap,
length_function=len,
add_start_index=True
)
docs = text_splitter.split_documents(documents)
return docs
docs = split_docs(documents)
persist_directory = 'chroma_db'
embedding = OpenAIEmbeddings()
vectordb = Chroma.from_documents(
documents=docs,
embedding=embedding,
persist_directory=persist_directory,
collection_name='semp6_portfolio'
)
vectordb.persist()
vectordb = None
prsstdb = Chroma(
persist_directory=persist_directory,
embedding_function=embedding,
collection_name='semp6_portfolio'
)
qa = RetrievalQA.from_chain_type(
llm=OpenAI(),
chain_type="stuff",
retriever=prsstdb.as_retriever(),
)
qa.run(query)
Am I doing something wrong here? Need help.