Skip to content

Commit d5f949e

Browse files
committed
refactor: use arkruntime response api for ark
openai response api & add context prefix cache for response api
1 parent bd6375a commit d5f949e

File tree

11 files changed

+971
-999
lines changed

11 files changed

+971
-999
lines changed

components/model/ark/chatmodel.go

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import (
2424
"net/http"
2525
"time"
2626

27-
"github.com/openai/openai-go/option"
28-
"github.com/openai/openai-go/responses"
2927
"github.com/volcengine/volcengine-go-sdk/service/arkruntime"
3028
"github.com/volcengine/volcengine-go-sdk/service/arkruntime/model"
3129

@@ -246,32 +244,41 @@ func buildResponsesAPIChatModel(config *ChatModelConfig) (*responsesAPIChatModel
246244
return nil, err
247245
}
248246
}
247+
var opts []arkruntime.ConfigOption
249248

250-
var opts []option.RequestOption
249+
if config.Region == "" {
250+
opts = append(opts, arkruntime.WithRegion(defaultRegion))
251+
} else {
252+
opts = append(opts, arkruntime.WithRegion(config.Region))
253+
}
251254

252255
if config.Timeout != nil {
253-
opts = append(opts, option.WithRequestTimeout(*config.Timeout))
256+
opts = append(opts, arkruntime.WithTimeout(*config.Timeout))
254257
} else {
255-
opts = append(opts, option.WithRequestTimeout(defaultTimeout))
258+
opts = append(opts, arkruntime.WithTimeout(defaultTimeout))
256259
}
257260
if config.HTTPClient != nil {
258-
opts = append(opts, option.WithHTTPClient(config.HTTPClient))
261+
opts = append(opts, arkruntime.WithHTTPClient(config.HTTPClient))
259262
}
260263
if config.BaseURL != "" {
261-
opts = append(opts, option.WithBaseURL(config.BaseURL))
264+
opts = append(opts, arkruntime.WithBaseUrl(config.BaseURL))
262265
} else {
263-
opts = append(opts, option.WithBaseURL(defaultBaseURL))
266+
opts = append(opts, arkruntime.WithBaseUrl(defaultBaseURL))
264267
}
265268
if config.RetryTimes != nil {
266-
opts = append(opts, option.WithMaxRetries(*config.RetryTimes))
269+
opts = append(opts, arkruntime.WithRetryTimes(*config.RetryTimes))
267270
} else {
268-
opts = append(opts, option.WithMaxRetries(defaultRetryTimes))
269-
}
270-
if config.APIKey != "" {
271-
opts = append(opts, option.WithAPIKey(config.APIKey))
271+
opts = append(opts, arkruntime.WithRetryTimes(defaultRetryTimes))
272272
}
273273

274-
client := responses.NewResponseService(opts...)
274+
var client *arkruntime.Client
275+
if len(config.APIKey) > 0 {
276+
client = arkruntime.NewClientWithApiKey(config.APIKey, opts...)
277+
} else if config.AccessKey != "" && config.SecretKey != "" {
278+
client = arkruntime.NewClientWithAkSk(config.AccessKey, config.SecretKey, opts...)
279+
} else {
280+
return nil, fmt.Errorf("new arkruntime client fail, missing credentials: set 'APIKey' or both 'AccessKey' and 'SecretKey'")
281+
}
275282

276283
cm := &responsesAPIChatModel{
277284
client: client,
@@ -285,22 +292,15 @@ func buildResponsesAPIChatModel(config *ChatModelConfig) (*responsesAPIChatModel
285292
cache: config.Cache,
286293
serviceTier: config.ServiceTier,
287294
}
288-
289295
return cm, nil
290296
}
291297

292298
func checkResponsesAPIConfig(config *ChatModelConfig) error {
293-
if config.Region != "" {
294-
return fmt.Errorf("'Region' is not supported by ResponsesAPI")
295-
}
296-
if config.APIKey == "" {
297-
if config.AccessKey != "" {
298-
return fmt.Errorf("'AccessKey' is not supported by ResponsesAPI")
299-
}
300-
if config.SecretKey != "" {
301-
return fmt.Errorf("'SecretKey' is not supported by ResponsesAPI")
302-
}
299+
300+
if config.APIKey == "" && (config.AccessKey == "" && config.SecretKey == "") {
301+
return fmt.Errorf("missing credentials: set 'APIKey' or both 'AccessKey' and 'SecretKey'")
303302
}
303+
304304
if len(config.Stop) > 0 {
305305
return fmt.Errorf("'Stop' is not supported by ResponsesAPI")
306306
}
@@ -505,10 +505,9 @@ func (cm *ChatModel) IsCallbacksEnabled() bool {
505505
//
506506
// Note:
507507
// - It is unavailable for doubao models of version 1.6 and above.
508-
// - Currently, only supports calling by ContextAPI.
509-
func (cm *ChatModel) CreatePrefixCache(ctx context.Context, prefix []*schema.Message, ttl int) (info *CacheInfo, err error) {
508+
func (cm *ChatModel) CreatePrefixCache(ctx context.Context, prefix []*schema.Message, ttl int, opts ...fmodel.Option) (info *CacheInfo, err error) {
510509
if cm.respChatModel.cache != nil && ptrFromOrZero(cm.respChatModel.cache.APIType) == ResponsesAPI {
511-
return nil, fmt.Errorf("CreatePrefixCache is not supported by ResponsesAPI")
510+
return cm.respChatModel.createPrefixCacheByResponseAPI(ctx, prefix, ttl, opts...)
512511
}
513512
return cm.createContextByContextAPI(ctx, prefix, ttl, model.ContextModeCommonPrefix, nil)
514513
}

components/model/ark/chatmodel_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestBindTools(t *testing.T) {
3030
t.Run("chat model force tool call", func(t *testing.T) {
3131
ctx := context.Background()
3232

33-
chatModel, err := NewChatModel(ctx, &ChatModelConfig{Model: "gpt-3.5-turbo"})
33+
chatModel, err := NewChatModel(ctx, &ChatModelConfig{Model: "gpt-3.5-turbo", APIKey: "test"})
3434
assert.NoError(t, err)
3535

3636
doNothingParams := map[string]*schema.ParameterInfo{
@@ -193,7 +193,8 @@ func TestCallByResponsesAPI(t *testing.T) {
193193
func TestBuildResponsesAPIChatModel(t *testing.T) {
194194
mockey.PatchConvey("invalid config", t, func() {
195195
_, err := buildResponsesAPIChatModel(&ChatModelConfig{
196-
Stop: []string{"test"},
196+
Stop: []string{"test"},
197+
APIKey: "test",
197198
Cache: &CacheConfig{
198199
APIType: ptrOf(ResponsesAPI),
199200
},
@@ -203,6 +204,7 @@ func TestBuildResponsesAPIChatModel(t *testing.T) {
203204

204205
mockey.PatchConvey("valid config", t, func() {
205206
_, err := buildResponsesAPIChatModel(&ChatModelConfig{
207+
APIKey: "test",
206208
Cache: &CacheConfig{
207209
APIType: ptrOf(ResponsesAPI),
208210
},

components/model/ark/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/openai/openai-go v1.10.1
1111
github.com/smartystreets/goconvey v1.8.1
1212
github.com/stretchr/testify v1.11.1
13-
github.com/volcengine/volcengine-go-sdk v1.1.44
13+
github.com/volcengine/volcengine-go-sdk v1.1.48
1414

1515
)
1616

components/model/ark/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ github.com/volcengine/volc-sdk-golang v1.0.23 h1:anOslb2Qp6ywnsbyq9jqR0ljuO63kg9
155155
github.com/volcengine/volc-sdk-golang v1.0.23/go.mod h1:AfG/PZRUkHJ9inETvbjNifTDgut25Wbkm2QoYBTbvyU=
156156
github.com/volcengine/volcengine-go-sdk v1.1.44 h1:WLoLlzt67ZlJeow55PPx65/Mh52DewVXqkHcFSodM9w=
157157
github.com/volcengine/volcengine-go-sdk v1.1.44/go.mod h1:oxoVo+A17kvkwPkIeIHPVLjSw7EQAm+l/Vau1YGHN+A=
158+
github.com/volcengine/volcengine-go-sdk v1.1.47 h1:aSHWIfo0fKhG+V6IO76W9Cf+1fzp3pykNoT5ulBVMsw=
159+
github.com/volcengine/volcengine-go-sdk v1.1.47/go.mod h1:oxoVo+A17kvkwPkIeIHPVLjSw7EQAm+l/Vau1YGHN+A=
160+
github.com/volcengine/volcengine-go-sdk v1.1.48 h1:eHffKYHLUMKdZDmUQgmD1+dEzoVTIsiSlNNdnmEBs8A=
161+
github.com/volcengine/volcengine-go-sdk v1.1.48/go.mod h1:oxoVo+A17kvkwPkIeIHPVLjSw7EQAm+l/Vau1YGHN+A=
158162
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
159163
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
160164
github.com/x-cray/logrus-prefixed-formatter v0.5.2 h1:00txxvfBM9muc0jiLIEAkAcIMJzfthRT6usrui8uGmg=

0 commit comments

Comments
 (0)