struct Response
pub struct Response {
pub status_code int
pub headers string
pub body string
}用途
Chroma 服务端响应结果,包含状态码、响应头与正文数据。
公开字段
status_codeint- 字段(int)
headersstring- 字段(string)
bodystring- 正文
#github.com/xulang-org/packages/chroma扩展包 `chroma #github.com/xulang-org/packages/chroma`,由模块 github.com/xulang-org/packages 提供。
github.com/xulang-org/packages · 1.17.2 · 安装与使用
以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。
44_standalone_chroma/main.xu@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]")
}struct Responsepub struct Response {
pub status_code int
pub headers string
pub body string
}Chroma 服务端响应结果,包含状态码、响应头与正文数据。
status_code intheaders stringbody stringstruct Clientpub struct Client {
pub host string
pub port int
pub secure bool
pub tenant string
pub database string
}Chroma 客户端会话实例,维护与服务端的连接并提供操作接口。
host stringport intsecure booltenant stringdatabase stringnew_clientpub fn new_client(endpoint string) Client构造 client。
endpoint stringClient — 该类型的值
(Client) requestpub fn (c Client) request(method string, path string, body string) ResponseClient 上的操作:请求。失败时抛出错误,调用方需处理或继续传播。
c ClientClient 实例;按值接收。method stringpath stringbody stringResponse — 该类型的值
(Client) heartbeatpub fn (c Client) heartbeat() bool在 Client 上执行 heartbeat 操作。
c ClientClient 实例;按值接收。bool — 是否成立
(Client) create_collectionpub fn (c Client) create_collection(name string) stringClient 上的操作:执行 create collection 操作。失败时抛出错误,调用方需处理或继续传播。
c ClientClient 实例;按值接收。name stringstring — 结果字符串
(Client) addpub fn (c Client) add(col_id string, ids_json string, embeddings_json string, documents_json string) boolClient 上的操作:相加。
c ClientClient 实例;按值接收。col_id stringids_json stringembeddings_json stringdocuments_json stringbool — 是否成立
(Client) querypub fn (c Client) query(col_id string, query_embeddings_json string, n_results int) stringClient 上的操作:查询串。失败时抛出错误,调用方需处理或继续传播。
c ClientClient 实例;按值接收。col_id stringquery_embeddings_json stringn_results intstring — 结果字符串
(Client) delete_collectionpub fn (c Client) delete_collection(name string) boolClient 上的操作:执行 delete collection 操作。
c ClientClient 实例;按值接收。name stringbool — 是否成立
struct Poolpub struct Pool {
pub client Client
pub state chan pool.State
pub max_idle int
}Chroma 连接池管理器,并发安全地维护和调度复用套接字连接。
client Clientstate chan pool.Statemax_idle intnew_poolpub fn new_pool(endpoint string, max_idle int) Pool构造 pool。
endpoint stringmax_idle intPool — 该类型的值
(&Pool) getpub fn (p &Pool) get() Client在 &Pool 上执行 get 操作。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。Client — 该类型的值
(&Pool) duppub fn (p &Pool) dup() Pool创建并返回持有独立状态或底层的副本对象。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。Pool — 该类型的值
(&Pool) get_connpub fn (p &Pool) get_conn() int&Pool 上的操作:读取 conn。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。int — 该类型的值
(&Pool) put_connpub fn (p &Pool) put_conn(fd int)&Pool 上的操作:执行 put conn 操作。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。fd int不返回值。
(&Pool) discard_connpub fn (p &Pool) discard_conn(fd int)&Pool 上的操作:丢弃连接。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。fd int不返回值。
open_poolpub fn open_pool(endpoint string, max_idle int) Pool执行 open pool 操作。
endpoint stringmax_idle intPool — 该类型的值
(&Pool) heartbeatpub fn (p &Pool) heartbeat() bool在 &Pool 上执行 heartbeat 操作。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。bool — 是否成立
(&Pool) create_collectionpub fn (p &Pool) create_collection(name string) string&Pool 上的操作:执行 create collection 操作。失败时抛出错误,调用方需处理或继续传播。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。name stringstring — 结果字符串
(&Pool) addpub fn (p &Pool) add(col_id string, ids_json string, embeddings_json string, documents_json string) bool&Pool 上的操作:相加。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。col_id stringids_json stringembeddings_json stringdocuments_json stringbool — 是否成立
(&Pool) querypub fn (p &Pool) query(col_id string, query_embeddings_json string, n_results int) string&Pool 上的操作:查询串。失败时抛出错误,调用方需处理或继续传播。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。col_id stringquery_embeddings_json stringn_results intstring — 结果字符串
(&Pool) delete_collectionpub fn (p &Pool) delete_collection(name string) bool&Pool 上的操作:执行 delete collection 操作。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。name stringbool — 是否成立
(&Pool) closepub fn (p &Pool) close()&Pool 上的操作:关闭资源。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。不返回值。