-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(llm-bridge): add x.ai provider (#927)
add [x.ai](https://x.ai) support - docs: https://docs.x.ai/api#introduction > [!CAUTION] > Function calling is currently not working due to an API bug. `/chat/completions` endpoint works, but the function calling feature is still affected by the x.ai API error. [Official Docs](https://docs.x.ai/docs/guides#function-calling) state that the `finish_reason` should be `tool_calls`, but API returns `stop`, this breaks the system. ![image](https://github.com/user-attachments/assets/ec92123a-53ac-470a-b92a-db8b7ff31afe) We will fix it as soon as x.ai resolves the API bug.
- Loading branch information
1 parent
3c2ad67
commit 4dcfc56
Showing
9 changed files
with
187 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,3 +44,7 @@ bridge: | |
anthropic: | ||
api_key: | ||
model: | ||
|
||
xai: | ||
api_key: | ||
model: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Package xai is the x.ai provider | ||
package xai | ||
|
||
import ( | ||
"context" | ||
|
||
_ "github.com/joho/godotenv/autoload" | ||
"github.com/sashabaranov/go-openai" | ||
"github.com/yomorun/yomo/core/metadata" | ||
|
||
provider "github.com/yomorun/yomo/pkg/bridge/ai/provider" | ||
) | ||
|
||
const BaseURL = "https://api.x.ai/v1" | ||
const DefaultModel = "grok-beta" | ||
|
||
// check if implements ai.Provider | ||
var _ provider.LLMProvider = &Provider{} | ||
|
||
// Provider is the provider for x.ai | ||
type Provider struct { | ||
// APIKey is the API key for x.ai | ||
APIKey string | ||
// Model is the model for x.ai | ||
// eg. "grok-beta" | ||
Model string | ||
client *openai.Client | ||
} | ||
|
||
// NewProvider creates a new x.ai ai provider | ||
func NewProvider(apiKey string, model string) *Provider { | ||
if model == "" { | ||
model = DefaultModel | ||
} | ||
|
||
c := openai.DefaultConfig(apiKey) | ||
c.BaseURL = BaseURL | ||
|
||
return &Provider{ | ||
APIKey: apiKey, | ||
Model: model, | ||
client: openai.NewClientWithConfig(c), | ||
} | ||
} | ||
|
||
// Name returns the name of the provider | ||
func (p *Provider) Name() string { | ||
return "xai" | ||
} | ||
|
||
// GetChatCompletions implements ai.LLMProvider. | ||
func (p *Provider) GetChatCompletions(ctx context.Context, req openai.ChatCompletionRequest, _ metadata.M) (openai.ChatCompletionResponse, error) { | ||
if req.Model == "" { | ||
req.Model = p.Model | ||
} | ||
|
||
return p.client.CreateChatCompletion(ctx, req) | ||
} | ||
|
||
// GetChatCompletionsStream implements ai.LLMProvider. | ||
func (p *Provider) GetChatCompletionsStream(ctx context.Context, req openai.ChatCompletionRequest, _ metadata.M) (provider.ResponseRecver, error) { | ||
if req.Model == "" { | ||
req.Model = p.Model | ||
} | ||
// it does not support streaming calls when tools are present | ||
return p.client.CreateChatCompletionStream(ctx, req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package xai | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/sashabaranov/go-openai" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestXAIProvider_Name(t *testing.T) { | ||
provider := &Provider{} | ||
name := provider.Name() | ||
|
||
assert.Equal(t, "xai", name) | ||
} | ||
|
||
func TestXAIProvider_GetChatCompletions(t *testing.T) { | ||
provider := NewProvider("", "") | ||
msgs := []openai.ChatCompletionMessage{ | ||
{ | ||
Role: "user", | ||
Content: "hello", | ||
}, | ||
{ | ||
Role: "system", | ||
Content: "I'm a bot", | ||
}, | ||
} | ||
req := openai.ChatCompletionRequest{ | ||
Messages: msgs, | ||
Model: "groq-beta", | ||
} | ||
|
||
_, err := provider.GetChatCompletions(context.TODO(), req, nil) | ||
assert.Error(t, err) | ||
t.Log(err) | ||
|
||
_, err = provider.GetChatCompletionsStream(context.TODO(), req, nil) | ||
assert.Error(t, err) | ||
t.Log(err) | ||
|
||
req = openai.ChatCompletionRequest{ | ||
Messages: msgs, | ||
} | ||
|
||
_, err = provider.GetChatCompletions(context.TODO(), req, nil) | ||
assert.Error(t, err) | ||
t.Log(err) | ||
|
||
_, err = provider.GetChatCompletionsStream(context.TODO(), req, nil) | ||
assert.Error(t, err) | ||
t.Log(err) | ||
} |