-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.go
More file actions
305 lines (261 loc) · 7.89 KB
/
Copy pathmodel.go
File metadata and controls
305 lines (261 loc) · 7.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package neko
import (
"context"
"encoding/json"
"fmt"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/option"
)
// Model is the interface for LLM backends.
type Model interface {
Generate(ctx context.Context, messages []Message, opts ...GenerateOption) (*Message, error)
ModelID() string
}
// GenerateOptions holds generation parameters.
type GenerateOptions struct {
StopSequences []string
Tools []Tool
Temperature float64
MaxTokens int64
}
// GenerateOption is a functional option for Generate.
type GenerateOption func(*GenerateOptions)
// WithStopSequences sets stop sequences.
func WithStopSequences(seqs ...string) GenerateOption {
return func(o *GenerateOptions) { o.StopSequences = seqs }
}
// WithTools provides tools for function calling.
func WithTools(tools ...Tool) GenerateOption {
return func(o *GenerateOptions) { o.Tools = tools }
}
// WithTemperature sets generation temperature.
func WithTemperature(t float64) GenerateOption {
return func(o *GenerateOptions) { o.Temperature = t }
}
// WithMaxTokens sets max output tokens.
func WithMaxTokens(n int64) GenerateOption {
return func(o *GenerateOptions) { o.MaxTokens = n }
}
// OpenAIModel implements Model using official OpenAI Go SDK.
type OpenAIModel struct {
client openai.Client
modelID string
temperature float64
maxTokens int64
}
// OpenAIOption configures OpenAIModel.
type OpenAIOption func(*OpenAIModel)
// WithOpenAITemperature sets default temperature.
func WithOpenAITemperature(t float64) OpenAIOption {
return func(m *OpenAIModel) { m.temperature = t }
}
// WithOpenAIMaxTokens sets default max tokens.
func WithOpenAIMaxTokens(n int64) OpenAIOption {
return func(m *OpenAIModel) { m.maxTokens = n }
}
// NewOpenAIModel creates an OpenAI model using the official SDK.
func NewOpenAIModel(modelID, apiKey string, opts ...OpenAIOption) *OpenAIModel {
client := openai.NewClient(option.WithAPIKey(apiKey))
m := &OpenAIModel{
client: client,
modelID: modelID,
temperature: 0.7,
maxTokens: 4096,
}
for _, opt := range opts {
opt(m)
}
return m
}
// NewOpenAIModelWithBaseURL creates an OpenAI-compatible model with custom base URL.
func NewOpenAIModelWithBaseURL(modelID, apiKey, baseURL string, opts ...OpenAIOption) *OpenAIModel {
client := openai.NewClient(
option.WithAPIKey(apiKey),
option.WithBaseURL(baseURL),
)
m := &OpenAIModel{
client: client,
modelID: modelID,
temperature: 0.7,
maxTokens: 4096,
}
for _, opt := range opts {
opt(m)
}
return m
}
func (m *OpenAIModel) ModelID() string { return m.modelID }
// Generate sends messages to OpenAI and returns response.
func (m *OpenAIModel) Generate(ctx context.Context, messages []Message, opts ...GenerateOption) (*Message, error) {
options := &GenerateOptions{
Temperature: m.temperature,
MaxTokens: m.maxTokens,
}
for _, opt := range opts {
opt(options)
}
// Convert messages to OpenAI format
oaiMsgs := m.convertMessages(messages)
// Build request params
params := openai.ChatCompletionNewParams{
Model: m.modelID,
Messages: oaiMsgs,
Temperature: openai.Float(options.Temperature),
MaxTokens: openai.Int(options.MaxTokens),
}
// Add stop sequences if provided
if len(options.StopSequences) > 0 {
params.Stop = openai.ChatCompletionNewParamsStopUnion{
OfStringArray: options.StopSequences,
}
}
// Add tools if provided
if len(options.Tools) > 0 {
params.Tools = m.convertTools(options.Tools)
}
// Make the API call
resp, err := m.client.Chat.Completions.New(ctx, params)
if err != nil {
return nil, fmt.Errorf("openai completion failed: %w", err)
}
if len(resp.Choices) == 0 {
return nil, fmt.Errorf("no response choices returned")
}
choice := resp.Choices[0]
result := &Message{
Role: RoleAssistant,
Content: choice.Message.Content,
TokenUsage: &TokenUsage{
InputTokens: int(resp.Usage.PromptTokens),
OutputTokens: int(resp.Usage.CompletionTokens),
},
}
// Parse tool calls if present
if len(choice.Message.ToolCalls) > 0 {
for _, tc := range choice.Message.ToolCalls {
var args map[string]any
if err := json.Unmarshal([]byte(tc.Function.Arguments), &args); err != nil {
args = map[string]any{"raw": tc.Function.Arguments}
}
result.ToolCalls = append(result.ToolCalls, ToolCall{
ID: tc.ID,
Name: tc.Function.Name,
Arguments: args,
})
}
}
return result, nil
}
func (m *OpenAIModel) convertMessages(messages []Message) []openai.ChatCompletionMessageParamUnion {
result := make([]openai.ChatCompletionMessageParamUnion, 0, len(messages))
for _, msg := range messages {
switch msg.Role {
case RoleSystem:
result = append(result, openai.SystemMessage(msg.Content))
case RoleUser:
result = append(result, openai.UserMessage(msg.Content))
case RoleAssistant:
result = append(result, openai.AssistantMessage(msg.Content))
case RoleTool:
// Tool messages converted to user messages (like Python version)
result = append(result, openai.UserMessage(msg.Content))
}
}
return result
}
func (m *OpenAIModel) convertTools(tools []Tool) []openai.ChatCompletionToolUnionParam {
result := make([]openai.ChatCompletionToolUnionParam, 0, len(tools))
for _, tool := range tools {
props := make(map[string]any)
required := []string{}
for name, input := range tool.Inputs() {
props[name] = map[string]any{
"type": input.Type,
"description": input.Description,
}
if input.Required {
required = append(required, name)
}
}
result = append(result, openai.ChatCompletionToolUnionParam{
OfFunction: &openai.ChatCompletionFunctionToolParam{
Function: openai.FunctionDefinitionParam{
Name: tool.Name(),
Description: openai.String(tool.Description()),
Parameters: openai.FunctionParameters{
"type": "object",
"properties": props,
"required": required,
},
},
},
})
}
return result
}
// StreamingModel extends Model with streaming support.
type StreamingModel interface {
Model
GenerateStream(ctx context.Context, messages []Message, opts ...GenerateOption) (<-chan StreamDelta, error)
}
// StreamDelta represents a streaming chunk.
type StreamDelta struct {
Content string `json:"content,omitempty"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
Done bool `json:"done"`
Error error `json:"error,omitempty"`
}
// GenerateStream implements streaming generation using official SDK.
func (m *OpenAIModel) GenerateStream(ctx context.Context, messages []Message, opts ...GenerateOption) (<-chan StreamDelta, error) {
options := &GenerateOptions{
Temperature: m.temperature,
MaxTokens: m.maxTokens,
}
for _, opt := range opts {
opt(options)
}
oaiMsgs := m.convertMessages(messages)
params := openai.ChatCompletionNewParams{
Model: m.modelID,
Messages: oaiMsgs,
Temperature: openai.Float(options.Temperature),
MaxTokens: openai.Int(options.MaxTokens),
}
if len(options.StopSequences) > 0 {
params.Stop = openai.ChatCompletionNewParamsStopUnion{
OfStringArray: options.StopSequences,
}
}
stream := m.client.Chat.Completions.NewStreaming(ctx, params)
ch := make(chan StreamDelta)
go func() {
defer close(ch)
acc := openai.ChatCompletionAccumulator{}
for stream.Next() {
chunk := stream.Current()
acc.AddChunk(chunk)
// Send content delta
if len(chunk.Choices) > 0 && chunk.Choices[0].Delta.Content != "" {
ch <- StreamDelta{Content: chunk.Choices[0].Delta.Content}
}
// Check for finished tool calls
if tool, ok := acc.JustFinishedToolCall(); ok {
var args map[string]any
json.Unmarshal([]byte(tool.Arguments), &args)
ch <- StreamDelta{
ToolCalls: []ToolCall{{
ID: tool.ID,
Name: tool.Name,
Arguments: args,
}},
}
}
}
if stream.Err() != nil {
ch <- StreamDelta{Error: stream.Err(), Done: true}
return
}
ch <- StreamDelta{Done: true}
}()
return ch, nil
}