blob: 2fd3bf3325e810401856295734009830997639f3 [file] [log] [blame]
iomodoa97eb222025-07-26 11:18:17 +04001package llm
2
3import (
4 "context"
5 "time"
6)
7
8// LLMProvider defines the interface that all LLM providers must implement
9type LLMProvider interface {
10 // ChatCompletion creates a chat completion
11 ChatCompletion(ctx context.Context, req ChatCompletionRequest) (*ChatCompletionResponse, error)
12
13 // CreateEmbeddings generates embeddings for the given input
14 CreateEmbeddings(ctx context.Context, req EmbeddingRequest) (*EmbeddingResponse, error)
15
16 // Close performs any necessary cleanup
17 Close() error
18}
19
20// ProviderFactory creates LLM provider instances
21type ProviderFactory interface {
22 // CreateProvider creates a new LLM provider instance
23 CreateProvider(config Config) (LLMProvider, error)
24
25 // SupportsProvider checks if the factory supports the given provider
26 SupportsProvider(provider Provider) bool
27}
28
29// Provider represents different LLM service providers
30type Provider string
31
32const (
33 ProviderOpenAI Provider = "openai"
34 ProviderXAI Provider = "xai"
35 ProviderClaude Provider = "claude"
36 ProviderGemini Provider = "gemini"
37 ProviderLocal Provider = "local"
38)
39
40// Role represents the role of a message participant
41type Role string
42
43const (
44 RoleSystem Role = "system"
45 RoleUser Role = "user"
46 RoleAssistant Role = "assistant"
47 RoleTool Role = "tool"
48)
49
50// Message represents a single message in a conversation
51type Message struct {
52 Role Role `json:"role"`
53 Content string `json:"content"`
54 ToolCalls []ToolCall `json:"tool_calls,omitempty"`
55 ToolCallID string `json:"tool_call_id,omitempty"`
56 Name string `json:"name,omitempty"`
57}
58
59// ToolCall represents a function/tool call request
60type ToolCall struct {
61 ID string `json:"id"`
62 Type string `json:"type"`
63 Function Function `json:"function"`
64}
65
66// Function represents a function definition
67type Function struct {
68 Name string `json:"name"`
69 Description string `json:"description,omitempty"`
70 Parameters map[string]interface{} `json:"parameters,omitempty"`
71}
72
73// Tool represents a tool that can be called by the model
74type Tool struct {
75 Type string `json:"type"`
76 Function Function `json:"function"`
77}
78
79// ChatCompletionRequest represents a request to complete a chat conversation
80type ChatCompletionRequest struct {
81 Model string `json:"model"`
82 Messages []Message `json:"messages"`
83 MaxTokens *int `json:"max_tokens,omitempty"`
84 Temperature *float64 `json:"temperature,omitempty"`
85 TopP *float64 `json:"top_p,omitempty"`
86 N *int `json:"n,omitempty"`
87 Stream *bool `json:"stream,omitempty"`
88 Stop []string `json:"stop,omitempty"`
89 PresencePenalty *float64 `json:"presence_penalty,omitempty"`
90 FrequencyPenalty *float64 `json:"frequency_penalty,omitempty"`
91 LogitBias map[string]int `json:"logit_bias,omitempty"`
92 User string `json:"user,omitempty"`
93 Tools []Tool `json:"tools,omitempty"`
94 ToolChoice interface{} `json:"tool_choice,omitempty"`
95 ResponseFormat *ResponseFormat `json:"response_format,omitempty"`
96 Seed *int64 `json:"seed,omitempty"`
97 ExtraParams map[string]interface{} `json:"-"` // For provider-specific parameters
98}
99
100// ResponseFormat specifies the format of the response
101type ResponseFormat struct {
102 Type string `json:"type"` // "text" or "json_object"
103}
104
105// ChatCompletionResponse represents a response from a chat completion request
106type ChatCompletionResponse struct {
107 ID string `json:"id"`
108 Object string `json:"object"`
109 Created int64 `json:"created"`
110 Model string `json:"model"`
111 SystemFingerprint string `json:"system_fingerprint,omitempty"`
112 Choices []ChatCompletionChoice `json:"choices"`
113 Usage Usage `json:"usage"`
114 Provider Provider `json:"provider"`
115}
116
117// ChatCompletionChoice represents a single choice in a chat completion response
118type ChatCompletionChoice struct {
119 Index int `json:"index"`
120 Message Message `json:"message"`
121 Logprobs *Logprobs `json:"logprobs,omitempty"`
122 FinishReason string `json:"finish_reason"`
123 Delta *Message `json:"delta,omitempty"` // For streaming
124 ExtraData map[string]interface{} `json:"-"` // For provider-specific data
125}
126
127// Logprobs represents log probability information
128type Logprobs struct {
129 Content []LogprobContent `json:"content,omitempty"`
130}
131
132// LogprobContent represents content with log probabilities
133type LogprobContent struct {
134 Token string `json:"token"`
135 Logprob float64 `json:"logprob"`
136 Bytes []int `json:"bytes,omitempty"`
137 TopLogprobs []TopLogprob `json:"top_logprobs,omitempty"`
138}
139
140// TopLogprob represents a top log probability
141type TopLogprob struct {
142 Token string `json:"token"`
143 Logprob float64 `json:"logprob"`
144 Bytes []int `json:"bytes,omitempty"`
145}
146
147// Usage represents token usage information
148type Usage struct {
149 PromptTokens int `json:"prompt_tokens"`
150 CompletionTokens int `json:"completion_tokens"`
151 TotalTokens int `json:"total_tokens"`
152}
153
154// EmbeddingRequest represents a request to generate embeddings
155type EmbeddingRequest struct {
156 Input interface{} `json:"input"` // string, []string, or []int
157 Model string `json:"model"`
158 EncodingFormat string `json:"encoding_format,omitempty"`
159 Dimensions *int `json:"dimensions,omitempty"`
160 User string `json:"user,omitempty"`
161 ExtraParams map[string]interface{} `json:"-"` // For provider-specific parameters
162}
163
164// EmbeddingResponse represents a response from an embedding request
165type EmbeddingResponse struct {
166 Object string `json:"object"`
167 Data []Embedding `json:"data"`
168 Usage Usage `json:"usage"`
169 Model string `json:"model"`
170 Provider Provider `json:"provider"`
171}
172
173// Embedding represents a single embedding
174type Embedding struct {
175 Object string `json:"object"`
176 Embedding []float64 `json:"embedding"`
177 Index int `json:"index"`
178}
179
180// ModelInfo represents information about an available model
181type ModelInfo struct {
182 ID string `json:"id"`
183 Object string `json:"object"`
184 Created int64 `json:"created"`
185 OwnedBy string `json:"owned_by"`
186 Permission []ModelPermission `json:"permission"`
187 Root string `json:"root"`
188 Parent string `json:"parent"`
189 Provider Provider `json:"provider"`
190 ExtraData map[string]interface{} `json:"-"` // For provider-specific data
191}
192
193// ModelPermission represents permissions for a model
194type ModelPermission struct {
195 ID string `json:"id"`
196 Object string `json:"object"`
197 Created int64 `json:"created"`
198 AllowCreateEngine bool `json:"allow_create_engine"`
199 AllowSampling bool `json:"allow_sampling"`
200 AllowLogprobs bool `json:"allow_logprobs"`
201 AllowSearchIndices bool `json:"allow_search_indices"`
202 AllowView bool `json:"allow_view"`
203 AllowFineTuning bool `json:"allow_fine_tuning"`
204 Organization string `json:"organization"`
205 Group string `json:"group"`
206 IsBlocking bool `json:"is_blocking"`
207}
208
209// Error represents an error response from an LLM provider
210type Error struct {
211 Error struct {
212 Message string `json:"message"`
213 Type string `json:"type"`
214 Code string `json:"code,omitempty"`
215 Param string `json:"param,omitempty"`
216 } `json:"error"`
217}
218
219// Config represents configuration for an LLM provider
220type Config struct {
221 Provider Provider `json:"provider"`
222 APIKey string `json:"api_key"`
223 BaseURL string `json:"base_url,omitempty"`
224 Timeout time.Duration `json:"timeout,omitempty"`
225 MaxRetries int `json:"max_retries,omitempty"`
226 ExtraConfig map[string]interface{} `json:"extra_config,omitempty"`
227}
228
229// StreamResponse represents a streaming response chunk
230type StreamResponse struct {
231 ID string `json:"id"`
232 Object string `json:"object"`
233 Created int64 `json:"created"`
234 Model string `json:"model"`
235 SystemFingerprint string `json:"system_fingerprint,omitempty"`
236 Choices []ChatCompletionChoice `json:"choices"`
237 Usage *Usage `json:"usage,omitempty"`
238 Provider Provider `json:"provider"`
239}
240
241// DefaultConfigs provides default configurations for different providers
242var DefaultConfigs = map[Provider]Config{
243 ProviderOpenAI: {
244 Provider: ProviderOpenAI,
245 BaseURL: "https://api.openai.com/v1",
246 Timeout: 30 * time.Second,
247 MaxRetries: 3,
248 },
249 ProviderXAI: {
250 Provider: ProviderXAI,
251 BaseURL: "https://api.x.ai/v1",
252 Timeout: 30 * time.Second,
253 MaxRetries: 3,
254 },
255 ProviderClaude: {
256 Provider: ProviderClaude,
257 BaseURL: "https://api.anthropic.com/v1",
258 Timeout: 30 * time.Second,
259 MaxRetries: 3,
260 },
261 ProviderGemini: {
262 Provider: ProviderGemini,
263 BaseURL: "https://generativelanguage.googleapis.com/v1",
264 Timeout: 30 * time.Second,
265 MaxRetries: 3,
266 },
267 ProviderLocal: {
268 Provider: ProviderLocal,
269 BaseURL: "http://localhost:11434",
270 Timeout: 60 * time.Second,
271 MaxRetries: 1,
272 },
273}