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

Weaviate: Add QnA with sources example #5247

Merged
merged 1 commit into from
May 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions docs/modules/indexes/vectorstores/examples/weaviate.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down