Rag Client

Classes

Classes

RAGClient

RAG client for semantic search and context building

Provides methods for querying the RAG system (SurrealDB) with semantic search capabilities. Embeds queries using Ollama and performs vector similarity search against indexed documents.

Constructor

constructor(config: RAGClientConfig)

Parameters:

ParameterTypeRequiredDescription
configRAGClientConfigYes

Methods

connect

Connect to SurrealDB

Establishes connection to SurrealDB for querying the RAG index.

connect(): Promise<void>

Returns:

Promise<void> -

disconnect

Disconnect from SurrealDB

disconnect(): Promise<void>

Returns:

Promise<void> -

isConnected

Check if client is connected

isConnected(): boolean

Returns:

boolean - true if connected, false otherwise

Perform semantic search across indexed documents

Embeds the query text and searches for similar documents using vector similarity. Results can be filtered by workspace, collections, and similarity threshold.

search(options: SearchOptions): Promise<SearchResult[]>

Parameters:

ParameterTypeRequiredDescription
optionsSearchOptionsYes- Search options

Returns:

Promise<SearchResult[]> - Array of matching documents with similarity scores

Examples:

const results = await client.search({
  query: 'high priority bugs',
  workspaceId: 'ws_1',
  collections: ['tasks', 'issues'],
  limit: 10,
  minScore: 0.7
});

getContext

Build context string from relevant documents

Performs semantic search and formats results into a context string suitable for LLM consumption. Respects token limits and provides source attribution.

getContext(options: ContextOptions): Promise<ContextResult>

Parameters:

ParameterTypeRequiredDescription
optionsContextOptionsYes- Context building options

Returns:

Promise<ContextResult> - Context result with formatted string, sources, and token estimate

Examples:

const context = await client.getContext({
  topic: 'What are the current sprint goals?',
  workspaceId: 'ws_1',
  includeMemories: true,
  maxTokens: 2000
});

console.log(context.context);
// "Based on the following information:
// [Task] Fix login bug - High priority...
// [Note] Sprint planning notes..."

recall

Recall memories about a topic

Searches the memories collection for relevant memories about a specific topic. Useful for retrieving conversation history or agent memories.

recall(options: RecallOptions): Promise<RecallResult>

Parameters:

ParameterTypeRequiredDescription
optionsRecallOptionsYes- Recall options

Returns:

Promise<RecallResult> - Array of recalled memories

Examples:

const result = await client.recall({
  topic: 'project requirements',
  namespace: 'user_123',
  sessionId: 'session_abc',
  limit: 10
});

findSimilar

Find similar documents to a given document

Retrieves the embedding for a specific document and finds other documents with similar embeddings. Useful for "related items" features.

findSimilar(options: FindSimilarOptions): Promise<SearchResult[]>

Parameters:

ParameterTypeRequiredDescription
optionsFindSimilarOptionsYes- Find similar options

Returns:

Promise<SearchResult[]> - Array of similar documents

Examples:

const similar = await client.findSimilar({
  collection: 'tasks',
  docId: 'task_123',
  limit: 5
});

Examples:

const client = new RAGClient({
  surrealdbUrl: 'ws://localhost:8000/rpc',
  surrealdbUser: 'root',
  surrealdbPass: 'root',
  surrealdbNamespace: 'flowstate',
  surrealdbDatabase: 'rag',
  ollamaUrl: 'http://localhost:11434',
  embeddingModel: 'nomic-embed-text'
});

await client.connect();

// Semantic search
const results = await client.search({
  query: 'What tasks are high priority?',
  workspaceId: 'ws_1',
  collections: ['tasks']
});

// Build context for LLM
const context = await client.getContext({
  topic: 'Current sprint progress',
  workspaceId: 'ws_1',
  maxTokens: 2000
});

await client.disconnect();
Previous
Interfaces