File size: 5,294 Bytes
562a3ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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"
}