A macro that uses OpenAI API?

Does anybody have an example script that uses the OpenAI API? I would like to feed some copied text for spell checking and grammar requests. It seems easy enough, but I'm looking for any example code that sends input to OpenAI through the API and retrieves the resulting text.

1 Like

They have lots of examples in the documentation that you can implement via the script actions :wink:

https://platform.openai.com/docs/api-reference/authentication

Here is a subroutine I use in my macros - you need to define your OpenAI API key in the first configuration block (plus your preferred default model)

When calling the subroutine, you can specify any prompt, and it will return the AI response.

Note: This subroutine runs a python3 script; it does not come with macOS anymore, so maybe you need to install it manually: "brew install python3" will do the trick


Here's a sample usage:


OpenAI Completion.kmmacros (8.1 KB)

2 Likes

This was great. Thanks!!!

Interesting. What's an example usage scenario?

I've assigned the shortcut ⌥S to a custom macro that will select the current line and fix the grammar using an AI prompt.

This is how it looks like:
2024-05-14_13-49-58 (1)

And here's the macro:

3 Likes

Ok, that's pretty brilliant. I wonder if it might make a good contextual rhyming dictionary. Now that Chat GPT 4o is out, I think the results may prove more useful than before. I'll have a go with it when I get back!

Possibly, though another reading is that the 4a-z schtick is a sign that returns are now diminishing.

The prospect of transubstantiating linguistic vector slurry into some kind of actual knowledge, or even "intelligence" – envisaged (or at least marketed) as a mystery somehow inherent in (massively costly) scaling, and branded as the "coming soon" of a "5.0", does not appear to walk among us ...

1 Like

EDIT: Solved by executing the command in the first answer here.

Hello Phil,
I'm having troubles to install Python on my iMac running Sonoma. I get an error message.

I tried to change permissions in the Finder. But after repeating the install command, I get the same error message.

Needless to say that I don't know the first thing about Python ...

Perhaps you can help? Thank you in advance!
Cheers,

Hans

hl@Hanss-iMac-2 ~ % brew install python3
==> Downloading https://formulae.brew.sh/api/formula.jws.json
######################################################################### 100.0%
==> Downloading https://formulae.brew.sh/api/cask.jws.json
######################################################################### 100.0%
python@3.12 3.6.2 is already installed but outdated (so it will be upgraded).
==> Downloading https://ghcr.io/v2/homebrew/core/python/3.12/manifests/3.12.3
Already downloaded: /Users/hl/Library/Caches/Homebrew/downloads/5065c4d76c933aad4e13bd77c1bf37f0afe78f4608261edd170e331cb36c8a0d--python@3.12-3.12.3.bottle_manifest.json
==> Fetching dependencies for python@3.12: mpdecimal, ca-certificates, openssl@3, readline, sqlite and xz
==> Downloading https://ghcr.io/v2/homebrew/core/mpdecimal/manifests/4.0.0-1
Already downloaded: /Users/hl/Library/Caches/Homebrew/downloads/7b63c3b34bee402290af49fac829a6682ab45ea5c9258b6fe03b590a03a4c4a9--mpdecimal-4.0.0-1.bottle_manifest.json
==> Fetching mpdecimal
==> Downloading https://ghcr.io/v2/homebrew/core/mpdecimal/blobs/sha256:377dc5e3
Already downloaded: /Users/hl/Library/Caches/Homebrew/downloads/a9dadc90cf8070e93fdea98523c5750106ae071d268c12827376dc68b3413fe7--mpdecimal--4.0.0.sonoma.bottle.1.tar.gz
==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/manifests/2024-
Already downloaded: /Users/hl/Library/Caches/Homebrew/downloads/c431e0186df2ccc2ea942b34a3c26c2cebebec8e07ad6abdae48447a52c5f506--ca-certificates-2024-03-11.bottle_manifest.json
==> Fetching ca-certificates
==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/blobs/sha256:ca
Already downloaded: /Users/hl/Library/Caches/Homebrew/downloads/2c0842d836eaa991deac09d2f933bf5ff5b214d7c5d225c7fe042f6262ca21a9--ca-certificates--2024-03-11.all.bottle.tar.gz
==> Downloading https://ghcr.io/v2/homebrew/core/openssl/3/manifests/3.3.0-1
Already downloaded: /Users/hl/Library/Caches/Homebrew/downloads/cd7e4cb72da375d51619d8c60fb91a70696e352edb515631b5751d05342e471d--openssl@3-3.3.0-1.bottle_manifest.json
==> Fetching openssl@3
==> Downloading https://ghcr.io/v2/homebrew/core/openssl/3/blobs/sha256:f37328ad
Already downloaded: /Users/hl/Library/Caches/Homebrew/downloads/3455162bf343a308a878ca43d6dbf80074b6095d437ef2881e2286eae64b9818--openssl@3--3.3.0.sonoma.bottle.1.tar.gz
==> Downloading https://ghcr.io/v2/homebrew/core/readline/manifests/8.2.10
Already downloaded: /Users/hl/Library/Caches/Homebrew/downloads/4ddd52803319828799f1932d4c7fa8d11c667049b20a56341c0c19246a1be93b--readline-8.2.10.bottle_manifest.json
==> Fetching readline
==> Downloading https://ghcr.io/v2/homebrew/core/readline/blobs/sha256:9796e0ff1
Already downloaded: /Users/hl/Library/Caches/Homebrew/downloads/59bbcc80140931455ac84229d716c168eb192d60b51e5e145505b26a86f590f2--readline--8.2.10.sonoma.bottle.tar.gz
==> Downloading https://ghcr.io/v2/homebrew/core/sqlite/manifests/3.45.3
Already downloaded: /Users/hl/Library/Caches/Homebrew/downloads/9eebda014aefbd585433274db989aa060c29200f656e6e106b4c1956438a2401--sqlite-3.45.3.bottle_manifest.json
==> Fetching sqlite
==> Downloading https://ghcr.io/v2/homebrew/core/sqlite/blobs/sha256:47e8a06001c
Already downloaded: /Users/hl/Library/Caches/Homebrew/downloads/a0da99184f68adc1a919dec9c8cc85c199776544a99e0b19331f21d1e0f85a01--sqlite--3.45.3.sonoma.bottle.tar.gz
==> Downloading https://ghcr.io/v2/homebrew/core/xz/manifests/5.4.6
Already downloaded: /Users/hl/Library/Caches/Homebrew/downloads/b2cc4077807c100af6e0253f51d186f187ff55165638cbe3a4aa16d1c4762660--xz-5.4.6.bottle_manifest.json
==> Fetching xz
==> Downloading https://ghcr.io/v2/homebrew/core/xz/blobs/sha256:139fcf6d46fb85d
Already downloaded: /Users/hl/Library/Caches/Homebrew/downloads/43f3e389ca6041ad27899272abc383096ddb15ec4c51b83d5873dbc44829cabe--xz--5.4.6.sonoma.bottle.tar.gz
==> Fetching python@3.12
==> Downloading https://ghcr.io/v2/homebrew/core/python/3.12/blobs/sha256:2bd309
Already downloaded: /Users/hl/Library/Caches/Homebrew/downloads/fdd7186f05e876456ea0bb6bf1201c13a917626753b8aacab6f707d7f8aab055--python@3.12--3.12.3.sonoma.bottle.tar.gz
==> Upgrading python3
  -> 3.12.3 
Error: Permission denied @ apply2files - /usr/local/Frameworks/Python.framework/Versions/3.6

2024-05-19 12:24:46 Action 13706204 failed: Execute a Shell Script failed with script error: Failed to reach the server: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:992)

I'm using a newly created project API key. Any ideas?

@strackerphil Thank you for sharing this. This is going to be fun!

AI Test.kmmacros (2.6 KB)

Here's another example of how I use this subroutine:
2

Ditto. I wonder if the API is only for paid accounts? I'm still using the freebie.

-rob.

I'm on a paid account, so I'd wager there's something else amiss. Eager to have a play with this if I can get it working...

That's a certificate error. The cert for https://api.openai.com is valid, but was renewed at the beginning of this month -- perhaps you've got an old version stuck in your keychain?

I might be missing the point of what you're saying but I created the API key yesterday specifically for this purpose.

The error is saying the SSL certificate is not validating -- nothing to do with your API key. Try going to the linked page with Safari and seeing what the "padlock" reveals.

This is what I see at the URL you gave me.

Now you have (hopefully) refreshed the certificate in your Keychain, see if the macro works. If you're still getting the same error, open up Keychain Access, search for "gts", and make sure "GTS Root R1" is valid and its trust is set to "Use system defaults".

Your original error implies the verification server was unavailable -- we're proving a chain of trust exists now, so if there's still a problem it's on the Python side rather than an external issue.

Looks to be ok:

The macro still doesn't work, unfortunately. :man_shrugging:t2: