openai

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2025 License: MIT Imports: 11 Imported by: 1

Documentation

Overview

Package openai provides an OpenAI provider implementation for the gAI unified LLM interface.

Overview

This package is a provider for gAI (codeberg.org/gai-org/gai), a unified Go library for interacting with various Large Language Model providers. The openai provider implements the llm.ProviderClient interface using the official openai-go SDK, enabling seamless integration with OpenAI's API through gAI's provider-agnostic interface.

This provider supports chat completions, streaming responses, and tool calling capabilities with OpenAI's models including GPT-3.5, GPT-4, and newer models.

Installation

To use this provider, you need both the main gAI package and this provider:

go get codeberg.org/gai-org/gai
go get codeberg.org/gai-org/gai-provider-openai

Usage Examples

This provider is designed to be used with the main gAI client. See the example functions in this package for complete, testable usage examples:

  • ExampleBasicUsage: Basic usage with gAI client
  • ExampleStreaming: Streaming responses
  • ExampleConversation: Multi-turn conversations
  • ExampleImageInput: Image input handling
  • ExampleFileInput: File input handling
  • ExampleToolCalling: Tool/function calling
  • ExampleDirectProviderUsage: Direct provider usage (advanced)

Input Types

The provider supports all gAI input types:

  • TextInput: Simple text input
  • ImageInput: Image content (for vision-capable models)
  • FileInput: File content as text
  • Conversation: Multi-turn conversation with message history

Configuration Options

The provider supports several configuration options:

  • WithAPIKey: Sets the OpenAI API key
  • WithBaseURL: Sets a custom base URL for API requests
  • WithLogger: Sets a custom logger for the provider
  • WithTools: Sets available tools for the provider to reference
  • WithOrganization: Sets the organization ID for the provider

Tool Calling Support

The provider fully supports OpenAI's tool calling capabilities. See ExampleToolCalling for a complete example of defining and using tools with the provider.

Model Capabilities

The provider automatically detects capabilities of different OpenAI models:

  • Streaming support
  • JSON mode support
  • Function/tool calling support
  • Vision capabilities (for models like gpt-4-vision-preview, gpt-4o)

Streaming Implementation

The package includes a robust streaming implementation that handles:

  • Incremental content delivery
  • Tool call assembly across multiple chunks
  • JSON validation for tool arguments
  • Token usage tracking

Package openai provides an OpenAI provider for the LLM client using the official openai-go SDK.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExampleBasicUsage added in v0.0.6

func ExampleBasicUsage()

ExampleBasicUsage demonstrates basic usage of the OpenAI provider with gAI.

func ExampleConversation added in v0.0.6

func ExampleConversation()

ExampleConversation demonstrates conversation input with multiple messages.

func ExampleDirectProviderUsage added in v0.0.6

func ExampleDirectProviderUsage()

ExampleDirectProviderUsage demonstrates using the provider directly (advanced usage).

func ExampleFileInput added in v0.0.6

func ExampleFileInput()

ExampleFileInput demonstrates file input handling.

func ExampleImageInput added in v0.0.6

func ExampleImageInput()

ExampleImageInput demonstrates image input handling.

func ExampleStreaming added in v0.0.6

func ExampleStreaming()

ExampleStreaming demonstrates streaming responses with gAI.

func ExampleToolCalling added in v0.0.6

func ExampleToolCalling()

ExampleToolCalling demonstrates tool calling capabilities.

Types

type Provider

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

Provider implements the llm.ProviderClient interface for OpenAI.

func New

func New(opts ...ProviderOption) *Provider

New creates a new OpenAI provider with the given options.

func (*Provider) Generate

func (p *Provider) Generate(ctx context.Context, req llm.GenerateRequest) (*llm.Response, error)

Generate sends a response generation request.

func (*Provider) GenerateStream

func (p *Provider) GenerateStream(ctx context.Context, req llm.GenerateRequest) (llm.ResponseStream, error)

GenerateStream sends a streaming response generation request.

func (*Provider) GetModel

func (p *Provider) GetModel(ctx context.Context, modelID string) (*llm.Model, error)

GetModel returns information for a specific model.

func (*Provider) ID

func (p *Provider) ID() string

ID returns the unique identifier for this provider.

func (*Provider) ListModels

func (p *Provider) ListModels(ctx context.Context) ([]llm.Model, error)

ListModels returns all available models from this provider.

func (*Provider) Name

func (p *Provider) Name() string

Name returns a human-readable name for this provider.

type ProviderOption

type ProviderOption func(*Provider)

ProviderOption configures the OpenAI provider.

func WithAPIKey

func WithAPIKey(apiKey string) ProviderOption

WithAPIKey sets the API key for the provider.

func WithBaseURL

func WithBaseURL(baseURL string) ProviderOption

WithBaseURL sets the base URL for the provider.

func WithLogger

func WithLogger(logger *slog.Logger) ProviderOption

WithLogger sets the logger for the provider.

func WithOrganization

func WithOrganization(organization string) ProviderOption

WithOrganization sets the organization ID for the provider.

func WithTools

func WithTools(tools []llm.Tool) ProviderOption

WithTools sets the available tools for the provider to reference.

Jump to

Keyboard shortcuts

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