mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-06 21:43:44 +00:00
community[minor]: Adding Azure Cosmos Mongo vCore Vector DB Cache (#16856)
Description: This pull request introduces several enhancements for Azure Cosmos Vector DB, primarily focused on improving caching and search capabilities using Azure Cosmos MongoDB vCore Vector DB. Here's a summary of the changes: - **AzureCosmosDBSemanticCache**: Added a new cache implementation called AzureCosmosDBSemanticCache, which utilizes Azure Cosmos MongoDB vCore Vector DB for efficient caching of semantic data. Added comprehensive test cases for AzureCosmosDBSemanticCache to ensure its correctness and robustness. These tests cover various scenarios and edge cases to validate the cache's behavior. - **HNSW Vector Search**: Added HNSW vector search functionality in the CosmosDB Vector Search module. This enhancement enables more efficient and accurate vector searches by utilizing the HNSW (Hierarchical Navigable Small World) algorithm. Added corresponding test cases to validate the HNSW vector search functionality in both AzureCosmosDBSemanticCache and AzureCosmosDBVectorSearch. These tests ensure the correctness and performance of the HNSW search algorithm. - **LLM Caching Notebook** - The notebook now includes a comprehensive example showcasing the usage of the AzureCosmosDBSemanticCache. This example highlights how the cache can be employed to efficiently store and retrieve semantic data. Additionally, the example provides default values for all parameters used within the AzureCosmosDBSemanticCache, ensuring clarity and ease of understanding for users who are new to the cache implementation. @hwchase17,@baskaryan, @eyurtsev,
This commit is contained in:
@@ -12,9 +12,14 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 15,
|
||||
"id": "10ad9224",
|
||||
"metadata": {},
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2024-02-02T21:34:23.461332Z",
|
||||
"start_time": "2024-02-02T21:34:23.394461Z"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.globals import set_llm_cache\n",
|
||||
@@ -1349,6 +1354,144 @@
|
||||
"print(llm(\"Is is possible that something false can be also true?\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## Azure Cosmos DB Semantic Cache"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "40624c26e86b57a4"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.cache import AzureCosmosDBSemanticCache\n",
|
||||
"from langchain_community.vectorstores.azure_cosmos_db import (\n",
|
||||
" CosmosDBSimilarityType,\n",
|
||||
" CosmosDBVectorSearchType,\n",
|
||||
")\n",
|
||||
"from langchain_openai import OpenAIEmbeddings\n",
|
||||
"\n",
|
||||
"# Read more about Azure CosmosDB Mongo vCore vector search here https://learn.microsoft.com/en-us/azure/cosmos-db/mongodb/vcore/vector-search\n",
|
||||
"\n",
|
||||
"INDEX_NAME = \"langchain-test-index\"\n",
|
||||
"NAMESPACE = \"langchain_test_db.langchain_test_collection\"\n",
|
||||
"CONNECTION_STRING = (\n",
|
||||
" \"Please provide your azure cosmos mongo vCore vector db connection string\"\n",
|
||||
")\n",
|
||||
"DB_NAME, COLLECTION_NAME = NAMESPACE.split(\".\")\n",
|
||||
"\n",
|
||||
"# Default value for these params\n",
|
||||
"num_lists = 3\n",
|
||||
"dimensions = 1536\n",
|
||||
"similarity_algorithm = CosmosDBSimilarityType.COS\n",
|
||||
"kind = CosmosDBVectorSearchType.VECTOR_IVF\n",
|
||||
"m = 16\n",
|
||||
"ef_construction = 64\n",
|
||||
"ef_search = 40\n",
|
||||
"score_threshold = 0.1\n",
|
||||
"\n",
|
||||
"set_llm_cache(\n",
|
||||
" AzureCosmosDBSemanticCache(\n",
|
||||
" cosmosdb_connection_string=CONNECTION_STRING,\n",
|
||||
" cosmosdb_client=None,\n",
|
||||
" embedding=OpenAIEmbeddings(),\n",
|
||||
" database_name=DB_NAME,\n",
|
||||
" collection_name=COLLECTION_NAME,\n",
|
||||
" num_lists=num_lists,\n",
|
||||
" similarity=similarity_algorithm,\n",
|
||||
" kind=kind,\n",
|
||||
" dimensions=dimensions,\n",
|
||||
" m=m,\n",
|
||||
" ef_construction=ef_construction,\n",
|
||||
" ef_search=ef_search,\n",
|
||||
" score_threshold=score_threshold,\n",
|
||||
" )\n",
|
||||
")"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2024-02-02T21:34:49.457001Z",
|
||||
"start_time": "2024-02-02T21:34:49.411293Z"
|
||||
}
|
||||
},
|
||||
"id": "4a9d592db01b11b2",
|
||||
"execution_count": 16
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"CPU times: user 43.4 ms, sys: 7.23 ms, total: 50.7 ms\n",
|
||||
"Wall time: 1.61 s\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": "\"\\n\\nWhy couldn't the bicycle stand up by itself?\\n\\nBecause it was two-tired!\""
|
||||
},
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%%time\n",
|
||||
"# The first time, it is not yet in cache, so it should take longer\n",
|
||||
"llm(\"Tell me a joke\")"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2024-02-02T21:34:53.704234Z",
|
||||
"start_time": "2024-02-02T21:34:52.091096Z"
|
||||
}
|
||||
},
|
||||
"id": "8488cf9c97ec7ab",
|
||||
"execution_count": 17
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"CPU times: user 6.89 ms, sys: 2.24 ms, total: 9.13 ms\n",
|
||||
"Wall time: 337 ms\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": "\"\\n\\nWhy couldn't the bicycle stand up by itself?\\n\\nBecause it was two-tired!\""
|
||||
},
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%%time\n",
|
||||
"# The first time, it is not yet in cache, so it should take longer\n",
|
||||
"llm(\"Tell me a joke\")"
|
||||
],
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2024-02-02T21:34:56.004502Z",
|
||||
"start_time": "2024-02-02T21:34:55.650136Z"
|
||||
}
|
||||
},
|
||||
"id": "bc1570a2a77b58c8",
|
||||
"execution_count": 18
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0c69d84d",
|
||||
|
@@ -23,24 +23,34 @@
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [],
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"id": "8c493e205ce1dda5"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": 1,
|
||||
"id": "ab8e45f5bd435ade",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2023-10-10T17:20:00.721985Z",
|
||||
"start_time": "2023-10-10T17:19:57.996265Z"
|
||||
},
|
||||
"collapsed": false
|
||||
"end_time": "2024-02-08T18:25:05.278480Z",
|
||||
"start_time": "2024-02-08T18:24:51.560677Z"
|
||||
}
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Requirement already satisfied: pymongo in /Users/iekpo/Langchain/langchain-python/.venv/lib/python3.10/site-packages (4.5.0)\r\n",
|
||||
"Requirement already satisfied: dnspython<3.0.0,>=1.16.0 in /Users/iekpo/Langchain/langchain-python/.venv/lib/python3.10/site-packages (from pymongo) (2.4.2)\r\n"
|
||||
"\r\n",
|
||||
"\u001B[1m[\u001B[0m\u001B[34;49mnotice\u001B[0m\u001B[1;39;49m]\u001B[0m\u001B[39;49m A new release of pip is available: \u001B[0m\u001B[31;49m23.2.1\u001B[0m\u001B[39;49m -> \u001B[0m\u001B[32;49m23.3.2\u001B[0m\r\n",
|
||||
"\u001B[1m[\u001B[0m\u001B[34;49mnotice\u001B[0m\u001B[1;39;49m]\u001B[0m\u001B[39;49m To update, run: \u001B[0m\u001B[32;49mpip install --upgrade pip\u001B[0m\r\n",
|
||||
"Note: you may need to restart the kernel to use updated packages.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -50,20 +60,20 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"execution_count": 2,
|
||||
"id": "9c7ce9e7b26efbb0",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2023-10-10T17:50:03.615234Z",
|
||||
"start_time": "2023-10-10T17:50:03.604289Z"
|
||||
},
|
||||
"collapsed": false
|
||||
"end_time": "2024-02-08T18:25:56.926147Z",
|
||||
"start_time": "2024-02-08T18:25:56.900087Z"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"CONNECTION_STRING = \"AZURE COSMOS DB MONGO vCORE connection string\"\n",
|
||||
"CONNECTION_STRING = \"YOUR_CONNECTION_STRING\"\n",
|
||||
"INDEX_NAME = \"izzy-test-index\"\n",
|
||||
"NAMESPACE = \"izzy_test_db.izzy_test_collection\"\n",
|
||||
"DB_NAME, COLLECTION_NAME = NAMESPACE.split(\".\")"
|
||||
@@ -81,14 +91,14 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"execution_count": 3,
|
||||
"id": "4a052d99c6b8a2a7",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2023-10-10T17:50:11.712929Z",
|
||||
"start_time": "2023-10-10T17:50:11.703871Z"
|
||||
},
|
||||
"collapsed": false
|
||||
"end_time": "2024-02-08T18:26:06.558294Z",
|
||||
"start_time": "2024-02-08T18:26:06.550008Z"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -98,7 +108,7 @@
|
||||
"os.environ[\n",
|
||||
" \"OPENAI_API_BASE\"\n",
|
||||
"] = \"YOUR_OPEN_AI_ENDPOINT\" # https://example.openai.azure.com/\n",
|
||||
"os.environ[\"OPENAI_API_KEY\"] = \"YOUR_OPEN_AI_KEY\"\n",
|
||||
"os.environ[\"OPENAI_API_KEY\"] = \"YOUR_OPENAI_API_KEY\"\n",
|
||||
"os.environ[\n",
|
||||
" \"OPENAI_EMBEDDINGS_DEPLOYMENT\"\n",
|
||||
"] = \"smart-agent-embedding-ada\" # the deployment name for the embedding model\n",
|
||||
@@ -119,14 +129,14 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 26,
|
||||
"execution_count": 4,
|
||||
"id": "183741cf8f4c7c53",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2023-10-10T17:50:16.732718Z",
|
||||
"start_time": "2023-10-10T17:50:16.716642Z"
|
||||
},
|
||||
"collapsed": false
|
||||
"end_time": "2024-02-08T18:27:00.782280Z",
|
||||
"start_time": "2024-02-08T18:26:47.339151Z"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -134,6 +144,7 @@
|
||||
"from langchain_community.vectorstores.azure_cosmos_db import (\n",
|
||||
" AzureCosmosDBVectorSearch,\n",
|
||||
" CosmosDBSimilarityType,\n",
|
||||
" CosmosDBVectorSearchType,\n",
|
||||
")\n",
|
||||
"from langchain_openai import OpenAIEmbeddings\n",
|
||||
"from langchain_text_splitters import CharacterTextSplitter\n",
|
||||
@@ -159,21 +170,21 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 28,
|
||||
"execution_count": 5,
|
||||
"id": "39ae6058c2f7fdf1",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2023-10-10T17:51:17.980698Z",
|
||||
"start_time": "2023-10-10T17:51:11.786336Z"
|
||||
},
|
||||
"collapsed": false
|
||||
"end_time": "2024-02-08T18:31:13.486173Z",
|
||||
"start_time": "2024-02-08T18:30:54.175890Z"
|
||||
}
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": "{'raw': {'defaultShard': {'numIndexesBefore': 2,\n 'numIndexesAfter': 3,\n 'createdCollectionAutomatically': False,\n 'ok': 1}},\n 'ok': 1}"
|
||||
"text/plain": "{'raw': {'defaultShard': {'numIndexesBefore': 1,\n 'numIndexesAfter': 2,\n 'createdCollectionAutomatically': False,\n 'ok': 1}},\n 'ok': 1}"
|
||||
},
|
||||
"execution_count": 28,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -181,9 +192,9 @@
|
||||
"source": [
|
||||
"from pymongo import MongoClient\n",
|
||||
"\n",
|
||||
"INDEX_NAME = \"izzy-test-index-2\"\n",
|
||||
"NAMESPACE = \"izzy_test_db.izzy_test_collection\"\n",
|
||||
"DB_NAME, COLLECTION_NAME = NAMESPACE.split(\".\")\n",
|
||||
"# INDEX_NAME = \"izzy-test-index-2\"\n",
|
||||
"# NAMESPACE = \"izzy_test_db.izzy_test_collection\"\n",
|
||||
"# DB_NAME, COLLECTION_NAME = NAMESPACE.split(\".\")\n",
|
||||
"\n",
|
||||
"client: MongoClient = MongoClient(CONNECTION_STRING)\n",
|
||||
"collection = client[DB_NAME][COLLECTION_NAME]\n",
|
||||
@@ -200,23 +211,31 @@
|
||||
" index_name=INDEX_NAME,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Read more about these variables in detail here. https://learn.microsoft.com/en-us/azure/cosmos-db/mongodb/vcore/vector-search\n",
|
||||
"num_lists = 100\n",
|
||||
"dimensions = 1536\n",
|
||||
"similarity_algorithm = CosmosDBSimilarityType.COS\n",
|
||||
"kind = CosmosDBVectorSearchType.VECTOR_IVF\n",
|
||||
"m = 16\n",
|
||||
"ef_construction = 64\n",
|
||||
"ef_search = 40\n",
|
||||
"score_threshold = 0.1\n",
|
||||
"\n",
|
||||
"vectorstore.create_index(num_lists, dimensions, similarity_algorithm)"
|
||||
"vectorstore.create_index(\n",
|
||||
" num_lists, dimensions, similarity_algorithm, kind, m, ef_construction\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 29,
|
||||
"execution_count": 6,
|
||||
"id": "32c68d3246adc21f",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2023-10-10T17:51:44.840121Z",
|
||||
"start_time": "2023-10-10T17:51:44.498639Z"
|
||||
},
|
||||
"collapsed": false
|
||||
"end_time": "2024-02-08T18:31:47.468902Z",
|
||||
"start_time": "2024-02-08T18:31:46.053602Z"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -227,14 +246,14 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 31,
|
||||
"execution_count": 7,
|
||||
"id": "8feeeb4364efb204",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2023-10-10T17:52:08.049294Z",
|
||||
"start_time": "2023-10-10T17:52:08.038511Z"
|
||||
},
|
||||
"collapsed": false
|
||||
"end_time": "2024-02-08T18:31:50.982598Z",
|
||||
"start_time": "2024-02-08T18:31:50.977605Z"
|
||||
}
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -267,14 +286,14 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 32,
|
||||
"execution_count": 8,
|
||||
"id": "3c218ab6f59301f7",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2023-10-10T17:52:14.994861Z",
|
||||
"start_time": "2023-10-10T17:52:13.986379Z"
|
||||
},
|
||||
"collapsed": false
|
||||
"end_time": "2024-02-08T18:32:14.299599Z",
|
||||
"start_time": "2024-02-08T18:32:12.923464Z"
|
||||
}
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -305,14 +324,14 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 33,
|
||||
"execution_count": 9,
|
||||
"id": "fd67e4d92c9ab32f",
|
||||
"metadata": {
|
||||
"collapsed": false,
|
||||
"ExecuteTime": {
|
||||
"end_time": "2023-10-10T17:53:21.145431Z",
|
||||
"start_time": "2023-10-10T17:53:20.884531Z"
|
||||
},
|
||||
"collapsed": false
|
||||
"end_time": "2024-02-08T18:32:24.021434Z",
|
||||
"start_time": "2024-02-08T18:32:22.867658Z"
|
||||
}
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
|
Reference in New Issue
Block a user