-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
168 lines (144 loc) · 4.02 KB
/
main.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* @Author: Vincent Young
* @Date: 2023-11-13 11:16:26
* @LastEditors: Vincent Young
* @LastEditTime: 2024-01-16 15:27:45
* @FilePath: /openai-translate/main.go
* @Telegram: https://t.me/missuo
* @GitHub: https://github.com/missuo
*
* Copyright © 2023 by Vincent, All Rights Reserved.
*/
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/pkoukk/tiktoken-go"
openai "github.com/sashabaranov/go-openai"
)
type ResData struct {
TransText string `json:"text"`
SourceLang string `json:"source_lang"`
TargetLang string `json:"target_lang"`
}
func tokenCount(text string) (int, error) {
tkm, err := tiktoken.EncodingForModel("gpt-4")
if err != nil {
err = fmt.Errorf("getEncoding: %v", err)
return 0, err
}
token := len(tkm.Encode(text, nil, nil))
return token, nil
}
func translator(apiKey string, targetLang string, transText string, baseUrl string) (string, error) {
config := openai.DefaultConfig(apiKey)
config.BaseURL = baseUrl
c := openai.NewClientWithConfig(config)
// c := openai.NewClient(apiKey)
resp, err := c.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
// Model: openai.GPT3Dot5Turbo,
Model: openai.GPT4,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: "You're a translator. Translate to " + targetLang + ".",
},
{
Role: openai.ChatMessageRoleUser,
Content: transText,
},
},
},
)
if err != nil {
return "", err
}
return resp.Choices[0].Message.Content, nil
}
func main() {
// Define a command line flag
apiKeyFlag := flag.String("apiKey", "", "API key for OpenAI")
baseUrlFlag := flag.String("baseUrl", "", "Base URL, default is https://api.openai.com/v1")
// modelFlag := flag.String("model", "", "Model to use, default is gpt-3.5-turbo")
flag.Parse()
// First try to get the API key from the command line flag
apiKey := *apiKeyFlag
baseUrl := *baseUrlFlag
// model := *modelFlag
// If it's not provided, try to get it from the environment variable
if apiKey == "" {
apiKey = os.Getenv("OPENAI_KEY")
}
if baseUrl == "" {
if os.Getenv("BASE_URL") != "" {
baseUrl = os.Getenv("BASE_URL")
} else {
baseUrl = "https://api.openai.com/v1"
}
}
// if model == "" {
// if os.Getenv("MODEL") != "" {
// model = os.Getenv("MODEL")
// } else {
// model = "gpt-3.5-turbo"
// }
// }
// fmt.Printf(model)
// If the API key is still empty, return an error and exit
if apiKey == "" {
fmt.Println("Error: No API key provided. Set the apiKey flag or the OPENAI_KEY environment variable.")
os.Exit(1)
}
fmt.Println("Starting server on port 23333. Made by Vincent.")
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
r.Use(cors.Default())
r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"message": "Hello",
})
})
r.POST("/translate", func(c *gin.Context) {
req := ResData{}
c.BindJSON(&req)
sourceLang := req.SourceLang
targetLang := req.TargetLang
translateText := req.TransText
targetText, _ := translator(apiKey, targetLang, translateText, baseUrl)
if targetText == "" {
c.JSON(http.StatusTooManyRequests, gin.H{ // 429 Too Many Requests
"code": http.StatusTooManyRequests,
"message": "Translation limit exceeded or service unavailable",
})
return
}
importToken, _ := tokenCount(translateText)
importToken += 9 // 9 token for the prompt
exportToken, _ := tokenCount(targetText)
tokenConsumed := importToken + exportToken
cost := float64(importToken)*0.0000010 + float64(exportToken)*0.0000020
c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"data": targetText,
"source_lang": sourceLang,
"target_lang": targetLang,
"token_consumed": tokenConsumed,
"cost": cost,
})
})
r.NoRoute(func(c *gin.Context) {
c.JSON(http.StatusNotFound, gin.H{
"code": http.StatusNotFound,
"message": "Path not found",
})
})
r.Run(":23333")
}