示例
示例
聊天补全
1 import { JoyTokenClient } from "@joytoken/client-sdk-ts"; 2 3 const joytoken = new JoyTokenClient(); 4 5 const completion = await joytoken.chat.completions.create({ 6 model: "auto", 7 messages: [{ role: "user", content: "Say hello in one sentence." }], 8 }); 9 10 console.log(completion.choices[0]?.message?.content);
流式响应
1 for 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 }
模型列表
1 const models = await joytoken.models.list(); 2 3 for (const model of models.data) { 4 console.log(model.id); 5 }
错误处理
1 import { JoyTokenAPIError } from "@joytoken/client-sdk-ts"; 2 3 try { 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 }
运行 example 工程
$ cd joytoken-sdks $ pnpm install $ $ export JOY_TOKEN_API_KEY="jt_xxx" $ export JOY_TOKEN_MODEL="auto" $ $ pnpm --filter joy-token-example live
Go 示例:
$ cd joytoken-sdks/joy-token-example/go $ export JOY_TOKEN_API_KEY="jt_xxx" $ go run ./live