-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchatGPTMTPlugin.bas
executable file
·319 lines (293 loc) · 10.3 KB
/
chatGPTMTPlugin.bas
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
B4J=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=4.2
@EndOfDesignText@
Sub Class_Globals
Private fx As JFX
Private defaultPrompt As String = $"Translate the following into {langcode}: {source}"$
Private defaultPromptWithTerm As String = $"With the help of the terms defined in JSON: {term}, translate the following into {langcode}: {source}"$
Private defaultBatchPrompt As String = $"Your task is to translate a text in JSON format into {langcode} and return the text in valid JSON format. You should not mix the values of different keys into one. Here is the JSON string to translate: {source}"$
Private defaultBatchPromptWithTerm As String = $"Your task is to translate a text in JSON format into {langcode} and return the text in valid JSON format. You should use the terms defined in JSON: {term}. You should not mix the values of different keys into one. Here is the JSON string to translate: {source}"$
End Sub
'Initializes the object. You can NOT add parameters to this method!
Public Sub Initialize() As String
Log("Initializing plugin " & GetNiceName)
' Here return a key to prevent running unauthorized plugins
Return "MyKey"
End Sub
' must be available
public Sub GetNiceName() As String
Return "chatGPTMT"
End Sub
' must be available
public Sub Run(Tag As String, Params As Map) As ResumableSub
Select Tag
Case "getParams"
Dim paramsList As List
paramsList.Initialize
paramsList.Add("key")
paramsList.Add("prompt")
paramsList.Add("batch_prompt")
paramsList.Add("prompt_with_term")
paramsList.Add("batch_prompt_with_term")
paramsList.Add("host")
paramsList.Add("model")
Return paramsList
Case "batchtranslate"
Dim terms As Map
If Params.ContainsKey("terms") Then
terms = Params.Get("terms")
Else
terms.Initialize
End If
wait for (batchTranslate(Params.Get("source"),Params.Get("sourceLang"),Params.Get("targetLang"),Params.Get("preferencesMap"),terms)) complete (targetList As List)
Return targetList
Case "translate"
Dim terms As Map
If Params.ContainsKey("terms") Then
terms = Params.Get("terms")
Else
terms.Initialize
End If
wait for (translate(Params.Get("source"),Params.Get("sourceLang"),Params.Get("targetLang"),Params.Get("preferencesMap"),terms)) complete (result As String)
Return result
Case "supportBatchTranslation"
Return True
Case "getDefaultParamValues"
Return CreateMap("prompt":defaultPrompt, _
"batch_prompt":defaultBatchPrompt, _
"prompt_with_term":defaultPromptWithTerm, _
"batch_prompt_with_term":defaultBatchPromptWithTerm, _
"host":"https://api.openai.com/v1", _
"model":"gpt-3.5-turbo")
End Select
Return ""
End Sub
private Sub ConvertLang(lang As String) As String
Dim map1 As Map
map1.Initialize
map1.Put("zh","Chinese")
map1.Put("zh-CN","Simplified Chinese")
map1.Put("zh-TW","Traditional Chinese")
map1.Put("en","English")
map1.Put("ja","Japanese")
map1.Put("ko","Korean")
map1.Put("ar","Arabic")
map1.Put("de","German")
map1.Put("fi","Finish")
map1.Put("el","Greek")
map1.Put("da","Danish")
map1.Put("cs","Czech")
map1.Put("ca","Catalan")
map1.Put("fr","French")
map1.Put("it","Italian")
map1.Put("sv","Swedish")
map1.Put("pt","Portuguese")
map1.Put("nl","Dutch")
map1.Put("pl","Polish")
map1.Put("es","Spanish")
map1.Put("id","Indonesian")
map1.Put("hi","Hindi")
map1.Put("vi","Vietnamese")
map1.Put("ru","Russian")
If map1.ContainsKey(lang) Then
Return map1.Get(lang)
End If
Return lang
End Sub
Sub batchTranslate(sourceList As List, sourceLang As String, targetLang As String,preferencesMap As Map,terms As Map) As ResumableSub
Dim targetList As List
targetList.Initialize
Dim converted As Boolean
Dim langCode As String = targetLang
targetLang = ConvertLang(targetLang)
If langCode <> targetLang Then
converted = True
End If
Dim job As HttpJob
job.Initialize("job",Me)
Dim apikey As String = getMap("chatGPT",getMap("mt",preferencesMap)).Get("key")
Dim host As String = getMap("chatGPT",getMap("mt",preferencesMap)).GetDefault("host","https://api.openai.com/v1")
Dim prompt As String
If terms.Size>0 Then
prompt = getMap("chatGPT",getMap("mt",preferencesMap)).GetDefault("batch_prompt_with_term",defaultBatchPromptWithTerm)
Else
prompt = getMap("chatGPT",getMap("mt",preferencesMap)).GetDefault("batch_prompt",defaultBatchPrompt)
End If
Dim model As String = getMap("chatGPT",getMap("mt",preferencesMap)).GetDefault("model","gpt-3.5-turbo")
Dim url As String = host&"/chat/completions"
Dim messages As List
messages.Initialize
Dim message As Map
message.Initialize
message.Put("role","user")
Dim keyvalues As Map
keyvalues.Initialize
Dim index As Int = 0
For Each source As String In sourceList
Dim key As String = index
keyvalues.Put(key,source)
index = index + 1
Next
Dim jsonG As JSONGenerator
jsonG.Initialize(keyvalues)
Dim jsonString As String = jsonG.ToString
If prompt.Contains("{langcode}") Then
If converted Then
message.Put("content",prompt.Replace("{langcode}",targetLang).Replace("{source}",jsonString))
'$"Translate the following into ${targetLang}: ${source}"$
Else
If terms.Size>0 Then
message.Put("content",defaultBatchPromptWithTerm.Replace("{langcode}",$"the language whose ISO639-1 code is ${targetLang}"$).Replace("{source}",jsonString))
Else
message.Put("content",defaultBatchPrompt.Replace("{langcode}",$"the language whose ISO639-1 code is ${targetLang}"$).Replace("{source}",jsonString))
End If
End If
Else
message.Put("content",prompt.Replace("{source}",jsonString))
End If
If terms.Size>0 Then
Dim termsJsonG As JSONGenerator
termsJsonG.Initialize(terms)
Dim msg As String = message.Get("content")
message.Put("content",msg.Replace("{term}",termsJsonG.ToString))
End If
Log(message)
messages.Add(message)
Dim params As Map
params.Initialize
params.Put("model",model)
params.Put("messages",messages)
Dim jsonG As JSONGenerator
jsonG.Initialize(params)
job.PostString(url,jsonG.ToString)
Log(jsonG.ToString)
job.GetRequest.SetContentType("application/json")
job.GetRequest.SetHeader("Authorization","Bearer "&apikey)
wait For (job) JobDone(job As HttpJob)
If job.Success Then
Try
Log(job.GetString)
Dim json As JSONParser
json.Initialize(job.GetString)
Dim response As Map = json.NextObject
Dim choices As List
choices = response.Get("choices")
Dim choice As Map = choices.Get(0)
Dim message As Map = choice.Get("message")
Dim content As String = message.Get("content")
Dim jsonP As JSONParser
jsonP.Initialize(content)
Dim keyvalues As Map = jsonP.NextObject
For i = 0 To sourceList.Size - 1
Dim key As String = i
targetList.Add(keyvalues.GetDefault(key,""))
Next
Catch
Log(LastException)
Try
content = content.SubString2(content.IndexOf("{"),content.Length)
Dim jsonP As JSONParser
jsonP.Initialize(content)
Dim keyvalues As Map = jsonP.NextObject
For i = 0 To sourceList.Size - 1
Dim key As String = i
targetList.Add(keyvalues.GetDefault(key,""))
Next
Catch
Log(LastException)
End Try
End Try
End If
job.Release
If targetList.Size = 0 Then
For Each source As String In sourceList
targetList.Add("")
Next
End If
Return targetList
End Sub
Sub translate(source As String,sourceLang As String,targetLang As String,preferencesMap As Map,terms As Map) As ResumableSub
Dim converted As Boolean
Dim langCode As String = targetLang
targetLang = ConvertLang(targetLang)
If langCode <> targetLang Then
converted = True
End If
Dim target As String
Dim job As HttpJob
job.Initialize("job",Me)
Dim key As String = getMap("chatGPT",getMap("mt",preferencesMap)).Get("key")
Dim prompt As String
If terms.Size>0 Then
prompt = getMap("chatGPT",getMap("mt",preferencesMap)).GetDefault("prompt_with_term",defaultPromptWithTerm)
Else
prompt = getMap("chatGPT",getMap("mt",preferencesMap)).GetDefault("prompt",defaultPrompt)
End If
Dim host As String = getMap("chatGPT",getMap("mt",preferencesMap)).GetDefault("host","https://api.openai.com/v1")
Dim url As String = host&"/chat/completions"
Dim messages As List
messages.Initialize
Dim message As Map
message.Initialize
message.Put("role","user")
If prompt.Contains("{langcode}") Then
If converted Then
message.Put("content",prompt.Replace("{langcode}",targetLang).Replace("{source}",source))
'$"Translate the following into ${targetLang}: ${source}"$
Else
If terms.Size>0 Then
message.Put("content",defaultPromptWithTerm.Replace("{langcode}",$"the language whose ISO639-1 code is ${targetLang}"$).Replace("{source}",source))
Else
message.Put("content",$"Translate the following into the language whose ISO639-1 code is ${targetLang}: ${source}"$)
End If
End If
Else
message.Put("content",prompt.Replace("{source}",source))
End If
If terms.Size>0 Then
Dim termsJsonG As JSONGenerator
termsJsonG.Initialize(terms)
Dim msg As String = message.Get("content")
message.Put("content",msg.Replace("{term}",termsJsonG.ToString))
End If
Log(message)
messages.Add(message)
Dim model As String = getMap("chatGPT",getMap("mt",preferencesMap)).GetDefault("model","gpt-3.5-turbo")
Dim params As Map
params.Initialize
params.Put("model",model)
params.Put("messages",messages)
Dim jsonG As JSONGenerator
jsonG.Initialize(params)
job.PostString(url,jsonG.ToString)
job.GetRequest.SetContentType("application/json")
job.GetRequest.SetHeader("Authorization","Bearer "&key)
wait For (job) JobDone(job As HttpJob)
If job.Success Then
Try
Log(job.GetString)
Dim json As JSONParser
json.Initialize(job.GetString)
Dim response As Map = json.NextObject
Dim choices As List
choices = response.Get("choices")
Dim choice As Map = choices.Get(0)
Dim message As Map = choice.Get("message")
target = message.Get("content")
target = target.Trim
Catch
target=""
Log(LastException)
End Try
Else
target=""
End If
job.Release
Return target
End Sub
Sub getMap(key As String,parentmap As Map) As Map
Return parentmap.Get(key)
End Sub