2026-04-05 21:09:33 -05:00
|
|
|
/**
|
|
|
|
|
* Document RAG retrieval pipeline.
|
|
|
|
|
*
|
|
|
|
|
* Simpler than Graph RAG — embeds the query, finds similar document chunks,
|
|
|
|
|
* and synthesizes an answer from the chunk content.
|
|
|
|
|
*
|
|
|
|
|
* Python reference: trustgraph-flow/trustgraph/retrieval/document_rag/
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import type {
|
2026-05-12 08:06:58 -05:00
|
|
|
FlowRequestor,
|
2026-04-05 21:09:33 -05:00
|
|
|
TextCompletionRequest,
|
|
|
|
|
TextCompletionResponse,
|
|
|
|
|
EmbeddingsRequest,
|
|
|
|
|
EmbeddingsResponse,
|
feat: fix RAG pipelines, Beep Graph branding, PWA, and ambient glow UI
Pipeline fixes:
- Fix agent getting empty response from graph-rag by combining answer +
explain data in single message (RequestResponse returns first msg)
- Fix Doc RAG pipeline: add content field to Qdrant doc payload, seed 10
document chunks, fix type mismatches across base/flow/client
- Forward explainability events from agent's KnowledgeQuery to client
- Add "agent" to TERM_BEARING_RESPONSE_SERVICES for triple translation
- Fix embeddings env var (OLLAMA_URL), user/collection threading, edge
scoring threshold, and various protocol mismatches
Branding:
- Rename TrustGraph → Beep Graph (title, sidebar, settings, about)
- Custom lambda + ThugLife pixel glasses SVG logo component
- Forest green color palette (brand-50 through brand-900)
- SVG favicon + PNG icons (16/32/180/192/512)
- PWA manifest with service worker for offline shell caching
- Splash screen with animated logo pulse on app load
- Ambient glow background with drifting green radial blobs
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-12 10:19:10 -05:00
|
|
|
DocumentEmbeddingsRequest,
|
|
|
|
|
DocumentEmbeddingsResponse,
|
2026-04-05 21:09:33 -05:00
|
|
|
PromptRequest,
|
|
|
|
|
PromptResponse,
|
|
|
|
|
} from "@trustgraph/base";
|
|
|
|
|
|
|
|
|
|
export interface DocumentRagClients {
|
2026-05-12 08:06:58 -05:00
|
|
|
llm: FlowRequestor<TextCompletionRequest, TextCompletionResponse>;
|
|
|
|
|
embeddings: FlowRequestor<EmbeddingsRequest, EmbeddingsResponse>;
|
|
|
|
|
docEmbeddings: FlowRequestor<DocumentEmbeddingsRequest, DocumentEmbeddingsResponse>;
|
|
|
|
|
prompt: FlowRequestor<PromptRequest, PromptResponse>;
|
2026-04-05 21:09:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type ChunkCallback = (text: string, endOfStream: boolean) => Promise<void>;
|
|
|
|
|
|
|
|
|
|
export class DocumentRag {
|
2026-05-12 08:06:58 -05:00
|
|
|
private readonly clients: DocumentRagClients;
|
|
|
|
|
|
|
|
|
|
constructor(clients: DocumentRagClients) {
|
|
|
|
|
this.clients = clients;
|
|
|
|
|
}
|
2026-04-05 21:09:33 -05:00
|
|
|
|
|
|
|
|
async query(
|
|
|
|
|
queryText: string,
|
feat: fix RAG pipelines, Beep Graph branding, PWA, and ambient glow UI
Pipeline fixes:
- Fix agent getting empty response from graph-rag by combining answer +
explain data in single message (RequestResponse returns first msg)
- Fix Doc RAG pipeline: add content field to Qdrant doc payload, seed 10
document chunks, fix type mismatches across base/flow/client
- Forward explainability events from agent's KnowledgeQuery to client
- Add "agent" to TERM_BEARING_RESPONSE_SERVICES for triple translation
- Fix embeddings env var (OLLAMA_URL), user/collection threading, edge
scoring threshold, and various protocol mismatches
Branding:
- Rename TrustGraph → Beep Graph (title, sidebar, settings, about)
- Custom lambda + ThugLife pixel glasses SVG logo component
- Forest green color palette (brand-50 through brand-900)
- SVG favicon + PNG icons (16/32/180/192/512)
- PWA manifest with service worker for offline shell caching
- Splash screen with animated logo pulse on app load
- Ambient glow background with drifting green radial blobs
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-12 10:19:10 -05:00
|
|
|
options?: {
|
2026-04-05 21:09:33 -05:00
|
|
|
collection?: string;
|
|
|
|
|
streaming?: boolean;
|
|
|
|
|
chunkCallback?: ChunkCallback;
|
|
|
|
|
},
|
|
|
|
|
): Promise<string> {
|
feat: fix RAG pipelines, Beep Graph branding, PWA, and ambient glow UI
Pipeline fixes:
- Fix agent getting empty response from graph-rag by combining answer +
explain data in single message (RequestResponse returns first msg)
- Fix Doc RAG pipeline: add content field to Qdrant doc payload, seed 10
document chunks, fix type mismatches across base/flow/client
- Forward explainability events from agent's KnowledgeQuery to client
- Add "agent" to TERM_BEARING_RESPONSE_SERVICES for triple translation
- Fix embeddings env var (OLLAMA_URL), user/collection threading, edge
scoring threshold, and various protocol mismatches
Branding:
- Rename TrustGraph → Beep Graph (title, sidebar, settings, about)
- Custom lambda + ThugLife pixel glasses SVG logo component
- Forest green color palette (brand-50 through brand-900)
- SVG favicon + PNG icons (16/32/180/192/512)
- PWA manifest with service worker for offline shell caching
- Splash screen with animated logo pulse on app load
- Ambient glow background with drifting green radial blobs
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-12 10:19:10 -05:00
|
|
|
const collection = options?.collection ?? "default";
|
|
|
|
|
|
2026-04-05 21:09:33 -05:00
|
|
|
// Step 1: Embed the query
|
|
|
|
|
const embResp = await this.clients.embeddings.request({ text: [queryText] });
|
|
|
|
|
const vectors = (embResp as EmbeddingsResponse).vectors;
|
|
|
|
|
|
|
|
|
|
// Step 2: Find similar document chunks
|
feat: fix RAG pipelines, Beep Graph branding, PWA, and ambient glow UI
Pipeline fixes:
- Fix agent getting empty response from graph-rag by combining answer +
explain data in single message (RequestResponse returns first msg)
- Fix Doc RAG pipeline: add content field to Qdrant doc payload, seed 10
document chunks, fix type mismatches across base/flow/client
- Forward explainability events from agent's KnowledgeQuery to client
- Add "agent" to TERM_BEARING_RESPONSE_SERVICES for triple translation
- Fix embeddings env var (OLLAMA_URL), user/collection threading, edge
scoring threshold, and various protocol mismatches
Branding:
- Rename TrustGraph → Beep Graph (title, sidebar, settings, about)
- Custom lambda + ThugLife pixel glasses SVG logo component
- Forest green color palette (brand-50 through brand-900)
- SVG favicon + PNG icons (16/32/180/192/512)
- PWA manifest with service worker for offline shell caching
- Splash screen with animated logo pulse on app load
- Ambient glow background with drifting green radial blobs
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-12 10:19:10 -05:00
|
|
|
const docResp = await this.clients.docEmbeddings.request({
|
|
|
|
|
vectors,
|
|
|
|
|
limit: 10,
|
|
|
|
|
collection,
|
|
|
|
|
user: "default",
|
|
|
|
|
});
|
|
|
|
|
const chunks = (docResp as DocumentEmbeddingsResponse).chunks ?? [];
|
|
|
|
|
console.log(`[DocumentRag] Found ${chunks.length} matching chunks`);
|
2026-04-05 21:09:33 -05:00
|
|
|
|
|
|
|
|
// Step 3: Build context from chunks
|
feat: fix RAG pipelines, Beep Graph branding, PWA, and ambient glow UI
Pipeline fixes:
- Fix agent getting empty response from graph-rag by combining answer +
explain data in single message (RequestResponse returns first msg)
- Fix Doc RAG pipeline: add content field to Qdrant doc payload, seed 10
document chunks, fix type mismatches across base/flow/client
- Forward explainability events from agent's KnowledgeQuery to client
- Add "agent" to TERM_BEARING_RESPONSE_SERVICES for triple translation
- Fix embeddings env var (OLLAMA_URL), user/collection threading, edge
scoring threshold, and various protocol mismatches
Branding:
- Rename TrustGraph → Beep Graph (title, sidebar, settings, about)
- Custom lambda + ThugLife pixel glasses SVG logo component
- Forest green color palette (brand-50 through brand-900)
- SVG favicon + PNG icons (16/32/180/192/512)
- PWA manifest with service worker for offline shell caching
- Splash screen with animated logo pulse on app load
- Ambient glow background with drifting green radial blobs
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-12 10:19:10 -05:00
|
|
|
const context = chunks
|
2026-05-12 08:06:58 -05:00
|
|
|
.flatMap((c) =>
|
|
|
|
|
c.content !== undefined && c.content.length > 0 ? [c.content] : [],
|
|
|
|
|
)
|
2026-04-05 21:09:33 -05:00
|
|
|
.join("\n\n---\n\n");
|
|
|
|
|
|
|
|
|
|
// Step 4: Synthesize answer
|
|
|
|
|
const promptResp = await this.clients.prompt.request({
|
|
|
|
|
name: "document-rag-synthesize",
|
|
|
|
|
variables: { query: queryText, context },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const resp = await this.clients.llm.request({
|
|
|
|
|
system: (promptResp as PromptResponse).system,
|
|
|
|
|
prompt: (promptResp as PromptResponse).prompt,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (resp as TextCompletionResponse).response;
|
|
|
|
|
}
|
|
|
|
|
}
|