This repository contains a SuperCollider extension that encapsulates an API call to OpenAI's chat completions endpoint. The extension provides a MyAPIClass
with a method getResponse
that takes a custom prompt as an argument and returns the API's response.
-
Clone or Download the Repository
Clone this repository to your local machine or download it as a ZIP file and extract it:
git clone https://github.com/yourusername/SuperCollider-OpenAI-API-Call.git
-
Locate Your SuperCollider Extensions Folder
The extensions folder is typically located at:
-
macOS:
~/Library/Application Support/SuperCollider/Extensions
Go -> Go to folder... -> ~/Library -
Windows: Please refer to your SuperCollider installation documentation, or use your custom class path folder.
-
-
Copy the Extension File
Copy the file
MyAPIClass.sc
from this repository into your SuperCollider Extensions folder. -
Recompile the SuperCollider Class Library
Re-open the SuperCollider IDE to recompile the class library.
-
Using the Extension in Your SuperCollider Patch
Once the classes have been recompiled, you can asynchronously call the API from your SuperCollider code. For example:
( var customPrompt = "Write a haiku that explains the concept of recursion in a fun way."; MyAPIClass.getResponseAsync(customPrompt, { |response| response.postln; // You can now use this in a live coding context Pdef(\aiMusic, Pbind( \instrument, \default, \degree, Pseq([0, 2, 4, 5, 7], inf), \dur, 0.5, \amp, 0.2 ) ).play; }); ) Pdef(\aiMusic).stop;
This will execute the API call with the provided prompt and print the result in the post window.
-
API Key
Important: The API key is hard-coded in the
MyAPIClass.sc
file. Before using the extension, replace the API key YOUR-API-KEY with your valid API key.
- MyAPIClass.sc: Contains the SuperCollider class that performs the API call.
- README.md: Provides instructions on how to set up and use the extension.
This code is provided "as-is", without warranty of any kind. Use it at your own risk and ensure you manage your API key securely while complying with OpenAI's usage policies.
Happy live-coding!