mirror of
https://github.com/hwchase17/langchain
synced 2024-10-31 15:20:26 +00:00
87e502c6bc
Co-authored-by: jacoblee93 <jacoblee93@gmail.com> Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
265 lines
8.9 KiB
Plaintext
265 lines
8.9 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "ce0f17b9",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Weaviate Hybrid Search\n",
|
|
"\n",
|
|
">[Weaviate](https://weaviate.io/developers/weaviate) is an open source vector database.\n",
|
|
"\n",
|
|
">[Hybrid search](https://weaviate.io/blog/hybrid-search-explained) is a technique that combines multiple search algorithms to improve the accuracy and relevance of search results. It uses the best features of both keyword-based search algorithms with vector search techniques.\n",
|
|
"\n",
|
|
">The `Hybrid search in Weaviate` uses sparse and dense vectors to represent the meaning and context of search queries and documents.\n",
|
|
"\n",
|
|
"This notebook shows how to use `Weaviate hybrid search` as a LangChain retriever."
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"id": "c307b082",
|
|
"metadata": {},
|
|
"source": [
|
|
"Set up the retriever:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "bba863a2-977c-4add-b5f4-bfc33a80eae5",
|
|
"metadata": {
|
|
"tags": []
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"#!pip install weaviate-client"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "c10dd962",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import weaviate\n",
|
|
"import os\n",
|
|
"\n",
|
|
"WEAVIATE_URL = os.getenv(\"WEAVIATE_URL\")\n",
|
|
"client = weaviate.Client(\n",
|
|
" url=WEAVIATE_URL,\n",
|
|
" auth_client_secret=weaviate.AuthApiKey(api_key=os.getenv(\"WEAVIATE_API_KEY\")),\n",
|
|
" additional_headers={\n",
|
|
" \"X-Openai-Api-Key\": os.getenv(\"OPENAI_API_KEY\"),\n",
|
|
" },\n",
|
|
")\n",
|
|
"\n",
|
|
"# client.schema.delete_all()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"id": "f47a2bfe",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"/workspaces/langchain/langchain/vectorstores/analyticdb.py:20: MovedIn20Warning: The ``declarative_base()`` function is now available as sqlalchemy.orm.declarative_base(). (deprecated since: 2.0) (Background on SQLAlchemy 2.0 at: https://sqlalche.me/e/b8d9)\n",
|
|
" Base = declarative_base() # type: Any\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"from langchain.retrievers.weaviate_hybrid_search import WeaviateHybridSearchRetriever\n",
|
|
"from langchain.schema import Document"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"id": "f2eff08e",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"retriever = WeaviateHybridSearchRetriever(\n",
|
|
" client, index_name=\"LangChain\", text_key=\"text\"\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"id": "b68debff",
|
|
"metadata": {},
|
|
"source": [
|
|
"Add some data:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"id": "cd8a7b17",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"docs = [\n",
|
|
" Document(\n",
|
|
" metadata={\n",
|
|
" \"title\": \"Embracing The Future: AI Unveiled\",\n",
|
|
" \"author\": \"Dr. Rebecca Simmons\",\n",
|
|
" },\n",
|
|
" page_content=\"A comprehensive analysis of the evolution of artificial intelligence, from its inception to its future prospects. Dr. Simmons covers ethical considerations, potentials, and threats posed by AI.\",\n",
|
|
" ),\n",
|
|
" Document(\n",
|
|
" metadata={\n",
|
|
" \"title\": \"Symbiosis: Harmonizing Humans and AI\",\n",
|
|
" \"author\": \"Prof. Jonathan K. Sterling\",\n",
|
|
" },\n",
|
|
" page_content=\"Prof. Sterling explores the potential for harmonious coexistence between humans and artificial intelligence. The book discusses how AI can be integrated into society in a beneficial and non-disruptive manner.\",\n",
|
|
" ),\n",
|
|
" Document(\n",
|
|
" metadata={\"title\": \"AI: The Ethical Quandary\", \"author\": \"Dr. Rebecca Simmons\"},\n",
|
|
" page_content=\"In her second book, Dr. Simmons delves deeper into the ethical considerations surrounding AI development and deployment. It is an eye-opening examination of the dilemmas faced by developers, policymakers, and society at large.\",\n",
|
|
" ),\n",
|
|
" Document(\n",
|
|
" metadata={\n",
|
|
" \"title\": \"Conscious Constructs: The Search for AI Sentience\",\n",
|
|
" \"author\": \"Dr. Samuel Cortez\",\n",
|
|
" },\n",
|
|
" page_content=\"Dr. Cortez takes readers on a journey exploring the controversial topic of AI consciousness. The book provides compelling arguments for and against the possibility of true AI sentience.\",\n",
|
|
" ),\n",
|
|
" Document(\n",
|
|
" metadata={\n",
|
|
" \"title\": \"Invisible Routines: Hidden AI in Everyday Life\",\n",
|
|
" \"author\": \"Prof. Jonathan K. Sterling\",\n",
|
|
" },\n",
|
|
" page_content=\"In his follow-up to 'Symbiosis', Prof. Sterling takes a look at the subtle, unnoticed presence and influence of AI in our everyday lives. It reveals how AI has become woven into our routines, often without our explicit realization.\",\n",
|
|
" ),\n",
|
|
"]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"id": "3c5970db",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"['eda16d7d-437d-4613-84ae-c2e38705ec7a',\n",
|
|
" '04b501bf-192b-4e72-be77-2fbbe7e67ebf',\n",
|
|
" '18a1acdb-23b7-4482-ab04-a6c2ed51de77',\n",
|
|
" '88e82cc3-c020-4b5a-b3c6-ca7cf3fc6a04',\n",
|
|
" 'f6abd9d5-32ed-46c4-bd08-f8d0f7c9fc95']"
|
|
]
|
|
},
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"retriever.add_documents(docs)"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"id": "6e030694",
|
|
"metadata": {},
|
|
"source": [
|
|
"Do a hybrid search:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"id": "bf7dbb98",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"[Document(page_content='In her second book, Dr. Simmons delves deeper into the ethical considerations surrounding AI development and deployment. It is an eye-opening examination of the dilemmas faced by developers, policymakers, and society at large.', metadata={}),\n",
|
|
" Document(page_content='A comprehensive analysis of the evolution of artificial intelligence, from its inception to its future prospects. Dr. Simmons covers ethical considerations, potentials, and threats posed by AI.', metadata={}),\n",
|
|
" Document(page_content=\"In his follow-up to 'Symbiosis', Prof. Sterling takes a look at the subtle, unnoticed presence and influence of AI in our everyday lives. It reveals how AI has become woven into our routines, often without our explicit realization.\", metadata={}),\n",
|
|
" Document(page_content='Prof. Sterling explores the potential for harmonious coexistence between humans and artificial intelligence. The book discusses how AI can be integrated into society in a beneficial and non-disruptive manner.', metadata={})]"
|
|
]
|
|
},
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"retriever.get_relevant_documents(\"the ethical implications of AI\")"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"id": "d0c5bb4d",
|
|
"metadata": {},
|
|
"source": [
|
|
"Do a hybrid search with where filter:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"id": "b2bc87c1",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"[Document(page_content='Prof. Sterling explores the potential for harmonious coexistence between humans and artificial intelligence. The book discusses how AI can be integrated into society in a beneficial and non-disruptive manner.', metadata={}),\n",
|
|
" Document(page_content=\"In his follow-up to 'Symbiosis', Prof. Sterling takes a look at the subtle, unnoticed presence and influence of AI in our everyday lives. It reveals how AI has become woven into our routines, often without our explicit realization.\", metadata={})]"
|
|
]
|
|
},
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"retriever.get_relevant_documents(\n",
|
|
" \"AI integration in society\",\n",
|
|
" where_filter={\n",
|
|
" \"path\": [\"author\"],\n",
|
|
" \"operator\": \"Equal\",\n",
|
|
" \"valueString\": \"Prof. Jonathan K. Sterling\",\n",
|
|
" },\n",
|
|
")"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.9.16"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|