Skip to content

Quick Start

Get up and running with ai-lib-go.

Add the library to your Go project:

Terminal window
go get github.com/ailib-official/ai-lib-go

The Go SDK manages provider configurations dynamically. Here’s how to stream a completion using OpenAI:

package main
import (
"context"
"fmt"
"os"
"github.com/ailib-official/ai-lib-go/client"
)
func main() {
// Require AI_PROTOCOL_PATH to point to your manifests directory
// export OPENAI_API_KEY="sk-..."
ctx := context.Background()
// Create a new client, configured for OpenAI
aiClient, err := client.NewAiClient(ctx, "openai", nil)
if err != nil {
panic(err)
}
// Build the request
req := aiClient.Chat().
Model("gpt-4o").
User("Hello, how are you?").
MaxTokens(100)
// Stream the response using standard Go streaming pattern
stream, err := req.ExecuteStream(ctx)
if err != nil {
panic(err)
}
defer stream.Close()
for stream.Next() {
event := stream.Event()
if event.Type == "content" {
fmt.Print(event.Text)
}
}
if err := stream.Err(); err != nil {
fmt.Printf("\nError: %v\n", err)
}
}