Skip to Content
unii.docs
Sdks

Go SDK

A Go client for building with the Unii API.

Overview

The Unii Go SDK provides a typed client for the Unii API. It supports chat completions, streaming, knowledge bases, and more with idiomatic Go patterns.

Important

The SDK API surface shown on this page is illustrative. Verify the actual method signatures and types against the latest SDK documentation before using them in production.

Installation

Install the SDK using go get:

go get unii.dev/sdk

Requires Go 1.21 or later.

Initialize the client

Import the SDK and authenticate with your API key:

init.go
package main

import (
	"os"

	"unii.dev/sdk"
)

func main() {
	client := sdk.NewClient(sdk.WithAPIKey(os.Getenv("UNII_API_KEY")))
}

Create a chat completion

chat.go
resp, err := client.Chat.Completions.Create(ctx, &sdk.ChatRequest{
	Model: "unii-latest",
	Messages: []sdk.Message{
		{Role: "system", Content: "You are a helpful assistant."},
		{Role: "user", Content: "Explain RAG in one sentence."},
	},
	Temperature: 0.7,
	MaxTokens:   256,
})
if err != nil {
	log.Fatal(err)
}

fmt.Println(resp.Choices[0].Message.Content)

Streaming responses

stream.go
stream, err := client.Chat.Completions.CreateStream(ctx, &sdk.ChatRequest{
	Model:    "unii-latest",
	Messages: []sdk.Message{{Role: "user", Content: "Explain RAG in detail."}},
	Stream:   true,
})
if err != nil {
	log.Fatal(err)
}
defer stream.Close()

for stream.Next() {
	chunk := stream.Chunk()
	fmt.Print(chunk.Choices[0].Delta.Content)
}

Error handling

errors.go
resp, err := client.Chat.Completions.Create(ctx, req)
if err != nil {
	var apiErr *sdk.APIError
	if errors.As(err, &apiErr) {
		fmt.Printf("API error: %d - %s\n", apiErr.StatusCode, apiErr.Message)
	} else {
		fmt.Printf("Request error: %v\n", err)
	}
}

Methods reference

Key client methods:

ParameterTypeDescription
Chat.Completions.Create()method
Create a chat completion. Returns a completion response or an error.
Chat.Completions.CreateStream()method
Create a streaming chat completion. Returns a stream iterator for processing chunks.
KnowledgeBases.Create()method
Create a new knowledge base with a name and optional description.
VerifyAuth()method
Verify that the current API key is valid and active.

Next steps

On this page