Docs

Quickstart

Go quickstart

The Go client: standard library only, context aware, Go 1.22 or newer. Typed chat completions with streaming, models, embeddings, rerank and compute.

1. Add the module and set the key

The Go client is not published yet. It lives in the repository at sdks/go; point your module at a local checkout.

setup - zsh
go mod edit -require=github.com/ekempinski/siliconnetwork/sdks/go@v0.0.0go mod edit -replace=github.com/ekempinski/siliconnetwork/sdks/go=/path/to/siliconnetwork/sdks/go export SILICON_API_KEY=sn_live_...

With access to the repository, go get github.com/ekempinski/siliconnetwork/sdks/go@main works too. sn.NewClient() reads SILICON_API_KEY and SILICON_BASE_URL; sn.WithAPIKey and sn.WithBaseURL set them in code.

2. One chat completion, with errors handled

main.go
package main import (	"context"	"errors"	"fmt"	"log" 	sn "github.com/ekempinski/siliconnetwork/sdks/go") func main() {	client, err := sn.NewClient() // reads SILICON_API_KEY	if err != nil {		log.Fatal(err)	}	ctx := context.Background() 	out, err := client.ChatCompletion(ctx, sn.ChatCompletionRequest{		Model:    "llama-3.2-3b",		Messages: []sn.ChatMessage{sn.UserMessage("Say hello in one line.")},	})	var apiErr *sn.APIError	switch {	case errors.As(err, &apiErr) && apiErr.IsOutOfCredits():		log.Fatalf("add balance on the Billing page: %s", apiErr.Message)	case errors.As(err, &apiErr) && apiErr.IsRateLimited():		log.Fatalf("rate limited; retry after %s", apiErr.RetryAfter)	case errors.As(err, &apiErr):		log.Fatalf("%d %s (request %s)", apiErr.StatusCode, apiErr.Message, apiErr.RequestID)	case err != nil:		log.Fatal(err) // no answer: network, TLS, timeout	}	fmt.Println(out.Text())	fmt.Println(out.Meta.RequestID)}

3. Stream the answer

stream.go (inside main)
stream, err := client.ChatCompletionStream(ctx, sn.ChatCompletionRequest{	Model:    "llama-3.2-3b",	Messages: []sn.ChatMessage{sn.UserMessage("Write a haiku about silicon.")},})if err != nil {	log.Fatal(err)}defer stream.Close()for stream.Next() {	if c := stream.Chunk(); len(c.Choices) > 0 {		fmt.Print(c.Choices[0].Delta.Content)	}}if err := stream.Err(); err != nil {	log.Fatal(err)}fmt.Println()

stream.Text() and stream.Final() return the joined text or the whole completion instead. Streams are not retried and run until their context ends.

4. List the models

models.go (inside main)
models, err := client.ListModels(ctx)if err != nil {	log.Fatal(err)}for _, m := range models.Data {	fmt.Println(m.ID, m.Aliases)}

5. Errors

*sn.APIError
FieldMeaning
StatusCodeThe HTTP status: 401 bad key, 402 out of credits or over a spend cap, 403 not allowed, 429 rate limited, 5xx the gateway or node failed.
Message, CodeThe message and machine code from the body.
RequestIDThe gateway's request id; quote it to support.
RetryAfterThe wait the gateway asked for, or 0.
IsOutOfCredits(), IsRateLimited()402 and 429.

Non-streaming calls retry 429 and 5xx twice, honoring Retry-After (at most 30 seconds); sn.WithMaxRetries and sn.WithTimeout change that. The README in sdks/go covers embeddings, rerank, compute and the generated route methods.

Silicon Network›Docs›Guides›Quickstarts by language›Go54 items