openai

package
v1.0.21 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2025 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package openai provides integration with OpenAI's GPT models including chat completions, streaming, tool calling, and authentication support.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OpenAIChoice

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

OpenAIChoice represents a choice in the OpenAI API response

type OpenAIContentPart added in v1.0.13

type OpenAIContentPart struct {
	Type     string          `json:"type"`                // "text" or "image_url"
	Text     string          `json:"text,omitempty"`      // Text content
	ImageURL *OpenAIImageURL `json:"image_url,omitempty"` // Image URL content
}

OpenAIContentPart represents a content part in OpenAI's multimodal format

type OpenAIDelta

type OpenAIDelta struct {
	Role             string           `json:"role,omitempty"`
	Content          string           `json:"content,omitempty"`
	Reasoning        string           `json:"reasoning,omitempty"`         // Reasoning content (GLM-4.6, OpenCode/Zen)
	ReasoningContent string           `json:"reasoning_content,omitempty"` // Alternative reasoning field (vLLM/Synthetic)
	ToolCalls        []OpenAIToolCall `json:"tool_calls,omitempty"`
}

OpenAIDelta represents the delta content in a streaming response

type OpenAIError

type OpenAIError struct {
	Message string `json:"message"`
	Type    string `json:"type"`
	Code    string `json:"code"`
}

OpenAIError represents an error in the OpenAI response

type OpenAIErrorResponse

type OpenAIErrorResponse struct {
	Error OpenAIError `json:"error"`
}

OpenAIErrorResponse represents an error response from OpenAI

type OpenAIExtension

type OpenAIExtension struct {
	*types.BaseExtension
}

OpenAIExtension implements CoreProviderExtension for OpenAI

func NewOpenAIExtension

func NewOpenAIExtension() *OpenAIExtension

NewOpenAIExtension creates a new OpenAI extension

func (*OpenAIExtension) ProviderToStandard

func (e *OpenAIExtension) ProviderToStandard(response interface{}) (*types.StandardResponse, error)

ProviderToStandard converts an OpenAI response to standard format

func (*OpenAIExtension) ProviderToStandardChunk

func (e *OpenAIExtension) ProviderToStandardChunk(chunk interface{}) (*types.StandardStreamChunk, error)

ProviderToStandardChunk converts an OpenAI stream chunk to standard format

func (*OpenAIExtension) StandardToProvider

func (e *OpenAIExtension) StandardToProvider(request types.StandardRequest) (interface{}, error)

StandardToProvider converts a standard request to OpenAI format

func (*OpenAIExtension) ValidateOptions

func (e *OpenAIExtension) ValidateOptions(options map[string]interface{}) error

ValidateOptions validates OpenAI-specific options

type OpenAIFunctionDef

type OpenAIFunctionDef struct {
	Name        string                 `json:"name"`
	Description string                 `json:"description"`
	Parameters  map[string]interface{} `json:"parameters"` // JSON Schema
}

OpenAIFunctionDef represents a function definition in the OpenAI API

type OpenAIImageURL added in v1.0.13

type OpenAIImageURL struct {
	URL    string `json:"url"`              // URL or data URL
	Detail string `json:"detail,omitempty"` // "auto", "low", "high"
}

OpenAIImageURL represents an image URL in OpenAI format

type OpenAIMessage

type OpenAIMessage struct {
	Role             string           `json:"role"`
	Content          interface{}      `json:"content"`                     // string or []OpenAIContentPart
	Reasoning        string           `json:"reasoning,omitempty"`         // Reasoning content (GLM-4.6, OpenCode/Zen)
	ReasoningContent string           `json:"reasoning_content,omitempty"` // Alternative reasoning field (vLLM/Synthetic)
	ToolCalls        []OpenAIToolCall `json:"tool_calls,omitempty"`
	ToolCallID       string           `json:"tool_call_id,omitempty"`
}

OpenAIMessage represents a message in the OpenAI API

type OpenAIModel

type OpenAIModel struct {
	ID      string `json:"id"`
	Object  string `json:"object"`
	Created int64  `json:"created"`
	OwnedBy string `json:"owned_by"`
}

OpenAIModel represents a model in the OpenAI API

type OpenAIModelsResponse

type OpenAIModelsResponse struct {
	Object string        `json:"object"`
	Data   []OpenAIModel `json:"data"`
}

OpenAIModelsResponse represents the response from the models API

type OpenAIProvider

type OpenAIProvider struct {
	*base.BaseProvider
	// contains filtered or unexported fields
}

OpenAIProvider implements the Provider interface for OpenAI

func NewOpenAIProvider

func NewOpenAIProvider(config types.ProviderConfig) *OpenAIProvider

NewOpenAIProvider creates a new OpenAI provider

func (*OpenAIProvider) Authenticate

func (p *OpenAIProvider) Authenticate(ctx context.Context, authConfig types.AuthConfig) error

func (*OpenAIProvider) Configure

func (p *OpenAIProvider) Configure(config types.ProviderConfig) error

func (*OpenAIProvider) Description

func (p *OpenAIProvider) Description() string

func (*OpenAIProvider) GenerateChatCompletion

func (p *OpenAIProvider) GenerateChatCompletion(
	ctx context.Context,
	options types.GenerateOptions,
) (types.ChatCompletionStream, error)

GenerateChatCompletion generates a chat completion

func (*OpenAIProvider) GetAuthStatus

func (p *OpenAIProvider) GetAuthStatus() map[string]interface{}

GetAuthStatus provides detailed authentication status using shared helper

func (*OpenAIProvider) GetDefaultModel

func (p *OpenAIProvider) GetDefaultModel() string

func (*OpenAIProvider) GetModels

func (p *OpenAIProvider) GetModels(ctx context.Context) ([]types.Model, error)

func (*OpenAIProvider) GetToolFormat

func (p *OpenAIProvider) GetToolFormat() types.ToolFormat

func (*OpenAIProvider) InvokeServerTool

func (p *OpenAIProvider) InvokeServerTool(
	ctx context.Context,
	toolName string,
	params interface{},
) (interface{}, error)

InvokeServerTool invokes a server tool (not yet implemented)

func (*OpenAIProvider) IsAuthenticated

func (p *OpenAIProvider) IsAuthenticated() bool

func (*OpenAIProvider) Logout

func (p *OpenAIProvider) Logout(ctx context.Context) error

Logout clears the API keys and resets configuration

func (*OpenAIProvider) Name

func (p *OpenAIProvider) Name() string

func (*OpenAIProvider) SupportsResponsesAPI

func (p *OpenAIProvider) SupportsResponsesAPI() bool

func (*OpenAIProvider) SupportsStreaming

func (p *OpenAIProvider) SupportsStreaming() bool

func (*OpenAIProvider) SupportsToolCalling

func (p *OpenAIProvider) SupportsToolCalling() bool

func (*OpenAIProvider) TestConnectivity added in v1.0.14

func (p *OpenAIProvider) TestConnectivity(ctx context.Context) error

TestConnectivity performs a lightweight connectivity test using the /v1/models endpoint Results are cached for 30 seconds by default to prevent hammering the API during rapid health checks To bypass the cache and force a fresh check, use TestConnectivityWithOptions with bypassCache=true

func (*OpenAIProvider) TestConnectivityWithOptions added in v1.0.17

func (p *OpenAIProvider) TestConnectivityWithOptions(ctx context.Context, bypassCache bool) error

TestConnectivityWithOptions performs a connectivity test with optional cache bypass If bypassCache is true, the cache is bypassed and a fresh connectivity check is performed

func (*OpenAIProvider) Type

func (p *OpenAIProvider) Type() types.ProviderType

type OpenAIRequest

type OpenAIRequest struct {
	Model             string                 `json:"model"`
	Messages          []OpenAIMessage        `json:"messages"`
	MaxTokens         int                    `json:"max_tokens,omitempty"`
	Temperature       float64                `json:"temperature,omitempty"`
	Stream            bool                   `json:"stream,omitempty"`
	TopP              float64                `json:"top_p,omitempty"`
	Tools             []OpenAITool           `json:"tools,omitempty"`
	ToolChoice        interface{}            `json:"tool_choice,omitempty"`
	Stop              []string               `json:"stop,omitempty"`
	Seed              *int                   `json:"seed,omitempty"`
	ResponseFormat    map[string]interface{} `json:"response_format,omitempty"`
	ParallelToolCalls *bool                  `json:"parallel_tool_calls,omitempty"`
}

OpenAIRequest represents a request to the OpenAI chat completions API

type OpenAIResponse

type OpenAIResponse struct {
	ID                string         `json:"id"`
	Object            string         `json:"object"`
	Created           int64          `json:"created"`
	Model             string         `json:"model"`
	Choices           []OpenAIChoice `json:"choices"`
	Usage             OpenAIUsage    `json:"usage"`
	SystemFingerprint string         `json:"system_fingerprint,omitempty"`
}

OpenAIResponse represents a response from the OpenAI API

type OpenAIStreamChoice

type OpenAIStreamChoice struct {
	Index        int         `json:"index"`
	Delta        OpenAIDelta `json:"delta"`
	FinishReason string      `json:"finish_reason,omitempty"`
}

OpenAIStreamChoice represents a choice in the streaming response

type OpenAIStreamResponse

type OpenAIStreamResponse struct {
	ID                string               `json:"id"`
	Object            string               `json:"object"`
	Created           int64                `json:"created"`
	Model             string               `json:"model"`
	Choices           []OpenAIStreamChoice `json:"choices"`
	Usage             *OpenAIUsage         `json:"usage,omitempty"`
	SystemFingerprint string               `json:"system_fingerprint,omitempty"`
}

OpenAIStreamResponse represents a streaming response chunk

type OpenAITool

type OpenAITool struct {
	Type     string            `json:"type"` // Always "function"
	Function OpenAIFunctionDef `json:"function"`
}

OpenAITool represents a tool in the OpenAI API

type OpenAIToolCall

type OpenAIToolCall struct {
	ID       string                 `json:"id"`
	Type     string                 `json:"type"` // "function"
	Function OpenAIToolCallFunction `json:"function"`
}

OpenAIToolCall represents a tool call in the OpenAI API

type OpenAIToolCallFunction

type OpenAIToolCallFunction struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"` // JSON string
}

OpenAIToolCallFunction represents a function call in a tool call

type OpenAIUsage

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

OpenAIUsage represents token usage information from OpenAI

Jump to

Keyboard shortcuts

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