forked from songquanpeng/one-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support DeepL's model (close songquanpeng#1126)
- Loading branch information
1 parent
e64e770
commit 0079062
Showing
20 changed files
with
305 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package deepl | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/gin-gonic/gin" | ||
"github.com/songquanpeng/one-api/relay/adaptor" | ||
"github.com/songquanpeng/one-api/relay/meta" | ||
"github.com/songquanpeng/one-api/relay/model" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
type Adaptor struct { | ||
meta *meta.Meta | ||
promptText string | ||
} | ||
|
||
func (a *Adaptor) Init(meta *meta.Meta) { | ||
a.meta = meta | ||
} | ||
|
||
func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) { | ||
return fmt.Sprintf("%s/v2/translate", meta.BaseURL), nil | ||
} | ||
|
||
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *meta.Meta) error { | ||
adaptor.SetupCommonRequestHeader(c, req, meta) | ||
req.Header.Set("Authorization", "DeepL-Auth-Key "+meta.APIKey) | ||
return nil | ||
} | ||
|
||
func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *model.GeneralOpenAIRequest) (any, error) { | ||
if request == nil { | ||
return nil, errors.New("request is nil") | ||
} | ||
convertedRequest, text := ConvertRequest(*request) | ||
a.promptText = text | ||
return convertedRequest, nil | ||
} | ||
|
||
func (a *Adaptor) ConvertImageRequest(request *model.ImageRequest) (any, error) { | ||
if request == nil { | ||
return nil, errors.New("request is nil") | ||
} | ||
return request, nil | ||
} | ||
|
||
func (a *Adaptor) DoRequest(c *gin.Context, meta *meta.Meta, requestBody io.Reader) (*http.Response, error) { | ||
return adaptor.DoRequestHelper(a, c, meta, requestBody) | ||
} | ||
|
||
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, meta *meta.Meta) (usage *model.Usage, err *model.ErrorWithStatusCode) { | ||
if meta.IsStream { | ||
err = StreamHandler(c, resp, meta.ActualModelName) | ||
} else { | ||
err = Handler(c, resp, meta.ActualModelName) | ||
} | ||
promptTokens := len(a.promptText) | ||
usage = &model.Usage{ | ||
PromptTokens: promptTokens, | ||
TotalTokens: promptTokens, | ||
} | ||
return | ||
} | ||
|
||
func (a *Adaptor) GetModelList() []string { | ||
return ModelList | ||
} | ||
|
||
func (a *Adaptor) GetChannelName() string { | ||
return "deepl" | ||
} |
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,9 @@ | ||
package deepl | ||
|
||
// https://developers.deepl.com/docs/api-reference/glossaries | ||
|
||
var ModelList = []string{ | ||
"deepl-zh", | ||
"deepl-en", | ||
"deepl-ja", | ||
} |
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,11 @@ | ||
package deepl | ||
|
||
import "strings" | ||
|
||
func parseLangFromModelName(modelName string) string { | ||
parts := strings.Split(modelName, "-") | ||
if len(parts) == 1 { | ||
return "ZH" | ||
} | ||
return parts[1] | ||
} |
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,137 @@ | ||
package deepl | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/gin-gonic/gin" | ||
"github.com/songquanpeng/one-api/common" | ||
"github.com/songquanpeng/one-api/common/helper" | ||
"github.com/songquanpeng/one-api/relay/adaptor/openai" | ||
"github.com/songquanpeng/one-api/relay/constant" | ||
"github.com/songquanpeng/one-api/relay/constant/finishreason" | ||
"github.com/songquanpeng/one-api/relay/constant/role" | ||
"github.com/songquanpeng/one-api/relay/model" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
// https://developers.deepl.com/docs/getting-started/your-first-api-request | ||
|
||
func ConvertRequest(textRequest model.GeneralOpenAIRequest) (*Request, string) { | ||
var text string | ||
if len(textRequest.Messages) != 0 { | ||
text = textRequest.Messages[len(textRequest.Messages)-1].StringContent() | ||
} | ||
deeplRequest := Request{ | ||
TargetLang: parseLangFromModelName(textRequest.Model), | ||
Text: []string{text}, | ||
} | ||
return &deeplRequest, text | ||
} | ||
|
||
func StreamResponseDeepL2OpenAI(deeplResponse *Response) *openai.ChatCompletionsStreamResponse { | ||
var choice openai.ChatCompletionsStreamResponseChoice | ||
if len(deeplResponse.Translations) != 0 { | ||
choice.Delta.Content = deeplResponse.Translations[0].Text | ||
} | ||
choice.Delta.Role = role.Assistant | ||
choice.FinishReason = &constant.StopFinishReason | ||
openaiResponse := openai.ChatCompletionsStreamResponse{ | ||
Object: constant.StreamObject, | ||
Created: helper.GetTimestamp(), | ||
Choices: []openai.ChatCompletionsStreamResponseChoice{choice}, | ||
} | ||
return &openaiResponse | ||
} | ||
|
||
func ResponseDeepL2OpenAI(deeplResponse *Response) *openai.TextResponse { | ||
var responseText string | ||
if len(deeplResponse.Translations) != 0 { | ||
responseText = deeplResponse.Translations[0].Text | ||
} | ||
choice := openai.TextResponseChoice{ | ||
Index: 0, | ||
Message: model.Message{ | ||
Role: role.Assistant, | ||
Content: responseText, | ||
Name: nil, | ||
}, | ||
FinishReason: finishreason.Stop, | ||
} | ||
fullTextResponse := openai.TextResponse{ | ||
Object: constant.NonStreamObject, | ||
Created: helper.GetTimestamp(), | ||
Choices: []openai.TextResponseChoice{choice}, | ||
} | ||
return &fullTextResponse | ||
} | ||
|
||
func StreamHandler(c *gin.Context, resp *http.Response, modelName string) *model.ErrorWithStatusCode { | ||
responseBody, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return openai.ErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError) | ||
} | ||
err = resp.Body.Close() | ||
if err != nil { | ||
return openai.ErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError) | ||
} | ||
var deeplResponse Response | ||
err = json.Unmarshal(responseBody, &deeplResponse) | ||
if err != nil { | ||
return openai.ErrorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError) | ||
} | ||
fullTextResponse := StreamResponseDeepL2OpenAI(&deeplResponse) | ||
fullTextResponse.Model = modelName | ||
fullTextResponse.Id = helper.GetResponseID(c) | ||
jsonData, err := json.Marshal(fullTextResponse) | ||
if err != nil { | ||
return openai.ErrorWrapper(err, "marshal_response_body_failed", http.StatusInternalServerError) | ||
} | ||
common.SetEventStreamHeaders(c) | ||
c.Stream(func(w io.Writer) bool { | ||
if jsonData != nil { | ||
c.Render(-1, common.CustomEvent{Data: "data: " + string(jsonData)}) | ||
jsonData = nil | ||
return true | ||
} | ||
c.Render(-1, common.CustomEvent{Data: "data: [DONE]"}) | ||
return false | ||
}) | ||
_ = resp.Body.Close() | ||
return nil | ||
} | ||
|
||
func Handler(c *gin.Context, resp *http.Response, modelName string) *model.ErrorWithStatusCode { | ||
responseBody, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return openai.ErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError) | ||
} | ||
err = resp.Body.Close() | ||
if err != nil { | ||
return openai.ErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError) | ||
} | ||
var deeplResponse Response | ||
err = json.Unmarshal(responseBody, &deeplResponse) | ||
if err != nil { | ||
return openai.ErrorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError) | ||
} | ||
if deeplResponse.Message != "" { | ||
return &model.ErrorWithStatusCode{ | ||
Error: model.Error{ | ||
Message: deeplResponse.Message, | ||
Code: "deepl_error", | ||
}, | ||
StatusCode: resp.StatusCode, | ||
} | ||
} | ||
fullTextResponse := ResponseDeepL2OpenAI(&deeplResponse) | ||
fullTextResponse.Model = modelName | ||
fullTextResponse.Id = helper.GetResponseID(c) | ||
jsonResponse, err := json.Marshal(fullTextResponse) | ||
if err != nil { | ||
return openai.ErrorWrapper(err, "marshal_response_body_failed", http.StatusInternalServerError) | ||
} | ||
c.Writer.Header().Set("Content-Type", "application/json") | ||
c.Writer.WriteHeader(resp.StatusCode) | ||
_, err = c.Writer.Write(jsonResponse) | ||
return nil | ||
} |
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,16 @@ | ||
package deepl | ||
|
||
type Request struct { | ||
Text []string `json:"text"` | ||
TargetLang string `json:"target_lang"` | ||
} | ||
|
||
type Translation struct { | ||
DetectedSourceLanguage string `json:"detected_source_language,omitempty"` | ||
Text string `json:"text,omitempty"` | ||
} | ||
|
||
type Response struct { | ||
Translations []Translation `json:"translations,omitempty"` | ||
Message string `json:"message,omitempty"` | ||
} |
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 |
---|---|---|
|
@@ -16,6 +16,7 @@ const ( | |
Coze | ||
Cohere | ||
Cloudflare | ||
DeepL | ||
|
||
Dummy // this one is only for count, do not add any channel after this | ||
) |
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 |
---|---|---|
|
@@ -39,6 +39,7 @@ const ( | |
Cohere | ||
DeepSeek | ||
Cloudflare | ||
DeepL | ||
|
||
Dummy | ||
) |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
package constant | ||
|
||
var StopFinishReason = "stop" | ||
var StreamObject = "chat.completion.chunk" | ||
var NonStreamObject = "chat.completion" |
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,5 @@ | ||
package finishreason | ||
|
||
const ( | ||
Stop = "stop" | ||
) |
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,5 @@ | ||
package role | ||
|
||
const ( | ||
Assistant = "assistant" | ||
) |
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
Oops, something went wrong.