From 09e246f30611a30d25f5545b7cf171e5ff1164c4 Mon Sep 17 00:00:00 2001 From: Shukri Date: Thu, 25 May 2023 21:58:33 +0800 Subject: [PATCH] Weaviate: Add QnA with sources example (#5247) # Add QnA with sources example Fixes: see https://stackoverflow.com/questions/76207160/langchain-doesnt-work-with-weaviate-vector-database-getting-valueerror/76210017#76210017 ## Before submitting ## Who can review? Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested: @dev2049 --- .../vectorstores/examples/weaviate.ipynb | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/docs/modules/indexes/vectorstores/examples/weaviate.ipynb b/docs/modules/indexes/vectorstores/examples/weaviate.ipynb index 8233e8f3..e2494e38 100644 --- a/docs/modules/indexes/vectorstores/examples/weaviate.ipynb +++ b/docs/modules/indexes/vectorstores/examples/weaviate.ipynb @@ -257,6 +257,101 @@ "retriever = db.as_retriever(search_type=\"mmr\")\n", "retriever.get_relevant_documents(query)[0]" ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "fbd7a6cb", + "metadata": {}, + "source": [ + "## Question Answering with Sources" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "f349acb9", + "metadata": {}, + "source": [ + "This section goes over how to do question-answering with sources over an Index. It does this by using the `RetrievalQAWithSourcesChain`, which does the lookup of the documents from an Index. " + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "5e824f3b", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.chains import RetrievalQAWithSourcesChain\n", + "from langchain import OpenAI" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "61209cc3", + "metadata": {}, + "outputs": [], + "source": [ + "with open(\"../../../state_of_the_union.txt\") as f:\n", + " state_of_the_union = f.read()\n", + "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n", + "texts = text_splitter.split_text(state_of_the_union)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "4abc3d37", + "metadata": {}, + "outputs": [], + "source": [ + "docsearch = Weaviate.from_texts(\n", + " texts,\n", + " embeddings,\n", + " weaviate_url=WEAVIATE_URL,\n", + " by_text=False,\n", + " metadatas=[{\"source\": f\"{i}-pl\"} for i in range(len(texts))],\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "c7062393", + "metadata": {}, + "outputs": [], + "source": [ + "chain = RetrievalQAWithSourcesChain.from_chain_type(\n", + " OpenAI(temperature=0), chain_type=\"stuff\", retriever=docsearch.as_retriever()\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "7e41b773", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'answer': \" The president honored Justice Breyer for his service and mentioned his legacy of excellence. He also nominated Circuit Court of Appeals Judge Ketanji Brown Jackson to continue Justice Breyer's legacy.\\n\",\n", + " 'sources': '31-pl, 34-pl'}" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "chain(\n", + " {\"question\": \"What did the president say about Justice Breyer\"},\n", + " return_only_outputs=True,\n", + ")" + ] } ], "metadata": {