mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-19 12:50:51 +08:00
v6 version first commit
This commit is contained in:
@@ -3,55 +3,28 @@ package translator
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/luispater/CLIProxyAPI/v5/internal/interfaces"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces"
|
||||
sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator"
|
||||
)
|
||||
|
||||
var (
|
||||
Requests map[string]map[string]interfaces.TranslateRequestFunc
|
||||
Responses map[string]map[string]interfaces.TranslateResponse
|
||||
)
|
||||
|
||||
func init() {
|
||||
Requests = make(map[string]map[string]interfaces.TranslateRequestFunc)
|
||||
Responses = make(map[string]map[string]interfaces.TranslateResponse)
|
||||
}
|
||||
var registry = sdktranslator.Default()
|
||||
|
||||
func Register(from, to string, request interfaces.TranslateRequestFunc, response interfaces.TranslateResponse) {
|
||||
log.Debugf("Registering translator from %s to %s", from, to)
|
||||
if _, ok := Requests[from]; !ok {
|
||||
Requests[from] = make(map[string]interfaces.TranslateRequestFunc)
|
||||
}
|
||||
Requests[from][to] = request
|
||||
|
||||
if _, ok := Responses[from]; !ok {
|
||||
Responses[from] = make(map[string]interfaces.TranslateResponse)
|
||||
}
|
||||
Responses[from][to] = response
|
||||
registry.Register(sdktranslator.FromString(from), sdktranslator.FromString(to), request, response)
|
||||
}
|
||||
|
||||
func Request(from, to, modelName string, rawJSON []byte, stream bool) []byte {
|
||||
if translator, ok := Requests[from][to]; ok {
|
||||
return translator(modelName, rawJSON, stream)
|
||||
}
|
||||
return rawJSON
|
||||
return registry.TranslateRequest(sdktranslator.FromString(from), sdktranslator.FromString(to), modelName, rawJSON, stream)
|
||||
}
|
||||
|
||||
func NeedConvert(from, to string) bool {
|
||||
_, ok := Responses[from][to]
|
||||
return ok
|
||||
return registry.HasResponseTransformer(sdktranslator.FromString(from), sdktranslator.FromString(to))
|
||||
}
|
||||
|
||||
func Response(from, to string, ctx context.Context, modelName string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) []string {
|
||||
if translator, ok := Responses[from][to]; ok {
|
||||
return translator.Stream(ctx, modelName, originalRequestRawJSON, requestRawJSON, rawJSON, param)
|
||||
}
|
||||
return []string{string(rawJSON)}
|
||||
return registry.TranslateStream(ctx, sdktranslator.FromString(from), sdktranslator.FromString(to), modelName, originalRequestRawJSON, requestRawJSON, rawJSON, param)
|
||||
}
|
||||
|
||||
func ResponseNonStream(from, to string, ctx context.Context, modelName string, originalRequestRawJSON, requestRawJSON, rawJSON []byte, param *any) string {
|
||||
if translator, ok := Responses[from][to]; ok {
|
||||
return translator.NonStream(ctx, modelName, originalRequestRawJSON, requestRawJSON, rawJSON, param)
|
||||
}
|
||||
return string(rawJSON)
|
||||
return registry.TranslateNonStream(ctx, sdktranslator.FromString(from), sdktranslator.FromString(to), modelName, originalRequestRawJSON, requestRawJSON, rawJSON, param)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user