-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.go
57 lines (43 loc) · 1.12 KB
/
translate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package autonews
import (
"context"
"errors"
"fmt"
"strings"
"github.com/lemon-mint/coord/llm"
)
const translation_prompt = `Translate the document into Korean.
<document>
%s
</document>
Always use a friendly and bright tone.
Do not distort the meaning of the sentence.
Do not translate names or abbreviations.
Do not translate yaml keys.
Write the translated content inside the <korean> block.`
var ErrTranslationFailed = errors.New("translation failed")
func generateTranslation(ctx context.Context, model llm.LLM, document string) (string, error) {
response := model.GenerateStream(
ctx,
&llm.ChatContext{},
llm.TextContent(llm.RoleUser, fmt.Sprintf(translation_prompt, document)),
)
err := response.Wait()
if err != nil {
return "", err
}
texts := getTexts(response.Content)
if len(texts) == 0 {
return "", ErrTranslationFailed
}
text := strings.Join(texts, "")
_, after, ok := strings.Cut(text, "<korean>")
if !ok {
return "", ErrTranslationFailed
}
translation, _, ok := strings.Cut(after, "</korean>")
if !ok {
return "", ErrTranslationFailed
}
return strings.TrimSpace(translation), nil
}