Go SDK
Prerequisites
| Item | Value |
|---|---|
| Module | github.com/joytoken/client-sdk-golang |
| Go | 1.22+ |
| API key | JOY_TOKEN_API_KEY |
| API Base URL | JOY_TOKEN_API_BASE_URL from Environments |
| OpenAI Base URL | JOY_TOKEN_OPENAI_BASE_URL from Environments |
Step 1: Install
$ go get github.com/joytoken/client-sdk-golang
Step 2: Create a Client
1 client := 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
1 completion, err := client.CreateChatCompletion(ctx, joytoken.ChatCompletionRequest{ 2 Model: "auto", 3 Messages: []joytoken.ChatMessage{ 4 {Role: "user", Content: "Reply with exactly: pong"}, 5 }, 6 }) 7 if err != nil { 8 return err 9 } 10 fmt.Println(completion.Choices[0].Message.Content)
Step 4: Streaming Response
1 stream, err := client.StreamChatCompletion(ctx, joytoken.ChatCompletionRequest{ 2 Model: "auto", 3 Messages: []joytoken.ChatMessage{ 4 {Role: "user", Content: "Count from 1 to 5."}, 5 }, 6 }) 7 if err != nil { 8 return err 9 } 10 defer stream.Close() 11 12 for { 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
1 models, err := client.ListModels(ctx) 2 if err != nil { 3 return err 4 } 5 for _, model := range models.Data { 6 fmt.Println(model.ID) 7 }
Common Errors
| Error | Fix |
|---|---|
*joytoken.APIError | Inspect StatusCode, RequestID, and Body |
401 Unauthorized | Check JOY_TOKEN_API_KEY |
| Streaming response interrupted | Close the stream and retry idempotent requests |