julep2api / models.go
pauper-tarot-chain's picture
Upload 9 files
562a3ac verified
package main
// --- OpenAI Request Structures ---
// OpenAIRequest represents the incoming request body for chat completions.
type OpenAIRequest struct {
Model string `json:"model"`
Messages []OpenAIMessage `json:"messages"`
Stream bool `json:"stream"`
MaxTokens *int `json:"max_tokens,omitempty"` // Pointer for optional field
Temperature *float64 `json:"temperature,omitempty"` // Pointer for optional field
TopP *float64 `json:"top_p,omitempty"` // Pointer for optional field
Stop []string `json:"stop,omitempty"` // Can be string or array, handle accordingly if needed
PresencePenalty *float64 `json:"presence_penalty,omitempty"` // Pointer for optional field
FrequencyPenalty *float64 `json:"frequency_penalty,omitempty"` // Pointer for optional field
Tools []OpenAITool `json:"tools,omitempty"`
ToolChoice any `json:"tool_choice,omitempty"` // Can be string or object
// Add other OpenAI parameters as needed
}
// OpenAIMessage represents a single message in the chat history.
type OpenAIMessage struct {
Role string `json:"role"` // "system", "user", "assistant", "tool"
Content string `json:"content"`
Name *string `json:"name,omitempty"` // For tool role
ToolCalls []OpenAIToolCall `json:"tool_calls,omitempty"` // For assistant message with tool calls
ToolCallID *string `json:"tool_call_id,omitempty"` // For tool role message
}
// OpenAITool represents a tool definition.
type OpenAITool struct {
Type string `json:"type"` // e.g., "function"
Function OpenAIFunctionDef `json:"function"`
}
// OpenAIFunctionDef represents the definition of a function tool.
type OpenAIFunctionDef struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Parameters any `json:"parameters"` // Typically a map[string]any representing JSON Schema
}
// --- OpenAI Response Structures (Non-Streaming) ---
// OpenAIResponse represents the full response for a non-streaming chat completion.
type OpenAIResponse struct {
ID string `json:"id"`
Object string `json:"object"` // "chat.completion"
Created int64 `json:"created"` // Unix timestamp
Model string `json:"model"`
Choices []OpenAIChoice `json:"choices"`
Usage *OpenAIUsage `json:"usage,omitempty"`
}
// OpenAIChoice represents a single choice in the non-streaming response.
type OpenAIChoice struct {
Index int `json:"index"`
Message OpenAIMessage `json:"message"`
FinishReason string `json:"finish_reason"` // "stop", "length", "tool_calls", "content_filter", "function_call" (legacy)
}
// OpenAIToolCall represents a tool call made by the model.
type OpenAIToolCall struct {
ID string `json:"id"`
Type string `json:"type"` // always "function" for now
Function OpenAIFunction `json:"function"`
}
// OpenAIFunction represents the function call details.
type OpenAIFunction struct {
Name string `json:"name"`
Arguments string `json:"arguments"` // JSON string arguments
}
// OpenAIUsage represents token usage statistics.
type OpenAIUsage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}
// --- OpenAI Response Structures (Streaming) ---
// OpenAIChunk represents a single chunk in a streaming chat completion response.
type OpenAIChunk struct {
ID string `json:"id"`
Object string `json:"object"` // "chat.completion.chunk"
Created int64 `json:"created"`
Model string `json:"model"`
Choices []OpenAIChunkChoice `json:"choices"`
}
// OpenAIChunkChoice represents a choice within a streaming chunk.
type OpenAIChunkChoice struct {
Index int `json:"index"`
Delta OpenAIDelta `json:"delta"` // The changes in this chunk
FinishReason *string `json:"finish_reason,omitempty"` // Pointer as it's only in the last chunk for a choice
}
// OpenAIDelta represents the changed fields in a streaming chunk.
// Only one of these fields will typically be populated in a single chunk.
type OpenAIDelta struct {
Role *string `json:"role,omitempty"` // Pointer for optional field
Content *string `json:"content,omitempty"` // Pointer for optional field
ToolCalls []OpenAIToolCall `json:"tool_calls,omitempty"` // Sent as a complete array in one chunk
}
// --- Error Response ---
// ErrorResponse defines the standard JSON error format.
type ErrorResponse struct {
Error APIError `json:"error"`
}
// APIError defines the structure of the error object.
type APIError struct {
Message string `json:"message"`
Type *string `json:"type,omitempty"` // e.g., "invalid_request_error"
Param *string `json:"param,omitempty"` // e.g., "messages"
Code *string `json:"code,omitempty"` // e.g., "missing_field"
}