-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathopenai-client.js
More file actions
41 lines (35 loc) · 1.13 KB
/
Copy pathopenai-client.js
File metadata and controls
41 lines (35 loc) · 1.13 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
// Talks to the local server (see server/index.js) using the OpenAI chat
// completions format directly, no SDK required.
//
// Usage:
// node server/index.js &
// node examples/openai-client.js
const BASE_URL = process.env.GEMINI_NANO_SERVER || "http://localhost:8788";
async function main() {
const res = await fetch(`${BASE_URL}/v1/chat/completions`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
model: "gemini-nano",
messages: [{ role: "user", content: "What are you, in one sentence?" }],
stream: true,
}),
});
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split("\n\n");
buffer = lines.pop();
for (const line of lines) {
const data = line.replace(/^data: /, "");
if (data === "[DONE]") return;
const delta = JSON.parse(data).choices[0]?.delta?.content;
if (delta) process.stdout.write(delta);
}
}
}
main();