Simple example
This example uses the server mode of seekdb to demonstrate the basic operations of embedding functions, helping you understand how to use them quickly.
- Connect to seekdb.
- Create a collection with embedding functions.
- Add data using documents (vectors will be automatically generated).
- Query using query texts (vectors will be automatically generated).
- Print the query results.
Example
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);
Related documents
-
For information about the API interfaces supported by seekdb-js, see API reference.