Metadata-Version: 2.5
Name: swarmv
Version: 1.7.0
Summary: Python SDK for SwarmV: edge vector memory with built-in access control for AI agents.
Project-URL: Homepage, https://swarmv.dev
Project-URL: Documentation, https://swarmv.dev/docs
License-Expression: MIT
Keywords: agents,ai,embeddings,memory,rag,vector,vector-database
Requires-Python: >=3.9
Requires-Dist: httpx[http2]>=0.27
Provides-Extra: dev
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
Requires-Dist: pytest>=8; extra == 'dev'
Provides-Extra: fast
Requires-Dist: numpy>=1.21; extra == 'fast'
Requires-Dist: orjson>=3.9; extra == 'fast'
Description-Content-Type: text/markdown

# SwarmV Python SDK

Edge vector memory for AI agents, with built-in relationship-based access control (ReBAC).

```bash
pip install "swarmv[fast]"   # [fast] adds orjson + numpy for max throughput
```

## Quick Start

```python
from swarmv import (
    SwarmV,
    OpenAIEmbeddings,
    embed_document_chunks,
)

# 1. Initialize client and embedding provider
client = SwarmV(api_key="evx_...")
embedder = OpenAIEmbeddings(model="text-embedding-3-small", dimensions=768)

# 2. Chunk & embed documents with standard schemas and 32-bit uint IDs
records = embed_document_chunks(
    embedder=embedder,
    container_id="container:handbook",
    document_id="handbook.md",
    chunks=[
        "Employees are entitled to 25 days paid annual leave.",
        "Remote work stipends cover home office equipment.",
    ],
    source="https://wiki.corp/handbook.md",
    category="hr",
)

client.insert_many(records)

# 3. Query with semantic similarity & access control
query_vector = embedder.embed_query("vacation allowance")
hits = client.search(query_vector, top_k=5, fresh=True)

for hit in hits:
    print(hit.id, hit.distance, hit.metadata.get("text"))

# 4. Mint scoped user tokens
token = client.generate_user_token("user:alice", scopes=["vector:read"])
```

## Keyword and Hybrid Search

Records are also searchable by the words in their metadata `text` (accents and case ignored).
Keyword search needs no embedding, which suits exact terms: error codes, names, ids. Hybrid search
fuses vector and keyword matches. Access control is the same in every mode.

```python
# Keyword only
by_words = client.search_text("ERR_4021", top_k=5)

# Vector + keywords; keyword_weight: 0 = vector only, 1 = keyword only (default 0.5)
hybrid = client.hybrid_search(query_vector, "refund policy ERR_4021", top_k=5, keyword_weight=0.5)
for hit in hybrid:
    print(hit.id, hit.score, hit.match, (hit.metadata or {}).get("text"))  # match: vector | keyword | both
```

## Embedding Providers

SwarmV provides zero-dependency, lightweight adapters built on `httpx`:

### OpenAI (`OpenAIEmbeddings`)
```python
from swarmv import OpenAIEmbeddings

embedder = OpenAIEmbeddings(
    api_key="sk-...",
    model="text-embedding-3-small",
    dimensions=512,
)
```

### Cloudflare Workers AI (`WorkersAIEmbeddings`)
```python
from swarmv import WorkersAIEmbeddings

embedder = WorkersAIEmbeddings(
    account_id="...",
    api_token="...",
    model="@cf/baai/bge-base-en-v1.5",
)
```

### Voyage AI (`VoyageEmbeddings`)
```python
from swarmv import VoyageEmbeddings

embedder = VoyageEmbeddings(
    api_key="pa-...",
    model="voyage-code-3",
    output_dimension=512,
)
```

## Document Chunking & 32-Bit ID Allocation

SwarmV requires document IDs to be numeric strings representing 32-bit unsigned integers (`0` to `4,294,967,295`).

```python
from swarmv import create_document_chunks

# Option A: Sequential ID Allocation (Recommended for multi-document pipelines)
# Callers must allocate non-overlapping ranges across documents to prevent collisions.
chunks_1 = create_document_chunks(
    document_id="doc1.txt",
    container_id="container:docs",
    chunks=["chunk A", "chunk B"],
    vectors=[[0.1, 0.2], [0.3, 0.4]],
    start_id=0,  # IDs: "0", "1"
)

chunks_2 = create_document_chunks(
    document_id="doc2.txt",
    container_id="container:docs",
    chunks=["chunk C", "chunk D"],
    vectors=[[0.5, 0.6], [0.7, 0.8]],
    start_id=2,  # IDs: "2", "3" (advancing start_id by prior chunk count)
)

# Option B: Deterministic FNV-1a Hashing (Small datasets / local development)
hashed_chunks = create_document_chunks(
    document_id="manual.pdf",
    container_id="container:manuals",
    chunks=["chunk text..."],
    vectors=[[0.1, 0.2]],
)
```

> **Note on 32-Bit Uniqueness**: Stateless hashing functions (`hash_doc_chunk_id`) suffer from the birthday paradox (~50% collision risk at 77,000 items). For large datasets, always pass `start_id` backed by a sequence generator, database allocator, or custom `id_generator`.

## Semantic Caching

Cache expensive LLM calls by semantic proximity:

```python
from swarmv import SemanticCache, OpenAIEmbeddings

embedder = OpenAIEmbeddings()
cache = SemanticCache(client, embedder, threshold=0.95)

answer = cache.call("Explain Raft consensus", call_llm)
```

Full documentation: https://swarmv.dev/docs
