跳到主要内容
版本:V1.1.0

Simple 示例

本示例以服务器模式的 seekdb 为例,演示了 Embedding Functions 的基础操作,以帮助您快速了解如何使用 Embedding Functions。

  1. 连接 seekdb。
  2. 创建带有 Embedding Functions 的 collection。
  3. 使用 documents 添加数据(会自动生成 vectors)。
  4. 使用 query texts 进行查询(会自动生成 vectors)。
  5. 打印查询结果。

示例

import { SeekdbClient } from "seekdb";

async function main() {
// ==================== Step 1: Create Client Connection ====================
// Server mode (connecting to seekdb server or OceanBase)
const client = new SeekdbClient({
host: "127.0.0.1",
port: 2881,
database: "test",
user: "root",
password: "",
});

// ==================== Step 2: Create a Collection with Embedding Function ====================
// A collection is like a table that stores documents with vector embeddings
const collectionName = "my_simple_collection";

// Create collection with default embedding function
// The embedding function will automatically convert documents to embeddings

// Default embedding function is used if no embedding function is provided
const collection = await client.createCollection({
name: collectionName,
});

console.log(
`Created collection '${collection.name}' with dimension: ${collection.dimension}`,
);
console.log(`Embedding function: ${collection.embeddingFunction?.name}`);

// ==================== Step 3: Add Data to Collection ====================
// With embedding function, you can add documents directly without providing embeddings
// The embedding function will automatically generate embeddings from documents

const documents = [
"Machine learning is a subset of artificial intelligence",
"Python is a popular programming language",
"Vector databases enable semantic search",
"Neural networks are inspired by the human brain",
"Natural language processing helps computers understand text",
];

const ids = ["id1", "id2", "id3", "id4", "id5"];

// Add data with documents only - embeddings will be auto-generated by embedding function
await collection.add({
ids,
documents, // embeddings will be automatically generated
metadatas: [
{ category: "AI", index: 0 },
{ category: "Programming", index: 1 },
{ category: "Database", index: 2 },
{ category: "AI", index: 3 },
{ category: "NLP", index: 4 },
],
});

console.log(`\nAdded ${documents.length} documents to collection`);
console.log(
"Note: Embeddings were automatically generated from documents using the embedding function",
);

// ==================== Step 4: Query the Collection ====================
// With embedding function, you can query using text directly
// The embedding function will automatically convert query text to query vector

// Query using text - query vector will be auto-generated by embedding function
const queryText = "artificial intelligence and machine learning";

const results = await collection.query({
queryTexts: queryText, // Query text - will be embedded automatically
nResults: 3, // Return top 3 most similar documents
include: ["documents", "metadatas", "distances"],
});

console.log(`\nQuery: '${queryText}'`);
console.log(`Query results: ${results.ids[0].length} items found`);

// ==================== Step 5: Print Query Results ====================
for (let i = 0; i < results.ids[0].length; i++) {
console.log(`\nResult ${i + 1}:`);
console.log(` ID: ${results.ids[0][i]}`);
console.log(` Distance: ${results.distances?.[0][i]?.toFixed(4)}`);
if (results.documents) {
console.log(` Document: ${results.documents[0][i]}`);
}
if (results.metadatas) {
console.log(` Metadata: ${JSON.stringify(results.metadatas[0][i])}`);
}
}

// ==================== Step 6: Cleanup ====================
await client.deleteCollection(collectionName);
console.log(`\nDeleted collection '${collectionName}'`);

await client.close();
}

main().catch(console.error);

相关文档