@ancatag/n-r 0.2.26 → 0.2.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +151 -6
- package/dist/types.d.ts +14 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -484,22 +484,93 @@ Get details for a specific model.
|
|
|
484
484
|
|
|
485
485
|
**Returns:** `Promise<Model>`
|
|
486
486
|
|
|
487
|
-
### Type
|
|
487
|
+
### Type Definitions
|
|
488
488
|
|
|
489
|
+
The SDK provides full TypeScript interfaces for all requests and responses. Here are the core types you'll use most often:
|
|
490
|
+
|
|
491
|
+
#### `ChatCompletionRequest`
|
|
492
|
+
The main payload for creating a chat completion.
|
|
493
|
+
```typescript
|
|
494
|
+
interface ChatCompletionRequest {
|
|
495
|
+
model?: string; // Optional: uses project default if omitted
|
|
496
|
+
messages: ChatMessage[];
|
|
497
|
+
temperature?: number;
|
|
498
|
+
top_p?: number;
|
|
499
|
+
max_tokens?: number;
|
|
500
|
+
stream?: boolean;
|
|
501
|
+
stop?: string | string[];
|
|
502
|
+
|
|
503
|
+
// Structured Output & Function Calling
|
|
504
|
+
response_format?: { type: "json_object" } | any;
|
|
505
|
+
tools?: any[];
|
|
506
|
+
tool_choice?: "auto" | "none" | any;
|
|
507
|
+
|
|
508
|
+
// Nova-specific extensions
|
|
509
|
+
nova?: {
|
|
510
|
+
skipCache?: boolean;
|
|
511
|
+
routeConfigId?: string;
|
|
512
|
+
ragEnabled?: boolean;
|
|
513
|
+
systemPromptOverride?: string;
|
|
514
|
+
metadata?: Record<string, any>;
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
```
|
|
518
|
+
|
|
519
|
+
#### `ChatMessage`
|
|
520
|
+
```typescript
|
|
521
|
+
interface ChatMessage {
|
|
522
|
+
role: "system" | "user" | "assistant" | "function";
|
|
523
|
+
content: string;
|
|
524
|
+
name?: string;
|
|
525
|
+
function_call?: {
|
|
526
|
+
name: string;
|
|
527
|
+
arguments: string;
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
```
|
|
531
|
+
|
|
532
|
+
#### `ChatCompletionResponse`
|
|
533
|
+
```typescript
|
|
534
|
+
interface ChatCompletionResponse {
|
|
535
|
+
id: string;
|
|
536
|
+
object: "chat.completion";
|
|
537
|
+
created: number;
|
|
538
|
+
model: string;
|
|
539
|
+
choices: {
|
|
540
|
+
index: number;
|
|
541
|
+
message: {
|
|
542
|
+
role: "assistant";
|
|
543
|
+
content: string;
|
|
544
|
+
function_call?: { name: string; arguments: string; };
|
|
545
|
+
};
|
|
546
|
+
finish_reason: "stop" | "length" | "function_call" | "content_filter" | null;
|
|
547
|
+
}[];
|
|
548
|
+
usage: {
|
|
549
|
+
prompt_tokens: number;
|
|
550
|
+
completion_tokens: number;
|
|
551
|
+
total_tokens: number;
|
|
552
|
+
};
|
|
553
|
+
nova?: {
|
|
554
|
+
cacheHit: boolean;
|
|
555
|
+
cacheLayer?: "hot" | "semantic" | null;
|
|
556
|
+
tokensSaved: number;
|
|
557
|
+
responseTimeMs: number;
|
|
558
|
+
requestId: string;
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
```
|
|
562
|
+
|
|
563
|
+
#### Exported Types
|
|
564
|
+
You can import any of these types directly from the SDK:
|
|
489
565
|
```typescript
|
|
490
566
|
import type {
|
|
491
567
|
ChatMessage,
|
|
492
568
|
ChatCompletionRequest,
|
|
493
569
|
ChatCompletionResponse,
|
|
494
570
|
ChatCompletionChunk,
|
|
495
|
-
ChatCompletionChoice,
|
|
496
|
-
ChatCompletionUsage,
|
|
497
571
|
Model,
|
|
498
572
|
NovaClientConfig,
|
|
499
|
-
NovaTransport,
|
|
500
573
|
} from '@ancatag/n-r';
|
|
501
|
-
|
|
502
|
-
import { NovaError } from '@ancatag/n-r';
|
|
503
574
|
```
|
|
504
575
|
|
|
505
576
|
### Nova-Specific Extensions
|
|
@@ -672,6 +743,80 @@ async function chat(
|
|
|
672
743
|
4. **Generate an API key** (format: `nova_sk_...`)
|
|
673
744
|
5. **Install the SDK** and start saving on token costs!
|
|
674
745
|
|
|
746
|
+
## Integration Examples
|
|
747
|
+
|
|
748
|
+
### Express.js
|
|
749
|
+
|
|
750
|
+
```typescript
|
|
751
|
+
import express from 'express';
|
|
752
|
+
import { NovaClient } from '@ancatag/n-r';
|
|
753
|
+
|
|
754
|
+
const app = express();
|
|
755
|
+
app.use(express.json());
|
|
756
|
+
|
|
757
|
+
const nova = new NovaClient({
|
|
758
|
+
apiKey: process.env.NOVA_API_KEY,
|
|
759
|
+
});
|
|
760
|
+
|
|
761
|
+
app.post('/api/chat', async (req, res) => {
|
|
762
|
+
try {
|
|
763
|
+
const { messages } = req.body;
|
|
764
|
+
const response = await nova.chat.create({
|
|
765
|
+
messages,
|
|
766
|
+
nova: {
|
|
767
|
+
routeConfigId: process.env.NOVA_ROUTE_CONFIG_ID,
|
|
768
|
+
}
|
|
769
|
+
});
|
|
770
|
+
|
|
771
|
+
res.json(response);
|
|
772
|
+
} catch (error) {
|
|
773
|
+
res.status(500).json({ error: 'Failed to generate response' });
|
|
774
|
+
}
|
|
775
|
+
});
|
|
776
|
+
|
|
777
|
+
app.listen(3000, () => console.log('Server running on port 3000'));
|
|
778
|
+
```
|
|
779
|
+
|
|
780
|
+
### Next.js App Router (Streaming)
|
|
781
|
+
|
|
782
|
+
```typescript
|
|
783
|
+
// app/api/chat/route.ts
|
|
784
|
+
import { NovaClient } from '@ancatag/n-r';
|
|
785
|
+
|
|
786
|
+
// Ensure the Edge runtime if desired, or Node runtime automatically works
|
|
787
|
+
export const maxDuration = 60;
|
|
788
|
+
|
|
789
|
+
const nova = new NovaClient({
|
|
790
|
+
apiKey: process.env.NOVA_API_KEY,
|
|
791
|
+
});
|
|
792
|
+
|
|
793
|
+
export async function POST(req: Request) {
|
|
794
|
+
const { messages } = await req.json();
|
|
795
|
+
|
|
796
|
+
const stream = await nova.chat.createStream({
|
|
797
|
+
messages,
|
|
798
|
+
nova: {
|
|
799
|
+
routeConfigId: process.env.NOVA_ROUTE_CONFIG_ID,
|
|
800
|
+
}
|
|
801
|
+
});
|
|
802
|
+
|
|
803
|
+
// Create a ReadableStream from the AsyncIterable
|
|
804
|
+
const readableStream = new ReadableStream({
|
|
805
|
+
async start(controller) {
|
|
806
|
+
for await (const chunk of stream) {
|
|
807
|
+
const text = chunk.choices[0]?.delta?.content || '';
|
|
808
|
+
if (text) controller.enqueue(new TextEncoder().encode(text));
|
|
809
|
+
}
|
|
810
|
+
controller.close();
|
|
811
|
+
}
|
|
812
|
+
});
|
|
813
|
+
|
|
814
|
+
return new Response(readableStream, {
|
|
815
|
+
headers: { 'Content-Type': 'text/event-stream' }
|
|
816
|
+
});
|
|
817
|
+
}
|
|
818
|
+
```
|
|
819
|
+
|
|
675
820
|
## Migration from OpenAI
|
|
676
821
|
|
|
677
822
|
Switching from OpenAI to Nova-route is simple:
|
package/dist/types.d.ts
CHANGED
|
@@ -31,6 +31,20 @@ export interface ChatCompletionRequest {
|
|
|
31
31
|
frequency_penalty?: number;
|
|
32
32
|
logit_bias?: Record<string, number>;
|
|
33
33
|
user?: string;
|
|
34
|
+
/**
|
|
35
|
+
* Forced output format for the LLM
|
|
36
|
+
* Use { type: "json_object" } to force JSON output
|
|
37
|
+
*/
|
|
38
|
+
response_format?: any;
|
|
39
|
+
/**
|
|
40
|
+
* A list of tools the model may call. Usually functions.
|
|
41
|
+
*/
|
|
42
|
+
tools?: any[];
|
|
43
|
+
/**
|
|
44
|
+
* Controls which (if any) tool is called by the model.
|
|
45
|
+
* e.g., "auto", "none", or a specific tool object
|
|
46
|
+
*/
|
|
47
|
+
tool_choice?: any;
|
|
34
48
|
nova?: {
|
|
35
49
|
/** Skip cache lookup for this request */
|
|
36
50
|
skipCache?: boolean;
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,MAAM,CAAC;AAE5C;;GAEG;AACH,MAAM,WAAW,WAAW;IAC3B,IAAI,EAAE,QAAQ,GAAG,MAAM,GAAG,WAAW,GAAG,UAAU,CAAC;IACnD,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,aAAa,CAAC,EAAE;QACf,IAAI,EAAE,MAAM,CAAC;QACb,SAAS,EAAE,MAAM,CAAC;KAClB,CAAC;CACF;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IAErC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,WAAW,EAAE,CAAC;IAGxB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,CAAC,CAAC,EAAE,MAAM,CAAC;IACX,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACzB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACpC,IAAI,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,MAAM,CAAC;AAE5C;;GAEG;AACH,MAAM,WAAW,WAAW;IAC3B,IAAI,EAAE,QAAQ,GAAG,MAAM,GAAG,WAAW,GAAG,UAAU,CAAC;IACnD,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,aAAa,CAAC,EAA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,MAAM,GAAG,QAAQ,GAAG,eAAe,GAAG,gBAAgB,GAAG,IAAI,CAAC;CAC7E;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IACnC,KAAK,EAAE;QACN,OAAO,EAAE,MAAM,CAAC;QAChB,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;QACrB,IAAI,EAAE,MAAM,CAAC;KACb,CAAC;CACF;AAED;;GAEG;AACH,MAAM,WAAW,KAAK;IACrB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,OAAO,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAChC,mDAAmD;IACnD,MAAM,EAAE,MAAM,CAAC;IACf,6DAA6D;IAC7D,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,wCAAwC;IACxC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,0BAA0B;IAC1B,SAAS,CAAC,EAAE,aAAa,CAAC;IAC1B,uDAAuD;IACvD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,iEAAiE;IACjE,UAAU,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;GAEG;AACH,qBAAa,SAAU,SAAQ,KAAK;aAGlB,MAAM,CAAC,EAAE,MAAM;aACf,IAAI,CAAC,EAAE,MAAM;aACb,IAAI,CAAC,EAAE,MAAM;gBAH7B,OAAO,EAAE,MAAM,EACC,MAAM,CAAC,EAAE,MAAM,YAAA,EACf,IAAI,CAAC,EAAE,MAAM,YAAA,EACb,IAAI,CAAC,EAAE,MAAM,YAAA;CAK9B"}
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AA6KH;;GAEG;AACH,MAAM,OAAO,SAAU,SAAQ,KAAK;IAGlB;IACA;IACA;IAJjB,YACC,OAAe,EACC,MAAe,EACf,IAAa,EACb,IAAa;QAE7B,KAAK,CAAC,OAAO,CAAC,CAAC;QAJC,WAAM,GAAN,MAAM,CAAS;QACf,SAAI,GAAJ,IAAI,CAAS;QACb,SAAI,GAAJ,IAAI,CAAS;QAG7B,IAAI,CAAC,IAAI,GAAG,WAAW,CAAC;IACzB,CAAC;CACD"}
|