Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add QA with Langchain of AnalyticDB vector store and OpenAI example. #374

Merged
merged 1 commit into from
May 12, 2023

Conversation

wangxuqi
Copy link
Contributor

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.

@ted-at-openai
Copy link
Collaborator

Thanks, I'll take a look. What's your goal for this contribution?

@ted-at-openai
Copy link
Collaborator

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.

@wangxuqi
Copy link
Contributor Author

wangxuqi commented May 3, 2023

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)

Copy link
Collaborator

@ted-at-openai ted-at-openai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. I didn't run the whole thing myself, as I didn't want to go through the trouble of setting up a DB. I left some suggestions mostly on wording and formatting. Happy to merge once addressed. Thank you!

@wangxuqi wangxuqi force-pushed the analyticdb_langchain_example branch from 31d2786 to 57340b2 Compare May 6, 2023 03:05
@wangxuqi wangxuqi force-pushed the analyticdb_langchain_example branch from 4199400 to d12cb6c Compare May 6, 2023 03:10
@wangxuqi
Copy link
Contributor Author

wangxuqi commented May 6, 2023

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.

Copy link
Collaborator

@ted-at-openai ted-at-openai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't check carefully, but I trust you addressed the changes and are happy with the final result. Will merge.

@ted-at-openai ted-at-openai merged commit 2478232 into openai:main May 12, 2023
katia-openai pushed a commit that referenced this pull request Feb 29, 2024
Add QA with Langchain of AnalyticDB  vector store and OpenAI example.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants