XU XUv0.16.0

#github.com/xulang-org/packages/chroma

导入方式: chroma #github.com/xulang-org/packages/chroma

公开 API: 3 函数, 17 方法, 3 类型, 0 常量, 0 方法集

扩展包 `chroma #github.com/xulang-org/packages/chroma`,由模块 github.com/xulang-org/packages 提供。

github.com/xulang-org/packages · 1.17.2 · 安装与使用

示例代码 · API 声明

示例代码

以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。

44_standalone_chroma/main.xu

所需 SDK:Xu 0.8.3 · 完整工程 · 下载源码

Xu本地 SDK 示例
@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 声明

struct Response

XuAPI
pub struct Response {
    pub status_code int
    pub headers string
    pub body string
}

用途

Chroma 服务端响应结果,包含状态码、响应头与正文数据。

公开字段

status_code int
字段(int)
headers string
字段(string)
body string
正文

struct Client

XuAPI
pub struct Client {
    pub host string
    pub port int
    pub secure bool
    pub tenant string
    pub database string
}

用途

Chroma 客户端会话实例,维护与服务端的连接并提供操作接口。

公开字段

host string
主机
port int
端口
secure bool
字段(bool)
tenant string
字段(string)
database string
字段(string)

new_client

XuAPI
pub fn new_client(endpoint string) Client

用途

构造 client。

参数

endpoint string
endpoint(string)

返回值

Client — 该类型的值

(Client) request

XuAPI
pub fn (c Client) request(method string, path string, body string) Response

用途

Client 上的操作:请求。失败时抛出错误,调用方需处理或继续传播。

参数

c Client
本次调用使用的 Client 实例;按值接收。
method string
method(string)
path string
文件系统路径(string)
body string
正文(string)

返回值

Response — 该类型的值

(Client) heartbeat

XuAPI
pub fn (c Client) heartbeat() bool

用途

在 Client 上执行 heartbeat 操作。

参数

c Client
本次调用使用的 Client 实例;按值接收。

返回值

bool — 是否成立

(Client) create_collection

XuAPI
pub fn (c Client) create_collection(name string) string

用途

Client 上的操作:执行 create collection 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c Client
本次调用使用的 Client 实例;按值接收。
name string
名字(string)

返回值

string — 结果字符串

(Client) add

XuAPI
pub fn (c Client) add(col_id string, ids_json string, embeddings_json string, documents_json string) bool

用途

Client 上的操作:相加。

参数

c Client
本次调用使用的 Client 实例;按值接收。
col_id string
col_id(string)
ids_json string
ids_json(string)
embeddings_json string
embeddings_json(string)
documents_json string
documents_json(string)

返回值

bool — 是否成立

(Client) query

XuAPI
pub fn (c Client) query(col_id string, query_embeddings_json string, n_results int) string

用途

Client 上的操作:查询串。失败时抛出错误,调用方需处理或继续传播。

参数

c Client
本次调用使用的 Client 实例;按值接收。
col_id string
col_id(string)
query_embeddings_json string
query_embeddings_json(string)
n_results int
n_results(int)

返回值

string — 结果字符串

(Client) delete_collection

XuAPI
pub fn (c Client) delete_collection(name string) bool

用途

Client 上的操作:执行 delete collection 操作。

参数

c Client
本次调用使用的 Client 实例;按值接收。
name string
名字(string)

返回值

bool — 是否成立

struct Pool

XuAPI
pub struct Pool {
    pub client Client
    pub state chan pool.State
    pub max_idle int
}

用途

Chroma 连接池管理器,并发安全地维护和调度复用套接字连接。

公开字段

client Client
字段(Client)
state chan pool.State
字段(通道;类型 chan pool.State)
max_idle int
字段(int)

new_pool

XuAPI
pub fn new_pool(endpoint string, max_idle int) Pool

用途

构造 pool。

参数

endpoint string
endpoint(string)
max_idle int
max_idle(int)

返回值

Pool — 该类型的值

(&Pool) get

XuAPI
pub fn (p &Pool) get() Client

用途

在 &Pool 上执行 get 操作。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

Client — 该类型的值

(&Pool) dup

XuAPI
pub fn (p &Pool) dup() Pool

用途

创建并返回持有独立状态或底层的副本对象。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

Pool — 该类型的值

(&Pool) get_conn

XuAPI
pub fn (p &Pool) get_conn() int

用途

&Pool 上的操作:读取 conn。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

int — 该类型的值

(&Pool) put_conn

XuAPI
pub fn (p &Pool) put_conn(fd int)

用途

&Pool 上的操作:执行 put conn 操作。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
fd int
文件描述符(int)

返回值

不返回值。

(&Pool) discard_conn

XuAPI
pub fn (p &Pool) discard_conn(fd int)

用途

&Pool 上的操作:丢弃连接。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
fd int
文件描述符(int)

返回值

不返回值。

open_pool

XuAPI
pub fn open_pool(endpoint string, max_idle int) Pool

用途

执行 open pool 操作。

参数

endpoint string
endpoint(string)
max_idle int
max_idle(int)

返回值

Pool — 该类型的值

查看用法示例

(&Pool) heartbeat

XuAPI
pub fn (p &Pool) heartbeat() bool

用途

在 &Pool 上执行 heartbeat 操作。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

bool — 是否成立

(&Pool) create_collection

XuAPI
pub fn (p &Pool) create_collection(name string) string

用途

&Pool 上的操作:执行 create collection 操作。失败时抛出错误,调用方需处理或继续传播。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
name string
名字(string)

返回值

string — 结果字符串

(&Pool) add

XuAPI
pub fn (p &Pool) add(col_id string, ids_json string, embeddings_json string, documents_json string) bool

用途

&Pool 上的操作:相加。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
col_id string
col_id(string)
ids_json string
ids_json(string)
embeddings_json string
embeddings_json(string)
documents_json string
documents_json(string)

返回值

bool — 是否成立

(&Pool) query

XuAPI
pub fn (p &Pool) query(col_id string, query_embeddings_json string, n_results int) string

用途

&Pool 上的操作:查询串。失败时抛出错误,调用方需处理或继续传播。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
col_id string
col_id(string)
query_embeddings_json string
query_embeddings_json(string)
n_results int
n_results(int)

返回值

string — 结果字符串

(&Pool) delete_collection

XuAPI
pub fn (p &Pool) delete_collection(name string) bool

用途

&Pool 上的操作:执行 delete collection 操作。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
name string
名字(string)

返回值

bool — 是否成立

(&Pool) close

XuAPI
pub fn (p &Pool) close()

用途

&Pool 上的操作:关闭资源。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

不返回值。