langchain/docs/extras/integrations/vectorstores/docarray_hnsw.ipynb
2023-07-23 23:23:16 -07:00

245 lines
7.0 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "2ce41f46-5711-4311-b04d-2fe233ac5b1b",
"metadata": {},
"source": [
"# DocArrayHnswSearch\n",
"\n",
">[DocArrayHnswSearch](https://docs.docarray.org/user_guide/storing/index_hnswlib/) is a lightweight Document Index implementation provided by [Docarray](https://docs.docarray.org/) that runs fully locally and is best suited for small- to medium-sized datasets. It stores vectors on disk in [hnswlib](https://github.com/nmslib/hnswlib), and stores all other data in [SQLite](https://www.sqlite.org/index.html).\n",
"\n",
"This notebook shows how to use functionality related to the `DocArrayHnswSearch`."
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "7ee37d28",
"metadata": {},
"source": [
"## Setup\n",
"\n",
"Uncomment the below cells to install docarray and get/set your OpenAI api key if you haven't already done so."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8ce1b8cb-dbf0-40c3-99ee-04f28143331b",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# !pip install \"docarray[hnswlib]\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "878f17df-100f-4854-9e87-472cf36d51f3",
"metadata": {
"scrolled": true,
"tags": []
},
"outputs": [],
"source": [
"# Get an OpenAI token: https://platform.openai.com/account/api-keys\n",
"\n",
"# import os\n",
"# from getpass import getpass\n",
"\n",
"# OPENAI_API_KEY = getpass()\n",
"\n",
"# os.environ[\"OPENAI_API_KEY\"] = OPENAI_API_KEY"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "8dbb6de2",
"metadata": {
"tags": []
},
"source": [
"## Using DocArrayHnswSearch"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b757afef-ef0a-465d-8e8a-9aadb9c32b88",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from langchain.embeddings.openai import OpenAIEmbeddings\n",
"from langchain.text_splitter import CharacterTextSplitter\n",
"from langchain.vectorstores import DocArrayHnswSearch\n",
"from langchain.document_loaders import TextLoader"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "605e200e-e711-486b-b36e-cbe5dd2512d7",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"documents = TextLoader(\"../../../state_of_the_union.txt\").load()\n",
"text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
"docs = text_splitter.split_documents(documents)\n",
"\n",
"embeddings = OpenAIEmbeddings()\n",
"\n",
"db = DocArrayHnswSearch.from_documents(\n",
" docs, embeddings, work_dir=\"hnswlib_store/\", n_dim=1536\n",
")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "ed6f905b-4853-4a44-9730-614aa8e22b78",
"metadata": {},
"source": [
"### Similarity search"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "4d7e742f-2002-449d-a10e-16046890906c",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"query = \"What did the president say about Ketanji Brown Jackson\"\n",
"docs = db.similarity_search(query)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "0da9e26f-1fc2-48e6-95a7-f692c853bbd3",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while youre at it, pass the Disclose Act so Americans can know who is funding our elections. \n",
"\n",
"Tonight, Id like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. \n",
"\n",
"One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. \n",
"\n",
"And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nations top legal minds, who will continue Justice Breyers legacy of excellence.\n"
]
}
],
"source": [
"print(docs[0].page_content)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "3febb987-e903-416f-af26-6897d84c8d61",
"metadata": {},
"source": [
"### Similarity search with score"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "bb1df11a",
"metadata": {},
"source": [
"The returned distance score is cosine distance. Therefore, a lower score is better."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "40764fdd-357d-475a-8152-5f1979d61a45",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"docs = db.similarity_search_with_score(query)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "a479fc46-b299-4330-89b9-e9b5a218ea03",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(Document(page_content='Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while youre at it, pass the Disclose Act so Americans can know who is funding our elections. \\n\\nTonight, Id like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. \\n\\nOne of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. \\n\\nAnd I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nations top legal minds, who will continue Justice Breyers legacy of excellence.', metadata={}),\n",
" 0.36962226)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"docs[0]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "4d3d4e97-5d2b-4571-8ff9-e3f6b6778714",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import shutil\n",
"\n",
"# delete the dir\n",
"shutil.rmtree(\"hnswlib_store\")"
]
}
],
"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.10.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}