A Multifaceted Translation Tool: Integrating GPT-3.5 Turbo and Wox for Seamless Language Processing

中文版

Abstract

This article presents a powerful translation tool that combines the advanced GPT-3.5 Turbo language model by OpenAI with the user-friendly Wox launcher plugin system. The tool identifies the input text’s language, translates Chinese text to English or English text to Chinese, and provides an explanation and example in Chinese. The article explains the core functionality, interaction with Wox launcher, and clipboard integration using the pyperclip library. We also provide an example of the code to demonstrate its implementation.

Introduction

In this article, we introduce a powerful translation tool that integrates the cutting-edge GPT-3.5 Turbo language model by OpenAI with the user-friendly Wox launcher plugin system. This tool is designed to identify the input text’s language, translate Chinese text to English or other languages’ text to Chinese, and provide an explanation and an example in Chinese.

Harnessing GPT-3.5 Turbo

The GPT-3.5 Turbo language model by OpenAI is known for its ability to handle complex language tasks efficiently. This translation tool utilizes the model’s capabilities by implementing a translate function that takes a text input, constructs a formatted question to send to the model, and receives a response. The response is then parsed, extracting the relevant information and returning it as a list. You can get more information about ChatAPI here: openAI documentation

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
def translate(text: str) -> list:
openai.api_key = KEY
id = "gpt-3.5-turbo"
question = """
1: Check the text's language
2: If language != Chinese {
translate it into Chinese
} else {
translate it into English
}
3: Desired format:

language: -||-
translate: -||-
explanation: -||-
example: -||-
##

Text1: ###Hello###
language: English
translate: 你好
explanation: 这是常用的问候语
example: Hello, how are you today?
##

Text2: ###你好###
language: Chinese
translate: Hello
explanation: 这是常用的问候语
example: 你好,你今天怎么样?
##

Text3: ###こんにちは###
language: Japanese
translate: 你好
explanation: 这是常用的问候语
example: こんにちは、今日はお元気ですか?
##

Text4: ###안녕하세요###
language: Korean
translate: 你好
explanation: 这是常用的问候语
example: 안녕하세요, 오늘은 어떻게 지내세요?
##

Text5: """
question += "###" + text + "###"
response = openai.ChatCompletion.create(
model=id,
messages=[{
"role": "user",
"content": question
}],
temperature=0,
top_p=0.1,
max_tokens=2048,
)
usage = response.usage["total_tokens"]
response = response["choices"][0]["message"]["content"]
# print(response)
response = list(response.split("\n"))
response.append("usage: " + str(usage))
return response

Integrating with Wox Launcher

The Wox launcher plugin system offers a convenient and interactive interface for users to engage with the translation tool. By creating a main class that inherits from the Wox class, the tool defines a query method to handle user input and display results. When a user enters a keyword ending with an ampersand (&), the code calls the translate function, displaying the response as a list of selectable items. You can get more information about the Wox launcher plugin development here: Wox Documentation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class main(Wox):
def query(self, keyword: str):
results = []
if not keyword.endswith('&'):
return results
response: list[str] = translate(keyword[:-1])
for reponse in response:
results.append({
"IcoPath": "Images/ico.png",
"Title": reponse,
"SubTitle": "Copy",
"JsonRPCAction": {
"method": "copy",
"parameters": [reponse],
"dontHideAfterAction": False
},
})
return results

def copy(self, word: str):
pyperclip.copy(word)

Clipboard Integration for User Convenience

To enhance user experience, the script incorporates the pyperclip library, enabling users to copy the selected text to the clipboard with just a click. Upon selecting a result, the copy method is triggered, effortlessly copying the text for users to paste into other applications.

Display

Display

Code

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
import openai
from wox import Wox
import pyperclip

KEY = ""

def translate(text: str) -> list:
openai.api_key = KEY
id = "gpt-3.5-turbo"
question = """
1: Check the text's language
2: If language != Chinese {
translate it into Chinese
} else {
translate it into English
}
3: Desired format:

language: -||-
translate: -||-
explanation: -||-
example: -||-
##

Text1: ###Hello###
language: English
translate: 你好
explanation: 这是常用的问候语
example: Hello, how are you today?
##

Text2: ###你好###
language: Chinese
translate: Hello
explanation: 这是常用的问候语
example: 你好,你今天怎么样?
##

Text3: ###こんにちは###
language: Japanese
translate: 你好
explanation: 这是常用的问候语
example: こんにちは、今日はお元気ですか?
##

Text4: ###안녕하세요###
language: Korean
translate: 你好
explanation: 这是常用的问候语
example: 안녕하세요, 오늘은 어떻게 지내세요?
##

Text5: """
question += "###" + text + "###"
response = openai.ChatCompletion.create(
model=id,
messages=[{
"role": "user",
"content": question
}],
temperature=0,
top_p=0.1,
max_tokens=2048,
)
usage = response.usage["total_tokens"]
response = response["choices"][0]["message"]["content"]
# print(response)
response = list(response.split("\n"))
response.append("usage: " + str(usage))
return response


class main(Wox):
def query(self, keyword: str):
results = []
if not keyword.endswith('&'):
return results
response: list[str] = translate(keyword[:-1])
for reponse in response:
results.append({
"IcoPath": "Images/ico.png",
"Title": reponse,
"SubTitle": "Copy",
"JsonRPCAction": {
"method": "copy",
"parameters": [reponse],
"dontHideAfterAction": False # 运行后是否隐藏Wox窗口
},
})
return results

def copy(self, word: str):
pyperclip.copy(word)


if __name__ == "__main__":
main()