4D AIKit is a built-in 4D component that enables interaction with third-party AI APIs.
The OpenAI class allows you to make requests to the OpenAI API.
First of all, initialize the OpenAI client using your API key
var $client:=cs.AIKit.OpenAI.new("your api key")For a compatible provider API, you need to configure the server URL by setting the baseURL parameter.
var $client:=cs.AIKit.OpenAI.new({apiKey: "your api key"; baseURL: "https://your.server.ai"})or
$client.baseURL:="https://your.server.ai"OpenAI provides different endpoints called resources, each offering various functions.
var $result:=$client.<resource>.<function>(<parameters...>)The $result contains the HTTPRequest, a success status, a collection of errors and more. See OpenAIResult
See some examples below.
https://platform.openai.com/docs/api-reference/chat
https://platform.openai.com/docs/api-reference/chat/create
var $messages:=[{role: "system"; content: "You are a helpful assistant."}]
$messages.push({role: "user"; content: "Could you explain me why 42 is a special number"})
var $result:=$client.chat.completions.create($messages; {model: "gpt-4o-mini"})
// result in $result.choiceThis helper allows you to maintain a list of user messages and assistant responses.
var $helper:=$client.chat.create("You are a helpful assistant.")
var $result:=$helper.prompt("Could you explain me why 42 is a special number")
$result:=$helper.prompt("and could you decompose this number")
// conversation in $helper.messagesThis helper enables image analysis through the chat.
var $result:=$client.chat.vision.create($imageUrl).prompt("give me a description of the image")https://platform.openai.com/docs/api-reference/images
var $images:=$client.images.generate("A futuristic city skyline at sunset"; {size: "1024x1024"}).imageshttps://platform.openai.com/docs/api-reference/models
Get full list of models
var $models:=$client.models.list().models // you can then extract the `id`Get one model information by id
var $model:=$client.models.retrieve("a model id").modelhttps://platform.openai.com/docs/api-reference/files
Upload a file for use with other endpoints
var $file:=File("/path/to/your/file.jsonl")
var $result:=$client.files.create($file; "fine-tune")
var $fileId:=$result.file.idList all files
var $files:=$client.files.list().filesRetrieve file information
var $fileInfo:=$client.files.retrieve($fileId).fileDelete a file
var $deleteResult:=$client.files.delete($fileId)https://platform.openai.com/docs/api-reference/moderations
var $moderation:=$client.moderations.create("This text contains inappropriate language and offensive behavior.").moderationIf you do not want to wait for the OpenAPI response when sending a request to its API, you need to use asynchronous code. The result object will be received in a callback function.
See detailed documentation for examples
See the LICENSE file for details
- This library is not affiliated with, endorsed by, or officially connected to OpenAI in any way.
- "OpenAI" and any related marks are trademarks or registered trademarks of OpenAI, LLC. All rights related to OpenAI's services, APIs, and technologies remain the property of OpenAI.
- This project simply provides an interface to OpenAI’s services and does not claim any ownership over their technology, branding, or intellectual property.