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

English Version

摘要

本文介绍了一种功能强大的翻译工具,将 OpenAI 的先进 GPT-3.5 Turbo 语言模型与用户友好的 Wox 启动器插件系统相结合。该工具可以识别输入文本的语言,将中文文本翻译成英文或将英文文本翻译成中文,并用中文提供解释和示例。本文阐述了核心功能,与 Wox 启动器的交互以及使用 pyperclip 库实现的剪贴板集成。我们还提供了一段代码示例,以演示其实现。

引言

在本文中,我们介绍了一种功能强大的翻译工具,将 OpenAI 的尖端 GPT-3.5 Turbo 语言模型与用户友好的 Wox 启动器插件系统相结合。该工具旨在识别输入文本的语言,将中文文本翻译成英文或其他语言的文本翻译成中文,并提供中文解释和示例。

利用 GPT-3.5 Turbo

OpenAI 的 GPT-3.5 Turbo 语言模型以高效处理复杂语言任务而闻名。该翻译工具利用模型的功能,通过实现一个翻译函数来接收文本输入,构建一个格式化的问题发送给模型,并接收响应。然后解析响应,提取相关信息并将其作为列表返回。您可以在此处获取有关 ChatAPI 的更多信息:openAI 文档

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

与 Wox 启动器集成

Wox 启动器插件系统为用户提供了一个方便的互动的界面来使用翻译工具。通过创建一个继承自 Wox 类的主类,该工具定义了一个查询方法来处理用户输入并显示结果。当用户输入一个以 “&” 结尾的关键字时,代码调用翻译函数,将响应显示为可选择的项目列表。您可以在此处获取有关 Wox 启动器插件开发的更多信息:Wox 文档

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)

集成剪贴板

为了提高用户体验,脚本集成了 pyperclip 库,使用户只需点击即可将所选文本复制到剪贴板。在选择结果时,触发复制方法,轻松为用户复制文本以便粘贴到其他应用程序中。

显示

Display

代码

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()