openai

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

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"`
}

type ChatCompletionError

type ChatCompletionError struct {
	Message    string          `json:"message"`
	StatusCode int             `json:"status_code,omitempty"`
	RawBody    json.RawMessage `json:"raw_body,omitempty"`
}

ChatCompletionError wraps standard errors with raw response body for error logging

func (*ChatCompletionError) Error

func (e *ChatCompletionError) Error() string

func (*ChatCompletionError) GetRawResponseBody

func (e *ChatCompletionError) GetRawResponseBody() json.RawMessage

GetRawResponseBody returns the raw response body if available

type ChatCompletionRequest

type ChatCompletionRequest struct {
	Model               string          `json:"model"`
	User                string          `json:"user,omitempty"`
	Messages            []ChatMessage   `json:"messages"`
	MaxCompletionTokens int             `json:"max_completion_tokens,omitempty"`
	Temperature         float32         `json:"temperature,omitempty"`
	ToolChoice          any             `json:"tool_choice,omitempty"`
	PresencePenalty     float32         `json:"presence_penalty,omitempty"`
	FrequencyPenalty    float32         `json:"frequency_penalty,omitempty"`
	ResponseFormat      *ResponseFormat `json:"response_format,omitempty"`
	ReasoningEffort     ReasoningEffort `json:"reasoning_effort,omitempty"`
	Stream              bool            `json:"stream,omitempty"`
}

ChatCompletionRequest is the request body for the chat completion endpoint

type ChatCompletionResponse

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

The response from the chat completion endpoint

type ChatCompletionResponseError

type ChatCompletionResponseError struct {
	Error ChatError `json:"error"`
}

type ChatCompletionUsage

type ChatCompletionUsage struct {
	PromptTokens        int                 `json:"prompt_tokens"`
	CompletionTokens    int                 `json:"completion_tokens"`
	TotalTokens         int                 `json:"total_tokens"`
	PromptTokensDetails *PromptTokenDetails `json:"prompt_tokens_details,omitempty"`
}

type ChatError

type ChatError struct {
	Code             string `json:"code"`
	Message          string `json:"message"`
	Type             string `json:"type"`
	FailedGeneration string `json:"failed_generation,omitempty"`
}

type ChatMessage

type ChatMessage struct {
	Role       MessageRole `json:"role"`
	Content    *string     `json:"content,omitempty"`
	Reasoning  *string     `json:"reasoning,omitempty"`
	ToolCallID *string     `json:"tool_call_id,omitempty"`
}

type LanguageModelClient

type LanguageModelClient interface {
	ChatCompletion(ctx context.Context, req ChatCompletionRequest) (*ChatCompletionResponse, error)
	SetBaseURL(baseUrl string)
}

type MessageRole

type MessageRole string
const (
	MessageRoleUser      MessageRole = "user"
	MessageRoleAssistant MessageRole = "assistant"
	MessageRoleTool      MessageRole = "tool"
	MessageRoleSystem    MessageRole = "system"
)

type OpenAIClient

type OpenAIClient struct {
	APIKey       string
	Env          string
	DumpRequests bool
	BaseURL      string
	HTTPClient   *http.Client
	RetryConfig  retry.Config
}

OpenAIClient is a minimal client for the OpenAI Chat API

func NewClient

func NewClient(apiKey string) *OpenAIClient

Creates a new OpenAIClient

func (*OpenAIClient) ChatCompletion

Sends a chat completion request to OpenAI with retry logic

func (*OpenAIClient) SetBaseURL

func (c *OpenAIClient) SetBaseURL(baseUrl string)

Sets the base URL for the OpenAI client

type PromptTokenDetails

type PromptTokenDetails struct {
	CachedTokens int `json:"cached_tokens"`
}

type ReasoningEffort

type ReasoningEffort string
const (
	ReasoningEffortLow    ReasoningEffort = "low"
	ReasoningEffortMedium ReasoningEffort = "medium"
	ReasoningEffortHigh   ReasoningEffort = "high"
)

type ResponseFormat

type ResponseFormat struct {
	Type       string         `json:"type,omitempty"`
	JsonSchema map[string]any `json:"json_schema,omitempty"`
}

Jump to

Keyboard shortcuts

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