Spaces:
Running
Running
File size: 3,076 Bytes
634117a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | # `@kerdos/rag-client`
> TypeScript/JavaScript client for the **Kerdos RAG REST API**.
> Zero runtime dependencies — works in Node.js ≥ 18 and modern browsers.
---
## Installation
```bash
# npm
npm install @kerdos/rag-client
# pnpm
pnpm add @kerdos/rag-client
# yarn
yarn add @kerdos/rag-client
```
---
## Quick Start
```typescript
import { KerdosRAGClient } from "@kerdos/rag-client";
const client = new KerdosRAGClient({
baseUrl: "http://localhost:8000",
apiKey: "your-secret", // optional — only if server has API_KEY set
});
// 1. Index documents
const result = await client.indexFiles([
new File([pdfBuffer], "policy.pdf"),
new File([txtContent], "manual.txt"),
]);
console.log("Indexed:", result.indexed);
console.log("Skipped:", result.skipped);
// 2. Stream an answer
let answer = "";
for await (const token of client.chat("What is the refund policy?")) {
answer = token; // each yield is the full cumulative answer
process.stdout.write("\r" + answer);
}
// 3. Multi-turn conversation
const history = [
{ role: "user", content: "What is the refund policy?" },
{ role: "assistant", content: answer },
];
for await (const token of client.chat("Who do I contact for refunds?", {
history,
})) {
process.stdout.write("\r" + token);
}
// 4. Status & reset
const status = await client.status();
console.log("Chunks:", status.chunk_count);
await client.reset();
```
---
## API Reference
### `new KerdosRAGClient(options)`
| Option | Type | Default | Description |
| ----------- | -------- | ----------- | ----------------------------------------- |
| `baseUrl` | `string` | — | Server URL (e.g. `http://localhost:8000`) |
| `apiKey` | `string` | `undefined` | Sent as `X-Api-Key` header |
| `timeoutMs` | `number` | `30000` | Request timeout in ms |
### Methods
| Method | Returns | Description |
| -------------------- | ------------------------- | -------------------- |
| `health()` | `Promise<HealthResponse>` | Liveness probe |
| `status()` | `Promise<StatusResponse>` | KB metadata |
| `indexFiles(files)` | `Promise<IndexResult>` | Upload & index files |
| `chat(query, opts?)` | `AsyncGenerator<string>` | Stream answer tokens |
| `reset()` | `Promise<{ok, message}>` | Clear knowledge base |
---
## Server Setup
Start the REST server (Python backend required):
```bash
pip install kerdos-rag
kerdos-rag api --port 8000
```
Or with Docker:
```bash
docker build --build-arg MODE=api -t kerdos-rag .
docker run -p 8000:8000 -e HF_TOKEN=hf_... kerdos-rag
```
---
## Error Handling
```typescript
import { KerdosAPIError } from "@kerdos/rag-client";
try {
for await (const token of client.chat("...")) { ... }
} catch (err) {
if (err instanceof KerdosAPIError) {
console.error(`API ${err.statusCode}:`, err.message);
}
}
```
---
_© 2024–2026 Kerdos Infrasoft Private Limited | [kerdos.in](https://kerdos.in)_
|