#github.com/xulang-org/packages/chroma
Xu 0.16.0 API
import: chroma #github.com/xulang-org/packages/chroma
counts: 3 functions, 17 methods, 3 types, 0 constants, 0 method sets
Chroma AI Vector Database Client for Xu Supports collection management, document and vector embedding storage, and similarity search.
github.com/xulang-org/packages · 1.17.2 · Install and use
Examples · API declarations
Examples
These sources come from the official examples repository and can be copied or downloaded. Run them with a local Xu SDK in the complete project; network, database, and desktop examples require the corresponding services or assets.
44_standalone_chroma/main.xu
Required SDK: Xu 0.8.3 · Complete project · Download source
@main
chroma #github.com/xulang-org/packages/chroma
#errors
#strings
fn verify_chroma_pool() {
println("--- 1. Testing Chroma Connection Pool ---")
$p := chroma.open_pool("http://127.0.0.1:8000", 5)
// 1. Connection Reuse
fd1 := p.get_conn()
println(" Borrow 1 fd: " + str(fd1))
p.put_conn(fd1)
fd2 := p.get_conn()
println(" Borrow 2 (after return) fd: " + str(fd2))
if fd1 == fd2 {
println(" [PASS] Connection reused successfully (fd " + str(fd1) + " == " + str(fd2) + ")")
} else {
println(" [WARN] Different fd allocated: " + str(fd1) + " vs " + str(fd2))
}
// 2. Multi-connection concurrent borrow
fd3 := p.get_conn()
println(" Borrow 3 (concurrently) fd: " + str(fd3))
if fd2 != fd3 {
println(" [PASS] Concurrently allocated distinct fds (" + str(fd2) + " != " + str(fd3) + ")")
} else {
println(" [FAIL] Expected distinct fds for concurrent connections!")
}
p.put_conn(fd2)
p.put_conn(fd3)
p.close()
println(" [PASS] Chroma Pool closed cleanly.")
}
fn verify_chroma_crud() {
println("\n--- 2. Testing Chroma Vector CRUD & Query ---")
$p := chroma.open_pool("http://127.0.0.1:8000", 5)
col_name := "xu_test_vectors"
// Cleanup prior
p.delete_collection(col_name + "")
// 1. Create Collection
col_id := p.create_collection(col_name + "") >>> (err) {
println(" [FAIL] Create collection failed: " + err.msg())
p.close()
<<
}
println(" [PASS] Created Chroma Collection ID: " + col_id)
// 2. Add Vectors & Docs
ids := "[\"doc_101\", \"doc_102\"]"
embs := "[[0.15, 0.22, 0.88], [0.91, 0.12, 0.05]]"
docs := "[\"Compiler construction in Xu\", \"Database internals and storage\"]"
add_ok := p.add(col_id + "", ids, embs, docs)
if add_ok {
println(" [PASS] Added 2 vector embeddings and documents.")
} else {
println(" [FAIL] Failed to add vector embeddings.")
}
// 3. Similarity Query
query_emb := "[[0.14, 0.20, 0.85]]"
res := p.query(col_id + "", query_emb, 1) >>> (err) {
println(" [FAIL] Similarity query failed: " + err.msg())
p.close()
<<
}
println(" [PASS] Query Similarity Match Result preview: " + res)
// 4. Delete Collection
del_ok := p.delete_collection(col_name + "")
if del_ok {
println(" [PASS] Deleted Chroma Collection successfully.")
} else {
println(" [FAIL] Failed to delete collection.")
}
p.close()
}
fn main() {
println("=== Xu Chroma AI Vector Database Native Driver Verification ===\n")
verify_chroma_pool()
verify_chroma_crud()
println("\n[CHROMA VERIFICATION 100% COMPLETED]")
}
API declarations
struct Response
pub struct Response {
pub status_code int
pub headers string
pub body string
}
Purpose
Chroma Server response payload containing status, headers, and body.
Public fields
status_code int- field (int)
headers string- field (string)
body string- body
struct Client
pub struct Client {
pub host string
pub port int
pub secure bool
pub tenant string
pub database string
}
Purpose
Chroma Client session maintaining connection and operations.
Public fields
host string- host
port int- port
secure bool- field (bool)
tenant string- field (string)
database string- field (string)
new_client
pub fn new_client(endpoint string) Client
Purpose
Construct client.
Parameters
endpoint string- endpoint (string)
Returns
Client — a value of this type
(Client) request
pub fn (c Client) request(method string, path string, body string) Response
Purpose
Execute request on Client. Throws on failure; handle the error or propagate it to the caller.
Parameters
c Client- The
Client instance used by this call (value receiver).
method string- method (string)
path string- filesystem path (string)
body string- body (string)
Returns
Response — a value of this type
(Client) heartbeat
pub fn (c Client) heartbeat() bool
Purpose
Execute heartbeat on Client.
Parameters
c Client- The
Client instance used by this call (value receiver).
Returns
bool — true or false
(Client) create_collection
pub fn (c Client) create_collection(name string) string
Purpose
Operation on Client: create collection. Throws on failure; handle the error or propagate it to the caller.
Parameters
c Client- The
Client instance used by this call (value receiver).
name string- name (string)
Returns
string — result string
(Client) add
pub fn (c Client) add(col_id string, ids_json string, embeddings_json string, documents_json string) bool
Purpose
Operation on Client: add.
Parameters
c Client- The
Client instance used by this call (value receiver).
col_id string- col_id (string)
ids_json string- ids_json (string)
embeddings_json string- embeddings_json (string)
documents_json string- documents_json (string)
Returns
bool — true or false
(Client) query
pub fn (c Client) query(col_id string, query_embeddings_json string, n_results int) string
Purpose
Execute query on Client. Throws on failure; handle the error or propagate it to the caller.
Parameters
c Client- The
Client instance used by this call (value receiver).
col_id string- col_id (string)
query_embeddings_json string- query_embeddings_json (string)
n_results int- n_results (int)
Returns
string — result string
(Client) delete_collection
pub fn (c Client) delete_collection(name string) bool
Purpose
Operation on Client: delete collection.
Parameters
c Client- The
Client instance used by this call (value receiver).
name string- name (string)
Returns
bool — true or false
struct Pool
pub struct Pool {
pub client Client
pub state chan pool.State
pub max_idle int
}
Purpose
Chroma Connection pool manager scheduling reusable socket connections.
Public fields
client Client- field (Client)
state chan pool.State- field (channel; type chan pool.State)
max_idle int- field (int)
new_pool
pub fn new_pool(endpoint string, max_idle int) Pool
Purpose
Construct pool.
Parameters
endpoint string- endpoint (string)
max_idle int- max_idle (int)
Returns
Pool — a value of this type
(&Pool) get
pub fn (p &Pool) get() Client
Purpose
Execute get on &Pool.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
Returns
Client — a value of this type
(&Pool) dup
pub fn (p &Pool) dup() Pool
Purpose
Copies configuration while sharing the connection and close lifecycle.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
Returns
Pool — a value of this type
(&Pool) get_conn
pub fn (p &Pool) get_conn() int
Purpose
Operation on &Pool: read conn.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
Returns
int — a value of this type
(&Pool) put_conn
pub fn (p &Pool) put_conn(fd int)
Purpose
Operation on &Pool: put connection.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
fd int- file descriptor (int)
Returns
This call does not return a value.
(&Pool) discard_conn
pub fn (p &Pool) discard_conn(fd int)
Purpose
Operation on &Pool: discard connection.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
fd int- file descriptor (int)
Returns
This call does not return a value.
open_pool
pub fn open_pool(endpoint string, max_idle int) Pool
Purpose
Open pool.
Parameters
endpoint string- endpoint (string)
max_idle int- max_idle (int)
Returns
Pool — a value of this type
See usage example
(&Pool) heartbeat
pub fn (p &Pool) heartbeat() bool
Purpose
Execute heartbeat on &Pool.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
Returns
bool — true or false
(&Pool) create_collection
pub fn (p &Pool) create_collection(name string) string
Purpose
Operation on &Pool: create collection. Throws on failure; handle the error or propagate it to the caller.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
name string- name (string)
Returns
string — result string
(&Pool) add
pub fn (p &Pool) add(col_id string, ids_json string, embeddings_json string, documents_json string) bool
Purpose
Operation on &Pool: add.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
col_id string- col_id (string)
ids_json string- ids_json (string)
embeddings_json string- embeddings_json (string)
documents_json string- documents_json (string)
Returns
bool — true or false
(&Pool) query
pub fn (p &Pool) query(col_id string, query_embeddings_json string, n_results int) string
Purpose
Execute query on &Pool. Throws on failure; handle the error or propagate it to the caller.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
col_id string- col_id (string)
query_embeddings_json string- query_embeddings_json (string)
n_results int- n_results (int)
Returns
string — result string
(&Pool) delete_collection
pub fn (p &Pool) delete_collection(name string) bool
Purpose
Operation on &Pool: delete collection.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
name string- name (string)
Returns
bool — true or false
(&Pool) close
Purpose
Operation on &Pool: close the resource.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
Returns
This call does not return a value.