-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.mjs
More file actions
121 lines (107 loc) · 4.1 KB
/
example.mjs
File metadata and controls
121 lines (107 loc) · 4.1 KB
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
// NovaPAI JavaScript SDK Example
// Install: npm install openai
// Docs: https://novapai.ai
import OpenAI from "openai";
const client = new OpenAI({
apiKey: "your-api-key",
baseURL: "https://api.novapai.ai/router/v1",
});
// ── Basic Chat ──────────────────────────────────────────────
async function basicChat() {
const response = await client.chat.completions.create({
model: "deepseek-v4-pro",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Hello!" },
],
});
console.log(response.choices[0].message.content);
}
// ── Streaming ───────────────────────────────────────────────
async function streamChat() {
const stream = await client.chat.completions.create({
model: "deepseek-v4-pro",
messages: [{ role: "user", content: "Tell me a joke" }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}
console.log();
}
// ── Multi-turn Conversation ─────────────────────────────────
async function multiTurnChat() {
const messages = [{ role: "system", content: "You are a helpful assistant." }];
async function chat(userInput) {
messages.push({ role: "user", content: userInput });
const response = await client.chat.completions.create({
model: "deepseek-v4-pro",
messages,
});
const reply = response.choices[0].message.content;
messages.push({ role: "assistant", content: reply });
return reply;
}
console.log(await chat("What is 1+1?"));
console.log(await chat("Multiply that by 10"));
}
// ── Function Calling ────────────────────────────────────────
async function functionCalling() {
const tools = [{
type: "function",
function: {
name: "get_weather",
description: "Get current weather for a city",
parameters: {
type: "object",
properties: {
city: { type: "string", description: "City name" }
},
required: ["city"]
}
}
}];
const response = await client.chat.completions.create({
model: "deepseek-v4-pro",
messages: [{ role: "user", content: "What's the weather in Tokyo?" }],
tools,
});
const toolCall = response.choices[0].message.tool_calls[0];
console.log(`Function: ${toolCall.function.name}`);
console.log(`Args: ${toolCall.function.arguments}`);
// Simulate function result and continue
const functionResult = JSON.stringify({ city: "Tokyo", temperature: 22, condition: "sunny" });
const final = await client.chat.completions.create({
model: "deepseek-v4-pro",
messages: [
{ role: "user", content: "What's the weather in Tokyo?" },
response.choices[0].message,
{ role: "tool", tool_call_id: toolCall.id, content: functionResult }
]
});
console.log(final.choices[0].message.content);
}
// ── JSON Mode (Structured Output) ───────────────────────────
async function jsonMode() {
const response = await client.chat.completions.create({
model: "deepseek-v4-pro",
messages: [
{ role: "system", content: "Extract company info as JSON." },
{ role: "user", content: "Apple Inc. is based in Cupertino, founded in 1976." }
],
response_format: { type: "json_object" }
});
const data = JSON.parse(response.choices[0].message.content);
console.log(JSON.stringify(data, null, 2));
}
// ── List Available Models ───────────────────────────────────
async function listModels() {
const models = await client.models.list();
models.data.forEach((model) => console.log(model.id));
}
await basicChat();
await streamChat();
await multiTurnChat();
await functionCalling();
await jsonMode();
await listModels();