openai

package
v0.20.2 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultBaseURL is the default base URL for the OpenAI API
	DefaultBaseURL = "https://api.openai.com/v1"

	// DefaultRPM is the default rate limit for requests per minute
	DefaultRPM = 200

	// DefaultTPM is the default rate limit for tokens per minute
	DefaultTPM = 150000

	// DefaultMaxRetries is the default number of retries for rate limited requests
	DefaultMaxRetries = 5

	// DefaultRetryAfter is the default time to wait before retrying a rate limited request
	DefaultRetryAfter = 1 * time.Second

	DefaultAPIVersion = "2023-05-15"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type APIType

type APIType string
const (
	APITypeOpenAI  APIType = "OPEN_AI"
	APITypeAzure   APIType = "AZURE"
	APITypeAzureAD APIType = "AZURE_AD"
)

type ChatCompletionChoice

type ChatCompletionChoice struct {
	Index        int         `json:"index"`
	Message      ChatMessage `json:"message"`
	FinishReason string      `json:"finish_reason"`
}

ChatCompletionChoice represents a choice in a chat completion response

type ChatCompletionRequest

type ChatCompletionRequest struct {
	Model               string         `json:"model"`
	Messages            []ChatMessage  `json:"messages"`
	Tools               []ChatTool     `json:"tools,omitempty"`
	ToolChoice          interface{}    `json:"tool_choice,omitempty"`
	Temperature         float64        `json:"temperature,omitempty"`
	TopP                float64        `json:"top_p,omitempty"`
	FrequencyPenalty    float64        `json:"frequency_penalty,omitempty"`
	PresencePenalty     float64        `json:"presence_penalty,omitempty"`
	MaxCompletionTokens int            `json:"max_completion_tokens,omitempty"`
	Stream              bool           `json:"stream,omitempty"`
	StreamOptions       *StreamOptions `json:"stream_options,omitempty"`
	ResponseFormat      interface{}    `json:"response_format,omitempty"`
}

ChatCompletionRequest represents a request to the chat completions API

type ChatCompletionResponse

type ChatCompletionResponse struct {
	ID      string                 `json:"id"`
	Object  string                 `json:"object"`
	Created int64                  `json:"created"`
	Model   string                 `json:"model"`
	Choices []ChatCompletionChoice `json:"choices"`
	Usage   ChatCompletionUsage    `json:"usage"`
}

ChatCompletionResponse represents a response from the chat completions API

type ChatCompletionUsage

type ChatCompletionUsage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

ChatCompletionUsage represents usage information in a chat completion response

type ChatMessage

type ChatMessage struct {
	Role       string                `json:"role"`
	Content    interface{}           `json:"content"`
	Name       string                `json:"name,omitempty"`
	ToolCalls  []ChatMessageToolCall `json:"tool_calls,omitempty"`
	ToolCallID string                `json:"tool_call_id,omitempty"`
}

ChatMessage represents a message in a chat. Content is interface{} to support both plain strings and multimodal content arrays.

type ChatMessageToolCall

type ChatMessageToolCall struct {
	ID       string                      `json:"id"`
	Type     string                      `json:"type"`
	Function ChatMessageToolCallFunction `json:"function"`
}

ChatMessageToolCall represents a tool call in a chat message

type ChatMessageToolCallFunction

type ChatMessageToolCallFunction struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"`
}

ChatMessageToolCallFunction represents a function in a tool call

type ChatTool

type ChatTool struct {
	Type     string           `json:"type"`
	Function ChatToolFunction `json:"function"`
}

ChatTool represents a tool in a chat

type ChatToolFunction

type ChatToolFunction struct {
	Name        string                 `json:"name"`
	Description string                 `json:"description"`
	Parameters  map[string]interface{} `json:"parameters"`
}

ChatToolFunction represents a function in a tool

type ErrorResponse

type ErrorResponse struct {
	Error struct {
		Message string `json:"message"`
		Type    string `json:"type"`
		Param   string `json:"param"`
		Code    string `json:"code"`
	} `json:"error"`
}

ErrorResponse represents an error response from the API

type Model

type Model struct {
	// Configuration
	ModelName string
	Provider  *Provider
}

Model implements the model.Model interface for OpenAI

func (*Model) GetResponse

func (m *Model) GetResponse(ctx context.Context, request *model.Request) (*model.Response, error)

GetResponse gets a single response from the model with retry logic

func (*Model) StreamResponse

func (m *Model) StreamResponse(ctx context.Context, request *model.Request) (<-chan model.StreamEvent, error)

StreamResponse streams a response from the model with retry logic

type Provider

type Provider struct {
	// Configuration
	APIKey       string
	Organization string
	HTTPClient   *http.Client

	// Model configuration
	DefaultModel string

	// Rate limiting configuration
	RPM        int           // Requests per minute
	TPM        int           // Tokens per minute
	MaxRetries int           // Maximum number of retries
	RetryAfter time.Duration // Time to wait before retrying
	// contains filtered or unexported fields
}

Provider implements model.Provider for OpenAI

func NewOpenAIProvider

func NewOpenAIProvider(apiKey string) *Provider

NewOpenAIProvider creates a new Provider with default settings

func NewProvider

func NewProvider(apiKey string) *Provider

NewProvider creates a new provider with default settings, requires an API key

func (*Provider) GetModel

func (p *Provider) GetModel(name string) (model.Model, error)

GetModel returns a model by name

func (*Provider) ResetRateLimiter

func (p *Provider) ResetRateLimiter()

ResetRateLimiter resets the rate limit counters

func (*Provider) SetAPIType

func (p *Provider) SetAPIType(apiType APIType) *Provider

SetAPIType sets the api type for the provider

func (*Provider) SetAPIVersion

func (p *Provider) SetAPIVersion(apiVersion string) *Provider

SetAPIType sets the api version for the provider

func (*Provider) SetBaseURL

func (p *Provider) SetBaseURL(baseURL string) *Provider

SetBaseURL sets the base URL for the provider

func (*Provider) SetDefaultModel

func (p *Provider) SetDefaultModel(modelName string) *Provider

SetDefaultModel sets the default model for the provider

func (*Provider) UpdateTokenCount

func (p *Provider) UpdateTokenCount(tokens int)

UpdateTokenCount updates the token count for rate limiting

func (*Provider) WaitForRateLimit

func (p *Provider) WaitForRateLimit()

WaitForRateLimit waits for the rate limiter to allow a new request

func (*Provider) WithAPIKey

func (p *Provider) WithAPIKey(apiKey string) *Provider

WithAPIKey sets the API key for the provider

func (*Provider) WithDefaultModel

func (p *Provider) WithDefaultModel(modelName string) *Provider

WithDefaultModel sets the default model for the provider

func (*Provider) WithHTTPClient

func (p *Provider) WithHTTPClient(client *http.Client) *Provider

WithHTTPClient sets the HTTP client for the provider

func (*Provider) WithOrganization

func (p *Provider) WithOrganization(org string) *Provider

WithOrganization sets the organization for the provider

func (*Provider) WithRateLimit

func (p *Provider) WithRateLimit(rpm, tpm int) *Provider

WithRateLimit sets the rate limit configuration for the provider

func (*Provider) WithRetryConfig

func (p *Provider) WithRetryConfig(maxRetries int, retryAfter time.Duration) *Provider

WithRetryConfig sets the retry configuration for the provider

type StreamOptions added in v0.16.0

type StreamOptions struct {
	IncludeUsage bool `json:"include_usage"`
}

StreamOptions configures streaming behavior.

Jump to

Keyboard shortcuts

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