Documentation
¶
Index ¶
- Variables
- func Float64Ptr(f float64) *float64
- func Int64Ptr(i int64) *int64
- func StringPtr(s string) *string
- type AudioRequest
- type AudioResponse
- type ChatRequest
- type ChatResponse
- type Config
- type Message
- type Service
- func (s *Service) ChatCompletion(ctx context.Context, req ChatRequest) (*ChatResponse, error)
- func (s *Service) ChatCompletionStream(ctx context.Context, req ChatRequest, handler StreamHandler) (*ChatResponse, error)
- func (s *Service) TranscribeAudio(ctx context.Context, req AudioRequest) (*AudioResponse, error)
- func (s *Service) TranslateAudio(ctx context.Context, req AudioRequest) (*AudioResponse, error)
- type StreamHandler
- type TokenUsage
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidAPIKey is returned when the API key is invalid or missing ErrInvalidAPIKey = apperr.NewHTTPError(http.StatusUnauthorized, "INVALID_API_KEY", "Invalid or missing OpenAI API key") // ErrEmptyPrompt is returned when the prompt is empty ErrEmptyPrompt = apperr.NewHTTPError(http.StatusBadRequest, "EMPTY_PROMPT", "Prompt cannot be empty") // ErrUnsupportedFormat is returned when the file format is not supported ErrUnsupportedFormat = apperr.NewHTTPError(http.StatusBadRequest, "UNSUPPORTED_FORMAT", "Unsupported file format") // ErrStreamingFailed is returned when streaming fails ErrStreamingFailed = apperr.NewHTTPError(http.StatusInternalServerError, "STREAMING_FAILED", "Streaming response failed") // ErrNoMessages is returned when no messages are provided ErrNoMessages = apperr.NewHTTPError(http.StatusBadRequest, "NO_MESSAGES", "At least one message is required") // ErrNoFile is returned when no file is provided for audio transcription ErrNoFile = apperr.NewHTTPError(http.StatusBadRequest, "NO_FILE", "Audio file is required") // ErrAPIError is returned when the OpenAI API returns an error ErrAPIError = apperr.NewHTTPError(http.StatusBadGateway, "OPENAI_API_ERROR", "OpenAI API error") )
Custom errors for OpenAI service
Functions ¶
func Float64Ptr ¶
Float64Ptr returns a pointer to the float64 value
Types ¶
type AudioRequest ¶
type AudioRequest struct {
File io.Reader `json:"-"` // Audio file reader
FileName string `json:"file_name"` // File name (required for API)
Language *string `json:"language,omitempty"` // Optional language code (ISO-639-1, e.g., "en")
Prompt *string `json:"prompt,omitempty"` // Optional context to guide transcription
Format *string `json:"response_format,omitempty"` // Optional format (json, text, srt, vtt)
}
AudioRequest represents an audio transcription request
type AudioResponse ¶
type AudioResponse struct {
Text string `json:"text"` // Transcribed text
Language string `json:"language,omitempty"` // Detected language (if available)
Duration *float64 `json:"duration,omitempty"` // Duration in seconds (if available)
}
AudioResponse represents an audio transcription response
type ChatRequest ¶
type ChatRequest struct {
Messages []Message `json:"messages"` // List of messages
Model *string `json:"model,omitempty"` // Optional model override
MaxTokens *int64 `json:"max_tokens,omitempty"` // Optional max tokens override
Temperature *float64 `json:"temperature,omitempty"` // Optional temperature override (0.0 to 2.0)
SystemPrompt *string `json:"system_prompt,omitempty"` // Optional system prompt
TopP *float64 `json:"top_p,omitempty"` // Optional nucleus sampling
Stop []string `json:"stop,omitempty"` // Optional stop sequences
}
ChatRequest represents a chat completion request
type ChatResponse ¶
type ChatResponse struct {
ID string `json:"id"`
Content string `json:"content"`
Model string `json:"model"`
FinishReason string `json:"finish_reason"`
Usage TokenUsage `json:"usage"`
CreatedAt int64 `json:"created_at"`
}
ChatResponse represents a chat completion response
type Config ¶
type Config struct {
APIKey string
BaseURL string
Timeout int
MaxRetries int
// Model defaults per capability
TextModel string // Default model for chat/text completions (e.g. gpt-4o)
AudioModel string // Default model for audio transcription/translation (e.g. whisper-1)
}
Config holds the configuration for the OpenAI service
type Message ¶
type Message struct {
Role string `json:"role"` // system, user, or assistant
Content string `json:"content"` // Text content
ImageURLs []string `json:"image_urls"` // Optional image URLs for vision models
}
Message represents a chat message with optional image URLs
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service represents the OpenAI service
func (*Service) ChatCompletion ¶
func (s *Service) ChatCompletion(ctx context.Context, req ChatRequest) (*ChatResponse, error)
ChatCompletion performs a chat completion request
func (*Service) ChatCompletionStream ¶
func (s *Service) ChatCompletionStream(ctx context.Context, req ChatRequest, handler StreamHandler) (*ChatResponse, error)
ChatCompletionStream performs a streaming chat completion request The handler is called for each chunk, and a complete ChatResponse is returned at the end
func (*Service) TranscribeAudio ¶
func (s *Service) TranscribeAudio(ctx context.Context, req AudioRequest) (*AudioResponse, error)
TranscribeAudio transcribes audio using the Whisper model
func (*Service) TranslateAudio ¶
func (s *Service) TranslateAudio(ctx context.Context, req AudioRequest) (*AudioResponse, error)
TranslateAudio translates audio to English using the Whisper model
type StreamHandler ¶
StreamHandler is a callback function that receives streaming chunks chunk: the text content of the current chunk done: true when streaming is complete
type TokenUsage ¶
type TokenUsage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}
TokenUsage represents token usage statistics