Go SDK

Prerequisites

ItemValue
Modulegithub.com/joytoken/client-sdk-golang
Go1.22+
API keyJOY_TOKEN_API_KEY
API Base URLJOY_TOKEN_API_BASE_URL from Environments
OpenAI Base URLJOY_TOKEN_OPENAI_BASE_URL from Environments

Step 1: Install

$go get github.com/joytoken/client-sdk-golang

Step 2: Create a Client

1client := joytoken.NewClient(
2 joytoken.WithAPIKey(os.Getenv("JOY_TOKEN_API_KEY")),
3 joytoken.WithAPIBaseURL(os.Getenv("JOY_TOKEN_API_BASE_URL")),
4 joytoken.WithOpenAIBaseURL(os.Getenv("JOY_TOKEN_OPENAI_BASE_URL")),
5)

Step 3: Chat Completion

1completion, err := client.CreateChatCompletion(ctx, joytoken.ChatCompletionRequest{
2 Model: "auto",
3 Messages: []joytoken.ChatMessage{
4 {Role: "user", Content: "Reply with exactly: pong"},
5 },
6})
7if err != nil {
8 return err
9}
10fmt.Println(completion.Choices[0].Message.Content)

Step 4: Streaming Response

1stream, err := client.StreamChatCompletion(ctx, joytoken.ChatCompletionRequest{
2 Model: "auto",
3 Messages: []joytoken.ChatMessage{
4 {Role: "user", Content: "Count from 1 to 5."},
5 },
6})
7if err != nil {
8 return err
9}
10defer stream.Close()
11
12for {
13 chunk, err := stream.Recv()
14 if errors.Is(err, io.EOF) {
15 break
16 }
17 if err != nil {
18 return err
19 }
20 fmt.Print(chunk.Choices[0].Delta.Content)
21}

Step 5: Model List

1models, err := client.ListModels(ctx)
2if err != nil {
3 return err
4}
5for _, model := range models.Data {
6 fmt.Println(model.ID)
7}

Common Errors

ErrorFix
*joytoken.APIErrorInspect StatusCode, RequestID, and Body
401 UnauthorizedCheck JOY_TOKEN_API_KEY
Streaming response interruptedClose the stream and retry idempotent requests