HxOpenAI
A Haxe library for interacting with the OpenAI API.
Credits: This library is inspired by the work of FurretDev.
Table of Contents
Installation
You can install the library using the following methods:
- Method 1: Add it as a dependency in your
haxelib.json
file:
{
"dependencies": {
"hxopenai": "1.0.0"
}
}
- Method 2: Install it using Haxelib:
Ideally it would be best to simply
haxelib install hxopenai
, but there will be an issue with the naming and I haven't submitted the library just yet...
haxelib git hxopenai git@github.com:AxDSan/hxopenai.git
Usage
Import the library in your Haxe code:
import hxopenai.Instance;
Create an instance of the library:
var openai = new Instance();
openai.setApiKey("YOUR_OPENAI_API_KEY");
openai.setOrgId("YOUR_OPENAI_ORG_ID"); /* Optional */
You can obtain a openai api key from https://platform.openai.com/account/api-keys
Make requests to the OpenAI API using the available methods:
var textCompletion = {
model: "your_model",
prompt: "your_prompt",
// Additional parameters...
};
var response = openai.createTextCompletion(textCompletion);
Examples
If you're not a code adept and don't really want to dig the documentation (under construction) here you can find some bites set out for you; Examples on how you can use the library.
// Example 1 - TextCompletion using `text-davinci-003`
import hxopenai.*;
class Main {
static function main() {
var openai = new Instance();
openai.setApiKey("<YOUR-OPENAI-API-KEY>");
// Create a text completion.
var textCompletion:Typedefs.TextCompletion = {
prompt: "Once upon a time",
max_tokens: 256,
top_p: 1,
n: 1,
stream: false,
logprobs: null,
echo: false,
stop: ["\n"],
model: "text-davinci-003",
};
// Make the request.
var response = openai.createTextCompletion(textCompletion);
// Print the response.
trace(response.choices[0].text);
}
}
// Example 2 - ChatCompletion using `gpt-3.5-turbo`
import hxopenai.*;
class Main {
static function main() {
var openai = new Instance();
openai.setApiKey("<YOUR-OPENAI-API-KEY>");
Example usage of createChatCompletion
var chatCompletion:Typedefs.ChatCompletion = {
model: "gpt-3.5-turbo",
messages: [
{
role: "user",
content: "Hey there!"
}
]
};
var chatCompletionResult = openai.createChatCompletion(chatCompletion);
var messageContent = chatCompletionResult.choices[0].message.content;
trace(messageContent);
}
}
API Reference
Instance
new()
Creates a new instance of the library.
setApiKey(token:String)
Sets the OpenAI token to authenticate API requests.
setOrgId(orgId:String)
Sets the OpenAI organization ID.
makeRequest(endpoint:String, body:Dynamic):Dynamic
Makes a request to the OpenAI API using the specified endpoint and request body.
createTextCompletion(tc:Typedefs.TextCompletion):Dynamic
Creates a text completion request.
createChatCompletion(cc:Typedefs.ChatCompletion):Dynamic
Creates a chat completion request.
createImage(ic:Typedefs.CreateImage):Dynamic
Creates an image generation request.
createEdit(ce:Typedefs.CreateEdit)
Creates an edit request.
Typedefs
This section provides the typedefs used in the library. Refer to the source code for detailed information about each typedef.
Contributing
Contributions are welcome! Please refer to the CONTRIBUTING.md
file for guidelines.
License
This library is licensed under the MIT License.