A PopClip extension for ChatGPT

This is great. Thanks for putting the extension together @nick! I’ve been using the shortcut that’s been floating around but super handy to have the direct code (davinci-003) in popclip.

1 Like

@nick whelp it appears openai literally just released an api to chatgpt which is big news - time to update the script!

1 Like

Ooh. Here are the docs. A quick first attempt using the simplest possible approach:

// #popclip
// name: GPT-3.5
// icon: iconify:logos:openai-icon
// language: javascript
// after: paste-result
// entitlements: [network]
// options: [{
//   identifier: apikey, label: API Key, type: string,
//   description: 'Obtain API key from https://platform.openai.com/account/api-keys'
// }] 
const openai = require("axios").create({
  baseURL: 'https://api.openai.com/v1/',
  headers: { Authorization: `Bearer ${popclip.options.apikey}` }
});
const params = {
  model: 'gpt-3.5-turbo',
  messages: [{ "role": "user", "content": popclip.input.text }]
};
const { data }  = await openai.post('chat/completions', params);
return popclip.input.text + data.choices[0].message.content;

It uses the newly released gpt-3.5-turbo model, but it only works as a “one shot” question i.e. it doesn’t remember what the previous chat was. I need to look a bit more closely to see how to do that.

1 Like

OK, here is a snippet for the “real thing” ChatGPT, that you can actually chat with.

Update, 3 Mar 2023: A pre-packaged version of this extension is now available.

To use it, you’ll need to sign up for an OpenAI platform account, and get your own API key from https://platform.openai.com/account/api-keys .

// #popclip extension for ChatGPT
// name: ChatGPT
// icon: iconify:simple-icons:openai
// language: javascript
// module: true
// entitlements: [network]
// options: [{
//   identifier: apikey, label: API Key, type: string,
//   description: 'Obtain API key from https://platform.openai.com/account/api-keys'
// }]
const messages = []; // history of previous messages
async function chat (input, options) {
  const openai = require("axios").create({
    baseURL: "https://api.openai.com/v1",
    headers: { Authorization: `Bearer ${options.apikey}` },
  });
  messages.push({ "role": "user", "content": input.text });
  const { data } = await openai.post("/chat/completions", {
    model: "gpt-3.5-turbo", messages
  });
  messages.push(data.choices[0].message);
  return input.text.trimEnd() + "\n\n" + messages.at(-1).content.trim();
};
exports.actions = [{
  title: "ChatGPT: Chat",
  after: "paste-result",
  code: chat,
}, {
  title: "ChatGPT: Reset",
  icon: "iconify:game-icons:broom",
  requirements: [],
  after: "show-status",
  code: () => messages.length = 0 // clear the message history
}];

(The above block is an extension snippet – select the text then click “Install Extension” in PopClip.)

How to install it:
CleanShot 2023-02-02 at 09.57.52

Note there are two actions, Chat and Reset.

Chat (main icon): Type your message in any text field, select it so PopClip appears and choose the Chat action. After ChatGPT responds, type your response and repeat.

Reset (broom icon): click it whenever you need to start a new chat and forget the previous responses.

CleanShot 2023-03-01 at 21.08.55

I had to break out the module syntax (which I still haven’t properly documented) for this one because the extension itself has to remember the past messages in the chat and provide the whole thing to the API each time. But it’s really a very straightforward API to use.

3 Likes

Brilliant!

1 Like

2 posts were split to a new topic: “XmlHTTPRequest is not allowed here”

Since the ChatGPT extension seems to be working well and is getting pretty popular, I’ve created an “official” downloadable version and added it to the main extension directory.

The new version includes a few refinements over the one in this thread. See forum post.

Bizarrely, it doesn’t work for me.

I put an API Key and I’m getting no answer. I tried in Notes, Word…etc…

See here:

Ps: I have a ChatGPT Plus account but not a Paid API account but I understand I don’t need one?

Fairly sure you need to have a paid API account. For me it also only started working once I had configured that.

What do you say @nick ?

@rdouma is correct, and it is the most likely explanation. Have you checked the paid credit status of your OpenAI account @jberrebi? Failing that, there are debug instructions in this post.

I added my credit card to my OpenAI account and it works indeed :slight_smile:

This extension is really a killer app.

Thanks @nick and @rdouma

I need a proxy to access OpenAI. Is it possible to use axios proxy?

The proxy config is not working properly:

const openai = require("axios").create({
    baseURL: "https://api.openai.com/v1",
    headers: { Authorization: `Bearer ${options.apikey}` },
    proxy: {
        protocol: 'http',
        host: 'localhost',
        port: 7890,
    }
  });


I see that some people have found a workaround using HttpsProxyAgent, but this package seems not applicable in Popclip extensions.

It’s a little in-depth for me to dive into right now, but if Axios supports proxy config like that, I would expect it to work. However, the log shows that the underlying XMLHttpRequest is going directly to the API, so it isn’t using the proxy.

Modules loaded within popclip extensions are bundled inside PopClip’s executable. The current bundle axios version is 0.26.1. There are only a small number of other modules bundled, but none for networking.

However, I notice your proxy uses HTTP, and unfortunately, PopClip will refuse to make an outgoing connection that isn’t https anyway, even to localhost. So even if you get the proxy set-up working, it won’t connect :frowning:

A post was split to a new topic: Bing chat extension

I got an access to ChatGPT4 API.

I updated the code and installed the new extension.

I tried to use it but I’m getting axios errors

Error: Request failed with status code 429
axios.js.lzfse:667:24
axios.js.lzfse:923:23
axios.js.lzfse:67:13

Result: error: Error: Request failed with status code 429 (axios.js.lzfse:667:24)

‘paste-result’ was specified but there is no result text

according to OpenAI, they sent back an answer but I never got it…

Any idea?

1 Like

HTTP error code 429 is “too many requests”; perhaps you are encountering some sort of rate limiting?

It looks like OpenAI is an article about it: https://help.openai.com/en/articles/5955604-how-can-i-solve-429-too-many-requests-errors

I got the same error with my very first requestion…Bizarre

Do you have an access to ChatGPT4 API yourself? Is it working?

I don’t have it. Are you accessing it from your company network? It could be other usage from your organisation.

I’m the owner of the company and managing the main account.
Now it works. I don’t know why!
All good!

1 Like