openai

package
v0.38.0 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2026 License: Apache-2.0 Imports: 15 Imported by: 2 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package openai implements agent.LLMProvider for the OpenAI Chat Completions API and any OpenAI-compatible endpoint (Ollama, Groq, Together AI, vLLM, ...), plus the OpenAI-backed embedder, vision analyzer, and history summary provider.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Embedder

type Embedder struct {
	// contains filtered or unexported fields
}

Embedder implements tools.Embedder using OpenAI's embeddings API.

text-embedding-3-small is the default — 1536 dimensions, ~$0.02/1M tokens, and more than enough lexical/semantic signal for tool-description ranking. Pass model = "" to use the default, or a specific model id (e.g. openai.LargeEmbedding3 for higher fidelity at ~6x the cost).

func NewEmbedder

func NewEmbedder(apiKey string, model string) (*Embedder, error)

NewEmbedder constructs an embedder. apiKey falls back to OPENAI_API_KEY. model defaults to text-embedding-3-small.

func (*Embedder) Embed

func (e *Embedder) Embed(ctx context.Context, texts []string) ([][]float32, error)

Embed returns one vector per input in the same order. Empty input returns (nil, nil).

type Option

type Option func(*Provider)

Option configures a Provider at construction.

func WithSeed

func WithSeed(n int64) Option

WithSeed requests best-effort deterministic sampling. Determinism is not guaranteed across model versions or backend changes — pair with a pinned temperature for the strongest reproducibility OpenAI offers.

func WithTemperature

func WithTemperature(t float64) Option

WithTemperature pins the sampling temperature (0.0–2.0 for OpenAI). Use 0 for maximally reproducible classification/extraction turns; unset keeps the provider default. An explicit 0 is honored (the SDK's omitempty would otherwise drop it and silently revert to the provider default).

func WithTopP

func WithTopP(v float64) Option

WithTopP pins nucleus sampling. OpenAI recommends adjusting either temperature or top_p, not both. Unset keeps the provider default.

type Provider

type Provider struct {
	// contains filtered or unexported fields
}

Provider implements agent.LLMProvider using the OpenAI Chat Completions API.

func New

func New(apiKey string, model string, opts ...Option) (*Provider, error)

New creates a wrapper over OpenAI's API. Auto-discovers OPENAI_API_KEY from environment if apiKey is empty.

func NewCompat

func NewCompat(apiKey string, model string, baseURL string, opts ...Option) (*Provider, error)

NewCompat creates a provider for any OpenAI-compatible API endpoint. This covers: Google Gemini (via OpenAI compat), Ollama, Groq, Together AI, vLLM, etc.

Examples:

Gemini:  NewCompat("GEMINI_API_KEY", "gemini-2.0-flash", "https://generativelanguage.googleapis.com/v1beta/openai")
Ollama:  NewCompat("ollama", "llama3", "http://localhost:11434/v1")
Groq:    NewCompat("GROQ_KEY", "llama-3.3-70b-versatile", "https://api.groq.com/openai/v1")

func (*Provider) GenerateStream

func (p *Provider) GenerateStream(ctx context.Context, memory []history.Message, availableTools *tools.Registry, streamChan chan<- agent.StreamEvent) (agent.LLMResult, error)

GenerateStream maps GopherAgent history to OpenAI API format and triggers streaming generation.

type SummaryProvider

type SummaryProvider struct {
	// contains filtered or unexported fields
}

SummaryProvider implements history.SummaryProvider using OpenAI. It uses a cheap, fast model (default: gpt-4o-mini) to extract long-term behavioral patterns from conversation history, injected back into the system prompt on the next turn.

Usage:

sp, _ := openai.NewSummaryProvider("", "gpt-4o-mini")
sm := history.NewInMemSessionManager("You are an assistant.")
sm.SummaryProvider = sp

func NewSummaryProvider

func NewSummaryProvider(apiKey, model string) (*SummaryProvider, error)

NewSummaryProvider creates an OpenAI-backed SummaryProvider. Auto-discovers OPENAI_API_KEY from environment if apiKey is empty. model defaults to "gpt-4o-mini" (cheap + fast — ideal for background summarization).

func (*SummaryProvider) SummarizeBehaviors

func (s *SummaryProvider) SummarizeBehaviors(ctx context.Context, newMsgs []history.Message, previousSummary string) (string, error)

SummarizeBehaviors analyzes recent messages and merges new evidence into the existing previousSummary, returning an updated behavioral profile paragraph. Only newMessages (since last summarization) are analyzed — not the full history.

type VisionAnalyzer

type VisionAnalyzer struct {
	// contains filtered or unexported fields
}

VisionAnalyzer calls a vision-capable OpenAI model (default gpt-4o) and returns a natural-language answer about the supplied image.

It satisfies pkg/tools/builtin.MediaAnalyzer by structural compatibility — no import of that package is needed.

media may be:

  • an https:// URL publicly reachable by the OpenAI API
  • a data URI ("data:image/png;base64,...") for locally hosted images

func NewVisionAnalyzer

func NewVisionAnalyzer(apiKey, model string) (*VisionAnalyzer, error)

NewVisionAnalyzer builds an analyzer. apiKey defaults to OPENAI_API_KEY; model defaults to "gpt-4o" (smallest model with reliable vision support across all image tasks).

func (*VisionAnalyzer) Analyze

func (a *VisionAnalyzer) Analyze(ctx context.Context, media, prompt string) (string, error)

Analyze sends media + prompt to the vision model and returns the response.

Jump to

Keyboard shortcuts

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