Examples

Chat Completion

1import { JoyTokenClient } from "@joytoken/client-sdk-ts";
2
3const joytoken = new JoyTokenClient();
4
5const completion = await joytoken.chat.completions.create({
6 model: "auto",
7 messages: [{ role: "user", content: "Say hello in one sentence." }],
8});
9
10console.log(completion.choices[0]?.message?.content);

Streaming Response

1for await (const chunk of joytoken.chat.completions.stream({
2 model: "auto",
3 messages: [{ role: "user", content: "Count from 1 to 5." }],
4})) {
5 process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
6}

Model List

1const models = await joytoken.models.list();
2
3for (const model of models.data) {
4 console.log(model.id);
5}

Error Handling

1import { JoyTokenAPIError } from "@joytoken/client-sdk-ts";
2
3try {
4 await joytoken.chat.completions.create({
5 model: "auto",
6 messages: [{ role: "user", content: "Hello" }],
7 });
8} catch (error) {
9 if (error instanceof JoyTokenAPIError) {
10 console.error(error.status, error.requestId, error.body);
11 }
12 throw error;
13}

Run the Example Project

$cd joytoken-sdks
$pnpm install
$
$export JOY_TOKEN_API_KEY="jt_xxx"
$export JOY_TOKEN_MODEL="auto"
$
$pnpm --filter joy-token-example live

Go example:

$cd joytoken-sdks/joy-token-example/go
$export JOY_TOKEN_API_KEY="jt_xxx"
$go run ./live