feat(provider): add support for Grok provider in AI proxy (#2713)

Co-authored-by: 韩贤涛 <601803023@qq.com>
This commit is contained in:
aias00
2025-08-07 17:22:47 +08:00
committed by GitHub
parent b69e3a8f30
commit a1bf1ff009
6 changed files with 354 additions and 39 deletions

View File

@@ -158,6 +158,10 @@ DeepSeek 所对应的 `type` 为 `deepseek`。它并无特有的配置字段。
Groq 所对应的 `type``groq`。它并无特有的配置字段。
#### Grok
Grok 所对应的 `type``grok`。它并无特有的配置字段。
#### 文心一言Baidu
文心一言所对应的 `type``baidu`。它并无特有的配置字段。
@@ -863,6 +867,76 @@ provider:
}
```
### 使用 OpenAI 协议代理 Grok 服务
**配置信息**
```yaml
provider:
type: grok
apiTokens:
- 'YOUR_GROK_API_TOKEN'
```
**请求示例**
```json
{
"messages": [
{
"role": "system",
"content": "You are a helpful assistant that can answer questions and help with tasks."
},
{
"role": "user",
"content": "What is 101*3?"
}
],
"model": "grok-4"
}
```
**响应示例**
```json
{
"id": "a3d1008e-4544-40d4-d075-11527e794e4a",
"object": "chat.completion",
"created": 1752854522,
"model": "grok-4",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "101 multiplied by 3 is 303.",
"refusal": null
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 32,
"completion_tokens": 9,
"total_tokens": 135,
"prompt_tokens_details": {
"text_tokens": 32,
"audio_tokens": 0,
"image_tokens": 0,
"cached_tokens": 6
},
"completion_tokens_details": {
"reasoning_tokens": 94,
"audio_tokens": 0,
"accepted_prediction_tokens": 0,
"rejected_prediction_tokens": 0
},
"num_sources_used": 0
},
"system_fingerprint": "fp_3a7881249c"
}
```
### 使用 OpenAI 协议代理 Claude 服务
**配置信息**

View File

@@ -129,6 +129,10 @@ For DeepSeek, the corresponding `type` is `deepseek`. It has no unique configura
For Groq, the corresponding `type` is `groq`. It has no unique configuration fields.
#### Grok
For Grok, the corresponding `type` is `grok`. It has no unique configuration fields.
#### ERNIE Bot
For ERNIE Bot, the corresponding `type` is `baidu`. It has no unique configuration fields.
@@ -809,6 +813,76 @@ provider:
}
```
### Using OpenAI Protocol Proxy for Grok Service
**Configuration Information**
```yaml
provider:
type: grok
apiTokens:
- "YOUR_GROK_API_TOKEN"
```
**Example Request**
```json
{
"messages": [
{
"role": "system",
"content": "You are a helpful assistant that can answer questions and help with tasks."
},
{
"role": "user",
"content": "What is 101*3?"
}
],
"model": "grok-4"
}
```
**Example Response**
```json
{
"id": "a3d1008e-4544-40d4-d075-11527e794e4a",
"object": "chat.completion",
"created": 1752854522,
"model": "grok-4",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "101 multiplied by 3 is 303.",
"refusal": null
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 32,
"completion_tokens": 9,
"total_tokens": 135,
"prompt_tokens_details": {
"text_tokens": 32,
"audio_tokens": 0,
"image_tokens": 0,
"cached_tokens": 6
},
"completion_tokens_details": {
"reasoning_tokens": 94,
"audio_tokens": 0,
"accepted_prediction_tokens": 0,
"rejected_prediction_tokens": 0
},
"num_sources_used": 0
},
"system_fingerprint": "fp_3a7881249c"
}
```
### Using OpenAI Protocol Proxy for Claude Service
**Configuration Information**

View File

@@ -0,0 +1,75 @@
package provider
import (
"errors"
"net/http"
"strings"
"github.com/alibaba/higress/plugins/wasm-go/extensions/ai-proxy/util"
"github.com/higress-group/wasm-go/pkg/wrapper"
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
)
// grokProvider is the provider for Grok service.
const (
grokDomain = "api.x.ai"
grokChatCompletionPath = "/v1/chat/completions"
)
type grokProviderInitializer struct{}
func (g *grokProviderInitializer) ValidateConfig(config *ProviderConfig) error {
if config.apiTokens == nil || len(config.apiTokens) == 0 {
return errors.New("no apiToken found in provider config")
}
return nil
}
func (g *grokProviderInitializer) DefaultCapabilities() map[string]string {
return map[string]string{
string(ApiNameChatCompletion): grokChatCompletionPath,
}
}
func (g *grokProviderInitializer) CreateProvider(config ProviderConfig) (Provider, error) {
config.setDefaultCapabilities(g.DefaultCapabilities())
return &grokProvider{
config: config,
contextCache: createContextCache(&config),
}, nil
}
type grokProvider struct {
config ProviderConfig
contextCache *contextCache
}
func (g *grokProvider) GetProviderType() string {
return providerTypeGrok
}
func (g *grokProvider) OnRequestHeaders(ctx wrapper.HttpContext, apiName ApiName) error {
g.config.handleRequestHeaders(g, ctx, apiName)
return nil
}
func (g *grokProvider) OnRequestBody(ctx wrapper.HttpContext, apiName ApiName, body []byte) (types.Action, error) {
if !g.config.isSupportedAPI(apiName) {
return types.ActionContinue, errUnsupportedApiName
}
return g.config.handleRequestBody(g, g.contextCache, ctx, apiName, body)
}
func (g *grokProvider) TransformRequestHeaders(ctx wrapper.HttpContext, apiName ApiName, headers http.Header) {
util.OverwriteRequestPathHeaderByCapability(headers, string(apiName), g.config.capabilities)
util.OverwriteRequestHostHeader(headers, grokDomain)
util.OverwriteRequestAuthorizationHeader(headers, "Bearer "+g.config.GetApiTokenInUse(ctx))
headers.Del("Content-Length")
}
func (g *grokProvider) GetApiName(path string) ApiName {
if strings.Contains(path, grokChatCompletionPath) {
return ApiNameChatCompletion
}
return ""
}

View File

@@ -107,6 +107,7 @@ const (
providerTypeQwen = "qwen"
providerTypeOpenAI = "openai"
providerTypeGroq = "groq"
providerTypeGrok = "grok"
providerTypeBaichuan = "baichuan"
providerTypeYi = "yi"
providerTypeDeepSeek = "deepseek"
@@ -182,6 +183,7 @@ var (
providerTypeQwen: &qwenProviderInitializer{},
providerTypeOpenAI: &openaiProviderInitializer{},
providerTypeGroq: &groqProviderInitializer{},
providerTypeGrok: &grokProviderInitializer{},
providerTypeBaichuan: &baichuanProviderInitializer{},
providerTypeYi: &yiProviderInitializer{},
providerTypeDeepSeek: &deepseekProviderInitializer{},