Skip to main content

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

Metadata

Release files for nlweb-cosmos-object-db 0.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nlweb-cosmos-object-db 0.7.0
File Size Uploaded
nlweb_cosmos_object_db-0.7.0.tar.gz 4.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nlweb-cosmos-object-db 0.7.0
File Interpreter ABI Platform
nlweb_cosmos_object_db-0.7.0-py3-none-any.whl Python 3 none any Details

Total release size: 9.4 kB

Release files / nlweb_cosmos_object_db-0.7.0.tar.gz

Download URL nlweb_cosmos_object_db-0.7.0.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
3c7943eabec97d36824942d290f63c31692b0239ca7c0bcb482507cbd17416cb
BLAKE2b-256 checksum
How to use checksums
b4d4aa2503352e7e90edbb42e38fbe47e4ded1814bd507dd78712822e4467ff9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","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}

Release files / nlweb_cosmos_object_db-0.7.0-py3-none-any.whl

Download URL nlweb_cosmos_object_db-0.7.0-py3-none-any.whl
Size 4.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7f7c52908947404bdc66617a51278add0cb3bdf972cb935d1b526b760c061391
BLAKE2b-256 checksum
How to use checksums
ecb12719da4e1724d05ad3c0273a606ae18f75c320aebbb07ee35ff00a8789ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","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}

Release history Release notifications | RSS feed

This release

0.7.0 This release

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page