Documentation
¶
Overview ¶
openai package provides core transformation logic for partial compatibility with the OpenAI REST API
Index ¶
- func FromChatRequest(r ChatCompletionRequest) (*api.ChatRequest, error)
- func FromCompleteRequest(r CompletionRequest) (api.GenerateRequest, error)
- func FromCompletionToolCall(toolCalls []ToolCall) ([]api.ToolCall, error)
- func FromImageEditRequest(r ImageEditRequest) (api.GenerateRequest, error)
- func FromImageGenerationRequest(r ImageGenerationRequest) api.GenerateRequest
- func FromResponsesRequest(r ResponsesRequest) (*api.ChatRequest, error)
- func FromTranscriptionRequest(r TranscriptionRequest) (*api.ChatRequest, error)
- type ChatCompletion
- type ChatCompletionChunk
- type ChatCompletionRequest
- type Choice
- type ChoiceLogprobs
- type ChunkChoice
- type CompleteChunkChoice
- type Completion
- type CompletionChunk
- type CompletionRequest
- type EmbedRequest
- type Embedding
- type EmbeddingList
- type EmbeddingUsage
- type Error
- type ErrorResponse
- type ImageEditRequest
- type ImageGenerationRequest
- type ImageGenerationResponse
- type ImageURLOrData
- type JsonSchema
- type ListCompletion
- type Message
- type Model
- type Reasoning
- type ResponseFormat
- type ResponsesContent
- type ResponsesError
- type ResponsesFileContent
- type ResponsesFunctionCall
- type ResponsesFunctionCallOutput
- type ResponsesImageContent
- type ResponsesIncompleteDetails
- type ResponsesInput
- type ResponsesInputItem
- type ResponsesInputMessage
- type ResponsesInputTokensDetails
- type ResponsesOutputContent
- type ResponsesOutputItem
- type ResponsesOutputMessage
- type ResponsesOutputTextContent
- type ResponsesOutputTokensDetails
- type ResponsesReasoning
- type ResponsesReasoningInput
- type ResponsesReasoningOutput
- type ResponsesReasoningSummary
- type ResponsesRequest
- type ResponsesResponse
- type ResponsesStreamConverter
- type ResponsesStreamEvent
- type ResponsesText
- type ResponsesTextContent
- type ResponsesTextField
- type ResponsesTextFormat
- type ResponsesTool
- type ResponsesUsage
- type StreamOptions
- type ToolCall
- type TranscriptionRequest
- type TranscriptionResponse
- type Usage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FromChatRequest ¶ added in v0.12.4
func FromChatRequest(r ChatCompletionRequest) (*api.ChatRequest, error)
FromChatRequest converts a ChatCompletionRequest to api.ChatRequest
func FromCompleteRequest ¶ added in v0.12.4
func FromCompleteRequest(r CompletionRequest) (api.GenerateRequest, error)
FromCompleteRequest converts a CompletionRequest to api.GenerateRequest
func FromCompletionToolCall ¶ added in v0.12.6
FromCompletionToolCall converts OpenAI ToolCall format to api.ToolCall
func FromImageEditRequest ¶ added in v0.15.0
func FromImageEditRequest(r ImageEditRequest) (api.GenerateRequest, error)
FromImageEditRequest converts an OpenAI image edit request to an Ollama GenerateRequest.
func FromImageGenerationRequest ¶ added in v0.14.3
func FromImageGenerationRequest(r ImageGenerationRequest) api.GenerateRequest
FromImageGenerationRequest converts an OpenAI image generation request to an Ollama GenerateRequest.
func FromResponsesRequest ¶ added in v0.13.3
func FromResponsesRequest(r ResponsesRequest) (*api.ChatRequest, error)
FromResponsesRequest converts a ResponsesRequest to api.ChatRequest
func FromTranscriptionRequest ¶ added in v0.20.0
func FromTranscriptionRequest(r TranscriptionRequest) (*api.ChatRequest, error)
FromTranscriptionRequest converts a transcription request into a ChatRequest by wrapping the audio with a system prompt for transcription.
Types ¶
type ChatCompletion ¶
type ChatCompletion struct {
Id string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
SystemFingerprint string `json:"system_fingerprint"`
Choices []Choice `json:"choices"`
Usage Usage `json:"usage,omitempty"`
DebugInfo *api.DebugInfo `json:"_debug_info,omitempty"`
}
func ToChatCompletion ¶ added in v0.12.4
func ToChatCompletion(id string, r api.ChatResponse) ChatCompletion
ToChatCompletion converts an api.ChatResponse to ChatCompletion
type ChatCompletionChunk ¶
type ChatCompletionChunk struct {
Id string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
SystemFingerprint string `json:"system_fingerprint"`
Choices []ChunkChoice `json:"choices"`
Usage *Usage `json:"usage,omitempty"`
}
func ToChunk
deprecated
added in
v0.12.4
func ToChunk(id string, r api.ChatResponse, toolCallSent bool) ChatCompletionChunk
Deprecated: use ToChunks for streaming conversion.
func ToChunks ¶ added in v0.18.0
func ToChunks(id string, r api.ChatResponse, toolCallSent bool) []ChatCompletionChunk
ToChunks converts an api.ChatResponse to one or more ChatCompletionChunk values.
type ChatCompletionRequest ¶
type ChatCompletionRequest struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
Stream bool `json:"stream"`
StreamOptions *StreamOptions `json:"stream_options"`
MaxTokens *int `json:"max_tokens"`
Seed *int `json:"seed"`
Stop any `json:"stop"`
Temperature *float64 `json:"temperature"`
FrequencyPenalty *float64 `json:"frequency_penalty"`
PresencePenalty *float64 `json:"presence_penalty"`
TopP *float64 `json:"top_p"`
ResponseFormat *ResponseFormat `json:"response_format"`
Tools []api.Tool `json:"tools"`
Reasoning *Reasoning `json:"reasoning,omitempty"`
ReasoningEffort *string `json:"reasoning_effort,omitempty"`
Logprobs *bool `json:"logprobs"`
TopLogprobs int `json:"top_logprobs"`
DebugRenderOnly bool `json:"_debug_render_only"`
}
type Choice ¶
type Choice struct {
Index int `json:"index"`
Message Message `json:"message"`
FinishReason *string `json:"finish_reason"`
Logprobs *ChoiceLogprobs `json:"logprobs,omitempty"`
}
type ChoiceLogprobs ¶ added in v0.12.11
type ChunkChoice ¶
type ChunkChoice struct {
Index int `json:"index"`
Delta Message `json:"delta"`
FinishReason *string `json:"finish_reason"`
Logprobs *ChoiceLogprobs `json:"logprobs,omitempty"`
}
type CompleteChunkChoice ¶ added in v0.2.0
type CompleteChunkChoice struct {
Text string `json:"text"`
Index int `json:"index"`
FinishReason *string `json:"finish_reason"`
Logprobs *ChoiceLogprobs `json:"logprobs,omitempty"`
}
type Completion ¶ added in v0.2.0
type Completion struct {
Id string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
SystemFingerprint string `json:"system_fingerprint"`
Choices []CompleteChunkChoice `json:"choices"`
Usage Usage `json:"usage,omitempty"`
}
func ToCompletion ¶ added in v0.12.4
func ToCompletion(id string, r api.GenerateResponse) Completion
ToCompletion converts an api.GenerateResponse to Completion
type CompletionChunk ¶ added in v0.2.0
type CompletionChunk struct {
Id string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Choices []CompleteChunkChoice `json:"choices"`
Model string `json:"model"`
SystemFingerprint string `json:"system_fingerprint"`
Usage *Usage `json:"usage,omitempty"`
}
func ToCompleteChunk ¶ added in v0.12.4
func ToCompleteChunk(id string, r api.GenerateResponse) CompletionChunk
ToCompleteChunk converts an api.GenerateResponse to CompletionChunk
type CompletionRequest ¶ added in v0.2.0
type CompletionRequest struct {
Model string `json:"model"`
Prompt string `json:"prompt"`
FrequencyPenalty float32 `json:"frequency_penalty"`
MaxTokens *int `json:"max_tokens"`
PresencePenalty float32 `json:"presence_penalty"`
Seed *int `json:"seed"`
Stop any `json:"stop"`
Stream bool `json:"stream"`
StreamOptions *StreamOptions `json:"stream_options"`
Temperature *float32 `json:"temperature"`
TopP float32 `json:"top_p"`
Suffix string `json:"suffix"`
Logprobs *int `json:"logprobs"`
DebugRenderOnly bool `json:"_debug_render_only"`
}
TODO (https://github.com/ollama/ollama/issues/5259): support []string, []int and [][]int
type EmbedRequest ¶ added in v0.2.6
type EmbeddingList ¶ added in v0.2.6
type EmbeddingList struct {
Object string `json:"object"`
Data []Embedding `json:"data"`
Model string `json:"model"`
Usage EmbeddingUsage `json:"usage,omitempty"`
}
func ToEmbeddingList ¶ added in v0.12.4
func ToEmbeddingList(model string, r api.EmbedResponse, encodingFormat string) EmbeddingList
ToEmbeddingList converts an api.EmbedResponse to EmbeddingList encodingFormat can be "float", "base64", or empty (defaults to "float")
type EmbeddingUsage ¶ added in v0.3.3
type ErrorResponse ¶
type ErrorResponse struct {
Error Error `json:"error"`
}
func NewError ¶
func NewError(code int, message string) ErrorResponse
type ImageEditRequest ¶ added in v0.15.0
type ImageEditRequest struct {
Model string `json:"model"`
Prompt string `json:"prompt"`
Image string `json:"image"` // Base64-encoded image data
Size string `json:"size,omitempty"` // e.g., "1024x1024"
Seed *int64 `json:"seed,omitempty"`
}
ImageEditRequest is an OpenAI-compatible image edit request.
type ImageGenerationRequest ¶ added in v0.14.3
type ImageGenerationRequest struct {
Model string `json:"model"`
Prompt string `json:"prompt"`
N int `json:"n,omitempty"`
Size string `json:"size,omitempty"`
ResponseFormat string `json:"response_format,omitempty"`
Seed *int64 `json:"seed,omitempty"`
}
ImageGenerationRequest is an OpenAI-compatible image generation request.
type ImageGenerationResponse ¶ added in v0.14.3
type ImageGenerationResponse struct {
Created int64 `json:"created"`
Data []ImageURLOrData `json:"data"`
}
ImageGenerationResponse is an OpenAI-compatible image generation response.
func ToImageGenerationResponse ¶ added in v0.14.3
func ToImageGenerationResponse(resp api.GenerateResponse) ImageGenerationResponse
ToImageGenerationResponse converts an Ollama GenerateResponse to an OpenAI ImageGenerationResponse.
type ImageURLOrData ¶ added in v0.14.3
type ImageURLOrData struct {
URL string `json:"url,omitempty"`
B64JSON string `json:"b64_json,omitempty"`
}
ImageURLOrData contains either a URL or base64-encoded image data.
type JsonSchema ¶ added in v0.5.0
type JsonSchema struct {
Schema json.RawMessage `json:"schema"`
}
type ListCompletion ¶ added in v0.2.0
func ToListCompletion ¶ added in v0.12.4
func ToListCompletion(r api.ListResponse) ListCompletion
ToListCompletion converts an api.ListResponse to ListCompletion
type Model ¶ added in v0.2.0
type ResponseFormat ¶
type ResponseFormat struct {
Type string `json:"type"`
JsonSchema *JsonSchema `json:"json_schema,omitempty"`
}
type ResponsesContent ¶ added in v0.13.3
type ResponsesContent interface {
// contains filtered or unexported methods
}
ResponsesContent is a discriminated union for input content types. Concrete types: ResponsesTextContent, ResponsesImageContent, ResponsesOutputTextContent, ResponsesFileContent.
type ResponsesError ¶ added in v0.14.2
ResponsesError represents an error in the response.
type ResponsesFileContent ¶ added in v0.20.4
type ResponsesFunctionCall ¶ added in v0.13.3
type ResponsesFunctionCall struct {
ID string `json:"id,omitempty"` // item ID
Type string `json:"type"` // always "function_call"
CallID string `json:"call_id"` // the tool call ID
Name string `json:"name"` // function name
Arguments string `json:"arguments"` // JSON arguments string
}
ResponsesFunctionCall represents an assistant's function call in conversation history.
type ResponsesFunctionCallOutput ¶ added in v0.13.3
type ResponsesFunctionCallOutput struct {
Type string `json:"type"` // always "function_call_output"
CallID string `json:"call_id"` // links to the original function call
Output string `json:"output"` // the function result
// OutputItems is populated when output is provided as Responses content
// items instead of the string shorthand.
OutputItems []ResponsesContent `json:"-"`
}
ResponsesFunctionCallOutput represents a function call result from the client.
func (*ResponsesFunctionCallOutput) UnmarshalJSON ¶ added in v0.20.4
func (o *ResponsesFunctionCallOutput) UnmarshalJSON(data []byte) error
type ResponsesImageContent ¶ added in v0.13.3
type ResponsesImageContent struct {
Type string `json:"type"` // always "input_image"
// TODO(drifkin): is this really required? that seems verbose and a default is specified in the docs
Detail string `json:"detail"` // required
FileID string `json:"file_id,omitempty"` // optional
ImageURL string `json:"image_url,omitempty"` // optional
}
type ResponsesIncompleteDetails ¶ added in v0.14.2
type ResponsesIncompleteDetails struct {
Reason string `json:"reason"`
}
ResponsesIncompleteDetails represents details about why a response was incomplete.
type ResponsesInput ¶ added in v0.13.3
type ResponsesInput struct {
Text string // set if input was a plain string
Items []ResponsesInputItem // set if input was an array
}
ResponsesInput can be either: - a string (equivalent to a text input with the user role) - an array of input items (see ResponsesInputItem)
func (*ResponsesInput) UnmarshalJSON ¶ added in v0.13.3
func (r *ResponsesInput) UnmarshalJSON(data []byte) error
type ResponsesInputItem ¶ added in v0.13.3
type ResponsesInputItem interface {
// contains filtered or unexported methods
}
ResponsesInputItem is a discriminated union for input items. Concrete types: ResponsesInputMessage (more to come)
type ResponsesInputMessage ¶ added in v0.13.3
type ResponsesInputMessage struct {
Type string `json:"type"` // always "message"
Role string `json:"role"` // one of `user`, `system`, `developer`
Content []ResponsesContent `json:"content,omitempty"`
}
func (*ResponsesInputMessage) UnmarshalJSON ¶ added in v0.13.3
func (m *ResponsesInputMessage) UnmarshalJSON(data []byte) error
type ResponsesInputTokensDetails ¶ added in v0.14.2
type ResponsesInputTokensDetails struct {
CachedTokens int `json:"cached_tokens"`
}
type ResponsesOutputContent ¶ added in v0.13.3
type ResponsesOutputItem ¶ added in v0.13.3
type ResponsesOutputItem struct {
ID string `json:"id"`
Type string `json:"type"` // "message", "function_call", or "reasoning"
Status string `json:"status,omitempty"`
Role string `json:"role,omitempty"` // for message
Content []ResponsesOutputContent `json:"content,omitempty"` // for message
CallID string `json:"call_id,omitempty"` // for function_call
Name string `json:"name,omitempty"` // for function_call
Arguments string `json:"arguments,omitempty"` // for function_call
// Reasoning fields
Summary []ResponsesReasoningSummary `json:"summary,omitempty"` // for reasoning
EncryptedContent string `json:"encrypted_content,omitempty"` // for reasoning
}
type ResponsesOutputMessage ¶ added in v0.13.3
type ResponsesOutputMessage struct{}
type ResponsesOutputTextContent ¶ added in v0.13.3
type ResponsesOutputTextContent struct {
Type string `json:"type"` // always "output_text"
Text string `json:"text"`
}
ResponsesOutputTextContent represents output text from a previous assistant response that is being passed back as part of the conversation history.
type ResponsesOutputTokensDetails ¶ added in v0.14.2
type ResponsesOutputTokensDetails struct {
ReasoningTokens int `json:"reasoning_tokens"`
}
type ResponsesReasoning ¶ added in v0.13.3
type ResponsesReasoning struct {
// originally: optional, default is per-model
Effort string `json:"effort,omitempty"`
// originally: deprecated, use `summary` instead. One of `auto`, `concise`, `detailed`
GenerateSummary string `json:"generate_summary,omitempty"`
// originally: optional, one of `auto`, `concise`, `detailed`
Summary string `json:"summary,omitempty"`
}
type ResponsesReasoningInput ¶ added in v0.13.3
type ResponsesReasoningInput struct {
ID string `json:"id,omitempty"`
Type string `json:"type"` // always "reasoning"
Summary []ResponsesReasoningSummary `json:"summary,omitempty"`
EncryptedContent string `json:"encrypted_content,omitempty"`
}
ResponsesReasoningInput represents a reasoning item passed back as input. This is used when the client sends previous reasoning back for context.
type ResponsesReasoningOutput ¶ added in v0.14.2
type ResponsesReasoningOutput struct {
Effort *string `json:"effort,omitempty"`
Summary *string `json:"summary,omitempty"`
}
ResponsesReasoningOutput represents reasoning configuration in the response.
type ResponsesReasoningSummary ¶ added in v0.13.3
type ResponsesRequest ¶ added in v0.13.3
type ResponsesRequest struct {
Model string `json:"model"`
// originally: optional, default is false
// for us: not supported
Background bool `json:"background"`
// originally: optional `string | {id: string}`
// for us: not supported
Conversation json.RawMessage `json:"conversation"`
// originally: string[]
// for us: ignored
Include []string `json:"include"`
Input ResponsesInput `json:"input"`
// optional, inserts a system message at the start of the conversation
Instructions string `json:"instructions,omitempty"`
// optional, maps to num_predict
MaxOutputTokens *int `json:"max_output_tokens,omitempty"`
Reasoning ResponsesReasoning `json:"reasoning"`
// optional, default is 1.0
Temperature *float64 `json:"temperature"`
// optional, controls output format (e.g. json_schema)
Text *ResponsesText `json:"text,omitempty"`
// optional, default is 1.0
TopP *float64 `json:"top_p"`
// optional, default is `"disabled"`
Truncation *string `json:"truncation"`
Tools []ResponsesTool `json:"tools,omitempty"`
// optional, default is false
Stream *bool `json:"stream,omitempty"`
}
type ResponsesResponse ¶ added in v0.13.3
type ResponsesResponse struct {
ID string `json:"id"`
Object string `json:"object"`
CreatedAt int64 `json:"created_at"`
CompletedAt *int64 `json:"completed_at"`
Status string `json:"status"`
IncompleteDetails *ResponsesIncompleteDetails `json:"incomplete_details"`
Model string `json:"model"`
PreviousResponseID *string `json:"previous_response_id"`
Instructions *string `json:"instructions"`
Output []ResponsesOutputItem `json:"output"`
Error *ResponsesError `json:"error"`
Tools []ResponsesTool `json:"tools"`
ToolChoice any `json:"tool_choice"`
Truncation string `json:"truncation"`
ParallelToolCalls bool `json:"parallel_tool_calls"`
Text ResponsesTextField `json:"text"`
TopP float64 `json:"top_p"`
PresencePenalty float64 `json:"presence_penalty"`
FrequencyPenalty float64 `json:"frequency_penalty"`
TopLogprobs int `json:"top_logprobs"`
Temperature float64 `json:"temperature"`
Reasoning *ResponsesReasoningOutput `json:"reasoning"`
Usage *ResponsesUsage `json:"usage"`
MaxOutputTokens *int `json:"max_output_tokens"`
MaxToolCalls *int `json:"max_tool_calls"`
Store bool `json:"store"`
Background bool `json:"background"`
ServiceTier string `json:"service_tier"`
Metadata map[string]any `json:"metadata"`
SafetyIdentifier *string `json:"safety_identifier"`
PromptCacheKey *string `json:"prompt_cache_key"`
}
func ToResponse ¶ added in v0.13.3
func ToResponse(model, responseID, itemID string, chatResponse api.ChatResponse, request ResponsesRequest) ResponsesResponse
ToResponse converts an api.ChatResponse to a Responses API response. The request is used to echo back request parameters in the response.
type ResponsesStreamConverter ¶ added in v0.13.3
type ResponsesStreamConverter struct {
// contains filtered or unexported fields
}
ResponsesStreamConverter converts api.ChatResponse objects to Responses API streaming events. It maintains state across multiple calls to handle the streaming event sequence correctly.
func NewResponsesStreamConverter ¶ added in v0.13.3
func NewResponsesStreamConverter(responseID, itemID, model string, request ResponsesRequest) *ResponsesStreamConverter
NewResponsesStreamConverter creates a new converter with the given configuration.
func (*ResponsesStreamConverter) Process ¶ added in v0.13.3
func (c *ResponsesStreamConverter) Process(r api.ChatResponse) []ResponsesStreamEvent
Process takes a ChatResponse and returns the events that should be emitted. Events are returned in order. The caller is responsible for serializing and sending these events.
type ResponsesStreamEvent ¶ added in v0.13.3
type ResponsesStreamEvent struct {
Event string // The event type (e.g., "response.created")
Data any // The event payload (will be JSON-marshaled)
}
ResponsesStreamEvent represents a single Server-Sent Event for the Responses API.
type ResponsesText ¶ added in v0.13.3
type ResponsesText struct {
Format *ResponsesTextFormat `json:"format,omitempty"`
}
type ResponsesTextContent ¶ added in v0.13.3
type ResponsesTextField ¶ added in v0.14.2
type ResponsesTextField struct {
Format ResponsesTextFormat `json:"format"`
}
ResponsesTextField represents the text output configuration in the response.
type ResponsesTextFormat ¶ added in v0.13.3
type ResponsesTextFormat struct {
Type string `json:"type"` // "text", "json_schema"
Name string `json:"name,omitempty"` // for json_schema
Schema json.RawMessage `json:"schema,omitempty"` // for json_schema
Strict *bool `json:"strict,omitempty"` // for json_schema
}
type ResponsesTool ¶ added in v0.13.3
type ResponsesTool struct {
Type string `json:"type"` // "function"
Name string `json:"name"`
Description *string `json:"description"` // nullable but required
Strict *bool `json:"strict"` // nullable but required
Parameters map[string]any `json:"parameters"` // nullable but required
}
ResponsesTool represents a tool in the Responses API format. Note: This differs from api.Tool which nests fields under "function".
type ResponsesUsage ¶ added in v0.13.3
type ResponsesUsage struct {
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
TotalTokens int `json:"total_tokens"`
InputTokensDetails ResponsesInputTokensDetails `json:"input_tokens_details"`
OutputTokensDetails ResponsesOutputTokensDetails `json:"output_tokens_details"`
}
type StreamOptions ¶ added in v0.5.2
type StreamOptions struct {
IncludeUsage bool `json:"include_usage"`
}
type ToolCall ¶ added in v0.2.6
type ToolCall struct {
ID string `json:"id"`
Index int `json:"index"`
Type string `json:"type"`
Function struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
} `json:"function"`
}
func ToToolCalls ¶ added in v0.12.6
ToToolCalls converts api.ToolCall to OpenAI ToolCall format
type TranscriptionRequest ¶ added in v0.20.0
type TranscriptionRequest struct {
Model string
AudioData []byte
ResponseFormat string // "json", "text", "verbose_json"
Language string
Prompt string
}
TranscriptionRequest holds parsed fields from the multipart form.
type TranscriptionResponse ¶ added in v0.20.0
type TranscriptionResponse struct {
Text string `json:"text"`
}
TranscriptionResponse is the response format for /v1/audio/transcriptions.
type Usage ¶
type Usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}
func ToUsage ¶ added in v0.12.4
func ToUsage(r api.ChatResponse) Usage
ToUsage converts an api.ChatResponse to Usage
func ToUsageGenerate ¶ added in v0.12.4
func ToUsageGenerate(r api.GenerateResponse) Usage
ToUsageGenerate converts an api.GenerateResponse to Usage