Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 7b2c846

Browse files
committed
test(nitro-node): update chat completion test with more meaningful use case
1 parent e427816 commit 7b2c846

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

nitro-node/test/nitro-process.test.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import path from "node:path";
77
import download from "download";
88

99
import { Duplex } from "node:stream";
10+
import { WritableStream } from "node:stream/web";
1011
import {
1112
stopModel,
1213
runModel,
@@ -175,43 +176,50 @@ describe("Manage nitro process", () => {
175176
// Validate model status
176177
await validateModelStatus();
177178
// Arrays of all the chunked response
178-
let streamedContent: string[] = [];
179+
let streamedContent: Record<string, any>[] = [];
179180
// Run chat completion with stream
180181
const response = await chatCompletion(
181182
{
182183
messages: [
183-
{ content: "Hello there", role: "assistant" },
184-
{ content: "Write a long and sad story for me", role: "user" },
184+
{
185+
content:
186+
"You are a good productivity assistant. You help user with what they are asking in Markdown format . For responses that contain code, you must use ``` with the appropriate coding language to help display the code to user correctly.",
187+
role: "assistant",
188+
},
189+
{
190+
content: "Please give me a hello world code in cpp",
191+
role: "user",
192+
},
185193
],
186194
model: "gpt-3.5-turbo",
187-
max_tokens: 50,
188-
stop: ["hello"],
195+
max_tokens: 2048,
196+
stop: [],
189197
frequency_penalty: 0,
190198
presence_penalty: 0,
191-
temperature: 0.1,
199+
temperature: 0.7,
200+
top_p: 0.95,
201+
context_length: 4096,
192202
},
193203
new WritableStream({
194204
write(chunk: string) {
195-
if (chunk.trim() == "data: [DONE]") {
205+
const data = chunk.replace(/^\s*data:\s*/, "").trim();
206+
// Stop at [DONE] message
207+
if (data.match(/\[DONE\]/)) {
196208
return;
197209
}
198-
return new Promise((resolve) => {
199-
streamedContent.push(chunk.slice("data:".length).trim());
200-
resolve();
201-
});
210+
streamedContent.push(JSON.parse(data));
202211
},
203212
//close() {},
204213
//abort(_err) {}
205214
}),
206215
);
207-
// Remove the [DONE] message
208-
streamedContent.pop();
209-
// Parse json
210-
streamedContent = streamedContent.map((str) => JSON.parse(str));
211216
// Show the streamed content
212217
console.log(
213218
`[Streamed response] ${JSON.stringify(streamedContent, null, 2)}`,
214219
);
220+
console.log(
221+
`Generated reply: ${streamedContent.map((r) => r.choices[0].delta.content ?? "").join("")}`,
222+
);
215223

216224
// The response body is unusable if consumed by out stream
217225
await expect(response.text).rejects.toThrow();

0 commit comments

Comments
 (0)