|
| 1 | +// Package openai contains the followings are the OpenAI API schema definitions. |
| 2 | +// Note that we intentionally do not use the code generation tools like OpenAPI Generator not only to keep the code simple |
| 3 | +// but also because the OpenAI's OpenAPI definition is not compliant with the spec and the existing tools do not work well. |
| 4 | +package openai |
| 5 | + |
| 6 | +import ( |
| 7 | + "encoding/json" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +// ChatCompletionRequest represents a request to /v1/chat/completions. |
| 12 | +// https://platform.openai.com/docs/api-reference/chat/create |
| 13 | +type ChatCompletionRequest struct { |
| 14 | + // Model is described in the OpenAI API documentation: |
| 15 | + // https://platform.openai.com/docs/api-reference/chat/create#chat-create-model |
| 16 | + Model string `json:"model"` |
| 17 | + |
| 18 | + // Messages is described in the OpenAI API documentation: |
| 19 | + // https://platform.openai.com/docs/api-reference/chat/create#chat-create-messages |
| 20 | + Messages []ChatCompletionRequestMessage `json:"messages"` |
| 21 | + |
| 22 | + // Stream is described in the OpenAI API documentation: |
| 23 | + // https://platform.openai.com/docs/api-reference/chat/create#chat-create-stream |
| 24 | + Stream bool `json:"stream,omitempty"` |
| 25 | +} |
| 26 | + |
| 27 | +// ChatCompletionRequestMessage represents a message in a ChatCompletionRequest. |
| 28 | +// https://platform.openai.com/docs/api-reference/chat/create#chat-create-messages |
| 29 | +type ChatCompletionRequestMessage struct { |
| 30 | + // Role is the role of the message. The role of the message (whether it represents the user or the AI). |
| 31 | + Role string `json:"role,omitempty"` |
| 32 | + // Content is the content of the message. Mainly this is a string, but it can be more complex. |
| 33 | + Content any `json:"content,omitempty"` |
| 34 | +} |
| 35 | + |
| 36 | +// ChatCompletionResponse represents a response from /v1/chat/completions. |
| 37 | +// https://platform.openai.com/docs/api-reference/chat/object |
| 38 | +type ChatCompletionResponse struct { |
| 39 | + // Choices are described in the OpenAI API documentation: |
| 40 | + // https://platform.openai.com/docs/api-reference/chat/object#chat/object-choices |
| 41 | + Choices []ChatCompletionResponseChoice `json:"choices,omitempty"` |
| 42 | + |
| 43 | + // Object is always "chat.completion" for completions. |
| 44 | + // https://platform.openai.com/docs/api-reference/chat/object#chat/object-object |
| 45 | + Object string `json:"object,omitempty"` |
| 46 | + |
| 47 | + // Usage is described in the OpenAI API documentation: |
| 48 | + // https://platform.openai.com/docs/api-reference/chat/object#chat/object-usage |
| 49 | + Usage ChatCompletionResponseUsage `json:"usage,omitempty"` |
| 50 | +} |
| 51 | + |
| 52 | +// ChatCompletionResponseChoice is described in the OpenAI API documentation: |
| 53 | +// https://platform.openai.com/docs/api-reference/chat/object#chat/object-choices |
| 54 | +type ChatCompletionResponseChoice struct { |
| 55 | + // Message is described in the OpenAI API documentation: |
| 56 | + // https://platform.openai.com/docs/api-reference/chat/object#chat/object-choices |
| 57 | + Message ChatCompletionResponseChoiceMessage `json:"message,omitempty"` |
| 58 | +} |
| 59 | + |
| 60 | +// ChatCompletionResponseChoiceMessage is described in the OpenAI API documentation: |
| 61 | +// https://platform.openai.com/docs/api-reference/chat/object#chat/object-choices |
| 62 | +type ChatCompletionResponseChoiceMessage struct { |
| 63 | + Content *string `json:"content,omitempty"` |
| 64 | + Role string `json:"role,omitempty"` |
| 65 | +} |
| 66 | + |
| 67 | +// ChatCompletionResponseUsage is described in the OpenAI API documentation: |
| 68 | +// https://platform.openai.com/docs/api-reference/chat/object#chat/object-usage |
| 69 | +type ChatCompletionResponseUsage struct { |
| 70 | + CompletionTokens int `json:"completion_tokens,omitempty"` |
| 71 | + PromptTokens int `json:"prompt_tokens,omitempty"` |
| 72 | + TotalTokens int `json:"total_tokens,omitempty"` |
| 73 | +} |
| 74 | + |
| 75 | +// ChatCompletionResponseChunk is described in the OpenAI API documentation: |
| 76 | +// https://platform.openai.com/docs/api-reference/chat/streaming#chat-create-messages |
| 77 | +type ChatCompletionResponseChunk struct { |
| 78 | + // Choices are described in the OpenAI API documentation: |
| 79 | + // https://platform.openai.com/docs/api-reference/chat/streaming#chat/streaming-choices |
| 80 | + Choices []ChatCompletionResponseChunkChoice `json:"choices,omitempty"` |
| 81 | + |
| 82 | + // Object is always "chat.completion.chunk" for completions. |
| 83 | + // https://platform.openai.com/docs/api-reference/chat/streaming#chat/streaming-object |
| 84 | + Object string `json:"object,omitempty"` |
| 85 | + |
| 86 | + // Usage is described in the OpenAI API documentation: |
| 87 | + // https://platform.openai.com/docs/api-reference/chat/streaming#chat/streaming-usage |
| 88 | + Usage *ChatCompletionResponseUsage `json:"usage,omitempty"` |
| 89 | +} |
| 90 | + |
| 91 | +// String implements fmt.Stringer. |
| 92 | +func (c *ChatCompletionResponseChunk) String() string { |
| 93 | + buf, _ := json.Marshal(c) |
| 94 | + return strings.ReplaceAll(string(buf), ",", ", ") |
| 95 | +} |
| 96 | + |
| 97 | +// ChatCompletionResponseChunkChoice is described in the OpenAI API documentation: |
| 98 | +// https://platform.openai.com/docs/api-reference/chat/streaming#chat/streaming-choices |
| 99 | +type ChatCompletionResponseChunkChoice struct { |
| 100 | + Delta *ChatCompletionResponseChunkChoiceDelta `json:"delta,omitempty"` |
| 101 | +} |
| 102 | + |
| 103 | +// ChatCompletionResponseChunkChoiceDelta is described in the OpenAI API documentation: |
| 104 | +// https://platform.openai.com/docs/api-reference/chat/streaming#chat/streaming-choices |
| 105 | +type ChatCompletionResponseChunkChoiceDelta struct { |
| 106 | + Content *string `json:"content,omitempty"` |
| 107 | + Role *string `json:"role,omitempty"` |
| 108 | +} |
0 commit comments