Skip to main content

vikingdb Python SDK

Project description

VikingDB Python SDK (v2)

This package provides an idiomatic Python interface to the VikingDB v2 data-plane APIs. The SDK mirrors the behaviour and API surface of the official Java and Go clients while embracing Python conventions (dataclasses/pydantic models, requests-based transport, and pytest-driven examples).

Key Features

  • Simple client configuration with AK/SK signing (Volcano Engine V4) or API-key authentication.
  • Vector Database: Request envelope handling with typed request/response models covering collection, index, and embedding workflows.
  • Memory Management: Conversational memory APIs for managing user profiles, events, and session messages with semantic search capabilities.
  • Pluggable retry strategy (exponential backoff with jitter) and per-request overrides (RequestOptions).
  • Executable example guides (pytest integration tests and standalone scripts) that demonstrate connectivity, CRUD, search, analytics, embedding, and memory management scenarios against a real VikingDB environment.

Installation

Clone the repository and install the SDK in editable mode:

pip install -e .

Dependencies: The SDK relies on requests, pydantic>=2.5, and the Volcano Engine base SDK (volcengine) for request signing.

Quickstart

Vector Database

from vikingdb import IAM
from vikingdb.vector import UpsertDataRequest, VikingVector

auth = IAM(ak="<AK>", sk="<SK>")
client = VikingVector(
    host="api.vector.bytedance.com",
    region="cn-beijing",
    auth=auth,
    scheme="https",
    timeout=30,
)

collection = client.collection(collection_name="demo_collection")
index = client.index(collection_name="demo_collection", index_name="demo_index")
embedding = client.embedding()

# Upsert documents into a collection
upsert_request = UpsertDataRequest(
    data=[
        {"title": "Demo Chapter", "paragraph": 1, "score": 42.0, "text": "hello vikingdb"},
    ]
)
response = collection.upsert(upsert_request)
print("request_id:", response.request_id, "result:", response.result)

# Run a quick search
search_response = index.search_by_random({"limit": 1})
print("search hits:", len(search_response.result.data) if search_response.result else 0)

Memory Management

from vikingdb import IAM
from vikingdb.memory import VikingMem

auth = IAM(ak="<AK>", sk="<SK>")
client = VikingMem(
    host="api-knowledgebase.mlp.cn-beijing.volces.com",
    region="cn-beijing",
    auth=auth,
    scheme="http",
)

# Get collection
collection = client.get_collection(
    collection_name="demo_collection",
    project_name="default"
)

# Add session messages
collection.add_session(
    session_id="session_001",
    messages=[
        {"role": "user", "content": "Hello, how is the weather today?"},
        {"role": "assistant", "content": "Today is sunny, perfect for going out."}
    ],
    metadata={
        "default_user_id": "user_001",
        "default_assistant_id": "assistant_001",
    }
)

# Search memories
result = collection.search_memory(
    query="weather today",
    filter={"user_id": "user_001", "memory_type": ["event_v1"]},
    limit=10
)
print("search results:", result)

Example Guides

Vector Examples (Pytest)

The integration guides under examples/vector mirror the Go SDK walkthroughs (E1E5). Each test connects to a live VikingDB environment and exercises a specific workflow.

  1. Set the required environment variables (or create a .env file in the project root):

    VIKINGDB_AK=your-access-key
    VIKINGDB_SK=your-secret-key
    # Optional:
    # VIKINGDB_PROJECT=project-name
    # VIKINGDB_RESOURCE_ID=resource-id
    

    The pytest guides themselves lock to the ap-southeast-1 public datasets:

    • host: api-vikingdb.vikingdb.ap-southeast-1.volces.com
    • region: ap-southeast-1
    • text walkthroughs use collection=text, index=text_index
    • vector walkthroughs use collection=vector, index=vector_index
  2. Install pytest (if not already available):

    pip install pytest
    
  3. Execute the guides:

    pytest examples/vector -k guide
    

Each scenario writes temporary documents using unique session tags and cleans them up afterwards.

Memory Examples

The memory examples under examples/memory demonstrate the core workflows for managing conversational memories:

  1. 01_init_client_and_collection.py: Initialize the VikingMem client and get collection instances using either collection name + project name or resource ID.

  2. 02_add_session.py: Add session messages (user-assistant conversations) to the memory collection with metadata such as user ID, assistant ID, and timestamps.

  3. 03_search_memory.py: Search memories with various filters including:

    • User profile search
    • Event search by semantic query
    • Time range filtering for recent events

To run the memory examples:

# Set environment variables
export VIKINGDB_AK=your-access-key
export VIKINGDB_SK=your-secret-key

# Run individual examples
python examples/memory/01_init_client_and_collection.py
python examples/memory/02_add_session.py
python examples/memory/03_search_memory.py

Architecture Overview

  • vikingdb._client, vikingdb.auth, vikingdb.request_options, and vikingdb.vector.exceptions form the shared runtime used by all present and future SDK domains (vector, memory, knowledge).
  • Domain-specific features live under dedicated namespaces such as vikingdb.vector and vikingdb.memory, where the high-level clients (VikingVector, VikingMem) compose the shared auth stack atop the shared client.
  • Vector request/response models now surface directly from vikingdb.vector (backed internally by vikingdb/vector/models).
  • Memory APIs return plain dictionaries without object encapsulation, providing a lightweight interface for conversational memory management (session, profile, event operations).
  • Imports from the root package now focus on cross-cutting utilities (auth, config, request options), while application code should pull domain-specific functionality from vikingdb.vector or vikingdb.memory explicitly.

Project Structure

vikingdb/
├── _client.py          # Shared base client built on volcengine Service
├── auth.py              # Shared auth providers (IAM, API key)
├── request_options.py   # Per-request overrides shared by all services
├── version.py           # Package metadata
├── vector/              # Vector-specific clients and models
│   ├── __init__.py      # High-level vector client and namespace exports
│   ├── base.py          # Shared helpers for vector clients
│   ├── collection.py    # Collection operations
│   ├── embedding.py     # Embedding operations
│   ├── index.py         # Index/search operations
│   ├── client.py        # Vector service wrapper and high-level client
│   ├── exceptions.py    # Vector-specific exceptions
│   └── models/          # Vector request/response models (pydantic)
├── memory/              # Memory-specific clients and models
│   ├── __init__.py      # High-level memory client and namespace exports
│   ├── client.py        # VikingMem service client
│   ├── collection.py    # Memory collection operations
│   ├── types.py         # Type definitions for memory operations
│   └── exceptions.py    # Memory-specific exceptions

examples/
├── vector/              # Vector integration guides (pytest)
│   ├── E1_connectivity_test.py
│   ├── E2_collection_lifecycle_test.py
│   ├── E3_*_test.py     # Search and indexing examples
│   └── ...
└── memory/              # Memory usage examples
    ├── 01_init_client_and_collection.py
    ├── 02_add_session.py
    └── 03_search_memory.py

Contributing

Contributions and feedback are welcome. Please ensure any new APIs match the OpenAPI specification and include accompanying example coverage.

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

vikingdb_python_sdk-0.1.1.tar.gz (28.3 kB view details)

Uploaded Source

Built Distribution

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

vikingdb_python_sdk-0.1.1-py3-none-any.whl (34.3 kB view details)

Uploaded Python 3

File details

Details for the file vikingdb_python_sdk-0.1.1.tar.gz.

File metadata

  • Download URL: vikingdb_python_sdk-0.1.1.tar.gz
  • Upload date:
  • Size: 28.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.13

File hashes

Hashes for vikingdb_python_sdk-0.1.1.tar.gz
Algorithm Hash digest
SHA256 19a40100b199a50632b4778ddbc3541aa4d6f0a2fadc8b762a66f2dfa36a7223
MD5 013df344622d29a18c822944814c1870
BLAKE2b-256 38eb356d79cc1845bee07075b05885bb5a4c7bc5a8d8f1581bcd96a039034882

See more details on using hashes here.

File details

Details for the file vikingdb_python_sdk-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for vikingdb_python_sdk-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 92d72c65a575dc01bee9ae143458d13c7fe83b7ce751186fd5484fec3d09c588
MD5 3eaea34f6b2b61993c0585e2a06ed259
BLAKE2b-256 1a34a71d42705672ec1fd3f2b332be057ee2bb1c903fac4e0c026ab6e6c3596d

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