openai

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Copyright 2026 Teradata

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const (
	// DefaultOpenAIModel uses GPT-4.1 (latest general-purpose model as of 2025)
	DefaultOpenAIModel       = "gpt-4.1"
	DefaultOpenAIEndpoint    = "https://api.openai.com/v1/chat/completions"
	DefaultOpenAITimeout     = 60 * time.Second
	DefaultOpenAIMaxTokens   = 4096
	DefaultOpenAITemperature = 1.0
)

Default OpenAI configuration values. Can be overridden via environment variables:

  • OPENAI_DEFAULT_MODEL / LOOM_LLM_OPENAI_MODEL
  • OPENAI_API_ENDPOINT / LOOM_LLM_OPENAI_ENDPOINT

Variables

This section is empty.

Functions

This section is empty.

Types

type ChatCompletionChoice

type ChatCompletionChoice struct {
	Index        int         `json:"index"`
	Message      ChatMessage `json:"message"`
	FinishReason string      `json:"finish_reason"` // "stop", "length", "tool_calls", "content_filter", "function_call"
}

ChatCompletionChoice represents a completion choice.

type ChatCompletionRequest

type ChatCompletionRequest struct {
	Model               string                 `json:"model"`
	Messages            []ChatMessage          `json:"messages"`
	Temperature         float64                `json:"temperature,omitempty"`
	MaxTokens           int                    `json:"max_tokens,omitempty"`
	MaxCompletionTokens int                    `json:"max_completion_tokens,omitempty"` // Azure OpenAI newer models
	TopP                float64                `json:"top_p,omitempty"`
	FrequencyPenalty    float64                `json:"frequency_penalty,omitempty"`
	PresencePenalty     float64                `json:"presence_penalty,omitempty"`
	Tools               []Tool                 `json:"tools,omitempty"`
	ToolChoice          interface{}            `json:"tool_choice,omitempty"` // "auto", "none", or {"type": "function", "function": {"name": "..."}}
	Stream              bool                   `json:"stream,omitempty"`
	User                string                 `json:"user,omitempty"`
	ResponseFormat      map[string]interface{} `json:"response_format,omitempty"`
}

ChatCompletionRequest represents a request to the OpenAI 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"`
	Error   *OpenAIError           `json:"error,omitempty"`
}

ChatCompletionResponse represents the response from OpenAI.

type ChatCompletionStreamChoice

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

ChatCompletionStreamChoice represents a choice in a streaming chunk.

type ChatCompletionStreamChunk

type ChatCompletionStreamChunk struct {
	ID      string                       `json:"id"`
	Object  string                       `json:"object"` // "chat.completion.chunk"
	Created int64                        `json:"created"`
	Model   string                       `json:"model"`
	Choices []ChatCompletionStreamChoice `json:"choices"`
	Usage   *ChatCompletionUsage         `json:"usage,omitempty"` // Only present in final chunk
}

ChatCompletionStreamChunk represents a chunk in a streaming response.

type ChatCompletionUsage

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

ChatCompletionUsage represents token usage information.

type ChatMessage

type ChatMessage struct {
	Role       string      `json:"role"` // "system", "user", "assistant", "tool"
	Content    interface{} `json:"content,omitempty"`
	Name       string      `json:"name,omitempty"`
	ToolCalls  []ToolCall  `json:"tool_calls,omitempty"`
	ToolCallID string      `json:"tool_call_id,omitempty"` // For tool role messages
}

ChatMessage represents a message in the conversation.

type ChatMessageDelta

type ChatMessageDelta struct {
	Role      string          `json:"role,omitempty"`
	Content   interface{}     `json:"content,omitempty"` // string or null
	ToolCalls []ToolCallDelta `json:"tool_calls,omitempty"`
}

ChatMessageDelta represents a delta in a streaming message.

type Client

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

Client implements the LLMProvider interface for OpenAI's API.

func NewClient

func NewClient(config Config) *Client

NewClient creates a new OpenAI client.

func (*Client) Chat

func (c *Client) Chat(ctx context.Context, messages []llmtypes.Message, tools []shuttle.Tool) (*llmtypes.LLMResponse, error)

Chat sends a conversation to OpenAI and returns the response.

func (*Client) ChatStream

func (c *Client) ChatStream(ctx context.Context, messages []llmtypes.Message,
	tools []shuttle.Tool, tokenCallback llmtypes.TokenCallback) (*llmtypes.LLMResponse, error)

ChatStream implements token-by-token streaming for OpenAI. This method uses OpenAI's Chat Completions API with stream=true to stream tokens as they are generated. The tokenCallback is called for each token received.

func (*Client) Model

func (c *Client) Model() string

Model returns the model identifier.

func (*Client) Name

func (c *Client) Name() string

Name returns the provider name.

type Config

type Config struct {
	APIKey            string
	Model             string        // Default: gpt-4o
	Endpoint          string        // Default: https://api.openai.com/v1/chat/completions
	Timeout           time.Duration // Default: 60s
	MaxTokens         int           // Default: 4096
	Temperature       float64       // Default: 1.0
	RateLimiterConfig llm.RateLimiterConfig
}

Config holds configuration for the OpenAI client.

type FunctionCall

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

FunctionCall represents the function being called.

type FunctionCallDelta

type FunctionCallDelta struct {
	Name      string `json:"name,omitempty"`
	Arguments string `json:"arguments,omitempty"` // Partial JSON string
}

FunctionCallDelta represents a delta in a function call.

type FunctionDef

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

FunctionDef defines a function available to the model.

type OpenAIError

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

OpenAIError represents an error from the OpenAI API.

type Tool

type Tool struct {
	Type     string      `json:"type"` // Always "function"
	Function FunctionDef `json:"function"`
}

Tool represents a function that the model can call.

type ToolCall

type ToolCall struct {
	ID       string       `json:"id"`
	Type     string       `json:"type"` // Always "function"
	Function FunctionCall `json:"function"`
}

ToolCall represents a function call from the assistant.

type ToolCallDelta

type ToolCallDelta struct {
	Index    int               `json:"index"`
	ID       string            `json:"id,omitempty"`
	Type     string            `json:"type,omitempty"` // "function"
	Function FunctionCallDelta `json:"function,omitempty"`
}

ToolCallDelta represents a delta in a tool call.

Jump to

Keyboard shortcuts

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