|
| 1 | +# OpenAI |
| 2 | + |
| 3 | +Provides access to the OpenAI apis. To use them successfully, you need an |
| 4 | +OpenAI account with an API Key. The safest and easiest place to put your API Key is |
| 5 | +in the RGP Lua prefix for the script or the System Prefix in RGP Lua. Whichever |
| 6 | +you choose, add this line to your script: |
| 7 | + |
| 8 | +``` |
| 9 | +openai_api_key = "<your secure api key>" |
| 10 | +``` |
| 11 | + |
| 12 | +This library assumes that there is a global string variable `openai_api_key` that contains |
| 13 | +your API key. |
| 14 | + |
| 15 | +This code sends an https post message to OpenAI, so your `plugindef()` function must include |
| 16 | + |
| 17 | +``` |
| 18 | +finaleplugin.ExecuteHttpsCalls = true |
| 19 | +``` |
| 20 | + |
| 21 | +The `callback_or_timeout` parameters in each function determine if the call is synchronous or |
| 22 | +asynchronous. If it is a function, then this is the callback for an asynchronous call. |
| 23 | +If it is a number, it is the timout in seconds for a synchronous call. |
| 24 | +For synchronous calls, the function returns a session variable you should keep |
| 25 | +as long as the call is running. The callback function should have the following signature |
| 26 | + |
| 27 | +``` |
| 28 | +function my_callback(success, response) |
| 29 | +``` |
| 30 | + |
| 31 | +- success (boolean) indicates if the function succeeded |
| 32 | +- response (string) the table representing the json response from OpenAI on success or an error message on fail |
| 33 | + |
| 34 | +For asynchronous calls, the function returns the same two values directly. |
| 35 | + |
| 36 | +If you are calling this from a running dialog box, it is recommended to use async |
| 37 | +calls for better user responsiveness. If there is no dialog box, synchronous calls are |
| 38 | +okay as long as the timeout is reasonably short. |
| 39 | + |
| 40 | +## Functions |
| 41 | + |
| 42 | +- [create_completion(model, prompt, temperature, callback_or_timeout)](#create_completion) |
| 43 | +- [create_chat(model, prompt, temperature, max_tokens, callback_or_timeout)](#create_chat) |
| 44 | + |
| 45 | +### create_completion |
| 46 | + |
| 47 | +```lua |
| 48 | +openai.create_completion(model, prompt, temperature, callback_or_timeout) |
| 49 | +``` |
| 50 | + |
| 51 | +[View source](https://github.com/finale-lua/lua-scripts/tree/refs/heads/master/src/library/openai.lua#L107) |
| 52 | + |
| 53 | +Sends a request to the OpenAI API to generate a completion for a given prompt. |
| 54 | + |
| 55 | +| Input | Type | Description | |
| 56 | +| ----- | ---- | ----------- | |
| 57 | +| `model` | `string` | The model to use, e.g., "gpt-3.5-turbo" | |
| 58 | +| `prompt` | `string` | The prompt to send | |
| 59 | +| `temperature` | `number` | See the OpenAI documentation for information about this value. A reasonable value is between 1.0 and 2.0. | |
| 60 | +| `callback_or_timeout` (optional) | `function or number` | Defaults to 5.0. | |
| 61 | + |
| 62 | +| Return type | Description | |
| 63 | +| ----------- | ----------- | |
| 64 | +| `boolean` | if synchronous call. See above for asynchronous. | |
| 65 | +| `string` | if synchronous call. See above for asynchronous. | |
| 66 | + |
| 67 | +### create_chat |
| 68 | + |
| 69 | +```lua |
| 70 | +openai.create_chat(model, prompt, temperature, max_tokens, callback_or_timeout) |
| 71 | +``` |
| 72 | + |
| 73 | +[View source](https://github.com/finale-lua/lua-scripts/tree/refs/heads/master/src/library/openai.lua#L132) |
| 74 | + |
| 75 | +Sends a request to the OpenAI API to generate a completion for a given prompt. |
| 76 | + |
| 77 | +| Input | Type | Description | |
| 78 | +| ----- | ---- | ----------- | |
| 79 | +| `model` | `string` | The model to use, e.g., "gpt-3.5-turbo" | |
| 80 | +| `prompt` | `string` | The prompt to send | |
| 81 | +| `temperature` | `number` | A value between 0.0 and 1.0. See the OpenAI documentation for information. | |
| 82 | +| `max_tokens` | `number` | The maximum number of tokens to generate | |
| 83 | +| `callback_or_timeout` (optional) | `function or number` | Defaults to 5.0. | |
| 84 | + |
| 85 | +| Return type | Description | |
| 86 | +| ----------- | ----------- | |
| 87 | +| `boolean` | if synchronous call. See above for asynchronous. | |
| 88 | +| `string` | if synchronous call. See above for asynchronous. | |
0 commit comments