Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
chore: add test with OpenAI (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT authored Oct 9, 2023
1 parent 0571f30 commit 4936305
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 0 deletions.
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"lcov",
"markdownlintignore",
"npmpackagejsonlintrc",
"openai",
"outro",
"packagejson",
"quickstart",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"markdownlint-cli": "^0.37.0",
"npm-package-json-lint": "^7.0.0",
"npm-package-json-lint-config-default": "^6.0.0",
"openai": "^4.11.1",
"prettier": "^3.0.3",
"prettier-plugin-curly": "^0.1.3",
"prettier-plugin-packagejson": "^2.4.6",
Expand Down
124 changes: 124 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions src/extend/openai.test.ts
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."');
});

0 comments on commit 4936305

Please sign in to comment.