Azure CosmosDB MongoDB Vector Store
Source: llama_index

<a href="https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/docs/examples/vector_stores/AzureCosmosDBMongoDBvCoreDemo.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a># Azure CosmosDB MongoDB Vector StoreIn this notebook we are going to show how to use Azure Cosmosdb Mongodb vCore to perform vector searches in LlamaIndex. We will create the embedding using Azure Open AI. If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙.```python%pip install llama-index-embeddings-openai%pip install llama-index-vector-stores-azurecosmosmongo%pip install llama-index-llms-azure-openai``````python!pip install llama-index``````pythonimport osimport jsonimport openaifrom llama_index.llms.azure_openai import AzureOpenAIfrom llama_index.embeddings.openai import OpenAIEmbeddingfrom llama_index.core import VectorStoreIndex, SimpleDirectoryReader```### Setup Azure OpenAIThe first step is to configure the models. They will be used to create embeddings for the documents loaded into the db and for llm completions. ```pythonimport os# Set up the AzureOpenAI instancellm = AzureOpenAI(    model_name=os.getenv("OPENAI_MODEL_COMPLETION"),    deployment_name=os.getenv("OPENAI_MODEL_COMPLETION"),    api_base=os.getenv("OPENAI_API_BASE"),    api_key=os.getenv("OPENAI_API_KEY"),    api_type=os.getenv("OPENAI_API_TYPE"),    api_version=os.getenv("OPENAI_API_VERSION"),    temperature=0,)# Set up the OpenAIEmbedding instanceembed_model = OpenAIEmbedding(    model=os.getenv("OPENAI_MODEL_EMBEDDING"),    deployment_name=os.getenv("OPENAI_DEPLOYMENT_EMBEDDING"),    api_base=os.getenv("OPENAI_API_BASE"),    api_key=os.getenv("OPENAI_API_KEY"),    api_type=os.getenv("OPENAI_API_TYPE"),    api_version=os.getenv("OPENAI_API_VERSION"),)``````pythonfrom llama_index.core import SettingsSettings.llm = llmSettings.embed_model = embed_model```Download Data```python!mkdir -p 'data/paul_graham/'!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'```### Loading documentsLoad the documents stored in the `data/paul_graham/` using the SimpleDirectoryReader```pythondocuments = SimpleDirectoryReader("./data/paul_graham/").load_data()print("Document ID:", documents[0].doc_id)```    Document ID: c432ff1c-61ea-4c91-bd89-62be29078e79### Create the indexHere we establish the connection to an Azure Cosmosdb mongodb vCore cluster and create an vector search index.```pythonimport pymongofrom llama_index.vector_stores.azurecosmosmongo import (    AzureCosmosDBMongoDBVectorSearch,)from llama_index.core import VectorStoreIndexfrom llama_index.core import StorageContextfrom llama_index.core import SimpleDirectoryReaderconnection_string = os.environ.get("AZURE_COSMOSDB_MONGODB_URI")mongodb_client = pymongo.MongoClient(connection_string)store = AzureCosmosDBMongoDBVectorSearch(    mongodb_client=mongodb_client,    db_name="demo_vectordb",    collection_name="paul_graham_essay",)storage_context = StorageContext.from_defaults(vector_store=store)index = VectorStoreIndex.from_documents(    documents, storage_context=storage_context)```### Query the indexWe can now ask questions using our index.```pythonquery_engine = index.as_query_engine()response = query_engine.query("What did the author love working on?")``````pythonimport textwrapprint(textwrap.fill(str(response), 100))```    The author loved working on multiple projects that were not their thesis while in grad school,    including Lisp hacking and writing On Lisp. They eventually wrote a dissertation on applications of    continuations in just 5 weeks to graduate. Afterward, they applied to art schools and were accepted    into the BFA program at RISD.```pythonresponse = query_engine.query("What did he/she do in summer of 2016?")``````pythonprint(textwrap.fill(str(response), 100))```    The person moved to England with their family in the summer of 2016.