Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 27 additions & 28 deletions components/model/ark/chatmodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
"net/http"
"time"

"github.com/openai/openai-go/option"
"github.com/openai/openai-go/responses"
"github.com/volcengine/volcengine-go-sdk/service/arkruntime"
"github.com/volcengine/volcengine-go-sdk/service/arkruntime/model"

Expand Down Expand Up @@ -246,32 +244,41 @@ func buildResponsesAPIChatModel(config *ChatModelConfig) (*responsesAPIChatModel
return nil, err
}
}
var opts []arkruntime.ConfigOption

var opts []option.RequestOption
if config.Region == "" {
opts = append(opts, arkruntime.WithRegion(defaultRegion))
} else {
opts = append(opts, arkruntime.WithRegion(config.Region))
}

if config.Timeout != nil {
opts = append(opts, option.WithRequestTimeout(*config.Timeout))
opts = append(opts, arkruntime.WithTimeout(*config.Timeout))
} else {
opts = append(opts, option.WithRequestTimeout(defaultTimeout))
opts = append(opts, arkruntime.WithTimeout(defaultTimeout))
}
if config.HTTPClient != nil {
opts = append(opts, option.WithHTTPClient(config.HTTPClient))
opts = append(opts, arkruntime.WithHTTPClient(config.HTTPClient))
}
if config.BaseURL != "" {
opts = append(opts, option.WithBaseURL(config.BaseURL))
opts = append(opts, arkruntime.WithBaseUrl(config.BaseURL))
} else {
opts = append(opts, option.WithBaseURL(defaultBaseURL))
opts = append(opts, arkruntime.WithBaseUrl(defaultBaseURL))
}
if config.RetryTimes != nil {
opts = append(opts, option.WithMaxRetries(*config.RetryTimes))
opts = append(opts, arkruntime.WithRetryTimes(*config.RetryTimes))
} else {
opts = append(opts, option.WithMaxRetries(defaultRetryTimes))
}
if config.APIKey != "" {
opts = append(opts, option.WithAPIKey(config.APIKey))
opts = append(opts, arkruntime.WithRetryTimes(defaultRetryTimes))
}

client := responses.NewResponseService(opts...)
var client *arkruntime.Client
if len(config.APIKey) > 0 {
client = arkruntime.NewClientWithApiKey(config.APIKey, opts...)
} else if config.AccessKey != "" && config.SecretKey != "" {
client = arkruntime.NewClientWithAkSk(config.AccessKey, config.SecretKey, opts...)
} else {
return nil, fmt.Errorf("new client fail, missing credentials: set 'APIKey' or both 'AccessKey' and 'SecretKey'")
}

cm := &responsesAPIChatModel{
client: client,
Expand All @@ -285,22 +292,15 @@ func buildResponsesAPIChatModel(config *ChatModelConfig) (*responsesAPIChatModel
cache: config.Cache,
serviceTier: config.ServiceTier,
}

return cm, nil
}

func checkResponsesAPIConfig(config *ChatModelConfig) error {
if config.Region != "" {
return fmt.Errorf("'Region' is not supported by ResponsesAPI")
}
if config.APIKey == "" {
if config.AccessKey != "" {
return fmt.Errorf("'AccessKey' is not supported by ResponsesAPI")
}
if config.SecretKey != "" {
return fmt.Errorf("'SecretKey' is not supported by ResponsesAPI")
}

if config.APIKey == "" && (config.AccessKey == "" && config.SecretKey == "") {
return fmt.Errorf("missing credentials: set 'APIKey' or both 'AccessKey' and 'SecretKey'")
}

if len(config.Stop) > 0 {
return fmt.Errorf("'Stop' is not supported by ResponsesAPI")
}
Expand Down Expand Up @@ -505,10 +505,9 @@ func (cm *ChatModel) IsCallbacksEnabled() bool {
//
// Note:
// - It is unavailable for doubao models of version 1.6 and above.
// - Currently, only supports calling by ContextAPI.
func (cm *ChatModel) CreatePrefixCache(ctx context.Context, prefix []*schema.Message, ttl int) (info *CacheInfo, err error) {
func (cm *ChatModel) CreatePrefixCache(ctx context.Context, prefix []*schema.Message, ttl int, opts ...fmodel.Option) (info *CacheInfo, err error) {
if cm.respChatModel.cache != nil && ptrFromOrZero(cm.respChatModel.cache.APIType) == ResponsesAPI {
return nil, fmt.Errorf("CreatePrefixCache is not supported by ResponsesAPI")
return cm.respChatModel.createPrefixCacheByResponseAPI(ctx, prefix, ttl, opts...)
}
return cm.createContextByContextAPI(ctx, prefix, ttl, model.ContextModeCommonPrefix, nil)
}
Expand Down
6 changes: 4 additions & 2 deletions components/model/ark/chatmodel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestBindTools(t *testing.T) {
t.Run("chat model force tool call", func(t *testing.T) {
ctx := context.Background()

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

doNothingParams := map[string]*schema.ParameterInfo{
Expand Down Expand Up @@ -193,7 +193,8 @@ func TestCallByResponsesAPI(t *testing.T) {
func TestBuildResponsesAPIChatModel(t *testing.T) {
mockey.PatchConvey("invalid config", t, func() {
_, err := buildResponsesAPIChatModel(&ChatModelConfig{
Stop: []string{"test"},
Stop: []string{"test"},
APIKey: "test",
Cache: &CacheConfig{
APIType: ptrOf(ResponsesAPI),
},
Expand All @@ -203,6 +204,7 @@ func TestBuildResponsesAPIChatModel(t *testing.T) {

mockey.PatchConvey("valid config", t, func() {
_, err := buildResponsesAPIChatModel(&ChatModelConfig{
APIKey: "test",
Cache: &CacheConfig{
APIType: ptrOf(ResponsesAPI),
},
Expand Down
2 changes: 1 addition & 1 deletion components/model/ark/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/openai/openai-go v1.10.1
github.com/smartystreets/goconvey v1.8.1
github.com/stretchr/testify v1.11.1
github.com/volcengine/volcengine-go-sdk v1.1.44
github.com/volcengine/volcengine-go-sdk v1.1.49

)

Expand Down
4 changes: 2 additions & 2 deletions components/model/ark/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/volcengine/volc-sdk-golang v1.0.23 h1:anOslb2Qp6ywnsbyq9jqR0ljuO63kg9PY+4OehIk5R8=
github.com/volcengine/volc-sdk-golang v1.0.23/go.mod h1:AfG/PZRUkHJ9inETvbjNifTDgut25Wbkm2QoYBTbvyU=
github.com/volcengine/volcengine-go-sdk v1.1.44 h1:WLoLlzt67ZlJeow55PPx65/Mh52DewVXqkHcFSodM9w=
github.com/volcengine/volcengine-go-sdk v1.1.44/go.mod h1:oxoVo+A17kvkwPkIeIHPVLjSw7EQAm+l/Vau1YGHN+A=
github.com/volcengine/volcengine-go-sdk v1.1.49 h1:jkk3Zt6uFGiZshrVshsdRvadzuHIf4nLkekIZM+wLkY=
github.com/volcengine/volcengine-go-sdk v1.1.49/go.mod h1:oxoVo+A17kvkwPkIeIHPVLjSw7EQAm+l/Vau1YGHN+A=
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
github.com/x-cray/logrus-prefixed-formatter v0.5.2 h1:00txxvfBM9muc0jiLIEAkAcIMJzfthRT6usrui8uGmg=
Expand Down
Loading
Loading