llm

package
v0.0.0-...-f318275 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ProviderOpenAICompatible = "openai-compatible"
)

Core constants

Variables

This section is empty.

Functions

func CallFunction

func CallFunction(ctx context.Context, client Client, fn *config.LLMFunction, input map[string]interface{}) (string, error)

Function-related functions

func EstimateTokens

func EstimateTokens(requestJSON []byte) int

func HandleRetryAfter

func HandleRetryAfter(retryAfter time.Duration)

HandleRetryAfter handles 429 responses by waiting for the specified duration

Types

type APIError

type APIError struct {
	Message string `json:"message"`
	Type    string `json:"type"`
	Code    int    `json:"code,omitempty"`
}

type APIFunction

type APIFunction struct {
	Name        string     `json:"name"`
	Description string     `json:"description"`
	Parameters  Parameters `json:"parameters"`
}

API-related structures

type ChatRequest

type ChatRequest struct {
	Model     string     `json:"model"`
	Messages  []Message  `json:"messages"`
	Functions []Function `json:"tools,omitempty"`
}

Request/Response structures

type ChatResponse

type ChatResponse struct {
	Choices []MessageChoice `json:"choices"`
	Error   *APIError       `json:"error,omitempty"`
}

type Client

type Client interface {
	GenerateText(ctx context.Context, systemPrompt, userPrompt string, functions []APIFunction) (string, error)
}

Core interfaces

func New

func New(cfg *config.LLMConfig) (Client, error)

type Function

type Function struct {
	Type     string      `json:"type"`
	Function FunctionDef `json:"function"`
}

type FunctionCall

type FunctionCall struct {
	Function struct {
		Name      string `json:"name"`
		Arguments string `json:"arguments"`
	} `json:"function"`
}

type FunctionCallArguments

type FunctionCallArguments struct {
	File                  string `json:"file"`
	Status                string `json:"status"`
	Diff                  string `json:"diff"`
	HasSignificantChanges bool   `json:"hasSignificantChanges"`
	Summary               string `json:"summary"`
}

type FunctionChoice

type FunctionChoice struct {
	Type     string `json:"type,omitempty"`
	Function *struct {
		Name string `json:"name,omitempty"`
	} `json:"function,omitempty"`
}

type FunctionDef

type FunctionDef struct {
	Name        string     `json:"name"`
	Description string     `json:"description"`
	Parameters  Parameters `json:"parameters"`
}

type Message

type Message struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

type MessageChoice

type MessageChoice struct {
	Message MessageResponse `json:"message"`
	Index   int             `json:"index"`
	Role    string          `json:"role,omitempty"`
	Content string          `json:"content,omitempty"`
}

type MessageResponse

type MessageResponse struct {
	Content       string         `json:"content"`
	FunctionCalls []FunctionCall `json:"tool_calls,omitempty"` //nolint:tagliatelle // Following OpenAI API spec
}

type OpenAIClient

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

Primary client structure

func (*OpenAIClient) GenerateText

func (c *OpenAIClient) GenerateText(ctx context.Context, systemPrompt, userPrompt string, apiFunctions []APIFunction) (string, error)

type Parameters

type Parameters struct {
	Type       string              `json:"type"`
	Properties map[string]Property `json:"properties"`
	Required   []string            `json:"required"`
}

type Property

type Property struct {
	Type        string   `json:"type"`
	Description string   `json:"description"`
	Enum        []string `json:"enum,omitempty"`
}

type RateLimitInfo

type RateLimitInfo struct {
	RemainingTokens   int
	TokensResetIn     time.Duration
	RemainingRequests int
	RequestsResetIn   time.Duration
	RetryAfter        time.Duration // Only set when receiving 429
}

RateLimitInfo contains rate limit information from API headers

type RateLimiter

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

RateLimiter handles rate limiting for LLM API calls

func NewRateLimiter

func NewRateLimiter() *RateLimiter

NewRateLimiter creates a new rate limiter instance

func (*RateLimiter) UpdateLimits

func (rl *RateLimiter) UpdateLimits(info RateLimitInfo)

UpdateLimits updates the rate limiter with new information from API headers

func (*RateLimiter) WaitForCapacity

func (rl *RateLimiter) WaitForCapacity()

WaitForCapacity waits until there's capacity to make a request

type WaitInfo

type WaitInfo struct {
	NeedsToWait     bool
	RemainingTokens int
	WaitTime        time.Duration
	ResetAt         time.Time
}

WaitInfo contains information about why we're waiting

Jump to

Keyboard shortcuts

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