Skip to content

Getting Started

1. Connect to EigenLake

import eigenlake

client = eigenlake.connect(
    url="https://api.eigenlake.dev/",
    api_key="<sk_sbx_your_api_key_here>",
)

2. Define schema and index options

from eigenlake import schema as s
schema, index_options = (
    s.SchemaBuilder(additional_properties=False)
    .add("document_id", s.string(required=True, filterable=True))
    .add("document_title", s.string(filterable=True))
    .add("chunk_number", s.integer(filterable=True))
    .add("document_url", s.string(format="uri", filterable=True))
    .add("created_at", s.datetime(filterable=True))
    .add("tags", s.array(s.string(), filterable=False, max_items=20))
    .build()
)

3. Create or open an index

idx = client.indexes.create_or_get(
    namespace="demo-namespace",
    index="demo-index",
    dimensions=128,
    schema=schema,
    index_options=index_options,
)

Or open an existing index:

idx = client.indexes.open(
    namespace="demo-namespace",
    index="demo-index",
)

4. Insert records

record_id = idx.records.add(
    properties={"document_id": "doc-1", "text": "hello"},
    vector=[0.1] * 128,
)
print("inserted:", record_id)

Bulk insert:

result = idx.records.add_many(
    [
        {"id": "doc-2", "properties": {"document_id": "doc-2", "text": "hello 2"}, "vector": [0.2] * 128},
        {"id": "doc-3", "properties": {"document_id": "doc-3", "text": "hello 3"}, "vector": [0.3] * 128},
    ],
    on_error="continue",
)

print("inserted:", len(result))
print("errors:", result.number_errors)

5. Search and read

result = idx.search.nearest(
    vector=[0.1] * 128,
    limit=5,
)
print(result)

Fetch by id:

obj = idx.records.get("doc-1")
print(obj)

6. Cluster matching records

clusters = idx.search.cluster(
    filter={"document_id": {"$eq": "doc-1"}},
    limit=1000,
    algorithm="kmeans",
    auto_tune=True,
    min_clusters=2,
    max_clusters=4,
)
print(clusters["records_clustered"])

For natural language requests, use agent mode:

answer = idx.agent.query(
    "show me recent failures",
    mode="auto",
)
print(answer["action"])

See Clustering and Agent Queries for a full failure-analysis example.

7. Close client

client.close()

Use context manager to close automatically:

with eigenlake.connect(url="https://api.eigenlake.dev/", api_key="<sk_sbx_your_api_key_here>") as client:
    print(client.ready())