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

chore: add test with OpenAI #58

Merged
merged 2 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 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."');
});
Loading