Documentation
¶
Index ¶
- Constants
- func DetectMimeType(filePath string) string
- func DetectMimeTypeByContent(data []byte) string
- func FormatDimensionNote(r *ImageResizeResult) string
- func IsImageFile(filePath string) bool
- func IsImageMimeType(mimeType string) bool
- func IsSupportedMimeType(mimeType string) bool
- func IsTextFile(filePath string) bool
- func ReadFileForInline(filePath string) (string, error)
- type FinishReason
- type ImageResizeResult
- type ImageURLDetail
- type Message
- type MessageDelta
- type MessageFile
- type MessageImageURL
- type MessagePart
- type MessagePartType
- type MessageRole
- type MessageStream
- type MessageStreamChoice
- type MessageStreamResponse
- type RateLimit
- type Usage
Constants ¶
const ( // MaxImageDimension is the maximum width or height for images sent to LLM providers. MaxImageDimension = 2000 // MaxImageBytes is the maximum file size for images sent to LLM providers (4.5MB, // below Anthropic's 5MB limit). MaxImageBytes = 4_500_000 )
const MaxInlineFileSize = 5 * 1024 * 1024 // 5MB
MaxInlineFileSize is the maximum size of a text file that can be inlined directly into a message. Files larger than this are skipped with a warning. This is much smaller than the general upload limit because inline content expands token usage significantly.
Variables ¶
This section is empty.
Functions ¶
func DetectMimeType ¶ added in v1.20.6
DetectMimeType returns the MIME type for a file by reading its first 512 bytes and inspecting the content (magic bytes). For text-based files that http.DetectContentType cannot distinguish (e.g. source code vs plain text), it falls back to extension matching. This is the canonical implementation used across all packages for consistency.
func DetectMimeTypeByContent ¶ added in v1.28.1
DetectMimeTypeByContent detects the MIME type of data by inspecting its content (magic bytes). Returns the MIME type or "application/octet-stream" if unknown. This supplements extension-based detection for files with missing or wrong extensions.
func FormatDimensionNote ¶ added in v1.28.1
func FormatDimensionNote(r *ImageResizeResult) string
FormatDimensionNote produces a human-readable note describing the resize mapping. This helps the model translate coordinates from the resized image back to the original.
Because ResizeImage uses fitDimensions (which preserves aspect ratio), the X and Y scale factors are always equal in practice. If they ever differ (e.g. because the function is called with a manually constructed ImageResizeResult), we emit separate per-axis factors so that coordinate mapping remains correct.
func IsImageFile ¶ added in v1.28.1
IsImageFile returns true if the file at the given path is a supported image based on its extension. Supported formats: JPEG, PNG, GIF, WebP.
func IsImageMimeType ¶ added in v1.28.1
IsImageMimeType returns true if the MIME type is a supported image type.
func IsSupportedMimeType ¶ added in v1.20.6
IsSupportedMimeType returns true if the MIME type is supported for file attachments. Supported types include images (jpeg, png, gif, webp) and documents (pdf, text, markdown).
func IsTextFile ¶ added in v1.22.0
IsTextFile determines if a file at the given path is a text file that should be inlined into the message rather than uploaded via a provider's file API. It first checks the file extension against a broad allowlist of known text extensions. For unknown extensions, it falls back to reading the first 8KB and checking if the content is valid UTF-8 with no null bytes.
func ReadFileForInline ¶ added in v1.22.0
ReadFileForInline reads a text file and wraps it in an XML-like tag with the file path for context. Returns the formatted content and any error.
Types ¶
type FinishReason ¶
type FinishReason string
FinishReason represents the reason why the model finished generating a response
const ( // FinishReasonStop means the model reached a natural stopping point or the max tokens FinishReasonStop FinishReason = "stop" // FinishReasonLength means the model reached the token limit FinishReasonLength FinishReason = "length" // FinishReasonToolCalls means the model called a tool FinishReasonToolCalls FinishReason = "tool_calls" // FinishReasonNull means no finish reason was provided FinishReasonNull FinishReason = "null" )
type ImageResizeResult ¶ added in v1.28.1
type ImageResizeResult struct {
// Data is the (possibly re-encoded) image bytes.
Data []byte
// MimeType is the MIME type of the output image.
MimeType string
// OriginalWidth and OriginalHeight are the dimensions of the input image.
OriginalWidth, OriginalHeight int
// Width and Height are the dimensions of the output image.
Width, Height int
// Resized indicates whether the image was actually modified.
Resized bool
}
ImageResizeResult holds the output of an image resize operation.
func ResizeImage ¶ added in v1.28.1
func ResizeImage(data []byte, mimeType string) (*ImageResizeResult, error)
ResizeImage takes raw image bytes and ensures they fit within provider limits (max 2000×2000 pixels, max 4.5 MB). If the image already fits, it is returned unchanged. Otherwise it is scaled down (preserving aspect ratio) and re-encoded.
The function tries to produce the smallest output by comparing PNG and JPEG encoding, and progressively reducing JPEG quality and dimensions if needed.
func ResizeImageBase64 ¶ added in v1.28.1
func ResizeImageBase64(b64Data, mimeType string) (b64Result string, metadata *ImageResizeResult, err error)
ResizeImageBase64 is a convenience wrapper around ResizeImage that accepts and returns base64-encoded image data. The base64-encoded result is returned separately to avoid mutating the ImageResizeResult.Data field.
type ImageURLDetail ¶
type ImageURLDetail string
const ( ImageURLDetailHigh ImageURLDetail = "high" ImageURLDetailLow ImageURLDetail = "low" ImageURLDetailAuto ImageURLDetail = "auto" )
type Message ¶
type Message struct {
Role MessageRole `json:"role"`
Content string `json:"content"`
MultiContent []MessagePart `json:"multi_content,omitempty"`
// This property is used for the "reasoning" feature supported by deepseek-reasoner
// which is not in the official documentation.
// the doc from deepseek:
// - https://api-docs.deepseek.com/api/create-chat-completion#responses
ReasoningContent string `json:"reasoning_content,omitempty"`
// ThinkingSignature is used for Anthropic's extended thinking feature
ThinkingSignature string `json:"thinking_signature,omitempty"`
ThoughtSignature []byte `json:"thought_signature,omitempty"`
FunctionCall *tools.FunctionCall `json:"function_call,omitempty"`
// For Role=assistant prompts this may be set to the tool calls generated by the model, such as function calls.
ToolCalls []tools.ToolCall `json:"tool_calls,omitempty"`
// ToolDefinitions contains the definitions of tools referenced in ToolCalls.
// This is used to provide tool metadata (name, description, category) when loading historical sessions.
ToolDefinitions []tools.Tool `json:"tool_definitions,omitempty"`
// For Role=tool prompts this should be set to the ID given in the assistant's prior request to call a tool.
ToolCallID string `json:"tool_call_id,omitempty"`
// IsError indicates the tool call failed (only for Role=tool messages).
IsError bool `json:"is_error,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
// Usage tracks token usage for this message (only set for assistant messages)
Usage *Usage `json:"usage,omitempty"`
// Model is the model that generated this message (only set for assistant messages)
Model string `json:"model,omitempty"`
// Cost is the cost of this message in dollars (only set for assistant messages)
Cost float64 `json:"cost,omitempty"`
// CacheControl indicates whether this message is a cached message (only used by anthropic)
CacheControl bool `json:"cache_control,omitempty"`
}
type MessageDelta ¶
type MessageDelta struct {
Role string `json:"role,omitempty"`
Content string `json:"content,omitempty"`
ReasoningContent string `json:"reasoning_content,omitempty"`
ThinkingSignature string `json:"thinking_signature,omitempty"`
ThoughtSignature []byte `json:"thought_signature,omitempty"`
FunctionCall *tools.FunctionCall `json:"function_call,omitempty"`
ToolCalls []tools.ToolCall `json:"tool_calls,omitempty"`
}
MessageDelta represents a delta/chunk in a streaming response
type MessageFile ¶ added in v1.20.6
type MessageFile struct {
Path string `json:"path,omitempty"` // Local file path (used for upload)
FileID string `json:"file_id,omitempty"` // Provider-specific file ID (after upload)
MimeType string `json:"mime_type,omitempty"` // MIME type of the file
}
MessageFile represents a file attachment that can be uploaded to a provider's file storage.
type MessageImageURL ¶
type MessageImageURL struct {
URL string `json:"url,omitempty"`
Detail ImageURLDetail `json:"detail,omitempty"`
}
type MessagePart ¶
type MessagePart struct {
Type MessagePartType `json:"type,omitempty"`
Text string `json:"text,omitempty"`
ImageURL *MessageImageURL `json:"image_url,omitempty"`
File *MessageFile `json:"file,omitempty"`
}
type MessagePartType ¶
type MessagePartType string
const ( MessagePartTypeText MessagePartType = "text" MessagePartTypeImageURL MessagePartType = "image_url" MessagePartTypeFile MessagePartType = "file" )
type MessageRole ¶
type MessageRole string
const ( MessageRoleSystem MessageRole = "system" MessageRoleUser MessageRole = "user" MessageRoleAssistant MessageRole = "assistant" MessageRoleTool MessageRole = "tool" )
type MessageStream ¶
type MessageStream interface {
// Recv gets the next completion chunk
Recv() (MessageStreamResponse, error)
// Close closes the stream
Close()
}
MessageStream interface represents a stream of chat completions
type MessageStreamChoice ¶
type MessageStreamChoice struct {
Index int `json:"index"`
Delta MessageDelta `json:"delta"`
FinishReason FinishReason `json:"finish_reason,omitempty"`
}
MessageStreamChoice represents a choice in a streaming response
type MessageStreamResponse ¶
type MessageStreamResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []MessageStreamChoice `json:"choices"`
Usage *Usage `json:"usage,omitempty"`
RateLimit *RateLimit `json:"rate_limit,omitempty"`
}
MessageStreamResponse represents a streaming response from the model