openai

package
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultTimeOut     = 30 * time.Second
	DefaultMaxRetries  = 3
	DefaultRetryDelay  = 1 * time.Second
	DefaultTemperature = 0.1
	DefaultMaxTokens   = 10000
)
View Source
const (
	ChatMessageRoleSystem    = "system"
	ChatMessageRoleUser      = "user"
	ChatMessageRoleAssistant = "assistant"
)

Chat message roles

Variables

This section is empty.

Functions

This section is empty.

Types

type ChatCompletionMessage

type ChatCompletionMessage struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

ChatCompletionMessage represents a single message in conversation

type ChatCompletionRequest

type ChatCompletionRequest struct {
	Model       string                  `json:"model"`
	Messages    []ChatCompletionMessage `json:"messages"`
	Stream      bool                    `json:"stream,omitempty"`
	Temperature float64                 `json:"temperature,omitempty"` // Controls randomness (0-2)
	MaxTokens   int                     `json:"max_tokens,omitempty"`  // Maximum tokens to generate
}

ChatCompletionRequest defines chat completion parameters

type ChatCompletionResponse

type ChatCompletionResponse struct {
	ID      string `json:"id"`
	Object  string `json:"object"`
	Created int64  `json:"created"`
	Model   string `json:"model"`
	Choices []struct {
		Index   int                   `json:"index"`
		Message ChatCompletionMessage `json:"message"`
	} `json:"choices"`
	Usage struct {
		PromptTokens     int `json:"prompt_tokens"`
		CompletionTokens int `json:"completion_tokens"`
		TotalTokens      int `json:"total_tokens"`
	} `json:"usage,omitempty"`
}

ChatCompletionResponse contains full API response

type ChatCompletionStreamResponse

type ChatCompletionStreamResponse struct {
	ID      string `json:"id"`
	Object  string `json:"object"`
	Created int64  `json:"created"`
	Model   string `json:"model"`
	Choices []struct {
		Index        int                   `json:"index"`
		Delta        ChatCompletionMessage `json:"delta"`
		FinishReason *string               `json:"finish_reason,omitempty"`
	} `json:"choices"`
}

ChatCompletionStreamResponse contains streaming chunk

type Client

type Client interface {
	CreateChatCompletion(ctx context.Context, req ChatCompletionRequest) (ChatCompletionResponse, error)
	CreateChatCompletionStream(ctx context.Context, req ChatCompletionRequest) (Stream, error)
}

Client interface defines the contract for chat completion service

func NewClient

func NewClient(endPoint, apiKey string, opts ...ClientOption) Client

NewClient creates a new client with options

type ClientOption

type ClientOption func(*openAIClient)

ClientOption configures the client

func WithRetryPolicy

func WithRetryPolicy(maxRetries int, retryDelay time.Duration) ClientOption

WithRetryPolicy configures retry behavior

func WithTimeout

func WithTimeout(timeout time.Duration) ClientOption

WithTimeout sets request timeout

type Stream

type Stream interface {
	Recv() (ChatCompletionStreamResponse, error)
	Close() error
}

Stream handles streaming responses

func NewStreamReader

func NewStreamReader(resp *http.Response, reader *bufio.Reader, closed bool, logger *logger.Logger) Stream

Jump to

Keyboard shortcuts

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