Skip to main content

Azure Cosmos DB object lookup provider for NLWeb - enriches search results with full documents

Project description

nlweb-cosmos-object-db

Azure Cosmos DB object lookup provider for NLWeb.

Overview

This provider enables NLWeb to enrich vector search results with full documents from Azure Cosmos DB. When vector databases return truncated content, this provider fetches the complete documents from Cosmos DB using document IDs.

Installation

pip install nlweb-core nlweb-cosmos-object-db

For a complete setup with vector search:

pip install nlweb-core nlweb-azure-vectordb nlweb-cosmos-object-db

Configuration

Create config.yaml:

object_storage:
  type: cosmos
  enabled: true
  endpoint_env: AZURE_COSMOS_ENDPOINT
  database_name: your-database
  container_name: your-container
  partition_key: /"@id"
  import_path: nlweb_cosmos_object_db.cosmos_lookup
  class_name: CosmosObjectLookup

Authentication

This provider uses Azure AD Managed Identity authentication via DefaultAzureCredential. No API keys required.

Set environment variable:

export AZURE_COSMOS_ENDPOINT=https://your-account.documents.azure.com:443/

Azure AD Setup

Ensure your Azure identity has appropriate Cosmos DB permissions:

  • Cosmos DB Built-in Data Reader role
  • Or custom role with Microsoft.DocumentDB/databaseAccounts/readMetadata and read permissions

Usage

The provider automatically enriches search results when configured:

import nlweb_core

# Initialize with config
nlweb_core.init(config_path="./config.yaml")

from nlweb_core import retriever

# Search with automatic enrichment
results = await retriever.search(
    query="example query",
    site="example.com",
    num_results=10,
    enrich_from_storage=True  # Enable Cosmos DB enrichment
)

# Results now contain full documents from Cosmos DB
for result in results:
    print(result.content)  # Full content instead of truncated text

How It Works

  1. Vector Search: NLWeb queries the vector database (e.g., Azure AI Search) and gets IDs + truncated content
  2. ID Extraction: Document IDs are extracted from vector search results
  3. Cosmos DB Lookup: Provider queries Cosmos DB by @id field to fetch full documents
  4. Content Enrichment: Full documents replace truncated content in search results
  5. Ranking: LLM ranks the enriched results

Features

  • Azure AD managed identity authentication (no API keys)
  • Async-compatible using thread executors
  • Parameterized queries to prevent injection
  • Configurable database, container, and partition key
  • Seamless integration with NLWeb retrieval pipeline
  • Compatible with NLWeb Protocol v0.5+

Document Structure

Your Cosmos DB documents should have an @id field that matches the IDs returned by your vector database:

{
  "@id": "doc-12345",
  "content": "Full document content here...",
  "metadata": {
    "title": "Document Title",
    "url": "https://example.com/page"
  }
}

Configuration Options

Field Required Description
type Yes Must be "cosmos"
enabled Yes Set to true to enable enrichment
endpoint_env Yes Environment variable name for Cosmos endpoint
database_name Yes Cosmos DB database name
container_name Yes Cosmos DB container name
partition_key Yes Partition key path (e.g., /"@id")
import_path Yes nlweb_cosmos_object_db.cosmos_lookup
class_name Yes CosmosObjectLookup

Creating Your Own Object Lookup Provider

Use this package as a template:

  1. Create package structure:

    nlweb-your-objectdb/
    ├── pyproject.toml
    ├── README.md
    └── nlweb_your_objectdb/
        ├── __init__.py
        └── your_lookup.py
    
  2. Implement ObjectLookupInterface:

    from nlweb_core.retriever import ObjectLookupInterface
    
    class YourLookup(ObjectLookupInterface):
        async def get_by_id(self, doc_id: str) -> dict:
            # Your implementation
            pass
    
  3. Declare dependencies in pyproject.toml:

    dependencies = [
        "nlweb-core>=0.5.5",
        "your-database-sdk>=1.0.0",
    ]
    
  4. Configure in NLWeb:

    object_storage:
      import_path: nlweb_your_objectdb.your_lookup
      class_name: YourLookup
    

License

MIT License - Copyright (c) 2025 Microsoft Corporation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nlweb_cosmos_object_db-0.6.2.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nlweb_cosmos_object_db-0.6.2-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file nlweb_cosmos_object_db-0.6.2.tar.gz.

File metadata

  • Download URL: nlweb_cosmos_object_db-0.6.2.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for nlweb_cosmos_object_db-0.6.2.tar.gz
Algorithm Hash digest
SHA256 27ec6caa2699e4dd17d4594791c128043ab7f2619f160390095bb72f9926035f
MD5 099adce51a7090058b9c7504763be9ec
BLAKE2b-256 9f073e821ff97ded477c3a978927448f444cc1563d71196ede0497b9f9e1fec1

See more details on using hashes here.

File details

Details for the file nlweb_cosmos_object_db-0.6.2-py3-none-any.whl.

File metadata

  • Download URL: nlweb_cosmos_object_db-0.6.2-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for nlweb_cosmos_object_db-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a7d3174b2f7a8ee1b9b1cabdec904c0b75eb9c65fe7022c182e889c056be4b39
MD5 6529d1e78862731eea67d85ce0c1d414
BLAKE2b-256 288bfeb2d292a1c1d988d797d8e9c2402975f3c5e799b1142bf1e3111029c520

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page