-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate-cli.go
60 lines (52 loc) · 1.13 KB
/
translate-cli.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
58
59
60
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
)
var endpoint = String(os.Getenv("TRANSLATE_ENDPOINT")).
orElse("http://localhost:8080/translate").
get()
var original = false
func parseFlags() Ask {
var (
content string
lang string
model string
)
flag.StringVar(&content, "c", "", "The content to be translated")
flag.StringVar(&lang, "l", "English", "The language to be translated")
flag.StringVar(&model, "m", "gpt-3.5-turbo", "The chatGPT model to be chose")
flag.BoolVar(&original, "o", false, "Set to get original OpenAI response")
flag.Parse()
payload := Ask{
content,
lang,
model,
}
return payload
}
func translate(ask Ask) Answer {
_bytes, _ := json.Marshal(ask)
if original {
endpoint = "http://localhost:8080/translate/openai"
}
resp, _ := http.Post(endpoint, "application/json", bytes.NewBuffer(_bytes))
defer resp.Body.Close()
var ans Answer
if !original {
json.NewDecoder(resp.Body).Decode(&ans)
} else {
ans.Reply = dumpRespBody(resp)
}
return ans
}
func main() {
ask := parseFlags()
fmt.Println("ask", ask)
ans := translate(ask)
fmt.Println(ans.Reply)
}