@ancatag/n-r 0.2.26 → 0.2.27
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 +74 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -672,6 +672,80 @@ async function chat(
|
|
|
672
672
|
4. **Generate an API key** (format: `nova_sk_...`)
|
|
673
673
|
5. **Install the SDK** and start saving on token costs!
|
|
674
674
|
|
|
675
|
+
## Integration Examples
|
|
676
|
+
|
|
677
|
+
### Express.js
|
|
678
|
+
|
|
679
|
+
```typescript
|
|
680
|
+
import express from 'express';
|
|
681
|
+
import { NovaClient } from '@ancatag/n-r';
|
|
682
|
+
|
|
683
|
+
const app = express();
|
|
684
|
+
app.use(express.json());
|
|
685
|
+
|
|
686
|
+
const nova = new NovaClient({
|
|
687
|
+
apiKey: process.env.NOVA_API_KEY,
|
|
688
|
+
});
|
|
689
|
+
|
|
690
|
+
app.post('/api/chat', async (req, res) => {
|
|
691
|
+
try {
|
|
692
|
+
const { messages } = req.body;
|
|
693
|
+
const response = await nova.chat.create({
|
|
694
|
+
messages,
|
|
695
|
+
nova: {
|
|
696
|
+
routeConfigId: process.env.NOVA_ROUTE_CONFIG_ID,
|
|
697
|
+
}
|
|
698
|
+
});
|
|
699
|
+
|
|
700
|
+
res.json(response);
|
|
701
|
+
} catch (error) {
|
|
702
|
+
res.status(500).json({ error: 'Failed to generate response' });
|
|
703
|
+
}
|
|
704
|
+
});
|
|
705
|
+
|
|
706
|
+
app.listen(3000, () => console.log('Server running on port 3000'));
|
|
707
|
+
```
|
|
708
|
+
|
|
709
|
+
### Next.js App Router (Streaming)
|
|
710
|
+
|
|
711
|
+
```typescript
|
|
712
|
+
// app/api/chat/route.ts
|
|
713
|
+
import { NovaClient } from '@ancatag/n-r';
|
|
714
|
+
|
|
715
|
+
// Ensure the Edge runtime if desired, or Node runtime automatically works
|
|
716
|
+
export const maxDuration = 60;
|
|
717
|
+
|
|
718
|
+
const nova = new NovaClient({
|
|
719
|
+
apiKey: process.env.NOVA_API_KEY,
|
|
720
|
+
});
|
|
721
|
+
|
|
722
|
+
export async function POST(req: Request) {
|
|
723
|
+
const { messages } = await req.json();
|
|
724
|
+
|
|
725
|
+
const stream = await nova.chat.createStream({
|
|
726
|
+
messages,
|
|
727
|
+
nova: {
|
|
728
|
+
routeConfigId: process.env.NOVA_ROUTE_CONFIG_ID,
|
|
729
|
+
}
|
|
730
|
+
});
|
|
731
|
+
|
|
732
|
+
// Create a ReadableStream from the AsyncIterable
|
|
733
|
+
const readableStream = new ReadableStream({
|
|
734
|
+
async start(controller) {
|
|
735
|
+
for await (const chunk of stream) {
|
|
736
|
+
const text = chunk.choices[0]?.delta?.content || '';
|
|
737
|
+
if (text) controller.enqueue(new TextEncoder().encode(text));
|
|
738
|
+
}
|
|
739
|
+
controller.close();
|
|
740
|
+
}
|
|
741
|
+
});
|
|
742
|
+
|
|
743
|
+
return new Response(readableStream, {
|
|
744
|
+
headers: { 'Content-Type': 'text/event-stream' }
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
```
|
|
748
|
+
|
|
675
749
|
## Migration from OpenAI
|
|
676
750
|
|
|
677
751
|
Switching from OpenAI to Nova-route is simple:
|