Incorrect PopClip extension made by ChatGPT

Hello everyone, I am a beginner.
I am trying to write a PopClip extension to translate selected text into English.
This is the content of translation.py:

#!/Users/zmke/opt/anaconda3/bin/python
# -*- coding:utf-8 -*-
import openai
import sys

openai.api_key = 'XXXXXXXXX'


response = openai.Completion.create(
  engine="text-davinci-003",
  prompt=sys.argv[1] + "\nTranslate the above text to English:",
  temperature=0.5,
  max_tokens=60
)

print(response.choices[0].text.strip())

This file works properly in the Terminal.app.
The Config.plist is here:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Actions</key>
    <array>
        <dict>
            <key>Title</key>
            <string>Translate to English</string>
            <key>Script File</key>
            <string>translation.py</string>
            <key>Argument</key>
            <string>{popclip text}</string>
        </dict>
    </array>
    <key>Description</key>
    <string>Translates selected text to English</string>
    <key>Name</key>
    <string>MyTranslationExtension</string>
    <key>PopClip Version</key>
    <integer>1</integer>
</dict>
</plist>

There is another icon.png file, but it’s not important.
I installed this plugin normally, but when I selected text and the pop-up dialog box appeared, the Translate to English button was grayed out and I couldn’t click it. I don’t know where the error is, to be honest, these codes and files were generated by chatGPT for me.
Does anyone know what went wrong? Where do I need to make changes to properly execute the code?
Thanks in advanced!

It has made multiple errors and incorrect assumptions; too many to go through one by one.

The main problem is it has hallucinated the “Argument” field for passing the text parameter; in fact this field does not exist.

Rather than try to correct it, I suggest using this instead:

// #popclip
// name: Translate to English
// icon: circle en
// language: javascript
// after: paste-result
// entitlements: [network]
const apikey = 'INSERT YOUR API KEY HERE';
const openai = require("axios").create({
  baseURL: 'https://api.openai.com/v1/',
  headers: { Authorization: `Bearer ${apikey}` }
});
const { data } = await openai.post('completions', {
  model: 'text-davinci-003',
  max_tokens: 2048,
  prompt: popclip.input.text + "\n\n" + "Translate the above text to English:"
});
return data.choices[0].text.trim();

(The above block is an extension snippet — select it then click “Install Extension” in PopClip.)

You’ll need to copy and paste it to a text editor to put your one API key in.

If you really wanna go the Python route, you can do it, just need to get the Config.plist right and change the method of argument passing. I can go through it later if I get time, let me know if you want to do it.

Wow, this is so magical, so simple yet it really works.
I’m not familiar with Javascript, to put it more directly, I only know a little bit of Python. I also want to continue to write more Popclip extensions that are suitable for my own use with chatGPT. Therefore, if you have some free time, please help me to implement the translation function in Python, at least so I know how Python works in Popclip.
Thank you for your reply, it really excites me.

I can’t test it because I don;t have the same python set-up as you, but try this:

#!/Users/zmke/opt/anaconda3/bin/python
# -*- coding:utf-8 -*-
# #popclip
# name: Translate to English
# icon: circle en
# after: paste-result

import openai
import os

openai.api_key = 'XXXXXXXXX'

text = os.environ['POPCLIP_TEXT']

response = openai.Completion.create(
  engine="text-davinci-003",
  prompt=text + "\nTranslate the above text to English:",
  temperature=0.5,
  max_tokens=60
)

print(response.choices[0].text.strip(), end="")

(The above block is an extension snippet — select it then click “Install Extension” in PopClip.)

Or if you want to make it an packaged extension with a separate .py and Config file, use a Config.yaml file like this:

name: Translate to English
icon: circle en
after: paste-result
shell script file: translation.py

And you’ll need to take your input from os.environ['POPCLIP_TEXT'] instead of from argv.

The code is valid but there is one mistake. The line “import sys” needs to be changed to “import os”.
Another question is that I read the OpenAI documentation today, which mentions that the GPT-3.5-Turbo model now has the same effect as Text-Davinci-003, but at only one tenth of the cost.
What do you think of this model? I tried to modify the engine parameters but it didn’t work.

In terms of model selection, I don’t really have a strong opinion, but it makes sense to use the latest and lowest cost one. I would have thought changing it to gpt-3.5-turbo would do the trick presuming it is already working with text-davinci-003. https://platform.openai.com/docs/models/gpt-3-5

4 is more capable of course but only usable if you have access to the closed beta.

I switched to the gpt-3.5-turbo model and rewrote the code, and it worked successfully.
Thank you, Nick, for developing such a great App.

1 Like