ObjectBox Python

repository·main·Indexed 41 days ago

https://github.com/objectbox/objectbox-python

A lightweight, high-performance, on-device object and vector database for Python applications. It supports standard CRUD operations via Store and Box classes, and provides specialized capabilities for AI-driven workflows, including vector search using Float32Vector and HnswIndex for RAG and generative AI applications.

Tokens
2.5K
Snippets
17
Records
21
Agent score
51%

What's inside ObjectBox

  1. Define a data model using @Entity

    main

    To define a class as an ObjectBox entity, use the @Entity decorator. Each entity class defines a set of properties that will be persisted in the database. Common property types include:

    • Id: The unique identifier for the entity.
    • String: Text data.
    • Int32 / Int64: Integer data.
    • Float32 / Float64: Floating point data.
    • Date: Date and time data.
    • Float32Vector: Used specifically for on-device vector search.
  2. Core ObjectBox classes: Store, Box, and Query

    main

    Interacting with the ObjectBox database involves three primary classes:

    1. Store: Represents the database itself. You should maintain a reference to the Store instance for the duration of your application's lifecycle.
    2. Box: The main interaction point for a specific entity type. You obtain a Box for an entity to perform CRUD (Create, Read, Update, Delete) operations.
    3. Query: Used to search for and retrieve specific objects from a Box based on defined criteria.
  3. How the ObjectBox Store and Box work together

    main

    ObjectBox follows a hierarchical model for data access:

    • Store: Represents the entire database instance. It manages the underlying storage and should be treated as a long-lived object in your application.
    • Box: The primary interaction point for a specific entity type. You obtain a Box from a Store using store.box(EntityClass). All CRUD operations (put, get, remove) are performed through the Box.
  4. Perform CRUD operations with ObjectBox

    main

    ObjectBox uses a Store to represent the database and a Box to interact with specific entities.

    1. Define an Entity: Use the @Entity() decorator. Use Id for the primary key and type helpers like String for fields.
    2. Initialize the Store: Create a Store() instance. This represents the database; you should keep this instance alive for the duration of your application.
    3. Get a Box: Use store.box(EntityClass) to get a Box for a specific entity.
    4. Create/Update: Use box.put(object) to save an object. It returns the assigned ID.
    5. Read: Use box.get(id) to retrieve an object by its ID.
    6. Delete: Use box.remove(object) to delete an object.
    from objectbox import Entity, Id, Store, String
    
    @Entity()
    class Person:
        id = Id
        name = String
    
    # The ObjectBox Store represents a database; keep it around...
    store = Store()
    
    # Get a box for the "Person" entity; a Box is the main interaction point with objects and the database.
    box = store.box(Person)
    
    person = Person(name = "Joe Green")
    id = box.put(person)  # Create
    person = box.get(id)  # Read
    person.name = "Joe Black"
    box.put(person)       # Update
    box.remove(person)    # Delete
  5. Setup ObjectBox with Ollama

    main

    To use ObjectBox with Ollama for embedding-based workflows, follow these steps:

    1. Install Ollama: Download and install Ollama from https://ollama.com/download.
    2. Pull Models: Download the required models for LLM and embedding tasks:
      ollama pull llama3
      ollama pull mxbai-embed-large
    3. Environment Setup: Navigate to the example directory and create a virtual environment:
      cd example/ollama
      python3 -m venv .venv
      source .venv/bin/activate
    4. Install Dependencies: Install the ollama and objectbox Python packages:
      pip install ollama objectbox
      Alternatively, use the requirements file:
      pip install -r requirements.txt
    ollama pull llama3
    ollama pull mxbai-embed-large
    
    python3 -m venv .venv
    source .venv/bin/activate
    
    pip install ollama objectbox
  6. Install ObjectBox for Python

    main

    To use ObjectBox in your Python projects, you can install it via pip. It is recommended to use a virtual environment.

    cd example # assuming you are in project root dir 
    python3 -m venv venv
    source venv/bin/activate
    pip install objectbox
    python3 -m venv venv
    source venv/bin/activate
    pip install objectbox
  7. Example: CRUD operations with Tasks

    main

    The tasks example demonstrates basic Create, Read, Update, and Delete (CRUD) operations using a CLI interface. It manages a list of tasks with properties like ID, Created timestamp, Finished timestamp, and Text description.

    # Example CLI interaction for tasks
    > new buy oat
    > new buy yeast
    > ls
    > done 1
    > ls
    > exit