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
deftranslate(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
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.