This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
generated from JoshuaKGoldberg/create-typescript-app
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import OpenAI from "openai"; | ||
import { expect, test } from "vitest"; | ||
|
||
import { createTsonAsync, tsonAsyncIterator, tsonPromise } from "../index.js"; | ||
import { assert } from "../internals/assert.js"; | ||
|
||
const apiKey = process.env["OPENAI_API_KEY"]; | ||
|
||
// OPENAI_API_KEY=sk-xxxxxxx pnpm test openai | ||
test.skipIf(!apiKey)("openai", async () => { | ||
assert(apiKey); | ||
const openai = new OpenAI({ | ||
apiKey, | ||
}); | ||
|
||
const tson = createTsonAsync({ | ||
nonce: () => "__tson", | ||
types: [tsonAsyncIterator, tsonPromise], | ||
}); | ||
|
||
const stringified = tson.stringify({ | ||
stream: await openai.chat.completions.create({ | ||
messages: [{ content: "Say this is a test", role: "user" }], | ||
model: "gpt-4", | ||
stream: true, | ||
}), | ||
}); | ||
|
||
const parsed = await tson.parse(stringified); | ||
|
||
let buffer = ""; | ||
for await (const out of parsed.stream) { | ||
for (const choice of out.choices) { | ||
if (choice.delta.content) { | ||
buffer += choice.delta.content; | ||
} | ||
} | ||
} | ||
|
||
expect(buffer).toMatchInlineSnapshot('"This is a test."'); | ||
}); |