@neureus/sdk
Version:
Neureus Platform SDK - AI-native, edge-first application platform
289 lines (287 loc) • 7.66 kB
JavaScript
import ky from 'ky';
// src/rag.ts
var RAGClient = class {
http;
config;
constructor(config) {
this.config = {
apiKey: config.apiKey,
baseUrl: config.baseUrl || "https://api.neureus.ai",
timeout: config.timeout || 6e4,
retries: config.retries || 3,
userId: config.userId || "",
teamId: config.teamId || ""
};
this.http = ky.create({
prefixUrl: this.config.baseUrl,
timeout: this.config.timeout,
retry: {
limit: this.config.retries,
methods: ["get", "post"],
statusCodes: [408, 413, 429, 500, 502, 503, 504]
},
hooks: {
beforeRequest: [
(request) => {
request.headers.set("Authorization", `Bearer ${this.config.apiKey}`);
request.headers.set("Content-Type", "application/json");
request.headers.set("User-Agent", "Neureus-SDK/0.2.0");
}
]
}
});
}
/**
* Pipeline management API
*/
pipelines = {
/**
* Create a new RAG pipeline
*
* @example
* ```typescript
* await rag.pipelines.create({
* name: 'customer-support',
* description: 'Customer support knowledge base',
* embedding: {
* model: 'text-embedding-ada-002',
* provider: 'openai',
* dimensions: 1536
* },
* chunking: {
* strategy: 'recursive',
* size: 512,
* overlap: 128
* },
* generation: {
* model: 'gpt-4',
* provider: 'openai',
* temperature: 0.1
* }
* });
* ```
*/
create: async (config) => {
return this.http.post("rag/pipelines", {
json: config
}).json();
},
/**
* List all RAG pipelines
*
* @example
* ```typescript
* const pipelines = await rag.pipelines.list();
* console.log(pipelines); // [{ name: 'docs', status: 'ready', ... }]
* ```
*/
list: async () => {
return this.http.get("rag/pipelines").json();
},
/**
* Get pipeline information
*
* @example
* ```typescript
* const info = await rag.pipelines.get('product-docs');
* console.log(info.stats.documentsCount, info.stats.totalQueries);
* ```
*/
get: async (pipelineName) => {
return this.http.get(`rag/pipelines/${pipelineName}`).json();
},
/**
* Update pipeline configuration
*
* @example
* ```typescript
* await rag.pipelines.update('product-docs', {
* generation: {
* temperature: 0.5
* }
* });
* ```
*/
update: async (pipelineName, updates) => {
return this.http.patch(`rag/pipelines/${pipelineName}`, {
json: updates
}).json();
},
/**
* Delete a pipeline
*
* @example
* ```typescript
* await rag.pipelines.delete('old-docs');
* ```
*/
delete: async (pipelineName) => {
return this.http.delete(`rag/pipelines/${pipelineName}`).json();
}
};
/**
* Ingest documents into a RAG pipeline
*
* @example
* ```typescript
* // Ingest from file
* await rag.ingest('product-docs', {
* source: './docs',
* type: 'file',
* format: 'markdown',
* recursive: true
* });
*
* // Ingest from URL
* await rag.ingest('product-docs', {
* source: 'https://example.com/docs',
* type: 'url',
* format: 'html'
* });
*
* // Ingest text directly
* await rag.ingest('product-docs', {
* source: 'This is my document content...',
* type: 'text',
* metadata: { title: 'Introduction' }
* });
* ```
*/
async ingest(pipelineName, options) {
const { waitForCompletion, ...ingestionRequest } = options;
if (waitForCompletion) {
return this.http.post(`rag/pipelines/${pipelineName}/ingest`, {
json: ingestionRequest,
searchParams: { wait: "true" }
}).json();
}
return this.http.post(`rag/pipelines/${pipelineName}/ingest`, {
json: ingestionRequest
}).json();
}
/**
* Query a RAG pipeline (non-streaming)
*
* @example
* ```typescript
* const response = await rag.query('product-docs', {
* query: 'How do I authenticate users?',
* topK: 5,
* minSimilarity: 0.7,
* includeSource: true
* });
*
* console.log('Answer:', response.answer);
* console.log('Sources:', response.sources);
* console.log('Performance:', response.performance);
* ```
*/
async query(pipelineName, options) {
const { streaming, ...queryRequest } = options;
return this.http.post(`rag/pipelines/${pipelineName}/query`, {
json: {
...queryRequest,
streaming: false,
userId: this.config.userId || void 0,
teamId: this.config.teamId || void 0
}
}).json();
}
/**
* Query a RAG pipeline with streaming response
*
* @example
* ```typescript
* const stream = await rag.queryStream('product-docs', {
* query: 'Explain the authentication flow',
* topK: 5
* });
*
* for await (const chunk of stream) {
* if (chunk.type === 'answer') {
* process.stdout.write(chunk.data.content);
* } else if (chunk.type === 'complete') {
* console.log('\nSources:', chunk.data.sources);
* }
* }
* ```
*/
async queryStream(pipelineName, options) {
const { streaming: _, ...queryRequest } = options;
const response = await this.http.post(`rag/pipelines/${pipelineName}/query`, {
json: {
...queryRequest,
streaming: true,
userId: this.config.userId || void 0,
teamId: this.config.teamId || void 0
}
});
return this.parseSSEStream(response.body);
}
/**
* Get pipeline statistics
*
* @example
* ```typescript
* const stats = await rag.stats('product-docs');
* console.log(`Documents: ${stats.documentsCount}`);
* console.log(`Queries: ${stats.totalQueries}`);
* console.log(`Avg response time: ${stats.avgResponseTime}ms`);
* ```
*/
async stats(pipelineName) {
const info = await this.pipelines.get(pipelineName);
return info.stats;
}
/**
* Clear all documents from a pipeline
*
* @example
* ```typescript
* await rag.clear('product-docs');
* ```
*/
async clear(pipelineName) {
return this.http.delete(`rag/pipelines/${pipelineName}/documents`).json();
}
/**
* Parse Server-Sent Events stream into async iterable
*/
async *parseSSEStream(body) {
const reader = body.getReader();
const decoder = new TextDecoder();
let buffer = "";
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split("\n");
buffer = lines.pop() || "";
for (const line of lines) {
const trimmed = line.trim();
if (trimmed === "") continue;
if (trimmed.startsWith(":")) continue;
if (trimmed === "data: [DONE]") return;
if (trimmed.startsWith("data: ")) {
const data = trimmed.slice(6);
try {
const chunk = JSON.parse(data);
yield chunk;
} catch (error) {
console.error("Failed to parse SSE data:", data, error);
}
}
}
}
} finally {
reader.releaseLock();
}
}
};
function createRAGClient(config) {
return new RAGClient(config);
}
export { RAGClient, createRAGClient };
//# sourceMappingURL=rag.js.map
//# sourceMappingURL=rag.js.map