-
Notifications
You must be signed in to change notification settings - Fork 543
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into enhance-failover
- Loading branch information
Showing
8 changed files
with
195 additions
and
6 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
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
69 changes: 69 additions & 0 deletions
69
plugins/wasm-go/extensions/ai-proxy/provider/together_ai.go
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,69 @@ | ||
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" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
const ( | ||
togetherAIDomain = "api.together.xyz" | ||
togetherAICompletionPath = "/v1/chat/completions" | ||
) | ||
|
||
type togetherAIProviderInitializer struct{} | ||
|
||
func (m *togetherAIProviderInitializer) 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 *togetherAIProviderInitializer) CreateProvider(config ProviderConfig) (Provider, error) { | ||
return &togetherAIProvider{ | ||
config: config, | ||
contextCache: createContextCache(&config), | ||
}, nil | ||
} | ||
|
||
type togetherAIProvider struct { | ||
config ProviderConfig | ||
contextCache *contextCache | ||
} | ||
|
||
func (m *togetherAIProvider) GetProviderType() string { | ||
return providerTypeTogetherAI | ||
} | ||
|
||
func (m *togetherAIProvider) OnRequestHeaders(ctx wrapper.HttpContext, apiName ApiName, log wrapper.Log) error { | ||
if apiName != ApiNameChatCompletion { | ||
return errUnsupportedApiName | ||
} | ||
m.config.handleRequestHeaders(m, ctx, apiName, log) | ||
return nil | ||
} | ||
|
||
func (m *togetherAIProvider) OnRequestBody(ctx wrapper.HttpContext, apiName ApiName, body []byte, log wrapper.Log) (types.Action, error) { | ||
if apiName != ApiNameChatCompletion { | ||
return types.ActionContinue, errUnsupportedApiName | ||
} | ||
return m.config.handleRequestBody(m, m.contextCache, ctx, apiName, body, log) | ||
} | ||
|
||
func (m *togetherAIProvider) TransformRequestHeaders(ctx wrapper.HttpContext, apiName ApiName, headers http.Header, log wrapper.Log) { | ||
util.OverwriteRequestPathHeader(headers, togetherAICompletionPath) | ||
util.OverwriteRequestHostHeader(headers, togetherAIDomain) | ||
util.OverwriteRequestAuthorizationHeader(headers, "Bearer "+m.config.GetApiTokenInUse(ctx)) | ||
headers.Del("Content-Length") | ||
} | ||
|
||
func (m *togetherAIProvider) GetApiName(path string) ApiName { | ||
if strings.Contains(path, togetherAICompletionPath) { | ||
return ApiNameChatCompletion | ||
} | ||
return "" | ||
} |