Skip to content

Commit

Permalink
Ai proxy support coze (#1387)
Browse files Browse the repository at this point in the history
  • Loading branch information
rinfx authored Oct 14, 2024
1 parent 1a53c7b commit 952c9ec
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
12 changes: 12 additions & 0 deletions plugins/wasm-go/extensions/ai-proxy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,18 @@ provider:
timeout: 1200000
```
### 使用 original 协议代理 Coze 应用
**配置信息**
```yaml
provider:
type: coze
apiTokens:
- YOUR_COZE_API_KEY
protocol: original
```
### 使用月之暗面配合其原生的文件上下文
提前上传文件至月之暗面,以文件内容作为上下文使用其 AI 服务。
Expand Down
10 changes: 10 additions & 0 deletions plugins/wasm-go/extensions/ai-proxy/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,16 @@ providers:
timeout: 1200000
```
### Using original Protocol Proxy for Coze applications
```yaml
provider:
type: coze
apiTokens:
- YOUR_COZE_API_KEY
protocol: original
```
### Utilizing Moonshot with its Native File Context
Upload files to Moonshot in advance and use its AI services based on file content.
Expand Down
44 changes: 44 additions & 0 deletions plugins/wasm-go/extensions/ai-proxy/provider/coze.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package provider

import (
"errors"

"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-proxy/util"
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
)

const (
cozeDomain = "api.coze.cn"
)

type cozeProviderInitializer struct{}

func (m *cozeProviderInitializer) ValidateConfig(config ProviderConfig) error {
if config.apiTokens == nil || len(config.apiTokens) == 0 {
return errors.New("no apiToken found in provider config")
}
return nil
}

func (m *cozeProviderInitializer) CreateProvider(config ProviderConfig) (Provider, error) {
return &cozeProvider{
config: config,
contextCache: createContextCache(&config),
}, nil
}

type cozeProvider struct {
config ProviderConfig
contextCache *contextCache
}

func (m *cozeProvider) GetProviderType() string {
return providerTypeCoze
}

func (m *cozeProvider) OnRequestHeaders(ctx wrapper.HttpContext, apiName ApiName, log wrapper.Log) (types.Action, error) {
_ = util.OverwriteRequestHost(cozeDomain)
_ = util.OverwriteRequestAuthorization("Bearer " + m.config.GetRandomToken())
return types.ActionContinue, nil
}
2 changes: 2 additions & 0 deletions plugins/wasm-go/extensions/ai-proxy/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const (
providerTypeMistral = "mistral"
providerTypeCohere = "cohere"
providerTypeDoubao = "doubao"
providerTypeCoze = "coze"

protocolOpenAI = "openai"
protocolOriginal = "original"
Expand Down Expand Up @@ -101,6 +102,7 @@ var (
providerTypeMistral: &mistralProviderInitializer{},
providerTypeCohere: &cohereProviderInitializer{},
providerTypeDoubao: &doubaoProviderInitializer{},
providerTypeCoze: &cozeProviderInitializer{},
}
)

Expand Down

0 comments on commit 952c9ec

Please sign in to comment.