api

package
v0.15.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultBufferTokens is the safety buffer for estimation errors
	DefaultBufferTokens = 1000
	// MinOutputTokens is the minimum output tokens to reserve
	MinOutputTokens = 512
	// ToolTokenEstimate is the approximate token count per tool definition
	ToolTokenEstimate = 200
	// SystemInstructionBuffer accounts for system prompt overhead
	SystemInstructionBuffer = 500
	// MessageOverheadTokens accounts for role/message wrapper overhead
	MessageOverheadTokens = 4
	// ToolCallOverheadTokens accounts for assistant tool_call wrapper overhead
	ToolCallOverheadTokens = 12
	// ToolCallIDOverheadTokens accounts for tool response tool_call_id overhead
	ToolCallIDOverheadTokens = 8
	// ImageMessageOverheadTokens conservatively accounts for multimodal image parts
	ImageMessageOverheadTokens = 256
)

Token estimation constants

Variables

This section is empty.

Functions

func CalculateOutputBudget added in v0.11.5

func CalculateOutputBudget(contextLimit int, inputTokens int) (int, bool)

CalculateOutputBudget calculates the safe output token budget given context constraints. It returns the maximum tokens that can be requested for completion. If the input exceeds the context limit, returns 0 and an error message.

func EstimateInputTokens added in v0.11.5

func EstimateInputTokens(messages []Message, tools []Tool) int

EstimateInputTokens estimates total input tokens for messages and tools. This includes a buffer for system instructions and message formatting overhead.

func EstimateTokens added in v0.11.5

func EstimateTokens(text string) int

EstimateTokens provides a token estimation based on OpenAI's tiktoken approach. This is the centralized implementation that all providers should use for consistency.

func FormatHTTPResponseError added in v0.15.4

func FormatHTTPResponseError(statusCode int, headers http.Header, body []byte) error

FormatHTTPResponseError converts an HTTP error response into a concise, user-facing error that avoids dumping full HTML or JSON payloads.

func GetProviderName

func GetProviderName(clientType ClientType) string

GetProviderName returns the human-readable name for a provider

func IsProviderAvailable added in v0.5.8

func IsProviderAvailable(provider ClientType) bool

IsProviderAvailable checks if a provider can be used. Uses credentials.HasProviderCredential to avoid hardcoding env var strings.

Types

type BaseProvider added in v0.5.4

type BaseProvider struct {
	// contains filtered or unexported fields
}

BaseProvider implements common functionality for all providers

func NewBaseProvider added in v0.5.4

func NewBaseProvider(name string, clientType ClientType, endpoint string, apiKey string) *BaseProvider

NewBaseProvider creates a base provider with common settings

func (*BaseProvider) EstimateCost added in v0.5.4

func (p *BaseProvider) EstimateCost(promptTokens, completionTokens int, model string) float64

EstimateCost calculates the estimated cost for a response

func (*BaseProvider) GetEndpoint added in v0.5.4

func (p *BaseProvider) GetEndpoint() string

GetEndpoint returns the API endpoint

func (*BaseProvider) GetModel added in v0.5.4

func (p *BaseProvider) GetModel() string

GetModel returns the current model

func (*BaseProvider) GetName added in v0.5.4

func (p *BaseProvider) GetName() string

GetName returns the provider name

func (*BaseProvider) GetType added in v0.5.4

func (p *BaseProvider) GetType() ClientType

GetType returns the provider type

func (*BaseProvider) IsDebug added in v0.5.4

func (p *BaseProvider) IsDebug() bool

IsDebug returns whether debug mode is enabled

func (*BaseProvider) MakeAuthRequest added in v0.5.4

func (p *BaseProvider) MakeAuthRequest(ctx context.Context, method, url string, body io.Reader) (*http.Request, error)

MakeAuthRequest creates an HTTP request with authentication

func (*BaseProvider) SetDebug added in v0.5.4

func (p *BaseProvider) SetDebug(debug bool)

SetDebug enables or disables debug mode

func (*BaseProvider) SetModel added in v0.5.4

func (p *BaseProvider) SetModel(model string) error

SetModel sets the current model

func (*BaseProvider) SupportsReasoning added in v0.5.4

func (p *BaseProvider) SupportsReasoning() bool

SupportsReasoning returns whether the provider supports reasoning

func (*BaseProvider) SupportsStreaming added in v0.5.4

func (p *BaseProvider) SupportsStreaming() bool

SupportsStreaming returns whether the provider supports streaming

func (*BaseProvider) SupportsTools added in v0.5.4

func (p *BaseProvider) SupportsTools() bool

SupportsTools returns whether the provider supports tools

func (*BaseProvider) SupportsVision added in v0.5.4

func (p *BaseProvider) SupportsVision() bool

SupportsVision returns whether the provider supports vision

type ChatRequest

type ChatRequest struct {
	Model      string    `json:"model"`
	Messages   []Message `json:"messages"`
	Tools      []Tool    `json:"tools,omitempty"`
	ToolChoice string    `json:"tool_choice,omitempty"`
	MaxTokens  int       `json:"max_tokens,omitempty"`
	Reasoning  string    `json:"reasoning,omitempty"`
	Stream     bool      `json:"stream,omitempty"`
}

type ChatResponse

type ChatResponse struct {
	ID      string   `json:"id"`
	Object  string   `json:"object"`
	Created int64    `json:"created"`
	Model   string   `json:"model"`
	Choices []Choice `json:"choices"`
	Usage   struct {
		PromptTokens        int     `json:"prompt_tokens"`
		CompletionTokens    int     `json:"completion_tokens"`
		TotalTokens         int     `json:"total_tokens"`
		EstimatedCost       float64 `json:"estimated_cost"`
		PromptTokensDetails struct {
			CachedTokens     int  `json:"cached_tokens"`
			CacheWriteTokens *int `json:"cache_write_tokens"`
		} `json:"prompt_tokens_details,omitempty"`
	} `json:"usage"`
}

type Choice

type Choice struct {
	Index   int `json:"index"`
	Message struct {
		Role             string      `json:"role"`
		Content          string      `json:"content"`
		ReasoningContent string      `json:"reasoning_content,omitempty"`
		Images           []ImageData `json:"images,omitempty"`
		ToolCalls        []ToolCall  `json:"tool_calls,omitempty"`
	} `json:"message"`
	FinishReason string `json:"finish_reason"`
}

type ClientInterface

type ClientInterface interface {
	SendChatRequest(messages []Message, tools []Tool, reasoning string, disableThinking bool) (*ChatResponse, error)
	SendChatRequestStream(messages []Message, tools []Tool, reasoning string, disableThinking bool, callback StreamCallback) (*ChatResponse, error)
	CheckConnection() error
	SetDebug(debug bool)
	SetModel(model string) error
	GetModel() string
	GetProvider() string
	GetModelContextLimit() (int, error)
	ListModels(ctx context.Context) ([]ModelInfo, error)
	SupportsVision() bool
	GetVisionModel() string
	SendVisionRequest(messages []Message, tools []Tool, reasoning string, disableThinking bool) (*ChatResponse, error)
	// TPS (Tokens Per Second) tracking methods
	GetLastTPS() float64
	GetAverageTPS() float64
	GetTPSStats() map[string]float64
	ResetTPSStats()
}

ClientInterface defines the common interface for all API clients

func NewDeepInfraClientWrapper

func NewDeepInfraClientWrapper(model string) (ClientInterface, error)

NewDeepInfraClientWrapper creates a DeepInfra client wrapper

func NewOpenRouterClientWrapper

func NewOpenRouterClientWrapper(model string) (ClientInterface, error)

NewOpenRouterClientWrapper is deprecated - use factory.CreateProviderClient instead

func NewUnifiedClient

func NewUnifiedClient(clientType ClientType) (ClientInterface, error)

NewUnifiedClient creates a client with default model for the provider

func NewUnifiedClientWithModel

func NewUnifiedClientWithModel(clientType ClientType, model string) (ClientInterface, error)

NewUnifiedClientWithModel creates a client with a specific model

type ClientType

type ClientType string

ClientType represents the type of client to use

const (
	ChutesClientType      ClientType = "chutes"
	DeepInfraClientType   ClientType = "deepinfra"
	DeepSeekClientType    ClientType = "deepseek"
	LMStudioClientType    ClientType = "lmstudio"
	MinimaxClientType     ClientType = "minimax"
	MistralClientType     ClientType = "mistral"
	OllamaClientType      ClientType = "ollama" // Maps to local ollama
	OllamaLocalClientType ClientType = "ollama-local"
	OllamaTurboClientType ClientType = "ollama-turbo"
	OpenRouterClientType  ClientType = "openrouter"
	OpenAIClientType      ClientType = "openai"
	ZAIClientType         ClientType = "zai"    // Z.AI Coding Plan (OpenAI-compatible)
	TestClientType        ClientType = "test"   // Mock provider for CI/testing
	EditorClientType      ClientType = "editor" // Editor-only mode, no AI provider
)

func DetermineProvider added in v0.5.8

func DetermineProvider(explicitProvider string, lastUsedProvider ClientType) (ClientType, error)

DetermineProvider provides unified provider detection with clear precedence: 1. Command-line flag (if provided) 2. Environment variable (LEDIT_PROVIDER) 3. Config file (last_used_provider) 4. First available provider based on API keys 5. Fallback to Ollama

func GetAvailableProviders

func GetAvailableProviders() []ClientType

GetAvailableProviders returns a list of all available providers

func ParseProviderName added in v0.6.0

func ParseProviderName(name string) (ClientType, error)

ParseProviderName converts a string provider name to ClientType

type HTTPClient added in v0.5.4

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient interface for testing

type HarmonyFormatter

type HarmonyFormatter struct {
	// contains filtered or unexported fields
}

HarmonyFormatter handles conversion from OpenAI format to harmony format

func NewHarmonyFormatter

func NewHarmonyFormatter() *HarmonyFormatter

NewHarmonyFormatter creates a new harmony formatter

func NewHarmonyFormatterWithReasoning

func NewHarmonyFormatterWithReasoning(reasoning string) *HarmonyFormatter

NewHarmonyFormatterWithReasoning creates a harmony formatter with specific reasoning level

func (*HarmonyFormatter) AddReturnToken

func (h *HarmonyFormatter) AddReturnToken(response string) string

AddReturnToken adds the completion token to a harmony response

func (*HarmonyFormatter) ConvertReturnToEnd

func (h *HarmonyFormatter) ConvertReturnToEnd(conversation string) string

ConvertReturnToEnd converts <|return|> tokens to <|end|> for conversation history

func (*HarmonyFormatter) FormatMessagesForCompletion

func (h *HarmonyFormatter) FormatMessagesForCompletion(messages []Message, tools []Tool, opts *HarmonyOptions) string

FormatMessagesForCompletion converts OpenAI-style messages to harmony format

func (*HarmonyFormatter) StripReturnToken

func (h *HarmonyFormatter) StripReturnToken(response string) string

StripReturnToken removes <|return|> tokens from model responses

type HarmonyOptions

type HarmonyOptions struct {
	ReasoningLevel string // "low", "medium", "high" - empty disables explicit reasoning tag
	EnableAnalysis bool   // Whether to enable analysis channel guidance
}

HarmonyOptions configures the harmony formatting

type ImageData

type ImageData struct {
	URL    string `json:"url,omitempty"`    // URL to image
	Base64 string `json:"base64,omitempty"` // Base64 encoded image data
	Type   string `json:"type,omitempty"`   // MIME type (image/jpeg, image/png, etc.)
}

type Message

type Message struct {
	Role             string      `json:"role"`
	Content          string      `json:"content"`
	ReasoningContent string      `json:"reasoning_content,omitempty"`
	Images           []ImageData `json:"images,omitempty"`       // Support for multiple images
	ToolCallId       string      `json:"tool_call_id,omitempty"` // Required for tool role messages
	ToolCalls        []ToolCall  `json:"tool_calls,omitempty"`   // Required for assistant messages with tool calls
}

type ModelDetails added in v0.5.4

type ModelDetails struct {
	ID              string
	Name            string
	ContextLength   int
	InputCostPer1K  float64
	OutputCostPer1K float64
	Features        []string // e.g., "vision", "tools", "reasoning"
	IsDefault       bool
}

ModelDetails represents detailed information about a model from a provider

type ModelInfo

type ModelInfo struct {
	ID            string   `json:"id"`
	Name          string   `json:"name,omitempty"`
	Description   string   `json:"description,omitempty"`
	Provider      string   `json:"provider,omitempty"`
	Size          string   `json:"size,omitempty"`
	Cost          float64  `json:"cost,omitempty"`
	InputCost     float64  `json:"input_cost,omitempty"`
	OutputCost    float64  `json:"output_cost,omitempty"`
	ContextLength int      `json:"context_length,omitempty"`
	Tags          []string `json:"tags,omitempty"`
}

ModelInfo represents information about an available model

func GetAvailableModels

func GetAvailableModels() ([]ModelInfo, error)

GetAvailableModels returns available models for the current provider

func GetModelsForProvider

func GetModelsForProvider(clientType ClientType) ([]ModelInfo, error)

GetModelsForProvider returns available models for a specific provider

func GetModelsForProviderCtx added in v0.15.0

func GetModelsForProviderCtx(ctx context.Context, clientType ClientType) ([]ModelInfo, error)

GetModelsForProviderCtx returns available models for a specific provider with context support

type ModelSelection

type ModelSelection struct {
	// contains filtered or unexported fields
}

ModelSelection represents a model selection system This is a stub implementation for backward compatibility The actual model selection logic has been moved to configuration-based system

func NewModelSelection

func NewModelSelection(config interface{}) *ModelSelection

NewModelSelection creates a new ModelSelection instance This is a stub for backward compatibility - the actual model selection is now handled through the configuration system

type ModelsListInterface

type ModelsListInterface interface {
	ListAvailableModels() ([]ModelInfo, error)
	GetDefaultModel() string
	IsModelAvailable(modelID string) bool
}

ModelsListInterface defines methods for listing available models

type OllamaLocalClient added in v0.6.0

type OllamaLocalClient struct {
	*TPSBase
	// contains filtered or unexported fields
}

OllamaLocalClient handles local Ollama API requests

func NewOllamaLocalClient added in v0.6.0

func NewOllamaLocalClient(model string) (*OllamaLocalClient, error)

NewOllamaLocalClient creates a new local Ollama client

func (*OllamaLocalClient) CheckConnection added in v0.6.0

func (c *OllamaLocalClient) CheckConnection() error

CheckConnection verifies local Ollama is accessible

func (*OllamaLocalClient) GetModel added in v0.6.0

func (c *OllamaLocalClient) GetModel() string

GetModel returns the current model

func (*OllamaLocalClient) GetModelContextLimit added in v0.6.0

func (c *OllamaLocalClient) GetModelContextLimit() (int, error)

GetModelContextLimit returns the context limit for the model

func (*OllamaLocalClient) GetProvider added in v0.6.0

func (c *OllamaLocalClient) GetProvider() string

GetProvider returns the provider name

func (*OllamaLocalClient) GetVisionModel added in v0.6.0

func (c *OllamaLocalClient) GetVisionModel() string

GetVisionModel returns empty string as vision is not supported

func (*OllamaLocalClient) ListModels added in v0.6.0

func (c *OllamaLocalClient) ListModels(ctx context.Context) ([]ModelInfo, error)

ListModels returns available local models

func (*OllamaLocalClient) SendChatRequest added in v0.6.0

func (c *OllamaLocalClient) SendChatRequest(messages []Message, tools []Tool, reasoning string, disableThinking bool) (*ChatResponse, error)

SendChatRequest sends a chat request to local Ollama

func (*OllamaLocalClient) SendChatRequestStream added in v0.6.0

func (c *OllamaLocalClient) SendChatRequestStream(messages []Message, tools []Tool, reasoning string, disableThinking bool, callback StreamCallback) (*ChatResponse, error)

SendChatRequestStream streams responses from local Ollama as they arrive

func (*OllamaLocalClient) SendVisionRequest added in v0.6.0

func (c *OllamaLocalClient) SendVisionRequest(messages []Message, tools []Tool, reasoning string, disableThinking bool) (*ChatResponse, error)

SendVisionRequest handles vision/OCR requests for Ollama Delegates to SendChatRequest since the image handling is done in buildChatRequest

func (*OllamaLocalClient) SetDebug added in v0.6.0

func (c *OllamaLocalClient) SetDebug(debug bool)

SetDebug enables or disables debug mode

func (*OllamaLocalClient) SetModel added in v0.6.0

func (c *OllamaLocalClient) SetModel(model string) error

SetModel updates the active model after validating it exists locally

func (*OllamaLocalClient) SupportsVision added in v0.6.0

func (c *OllamaLocalClient) SupportsVision() bool

SupportsVision returns true for OCR-capable models

type Provider added in v0.5.4

type Provider interface {
	// Core functionality
	SendChatRequest(ctx context.Context, req *ProviderChatRequest) (*ChatResponse, error)
	CheckConnection(ctx context.Context) error

	// Model management
	GetModel() string
	SetModel(model string) error
	GetAvailableModels(ctx context.Context) ([]ModelDetails, error)
	GetModelContextLimit() (int, error)

	// Provider information
	GetName() string
	GetType() ClientType
	GetEndpoint() string

	// Feature support
	SupportsVision() bool
	SupportsTools() bool
	SupportsStreaming() bool
	SupportsReasoning() bool

	// Configuration
	SetDebug(debug bool)
	IsDebug() bool
}

Provider defines the interface all LLM providers must implement

func CreateProviderFromClient added in v0.5.4

func CreateProviderFromClient(clientType ClientType, client ClientInterface) Provider

CreateProviderFromClient creates a Provider from an existing ClientInterface

type ProviderAdapter added in v0.5.4

type ProviderAdapter struct {
	// contains filtered or unexported fields
}

ProviderAdapter adapts the existing ClientInterface to the new Provider interface

func NewProviderAdapter added in v0.5.4

func NewProviderAdapter(clientType ClientType, client ClientInterface) *ProviderAdapter

NewProviderAdapter creates an adapter for existing clients

func (*ProviderAdapter) CheckConnection added in v0.5.4

func (a *ProviderAdapter) CheckConnection(ctx context.Context) error

CheckConnection verifies connectivity

func (*ProviderAdapter) GetAvailableModels added in v0.5.4

func (a *ProviderAdapter) GetAvailableModels(ctx context.Context) ([]ModelDetails, error)

GetAvailableModels returns available models for this provider

func (*ProviderAdapter) GetEndpoint added in v0.5.4

func (a *ProviderAdapter) GetEndpoint() string

GetEndpoint returns the API endpoint

func (*ProviderAdapter) GetModel added in v0.5.4

func (a *ProviderAdapter) GetModel() string

GetModel returns the current model

func (*ProviderAdapter) GetModelContextLimit added in v0.5.4

func (a *ProviderAdapter) GetModelContextLimit() (int, error)

GetModelContextLimit returns the context window size

func (*ProviderAdapter) GetName added in v0.5.4

func (a *ProviderAdapter) GetName() string

GetName returns the provider name

func (*ProviderAdapter) GetType added in v0.5.4

func (a *ProviderAdapter) GetType() ClientType

GetType returns the provider type

func (*ProviderAdapter) IsDebug added in v0.5.4

func (a *ProviderAdapter) IsDebug() bool

IsDebug returns whether debug mode is enabled

func (*ProviderAdapter) SendChatRequest added in v0.5.4

func (a *ProviderAdapter) SendChatRequest(ctx context.Context, req *ProviderChatRequest) (*ChatResponse, error)

SendChatRequest adapts the old interface to the new one

func (*ProviderAdapter) SetDebug added in v0.5.4

func (a *ProviderAdapter) SetDebug(debug bool)

SetDebug enables or disables debug mode

func (*ProviderAdapter) SetModel added in v0.5.4

func (a *ProviderAdapter) SetModel(model string) error

SetModel sets the current model

func (*ProviderAdapter) SupportsReasoning added in v0.5.4

func (a *ProviderAdapter) SupportsReasoning() bool

SupportsReasoning returns whether the provider supports reasoning

func (*ProviderAdapter) SupportsStreaming added in v0.5.4

func (a *ProviderAdapter) SupportsStreaming() bool

SupportsStreaming returns whether the provider supports streaming

func (*ProviderAdapter) SupportsTools added in v0.5.4

func (a *ProviderAdapter) SupportsTools() bool

SupportsTools returns whether the provider supports tools

func (*ProviderAdapter) SupportsVision added in v0.5.4

func (a *ProviderAdapter) SupportsVision() bool

SupportsVision returns whether the provider supports vision

type ProviderChatRequest added in v0.5.4

type ProviderChatRequest struct {
	Messages []Message
	Tools    []Tool
	Options  *RequestOptions
}

ProviderChatRequest represents a unified request structure for providers This extends the basic ChatRequest with provider-specific options

type ProviderInterface added in v0.6.0

type ProviderInterface interface {
	SendChatRequest(messages []Message, tools []Tool, reasoning string, disableThinking bool) (*ChatResponse, error)
	SendChatRequestStream(messages []Message, tools []Tool, reasoning string, disableThinking bool, callback StreamCallback) (*ChatResponse, error)
	CheckConnection() error
	SetDebug(debug bool)
	SetModel(model string) error
	GetModel() string
	GetProvider() string
	GetModelContextLimit() (int, error)
	ListModels(ctx context.Context) ([]ModelInfo, error)
	SupportsVision() bool
	SendVisionRequest(messages []Message, tools []Tool, reasoning string, disableThinking bool) (*ChatResponse, error)
}

ProviderInterface defines the interface that all providers must implement

type RequestOptions added in v0.5.4

type RequestOptions struct {
	Temperature      *float64
	MaxTokens        *int
	TopP             *float64
	FrequencyPenalty *float64
	PresencePenalty  *float64
	StopSequences    []string
	Stream           bool
	ReasoningEffort  string // For reasoning models
	DisableThinking  *bool  // Disable thinking/reasoning mode for thinking-capable models
}

RequestOptions contains optional parameters for requests

type SSEReader added in v0.6.0

type SSEReader struct {
	// contains filtered or unexported fields
}

SSEReader reads Server-Sent Events from a reader

func NewSSEReader added in v0.6.0

func NewSSEReader(r io.Reader, onEvent func(event, data string) error) *SSEReader

NewSSEReader creates a new SSE reader

func (*SSEReader) Read added in v0.6.0

func (r *SSEReader) Read() error

Read processes the SSE stream

func (*SSEReader) ReadWithTimeout added in v0.8.1

func (r *SSEReader) ReadWithTimeout(timeout time.Duration) error

ReadWithTimeout processes the SSE stream with a timeout

type StreamCallback added in v0.6.0

type StreamCallback func(content string, contentType string)

StreamCallback is called for each content chunk received. contentType is "assistant_text" for regular content or "reasoning" for thinking/reasoning content.

type StreamingChatResponse added in v0.6.0

type StreamingChatResponse struct {
	ID      string            `json:"id"`
	Object  string            `json:"object"`
	Created int64             `json:"created"`
	Model   string            `json:"model"`
	Choices []StreamingChoice `json:"choices"`
	Usage   *struct {
		PromptTokens        int     `json:"prompt_tokens"`
		CompletionTokens    int     `json:"completion_tokens"`
		TotalTokens         int     `json:"total_tokens"`
		EstimatedCost       float64 `json:"estimated_cost"`
		PromptTokensDetails struct {
			CachedTokens     int  `json:"cached_tokens"`
			CacheWriteTokens *int `json:"cache_write_tokens"`
		} `json:"prompt_tokens_details,omitempty"`
	} `json:"usage,omitempty"`
}

StreamingChatResponse represents a streaming response chunk

func ParseSSEData added in v0.6.0

func ParseSSEData(data string) (*StreamingChatResponse, error)

ParseSSEData parses SSE data into a streaming response

type StreamingChoice added in v0.6.0

type StreamingChoice struct {
	Index        int            `json:"index"`
	Delta        StreamingDelta `json:"delta"`
	FinishReason *string        `json:"finish_reason"`
}

StreamingChoice represents a streaming response choice

type StreamingDelta added in v0.6.0

type StreamingDelta struct {
	Role             string              `json:"role,omitempty"`
	Content          string              `json:"content,omitempty"`
	Reasoning        string              `json:"reasoning,omitempty"` // GLM reasoning field
	ReasoningContent string              `json:"reasoning_content,omitempty"`
	ReasoningDetails json.RawMessage     `json:"reasoning_details,omitempty"` // Can be string (Minimax) or array (GLM models)
	ToolCalls        []StreamingToolCall `json:"tool_calls,omitempty"`
}

StreamingDelta contains incremental updates

type StreamingResponseBuilder added in v0.6.0

type StreamingResponseBuilder struct {
	// contains filtered or unexported fields
}

StreamingResponseBuilder accumulates streaming chunks into a complete response

func NewStreamingResponseBuilder added in v0.6.0

func NewStreamingResponseBuilder(callback StreamCallback) *StreamingResponseBuilder

NewStreamingResponseBuilder creates a new streaming response builder

func (*StreamingResponseBuilder) GetResponse added in v0.6.0

func (b *StreamingResponseBuilder) GetResponse() *ChatResponse

GetResponse returns the accumulated response

func (*StreamingResponseBuilder) GetTokenGenerationDuration added in v0.6.0

func (b *StreamingResponseBuilder) GetTokenGenerationDuration() time.Duration

GetTokenGenerationDuration returns the duration from first token to last token

func (*StreamingResponseBuilder) ProcessChunk added in v0.6.0

func (b *StreamingResponseBuilder) ProcessChunk(chunk *StreamingChatResponse) error

ProcessChunk processes a streaming chunk and updates the builder state

type StreamingToolCall added in v0.6.0

type StreamingToolCall struct {
	Index    int                        `json:"index"`
	ID       string                     `json:"id,omitempty"`
	Type     string                     `json:"type,omitempty"`
	Function *StreamingToolCallFunction `json:"function,omitempty"`
}

StreamingToolCall represents an incremental tool call update

type StreamingToolCallFunction added in v0.6.0

type StreamingToolCallFunction struct {
	Name      string `json:"name,omitempty"`
	Arguments string `json:"arguments,omitempty"`
}

StreamingToolCallFunction contains function details

type TPSBase added in v0.6.0

type TPSBase struct {
	// contains filtered or unexported fields
}

TPSBase provides a default implementation of TPS tracking methods Other providers can embed this struct to get TPS functionality

func NewTPSBase added in v0.6.0

func NewTPSBase() *TPSBase

NewTPSBase creates a new TPS base with tracker

func (*TPSBase) GetAverageTPS added in v0.6.0

func (t *TPSBase) GetAverageTPS() float64

GetAverageTPS returns the average TPS across all requests

func (*TPSBase) GetLastTPS added in v0.6.0

func (t *TPSBase) GetLastTPS() float64

GetLastTPS returns the most recent TPS measurement

func (*TPSBase) GetTPSStats added in v0.6.0

func (t *TPSBase) GetTPSStats() map[string]float64

GetTPSStats returns comprehensive TPS statistics

func (*TPSBase) GetTracker added in v0.6.0

func (t *TPSBase) GetTracker() *TPSTracker

GetTracker returns the underlying TPS tracker

func (*TPSBase) ResetTPSStats added in v0.6.0

func (t *TPSBase) ResetTPSStats()

ResetTPSStats clears all TPS tracking data

type TPSTracker added in v0.6.0

type TPSTracker struct {
	// contains filtered or unexported fields
}

func NewTPSTracker added in v0.6.0

func NewTPSTracker() *TPSTracker

NewTPSTracker creates a new TPS tracker

func (*TPSTracker) GetAverageTPS added in v0.6.0

func (t *TPSTracker) GetAverageTPS() float64

GetAverageTPS returns the average TPS across all recorded requests

func (*TPSTracker) GetCurrentTPS added in v0.6.0

func (t *TPSTracker) GetCurrentTPS() float64

GetCurrentTPS returns the most recent TPS value

func (*TPSTracker) GetSmoothTPS added in v0.6.0

func (t *TPSTracker) GetSmoothTPS() float64

GetSmoothTPS returns an exponentially smoothed TPS value

func (*TPSTracker) GetStats added in v0.6.0

func (t *TPSTracker) GetStats() map[string]interface{}

GetStats returns comprehensive TPS statistics

func (*TPSTracker) RecordRequest added in v0.6.0

func (t *TPSTracker) RecordRequest(duration time.Duration, completionTokens int) float64

RecordRequest records the timing and token usage of an API request

func (*TPSTracker) Reset added in v0.6.0

func (t *TPSTracker) Reset()

Reset clears all TPS tracking data

type Tool

type Tool struct {
	Type     string `json:"type"`
	Function struct {
		Name        string      `json:"name"`
		Description string      `json:"description"`
		Parameters  interface{} `json:"parameters"`
	} `json:"function"`
}

func GetToolDefinitions

func GetToolDefinitions() []Tool

type ToolCall

type ToolCall struct {
	ID       string `json:"id"`
	Type     string `json:"type"`
	Function struct {
		Name      string `json:"name"`
		Arguments string `json:"arguments"`
	} `json:"function"`
}

type UnifiedProviderWrapper

type UnifiedProviderWrapper struct {
	*TPSBase
	// contains filtered or unexported fields
}

UnifiedProviderWrapper wraps any provider that implements ProviderInterface

func NewUnifiedProviderWrapper

func NewUnifiedProviderWrapper(provider ProviderInterface) *UnifiedProviderWrapper

NewUnifiedProviderWrapper creates a wrapper for any provider

func (*UnifiedProviderWrapper) CheckConnection

func (w *UnifiedProviderWrapper) CheckConnection() error

Forward all other methods to the provider

func (*UnifiedProviderWrapper) GetLastRequestTPS added in v0.6.0

func (w *UnifiedProviderWrapper) GetLastRequestTPS() float64

GetLastRequestTPS returns the TPS for the last API request

func (*UnifiedProviderWrapper) GetModel

func (w *UnifiedProviderWrapper) GetModel() string

func (*UnifiedProviderWrapper) GetModelContextLimit

func (w *UnifiedProviderWrapper) GetModelContextLimit() (int, error)

func (*UnifiedProviderWrapper) GetProvider

func (w *UnifiedProviderWrapper) GetProvider() string

func (*UnifiedProviderWrapper) GetTPSStatistics added in v0.6.0

func (w *UnifiedProviderWrapper) GetTPSStatistics() (float64, float64, int)

GetTPSStatistics returns tokens per second statistics

func (*UnifiedProviderWrapper) GetVisionModel

func (w *UnifiedProviderWrapper) GetVisionModel() string

func (*UnifiedProviderWrapper) ListModels

func (w *UnifiedProviderWrapper) ListModels(ctx context.Context) ([]ModelInfo, error)

func (*UnifiedProviderWrapper) ResetTPSStatistics added in v0.6.0

func (w *UnifiedProviderWrapper) ResetTPSStatistics()

ResetTPSStatistics resets the TPS tracking

func (*UnifiedProviderWrapper) SendChatRequest

func (w *UnifiedProviderWrapper) SendChatRequest(messages []Message, tools []Tool, reasoning string, disableThinking bool) (*ChatResponse, error)

SendChatRequest converts types and forwards to provider

func (*UnifiedProviderWrapper) SendChatRequestStream added in v0.6.0

func (w *UnifiedProviderWrapper) SendChatRequestStream(messages []Message, tools []Tool, reasoning string, disableThinking bool, callback StreamCallback) (*ChatResponse, error)

SendChatRequestStream sends a streaming chat request (not yet implemented for unified providers)

func (*UnifiedProviderWrapper) SendVisionRequest

func (w *UnifiedProviderWrapper) SendVisionRequest(messages []Message, tools []Tool, reasoning string, disableThinking bool) (*ChatResponse, error)

func (*UnifiedProviderWrapper) SetDebug

func (w *UnifiedProviderWrapper) SetDebug(debug bool)

func (*UnifiedProviderWrapper) SetModel

func (w *UnifiedProviderWrapper) SetModel(model string) error

func (*UnifiedProviderWrapper) SupportsVision

func (w *UnifiedProviderWrapper) SupportsVision() bool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL