112 lines
3.1 KiB
Go
112 lines
3.1 KiB
Go
package openai
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// ChatMessage represents a single message within a chat completion request or response.
|
|
type ChatMessage struct {
|
|
Role string `json:"role"`
|
|
Content string `json:"content"`
|
|
}
|
|
|
|
// ChatCompletionRequest encapsulates the payload for the OpenAI Chat Completions API.
|
|
type ChatCompletionRequest struct {
|
|
Model string `json:"model"`
|
|
Messages []ChatMessage `json:"messages"`
|
|
MaxTokens *int `json:"max_tokens,omitempty"`
|
|
Temperature *float64 `json:"temperature,omitempty"`
|
|
Stream bool `json:"stream,omitempty"`
|
|
}
|
|
|
|
// ChatCompletionChoice captures an individual response choice returned from the API.
|
|
type ChatCompletionChoice struct {
|
|
Index int `json:"index"`
|
|
Message ChatMessage `json:"message"`
|
|
FinishReason string `json:"finish_reason"`
|
|
}
|
|
|
|
// Usage captures token accounting for a chat completion call.
|
|
type Usage struct {
|
|
PromptTokens int `json:"prompt_tokens"`
|
|
CompletionTokens int `json:"completion_tokens"`
|
|
TotalTokens int `json:"total_tokens"`
|
|
}
|
|
|
|
// ChatCompletionResponse represents the top-level response payload from the API.
|
|
type ChatCompletionResponse struct {
|
|
ID string `json:"id"`
|
|
Object string `json:"object"`
|
|
Choices []ChatCompletionChoice `json:"choices"`
|
|
Usage Usage `json:"usage"`
|
|
}
|
|
|
|
// ChatCompletionStreamEvent represents a single chunk in a streaming chat completion.
|
|
type ChatCompletionStreamEvent struct {
|
|
ID string `json:"-"`
|
|
Role string `json:"-"`
|
|
Content string `json:"-"`
|
|
FinishReason string `json:"-"`
|
|
Usage *Usage `json:"-"`
|
|
Done bool `json:"-"`
|
|
}
|
|
|
|
// ChatCompletionStreamHandler consumes streaming completion events.
|
|
type ChatCompletionStreamHandler func(ChatCompletionStreamEvent) error
|
|
|
|
// RequestError captures an error response returned by the API together with the HTTP status code.
|
|
type RequestError struct {
|
|
Status int
|
|
Response ErrorResponse
|
|
}
|
|
|
|
// Error implements the error interface.
|
|
func (e *RequestError) Error() string {
|
|
if e == nil {
|
|
return ""
|
|
}
|
|
msg := strings.TrimSpace(e.Response.Error.Message)
|
|
if msg == "" {
|
|
return fmt.Sprintf("api error (status %d)", e.Status)
|
|
}
|
|
return fmt.Sprintf("api error (status %d): %s", e.Status, msg)
|
|
}
|
|
|
|
// StatusCode returns the originating HTTP status code.
|
|
func (e *RequestError) StatusCode() int {
|
|
if e == nil {
|
|
return 0
|
|
}
|
|
return e.Status
|
|
}
|
|
|
|
// Message returns the raw message provided by the API, if any.
|
|
func (e *RequestError) Message() string {
|
|
if e == nil {
|
|
return ""
|
|
}
|
|
return e.Response.Error.Message
|
|
}
|
|
|
|
// Type returns the OpenAI error type string, when present.
|
|
func (e *RequestError) Type() string {
|
|
if e == nil {
|
|
return ""
|
|
}
|
|
return e.Response.Error.Type
|
|
}
|
|
|
|
// APIError captures structured error responses returned by the API.
|
|
type APIError struct {
|
|
Message string `json:"message"`
|
|
Type string `json:"type"`
|
|
Param string `json:"param"`
|
|
Code any `json:"code"`
|
|
}
|
|
|
|
// ErrorResponse is returned on non-2xx responses.
|
|
type ErrorResponse struct {
|
|
Error APIError `json:"error"`
|
|
}
|