openai-cookbook
openai-cookbook copied to clipboard
Add QA with Langchain of AnalyticDB vector store and OpenAI example.
Hi there,I hope you're doing well.
I'm excited to open this PR. This is another example of using a fully Postgres syntax compatible database 'AnalyticDB' as a datastore to build QA with Langchain and OpenAI APIs.
As AnalyticDB can be used with Langchain directly for now. This example is a better case for user who want to use 'AnalyticDB' as a vector store.
I would really appreciate your feedback.
Thanks, I'll take a look. What's your goal for this contribution?
Also, what kind of feedback are you looking for? If you think it's in good shape, I'll spend less time reviewing it before merging.
Also, what kind of feedback are you looking for? If you think it's in good shape, I'll spend less time reviewing it before merging.
So sorry if my commit message wasn't clear enough in conveying my intentions. I think it's in good shape, This notebook presents how to implement a Question Answering system with LangChain, AnalyticDB and OpenAI embeddings. This example mainly consists of the following main steps.
- prepare connection string with Environment variables and LangChain
from langchain.vectorstores.analyticdb import AnalyticDB
CONNECTION_STRING = AnalyticDB.connection_string_from_db_params(
driver=os.environ.get("PG_DRIVER", "psycopg2cffi"),
host=os.environ.get("PG_HOST", "localhost"),
port=int(os.environ.get("PG_PORT", "5432")),
database=os.environ.get("PG_DATABASE", "postgres"),
user=os.environ.get("PG_USER", "postgres"),
password=os.environ.get("PG_PASSWORD", "postgres"),
)
- Definition a VectorStore Chain of AnalyticDB
from langchain.vectorstores import AnalyticDB
from langchain.embeddings import OpenAIEmbeddings
from langchain import VectorDBQA, OpenAI
embeddings = OpenAIEmbeddings()
doc_store = AnalyticDB.from_texts(
texts=answers, embedding=embeddings, connection_string=CONNECTION_STRING,
pre_delete_collection=True,
)
- Define QA chain with OpenAI llm and vector store defined above.
llm = OpenAI()
qa = VectorDBQA.from_chain_type(
llm=llm,
chain_type="stuff",
vectorstore=doc_store,
return_source_documents=False,
)
- SearchData with the QA chain
qa.run(question)
Thank you for your feedback! I've addressed your suggestions regarding wording and formatting. Thank you once again for your guidance and support, and I look forward to our continued collaboration.