openai

package
v1.0.17 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package openai provides adapters and helpers for integrating ADK-Go agents with OpenAI-compatible APIs.

The primary entry point is New, which constructs a model.LLM that speaks the OpenAI Chat Completions API protocol. Any server that is compatible with that protocol (e.g. local LLM servers, Azure OpenAI, Together AI, etc.) can be targeted by setting Config.BaseURL.

Basic usage

m, err := openai.New(openai.Config{
    Model:  "gpt-4o",
    APIKey: os.Getenv("OPENAI_API_KEY"),
})
if err != nil {
    log.Fatal(err)
}

req := &model.LLMRequest{
    Contents: []*genai.Content{
        {Role: "user", Parts: []*genai.Part{{Text: "Hello!"}}},
    },
}

for resp, err := range m.GenerateContent(ctx, req, false) {
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(resp.Content.Parts[0].Text)
}

Streaming

Pass stream=true to model.LLM.GenerateContent to enable server-sent-event streaming. Each partial delta is yielded with Partial=true; the final sentinel is yielded with TurnComplete=true.

for resp, err := range m.GenerateContent(ctx, req, true) {
    if err != nil { break }
    if resp.TurnComplete { break }
    if resp.Partial {
        fmt.Print(resp.Content.Parts[0].Text)
    }
}

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyFunctionResponseParts = errors.New("function response has no parts")
	ErrMissingInlineData          = errors.New("function response part has no inline data")
	ErrInvalidMIMEType            = errors.New("function response part has non-JSON MIME type")
	ErrInvalidJSONData            = errors.New("function response part contains invalid JSON")
	ErrEmptyToolCallID            = errors.New("function response has empty tool call ID")
)

Sentinel errors for FunctionResponse validation.

Functions

func New

func New(cfg Config) (model.LLM, error)

New creates a model.LLM that communicates with an OpenAI-compatible Chat Completions API.

The returned LLM is safe for concurrent use. Config fields are copied at construction time; subsequent mutations to the Config do not affect the model.

New returns an error when cfg.Model is empty, because every request to the Chat Completions endpoint requires a model identifier.

Example — pointing at a local server:

m, err := openai.New(openai.Config{
    Model:   "llama3",
    BaseURL: "http://localhost:11434/v1",
})

Types

type Config

type Config struct {
	// Model is the model identifier to request, e.g. "gpt-4o" or "gpt-4o-mini".
	// This field is required; [New] returns an error if it is empty.
	Model string

	// APIKey is sent as "Authorization: Bearer <APIKey>" on every request.
	// Leave empty only when the target server does not require authentication.
	APIKey string

	// BaseURL is the base URL of the OpenAI-compatible API server, without a
	// trailing slash. Defaults to "https://api.openai.com/v1" when empty.
	BaseURL string

	// HTTPClient is the HTTP client used to send requests. When nil,
	// [http.DefaultClient] is used.
	HTTPClient *http.Client

	// Headers contains additional HTTP headers that are sent with every request.
	// These are applied after the standard Content-Type and Authorization headers,
	// so they can be used to override those values if needed.
	Headers map[string]string
}

Config holds the parameters needed to create an OpenAI-compatible LLM client.

Only Model is required. All other fields have sensible defaults:

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL