Skip to content

The agent engineering platform.

Weaviate: Add QnA with sources example (#5247)

Shukri09e246f30611
diff --git a/docs/modules/indexes/vectorstores/examples/weaviate.ipynb b/docs/modules/indexes/vectorstores/examples/weaviate.ipynb
index 8233e8f3f6..e2494e38d9 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": {