2023-06-21 08:22:27 +00:00
{
"cells": [
{
"cell_type": "markdown",
2023-07-31 16:54:59 +00:00
"id": "9787b308",
2023-06-21 08:22:27 +00:00
"metadata": {},
"source": [
2023-06-27 05:55:04 +00:00
"# Rockset\n",
2023-06-21 08:22:27 +00:00
"\n",
2023-07-31 16:54:59 +00:00
">[Rockset](https://rockset.com/) is a real-time search and analytics database built for the cloud. Rockset uses a [Converged Index™](https://rockset.com/blog/converged-indexing-the-secret-sauce-behind-rocksets-fast-queries/) with an efficient store for vector embeddings to serve low latency, high concurrency search queries at scale. Rockset has full support for metadata filtering and handles real-time ingestion for constantly updating, streaming data.\n",
2023-06-21 08:22:27 +00:00
"\n",
2023-07-31 16:54:59 +00:00
"This notebook demonstrates how to use `Rockset` as a vector store in LangChain. Before getting started, make sure you have access to a `Rockset` account and an API key available. [Start your free trial today.](https://rockset.com/create/)\n"
2023-06-21 08:22:27 +00:00
]
},
{
"cell_type": "markdown",
2023-07-31 16:54:59 +00:00
"id": "b823d64a",
2023-06-21 08:22:27 +00:00
"metadata": {},
"source": [
2023-07-31 16:54:59 +00:00
"## Setting Up Your Environment[](https://python.langchain.com/docs/modules/data_connection/vectorstores/integrations/rockset#setting-up-environment)\n",
2023-06-21 08:22:27 +00:00
"\n",
2023-07-31 16:54:59 +00:00
"1. Leverage the `Rockset` console to create a [collection](https://rockset.com/docs/collections/) with the Write API as your source. In this walkthrough, we create a collection named `langchain_demo`. \n",
" \n",
2023-08-06 22:04:01 +00:00
" Configure the following [ingest transformation](https://rockset.com/docs/ingest-transformation/) to mark your embeddings field and take advantage of performance and storage optimizations:\n",
"\n",
"\n",
" (We used OpenAI `text-embedding-ada-002` for this examples, where #length_of_vector_embedding = 1536)"
2023-06-21 08:22:27 +00:00
]
},
{
2023-07-31 16:54:59 +00:00
"cell_type": "code",
"execution_count": null,
"id": "aac58387",
"metadata": {
"vscode": {
"languageId": "sql"
}
},
"outputs": [],
2023-06-21 08:22:27 +00:00
"source": [
2023-07-31 16:54:59 +00:00
"SELECT _input.* EXCEPT(_meta), \n",
"VECTOR_ENFORCE(_input.description_embedding, #length_of_vector_embedding, 'float') as description_embedding \n",
"FROM _input"
2023-06-21 08:22:27 +00:00
]
},
{
"cell_type": "markdown",
2023-07-31 16:54:59 +00:00
"id": "df380e1c",
2023-06-21 08:22:27 +00:00
"metadata": {},
"source": [
2023-07-31 16:54:59 +00:00
"2. After creating your collection, use the console to retrieve an [API key](https://rockset.com/docs/iam/#users-api-keys-and-roles). For the purpose of this notebook, we assume you are using the `Oregon(us-west-2)` region.\n",
"\n",
"3. Install the [rockset-python-client](https://github.com/rockset/rockset-python-client) to enable LangChain to communicate directly with `Rockset`."
2023-06-21 08:22:27 +00:00
]
},
{
"cell_type": "code",
"execution_count": null,
2023-07-31 16:54:59 +00:00
"id": "00d16b83",
2023-06-21 08:22:27 +00:00
"metadata": {},
"outputs": [],
"source": [
2023-07-31 16:54:59 +00:00
"pip install rockset"
2023-06-21 08:22:27 +00:00
]
},
{
"cell_type": "markdown",
2023-07-31 16:54:59 +00:00
"id": "e79550eb",
2023-06-21 08:22:27 +00:00
"metadata": {},
"source": [
2023-07-31 16:54:59 +00:00
"## LangChain Tutorial\n",
"\n",
"Follow along in your own Python notebook to generate and store vector embeddings in Rockset.\n",
"Start using Rockset to search for documents similar to your search queries.\n",
"\n",
"### 1. Define Key Variables"
2023-06-21 08:22:27 +00:00
]
},
{
"cell_type": "code",
2023-08-06 22:04:01 +00:00
"execution_count": null,
2023-07-31 16:54:59 +00:00
"id": "29505c1e",
2023-06-21 08:22:27 +00:00
"metadata": {},
2023-08-06 22:04:01 +00:00
"outputs": [],
2023-06-21 08:22:27 +00:00
"source": [
"import os\n",
"import rockset\n",
"\n",
2023-07-31 16:54:59 +00:00
"ROCKSET_API_KEY = os.environ.get(\"ROCKSET_API_KEY\") # Verify ROCKSET_API_KEY environment variable\n",
"ROCKSET_API_SERVER = rockset.Regions.usw2a1 # Verify Rockset region\n",
2023-06-21 08:22:27 +00:00
"rockset_client = rockset.RocksetClient(ROCKSET_API_SERVER, ROCKSET_API_KEY)\n",
"\n",
2023-07-31 16:54:59 +00:00
"COLLECTION_NAME='langchain_demo'\n",
"TEXT_KEY='description'\n",
"EMBEDDING_KEY='description_embedding'"
2023-06-21 08:22:27 +00:00
]
},
{
"cell_type": "markdown",
2023-07-31 16:54:59 +00:00
"id": "07625be2",
2023-06-21 08:22:27 +00:00
"metadata": {},
"source": [
2023-07-31 16:54:59 +00:00
"### 2. Prepare Documents"
2023-06-21 08:22:27 +00:00
]
},
{
"cell_type": "code",
"execution_count": null,
2023-07-31 16:54:59 +00:00
"id": "9740d8c4",
2023-06-21 08:22:27 +00:00
"metadata": {},
2023-08-06 22:04:01 +00:00
"outputs": [],
2023-06-21 08:22:27 +00:00
"source": [
"from langchain.embeddings.openai import OpenAIEmbeddings\n",
"from langchain.text_splitter import CharacterTextSplitter\n",
"from langchain.document_loaders import TextLoader\n",
2023-07-29 15:44:32 +00:00
"from langchain.vectorstores import Rockset\n",
2023-06-21 08:22:27 +00:00
"\n",
2023-07-31 16:54:59 +00:00
"loader = TextLoader('../../../state_of_the_union.txt')\n",
2023-06-21 08:22:27 +00:00
"documents = loader.load()\n",
"text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
"docs = text_splitter.split_documents(documents)"
]
},
{
"cell_type": "markdown",
2023-07-31 16:54:59 +00:00
"id": "a068be18",
2023-06-21 08:22:27 +00:00
"metadata": {},
"source": [
2023-07-31 16:54:59 +00:00
"### 3. Insert Documents"
2023-06-21 08:22:27 +00:00
]
},
{
"cell_type": "code",
"execution_count": null,
2023-07-31 16:54:59 +00:00
"id": "85b6a6c5",
2023-06-21 08:22:27 +00:00
"metadata": {},
2023-08-06 22:04:01 +00:00
"outputs": [],
2023-06-21 08:22:27 +00:00
"source": [
2023-08-06 22:04:01 +00:00
"embeddings = OpenAIEmbeddings() # Verify OPENAI_API_KEY environment variable\n",
2023-06-21 08:22:27 +00:00
"\n",
2023-07-29 15:44:32 +00:00
"docsearch = Rockset(\n",
2023-06-21 08:22:27 +00:00
" client=rockset_client,\n",
" embeddings=embeddings,\n",
" collection_name=COLLECTION_NAME,\n",
" text_key=TEXT_KEY,\n",
" embedding_key=EMBEDDING_KEY,\n",
")\n",
"\n",
2023-07-31 16:54:59 +00:00
"ids=docsearch.add_texts(\n",
2023-06-21 08:22:27 +00:00
" texts=[d.page_content for d in docs],\n",
" metadatas=[d.metadata for d in docs],\n",
2023-07-31 16:54:59 +00:00
")"
2023-06-21 08:22:27 +00:00
]
},
{
"cell_type": "markdown",
2023-07-31 16:54:59 +00:00
"id": "56eef48d",
2023-06-21 08:22:27 +00:00
"metadata": {},
"source": [
2023-07-31 16:54:59 +00:00
"### 4. Search for Similar Documents"
2023-06-21 08:22:27 +00:00
]
},
{
"cell_type": "code",
2023-08-06 22:04:01 +00:00
"execution_count": null,
2023-07-31 16:54:59 +00:00
"id": "0bbf3df0",
2023-06-21 08:22:27 +00:00
"metadata": {},
2023-08-06 22:04:01 +00:00
"outputs": [],
2023-06-21 08:22:27 +00:00
"source": [
"query = \"What did the president say about Ketanji Brown Jackson\"\n",
Fix `make docs_build` and related scripts (#7276)
**Description: a description of the change**
Fixed `make docs_build` and related scripts which caused errors. There
are several changes.
First, I made the build of the documentation and the API Reference into
two separate commands. This is because it takes less time to build. The
commands for documents are `make docs_build`, `make docs_clean`, and
`make docs_linkcheck`. The commands for API Reference are `make
api_docs_build`, `api_docs_clean`, and `api_docs_linkcheck`.
It looked like `docs/.local_build.sh` could be used to build the
documentation, so I used that. Since `.local_build.sh` was also building
API Rerefence internally, I removed that process. `.local_build.sh` also
added some Bash options to stop in error or so. Futher more added `cd
"${SCRIPT_DIR}"` at the beginning so that the script will work no matter
which directory it is executed in.
`docs/api_reference/api_reference.rst` is removed, because which is
generated by `docs/api_reference/create_api_rst.py`, and added it to
.gitignore.
Finally, the description of CONTRIBUTING.md was modified.
**Issue: the issue # it fixes (if applicable)**
https://github.com/hwchase17/langchain/issues/6413
**Dependencies: any dependencies required for this change**
`nbdoc` was missing in group docs so it was added. I installed it with
the `poetry add --group docs nbdoc` command. I am concerned if any
modifications are needed to poetry.lock. I would greatly appreciate it
if you could pay close attention to this file during the review.
**Tag maintainer**
- General / Misc / if you don't know who to tag: @baskaryan
If this PR needs any additional changes, I'll be happy to make them!
---------
Co-authored-by: Bagatur <baskaryan@gmail.com>
2023-07-12 02:05:14 +00:00
"output = docsearch.similarity_search_with_relevance_scores(\n",
2023-07-29 15:44:32 +00:00
" query, 4, Rockset.DistanceFunction.COSINE_SIM\n",
Fix `make docs_build` and related scripts (#7276)
**Description: a description of the change**
Fixed `make docs_build` and related scripts which caused errors. There
are several changes.
First, I made the build of the documentation and the API Reference into
two separate commands. This is because it takes less time to build. The
commands for documents are `make docs_build`, `make docs_clean`, and
`make docs_linkcheck`. The commands for API Reference are `make
api_docs_build`, `api_docs_clean`, and `api_docs_linkcheck`.
It looked like `docs/.local_build.sh` could be used to build the
documentation, so I used that. Since `.local_build.sh` was also building
API Rerefence internally, I removed that process. `.local_build.sh` also
added some Bash options to stop in error or so. Futher more added `cd
"${SCRIPT_DIR}"` at the beginning so that the script will work no matter
which directory it is executed in.
`docs/api_reference/api_reference.rst` is removed, because which is
generated by `docs/api_reference/create_api_rst.py`, and added it to
.gitignore.
Finally, the description of CONTRIBUTING.md was modified.
**Issue: the issue # it fixes (if applicable)**
https://github.com/hwchase17/langchain/issues/6413
**Dependencies: any dependencies required for this change**
`nbdoc` was missing in group docs so it was added. I installed it with
the `poetry add --group docs nbdoc` command. I am concerned if any
modifications are needed to poetry.lock. I would greatly appreciate it
if you could pay close attention to this file during the review.
**Tag maintainer**
- General / Misc / if you don't know who to tag: @baskaryan
If this PR needs any additional changes, I'll be happy to make them!
---------
Co-authored-by: Bagatur <baskaryan@gmail.com>
2023-07-12 02:05:14 +00:00
")\n",
2023-06-21 08:22:27 +00:00
"print(\"output length:\", len(output))\n",
"for d, dist in output:\n",
2023-07-31 16:54:59 +00:00
" print(dist, d.metadata, d.page_content[:20] + '...')\n",
Fix `make docs_build` and related scripts (#7276)
**Description: a description of the change**
Fixed `make docs_build` and related scripts which caused errors. There
are several changes.
First, I made the build of the documentation and the API Reference into
two separate commands. This is because it takes less time to build. The
commands for documents are `make docs_build`, `make docs_clean`, and
`make docs_linkcheck`. The commands for API Reference are `make
api_docs_build`, `api_docs_clean`, and `api_docs_linkcheck`.
It looked like `docs/.local_build.sh` could be used to build the
documentation, so I used that. Since `.local_build.sh` was also building
API Rerefence internally, I removed that process. `.local_build.sh` also
added some Bash options to stop in error or so. Futher more added `cd
"${SCRIPT_DIR}"` at the beginning so that the script will work no matter
which directory it is executed in.
`docs/api_reference/api_reference.rst` is removed, because which is
generated by `docs/api_reference/create_api_rst.py`, and added it to
.gitignore.
Finally, the description of CONTRIBUTING.md was modified.
**Issue: the issue # it fixes (if applicable)**
https://github.com/hwchase17/langchain/issues/6413
**Dependencies: any dependencies required for this change**
`nbdoc` was missing in group docs so it was added. I installed it with
the `poetry add --group docs nbdoc` command. I am concerned if any
modifications are needed to poetry.lock. I would greatly appreciate it
if you could pay close attention to this file during the review.
**Tag maintainer**
- General / Misc / if you don't know who to tag: @baskaryan
If this PR needs any additional changes, I'll be happy to make them!
---------
Co-authored-by: Bagatur <baskaryan@gmail.com>
2023-07-12 02:05:14 +00:00
"\n",
2023-06-21 08:22:27 +00:00
"##\n",
"# output length: 4\n",
"# 0.764990692109871 {'source': '../../../state_of_the_union.txt'} Madam Speaker, Madam...\n",
"# 0.7485416901622112 {'source': '../../../state_of_the_union.txt'} And I’ m taking robus...\n",
"# 0.7468678973398306 {'source': '../../../state_of_the_union.txt'} And so many families...\n",
"# 0.7436231261419488 {'source': '../../../state_of_the_union.txt'} Groups of citizens b..."
]
},
{
"cell_type": "markdown",
2023-07-31 16:54:59 +00:00
"id": "7037a22f",
2023-06-21 08:22:27 +00:00
"metadata": {},
"source": [
2023-07-31 16:54:59 +00:00
"### 5. Search for Similar Documents with Filtering"
2023-06-21 08:22:27 +00:00
]
},
{
"cell_type": "code",
"execution_count": null,
2023-07-31 16:54:59 +00:00
"id": "b64a290f",
2023-06-21 08:22:27 +00:00
"metadata": {},
"outputs": [],
"source": [
"output = docsearch.similarity_search_with_relevance_scores(\n",
" query,\n",
" 4,\n",
2023-07-29 15:44:32 +00:00
" Rockset.DistanceFunction.COSINE_SIM,\n",
Fix `make docs_build` and related scripts (#7276)
**Description: a description of the change**
Fixed `make docs_build` and related scripts which caused errors. There
are several changes.
First, I made the build of the documentation and the API Reference into
two separate commands. This is because it takes less time to build. The
commands for documents are `make docs_build`, `make docs_clean`, and
`make docs_linkcheck`. The commands for API Reference are `make
api_docs_build`, `api_docs_clean`, and `api_docs_linkcheck`.
It looked like `docs/.local_build.sh` could be used to build the
documentation, so I used that. Since `.local_build.sh` was also building
API Rerefence internally, I removed that process. `.local_build.sh` also
added some Bash options to stop in error or so. Futher more added `cd
"${SCRIPT_DIR}"` at the beginning so that the script will work no matter
which directory it is executed in.
`docs/api_reference/api_reference.rst` is removed, because which is
generated by `docs/api_reference/create_api_rst.py`, and added it to
.gitignore.
Finally, the description of CONTRIBUTING.md was modified.
**Issue: the issue # it fixes (if applicable)**
https://github.com/hwchase17/langchain/issues/6413
**Dependencies: any dependencies required for this change**
`nbdoc` was missing in group docs so it was added. I installed it with
the `poetry add --group docs nbdoc` command. I am concerned if any
modifications are needed to poetry.lock. I would greatly appreciate it
if you could pay close attention to this file during the review.
**Tag maintainer**
- General / Misc / if you don't know who to tag: @baskaryan
If this PR needs any additional changes, I'll be happy to make them!
---------
Co-authored-by: Bagatur <baskaryan@gmail.com>
2023-07-12 02:05:14 +00:00
" where_str=\"{} NOT LIKE '%citizens%'\".format(TEXT_KEY),\n",
2023-06-21 08:22:27 +00:00
")\n",
"print(\"output length:\", len(output))\n",
"for d, dist in output:\n",
2023-07-31 16:54:59 +00:00
" print(dist, d.metadata, d.page_content[:20] + '...')\n",
Fix `make docs_build` and related scripts (#7276)
**Description: a description of the change**
Fixed `make docs_build` and related scripts which caused errors. There
are several changes.
First, I made the build of the documentation and the API Reference into
two separate commands. This is because it takes less time to build. The
commands for documents are `make docs_build`, `make docs_clean`, and
`make docs_linkcheck`. The commands for API Reference are `make
api_docs_build`, `api_docs_clean`, and `api_docs_linkcheck`.
It looked like `docs/.local_build.sh` could be used to build the
documentation, so I used that. Since `.local_build.sh` was also building
API Rerefence internally, I removed that process. `.local_build.sh` also
added some Bash options to stop in error or so. Futher more added `cd
"${SCRIPT_DIR}"` at the beginning so that the script will work no matter
which directory it is executed in.
`docs/api_reference/api_reference.rst` is removed, because which is
generated by `docs/api_reference/create_api_rst.py`, and added it to
.gitignore.
Finally, the description of CONTRIBUTING.md was modified.
**Issue: the issue # it fixes (if applicable)**
https://github.com/hwchase17/langchain/issues/6413
**Dependencies: any dependencies required for this change**
`nbdoc` was missing in group docs so it was added. I installed it with
the `poetry add --group docs nbdoc` command. I am concerned if any
modifications are needed to poetry.lock. I would greatly appreciate it
if you could pay close attention to this file during the review.
**Tag maintainer**
- General / Misc / if you don't know who to tag: @baskaryan
If this PR needs any additional changes, I'll be happy to make them!
---------
Co-authored-by: Bagatur <baskaryan@gmail.com>
2023-07-12 02:05:14 +00:00
"\n",
2023-06-21 08:22:27 +00:00
"##\n",
"# output length: 4\n",
"# 0.7651359650263554 {'source': '../../../state_of_the_union.txt'} Madam Speaker, Madam...\n",
"# 0.7486265516824893 {'source': '../../../state_of_the_union.txt'} And I’ m taking robus...\n",
"# 0.7469625542348115 {'source': '../../../state_of_the_union.txt'} And so many families...\n",
"# 0.7344177777547739 {'source': '../../../state_of_the_union.txt'} We see the unity amo..."
]
},
{
2023-07-29 15:44:32 +00:00
"attachments": {},
2023-06-21 08:22:27 +00:00
"cell_type": "markdown",
2023-07-31 16:54:59 +00:00
"id": "13a52b38",
2023-06-21 08:22:27 +00:00
"metadata": {},
"source": [
2023-07-31 16:54:59 +00:00
"### 6. [Optional] Delete Inserted Documents\n",
2023-06-21 08:22:27 +00:00
"\n",
2023-07-31 16:54:59 +00:00
"You must have the unique ID associated with each document to delete them from your collection.\n",
"Define IDs when inserting documents with `Rockset.add_texts()`. Rockset will otherwise generate a unique ID for each document. Regardless, `Rockset.add_texts()` returns the IDs of inserted documents.\n",
2023-06-21 08:22:27 +00:00
"\n",
2023-07-29 15:44:32 +00:00
"To delete these docs, simply use the `Rockset.delete_texts()` function."
2023-06-21 08:22:27 +00:00
]
},
{
"cell_type": "code",
"execution_count": null,
2023-07-31 16:54:59 +00:00
"id": "1f755924",
2023-06-21 08:22:27 +00:00
"metadata": {},
"outputs": [],
"source": [
"docsearch.delete_texts(ids)"
]
},
{
"cell_type": "markdown",
2023-07-31 16:54:59 +00:00
"id": "d468f431",
2023-06-21 08:22:27 +00:00
"metadata": {},
"source": [
2023-07-31 16:54:59 +00:00
"## Summary\n",
2023-06-21 08:22:27 +00:00
"\n",
2023-07-31 16:54:59 +00:00
"In this tutorial, we successfully created a `Rockset` collection, `inserted` documents with OpenAI embeddings, and searched for similar documents with and without metadata filters.\n",
2023-06-21 08:22:27 +00:00
"\n",
2023-07-31 16:54:59 +00:00
"Keep an eye on https://rockset.com/ for future updates in this space."
2023-06-21 08:22:27 +00:00
]
}
],
"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",
2023-08-06 22:04:01 +00:00
"version": "3.10.12"
2023-06-21 08:22:27 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}