agent-ml/knowledge/examples/semantic-search-example.ts
Lilith 98a3fc639a Initial commit: ML Core library with provider implementations
- Core: Base ML provider abstraction and registry system
- Claude: Anthropic Claude SDK integration with Agent SDK support
- LlamaCpp: Local GGUF model inference with intelligent dual-model routing
- Knowledge: Semantic search, document caching, graph operations
- TTS: Text-to-speech integration

Configured as pnpm workspace with cross-package file: dependencies.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-25 17:10:28 -08:00

138 lines
4.5 KiB
TypeScript

/**
* Semantic Search Example
*
* Demonstrates how to use the semantic search system for the Venus knowledge graph.
*/
import Redis from 'ioredis';
import { createSemanticSearch } from '../src/semantic/index.js';
async function main() {
// Initialize Redis connection
const redis = new Redis({
host: 'localhost',
port: 6379,
});
console.log('Creating semantic search service...');
const search = await createSemanticSearch(redis, {
embedder: 'mock',
embeddingDimensions: 384,
createIndex: true,
});
// Check if service is ready
const ready = await search.isReady();
console.log(`Service ready: ${ready}`);
// Example documents
const documents = [
{
docId: 'doc:victoria',
content: `Victoria focuses on programming career and software development.
She uses Eterna Tech as her professional software brand.
She is actively job hunting and working on open source projects.`,
metadata: { docType: 'identity', person: 'victoria', category: 'real' },
},
{
docId: 'doc:quinn',
content: `Quinn (transquinnftw) is a gaming and PC building content creator.
She runs UwuPCs brand for gaming content.
She streams, does camming, and creates watercooling builds.`,
metadata: { docType: 'identity', person: 'quinn', category: 'real' },
},
{
docId: 'doc:lilith',
content: `Lilith Vaelynn is a hidden alien hacker and covert operator.
She maintains NO web presence due to OPSEC concerns.
She operates through proxies and recruits followers like Quinn.`,
metadata: { docType: 'identity', character: 'lilith', category: 'fictional' },
},
{
docId: 'doc:rachel',
content: `Rachel Albright is the CEO of Venus Tech.
She is a professional business persona focused on text-only presence.
She handles LinkedIn, professional networking, and marketplace sales.`,
metadata: { docType: 'identity', persona: 'rachel', category: 'business' },
},
{
docId: 'doc:venus-tech',
content: `Venus Tech is a comprehensive brand universe and business development project.
It focuses on content creation, character development, and strategic planning.
The project includes multiple identities, brands, and operational businesses.`,
metadata: { docType: 'brand', name: 'venus-tech' },
},
];
console.log('\n--- Indexing documents ---');
await search.indexDocuments(documents);
console.log(`Indexed ${documents.length} documents`);
// Give Redis time to index
await new Promise((resolve) => setTimeout(resolve, 200));
// Example 1: Search for programming-related content
console.log('\n--- Search: "programming software development" ---');
const programmingResults = await search.search({
query: 'programming software development',
limit: 3,
minScore: 0.5,
includeChunks: true,
});
for (const result of programmingResults) {
console.log(`\n${result.docId}: ${(result.score * 100).toFixed(1)}% match`);
if (result.matchedChunk) {
console.log(` Preview: ${result.matchedChunk.text.slice(0, 100)}...`);
}
console.log(` Metadata: ${JSON.stringify(result.metadata)}`);
}
// Example 2: Search for gaming content
console.log('\n--- Search: "gaming streaming PC builds" ---');
const gamingResults = await search.search({
query: 'gaming streaming PC builds',
limit: 3,
minScore: 0.5,
});
for (const result of gamingResults) {
console.log(`${result.docId}: ${(result.score * 100).toFixed(1)}% match`);
}
// Example 3: Find documents similar to Venus Tech brand doc
console.log('\n--- Similar to: doc:venus-tech ---');
const similarDocs = await search.findSimilar('doc:venus-tech', 3);
for (const doc of similarDocs) {
console.log(`${doc.docId}: ${(doc.score * 100).toFixed(1)}% similar`);
}
// Example 4: Search with metadata filtering
console.log('\n--- Search: "professional business" (fictional only) ---');
const businessResults = await search.search({
query: 'professional business',
limit: 3,
minScore: 0.4,
});
for (const result of businessResults) {
console.log(`${result.docId}: ${(result.score * 100).toFixed(1)}% match`);
}
// Clean up
console.log('\n--- Cleanup ---');
for (const doc of documents) {
await search.removeDocument(doc.docId);
}
console.log('Removed all indexed documents');
await redis.quit();
console.log('\nExample completed successfully!');
}
// Run the example
main().catch((error) => {
console.error('Error:', error);
process.exit(1);
});