Skip to content

Commit

Permalink
Merge pull request #3 from xgd16/develop
Browse files Browse the repository at this point in the history
Add:  translation statistics
  • Loading branch information
xgd16 authored Aug 26, 2024
2 parents 1bc2e58 + a881274 commit 987677b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 34 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@

## 批量翻译支持情况

| 平台 | 是否支持批量翻译 | 是否完美支持 | 准确的源语言 | 备注 |
| :--------: | :--------------: | :----------: | :----------: | :-----------------------------------------: |
| 百度 || | | 不支持精确返回具体每条结果的源语言类型 |
| Google || | | |
| 有道 || | | 源语言类型识别不准确 |
| 火山 || | | |
| Deepl || | | 源语言类型识别不准确 |
| 讯飞 || | | 循环实现 |
| PaPaGo || | | 基于 \n 切割实现 且不可识别不同的源语言类型 |
| ChatGPT || | | |
| FreeGoogle || | | 循环实现 |
| 平台 | 是否支持批量翻译 | 是否完美支持 | 准确的源语言 | 备注 |
| :--------: | :--------------: |:------:|:------:|:------------------------:|
| 百度 ||| | 不支持精确返回具体每条结果的源语言类型 |
| Google |||| |
| 有道 |||| 源语言类型识别不准确 |
| 火山 |||| |
| Deepl |||| 源语言类型识别不准确 |
| 讯飞 |||| 循环实现 |
| PaPaGo ||| | 基于 \n 切割实现 且不可识别不同的源语言类型 |
| ChatGPT || | | 不在支持源语言类型检测 |
| FreeGoogle |||| 循环实现 |

## 未来支持 (优先级按照顺序,打勾为已实现) ✈️

Expand Down
7 changes: 7 additions & 0 deletions src/logic/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/gogf/gf/v2/net/gtrace"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/text/gstr"
"github.com/xgd16/gf-x-tool/xmonitor"
)

// Translate 翻译
Expand Down Expand Up @@ -41,11 +42,14 @@ func Translate(ctx context.Context, ip string, req *types.TranslateReq) (data *t
Text: req.Text,
TextStr: textStr,
}
isCache := true
// 判断是否进行缓存
if global.CacheMode == "off" {
isCache = false
data, err = translateHandler(translateReq)
} else {
dataT, err1 := global.GfCache.GetOrSetFunc(ctx, fmt.Sprintf("Translate:%s", md5), func(ctx context.Context) (value any, err error) {
isCache = false
return translateHandler(translateReq)
}, 0)
if err1 != nil {
Expand All @@ -56,6 +60,9 @@ func Translate(ctx context.Context, ip string, req *types.TranslateReq) (data *t
return
}
}
if isCache {
xmonitor.MetricHttpRequestTotal.WithLabelValues("cache_translate_count").Inc()
}
nowTime := gtime.Now().UnixMilli()
// 记录翻译
queueHandler.RequestRecordQueue.Push(&types.RequestRecordData{
Expand Down
31 changes: 8 additions & 23 deletions src/translate/chatGPT.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package translate

import (
"context"
"encoding/json"
"errors"
"fmt"

"github.com/sashabaranov/go-openai"
)

Expand Down Expand Up @@ -42,30 +40,18 @@ func (t *ChatGptConfigType) Translate(req *TranslateReq) (resp []*TranslateResp,
if from == "auto" {
from = ""
}
gptResp, err := SendToChatGpt(t.Key, t.OrgId, fmt.Sprintf("将[%s]数组翻译成%s严格按照格式 [{\"fromLang\":\"源语言\",\"text\":\"翻译结果\"}] 返回给我fromLang有这几种语言直接给我返回对应的key位置%s不需要其他任何回复严格按照我给你的格式翻译不要给我markdown", func() (str string) {
for k, v := range req.Text {
if k == 0 {
str = fmt.Sprintf("\"%s\"", v)
} else {
str = fmt.Sprintf("%s,\"%s\"", str, v)
}
for _, item := range req.Text {
respStr, err1 := SendToChatGpt(t.Key, t.OrgId, fmt.Sprintf("Translate the following text to %s and output only the translation: %s", to, item), t.Model)
if err1 != nil {
err = err1
return
}
return
}(), to, ChatGPTLangConfig), t.Model)
if err != nil {
return
}
httpResp := new(ChatGPTHTTPTranslateResp)
if err = json.Unmarshal([]byte(gptResp), httpResp); err != nil {
return
}
resp = make([]*TranslateResp, 0)
for _, item := range *httpResp {
resp = append(resp, &TranslateResp{
Text: item.Text,
FromLang: item.FromLang,
Text: respStr,
FromLang: from,
})
}

return
}

Expand All @@ -84,7 +70,6 @@ func SendToChatGpt(key, orgId, msg, modelStr string) (resp string, err error) {
case "gpt-3.5-turbo":
model = openai.GPT3Dot5Turbo
}

respData, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Expand Down

0 comments on commit 987677b

Please sign in to comment.