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.0.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.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nlweb_cosmos_object_db-0.6.0.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.0.tar.gz
Algorithm Hash digest
SHA256 4016e377b46b51aefbb9e2a80d18cc47a64a806071857c26c3900a34a1209050
MD5 6e93af0167115b8d451464b40f3c6d43
BLAKE2b-256 e8e32f5a63972a19571b7ba05f0e5ec85233ca703fdcfde8c6074ddf1ac7b17f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nlweb_cosmos_object_db-0.6.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8e91bb8b3552b6eb4bd9170b4392f5f0531d7ca22458aedc8d1e5e1571d5a646
MD5 efee8443801a7553abf87923714fc23a
BLAKE2b-256 b2f22db2ffd8d9c76be1c0870bd3cab4f8044abc651991159632d73f92d53644

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