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