OpenAiApi
in package
implements
AiApiInterface
uses
AiLoggingTrait, OpenAiClientTrait
General helper tools for interacting with the OpenAI API
Table of Contents
Interfaces
- AiApiInterface
- Base class to ensure common functions for API tooling
Constants
- ENDPOINTS : mixed = ['chatCompletion' => 'Chat Completions', 'image...
- ITEM_TYPES : mixed = ['files' => ['name' => 'Files', 'description' =...
- Items that can be created / stored / listed / deleted
Properties
- $_client : Client|null
- $_client_key : string
- $_last_response : array<string|int, mixed>
- $_log_id : int|null
- $_time_start : float
Methods
- chatCompletion() : string|null
- clearOverrideClient() : void
- createClient() : Client
- Create a new Open AI Client with the given API token.
- deleteItems() : int
- Delete a specified set of items of a given type
- getEndpoints() : array<string|int, mixed>
- Get a list of endpoints available for use with this class
- getLastRequestCost() : mixed
- Get the cost of the last request
- getLastResponse() : array<string|int, mixed>
- Get the last response from the API
- getOverrideClient() : Client|null
- Get the override client instance, if set
- getRequestCostUnit() : string
- Describe what the cost unit is for this system (e.g. dollars, tokens)
- getTokensUsed() : array<string|int, mixed>
- Get the number of tokens used in the last request
- getUsable() : bool
- Class specific check to see if we can use AI. E.g. do we have a key?
- imageGenerateBlob() : string
- Generate an image with Open AI and return a blob of image data
- imageGenerateSrc() : string|null
- Generate an image with Open AI and return a URL to the hosted file
- listItems() : array<string|int, mixed>
- List items of a given type. Note that returns the first page only.
- logAndThrowException() : SproutAiApiRequest
- Simple response logging wrapper to call in case of error
- logError() : SproutAiApiRequest
- Simple response logging wrapper to call in case of error
- logRequest() : SproutAiApiRequest
- Log a request on its way out to a provider
- logResponse() : SproutAiApiRequest
- Log a response from a provider. Associate with current request if possible.
- setOverrideClient() : void
- Set the client instance
Constants
ENDPOINTS
public
mixed
ENDPOINTS
= ['chatCompletion' => 'Chat Completions', 'imageGenerateSrc' => 'Image Generation - External URL', 'imageGenerateBlob' => 'Image Generation - Image Data']
ITEM_TYPES
Items that can be created / stored / listed / deleted
public
mixed
ITEM_TYPES
= ['files' => ['name' => 'Files', 'description' => 'Files are used to store data that can be used in completions, such as documents, images, and more.'], 'assistants' => ['name' => 'Assistants', 'description' => 'Assistants are used to aid in specific tasks, or drive highly customised chat bots.']]
Tags
Properties
$_client
private
static Client|null
$_client
= null
$_client_key
private
static string
$_client_key
= ''
$_last_response
private
static array<string|int, mixed>
$_last_response
= []
$_log_id
private
static int|null
$_log_id
$_time_start
private
static float
$_time_start
Methods
chatCompletion()
public
static chatCompletion(string|array<string|int, mixed> $prompt[, array<string|int, mixed> $config = [] ]) : string|null
Parameters
- $prompt : string|array<string|int, mixed>
- $config : array<string|int, mixed> = []
Return values
string|nullclearOverrideClient()
public
static clearOverrideClient() : void
createClient()
Create a new Open AI Client with the given API token.
public
static createClient(string $key[, string|null $organization = null ][, int|null $timeout = null ]) : Client
Parameters
- $key : string
- $organization : string|null = null
- $timeout : int|null = null
Tags
Return values
ClientdeleteItems()
Delete a specified set of items of a given type
public
static deleteItems(string $type, array<string|int, mixed> $item_ids[, null|array<string|int, mixed> $config = null ]) : int
Parameters
- $type : string
- $item_ids : array<string|int, mixed>
- $config : null|array<string|int, mixed> = null
Return values
int —The number deleted
getEndpoints()
Get a list of endpoints available for use with this class
public
getEndpoints() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>getLastRequestCost()
Get the cost of the last request
public
getLastRequestCost() : mixed
Tags
getLastResponse()
Get the last response from the API
public
static getLastResponse() : array<string|int, mixed>
This can be used to extract token usage, finish reason or other data
Return values
array<string|int, mixed>getOverrideClient()
Get the override client instance, if set
public
static getOverrideClient() : Client|null
Return values
Client|nullgetRequestCostUnit()
Describe what the cost unit is for this system (e.g. dollars, tokens)
public
getRequestCostUnit() : string
Tags
Return values
stringgetTokensUsed()
Get the number of tokens used in the last request
public
static getTokensUsed() : array<string|int, mixed>
Return values
array<string|int, mixed> —[completion_tokens => int, prompt_tokens => int, total_tokens => int]
getUsable()
Class specific check to see if we can use AI. E.g. do we have a key?
public
getUsable() : bool
Tags
Return values
boolimageGenerateBlob()
Generate an image with Open AI and return a blob of image data
public
static imageGenerateBlob(string $prompt[, array<string|int, mixed> $config = [] ]) : string
Output this with something like: echo "<img src="data:image/jpeg;base64,{$response}" style="max-width: 100%">";
https://platform.openai.com/docs/api-reference/images/create
Parameters
- $prompt : string
- $config : array<string|int, mixed> = []
Return values
string —Image contents
imageGenerateSrc()
Generate an image with Open AI and return a URL to the hosted file
public
static imageGenerateSrc(string $prompt[, array<string|int, mixed> $config = [] ]) : string|null
https://platform.openai.com/docs/api-reference/images/create
Parameters
- $prompt : string
- $config : array<string|int, mixed> = []
Return values
string|null —Image src URL
listItems()
List items of a given type. Note that returns the first page only.
public
static listItems(string $type[, null|array<string|int, mixed> $config = [] ]) : array<string|int, mixed>
Parameters
- $type : string
- $config : null|array<string|int, mixed> = []
Return values
array<string|int, mixed> —The item list
logAndThrowException()
Simple response logging wrapper to call in case of error
public
static logAndThrowException(string $function, string $endpoint, Exception $e) : SproutAiApiRequest
Parameters
- $function : string
-
The FUNCTION being ran at call time
- $endpoint : string
-
The provider endpoint we're calling
- $e : Exception
-
The exception thrown
Return values
SproutAiApiRequestlogError()
Simple response logging wrapper to call in case of error
public
static logError(string $function, string $endpoint, array<string|int, mixed> $data) : SproutAiApiRequest
Parameters
- $function : string
-
The FUNCTION being ran at call time
- $endpoint : string
-
The provider endpoint we're calling
- $data : array<string|int, mixed>
-
The response data
Return values
SproutAiApiRequestlogRequest()
Log a request on its way out to a provider
public
static logRequest(string $function, string $endpoint, array<string|int, mixed> $data) : SproutAiApiRequest
Parameters
- $function : string
-
The FUNCTION being ran at call time, if part of a class
- $endpoint : string
-
The provider endpoint we're calling
- $data : array<string|int, mixed>
-
The request data
Return values
SproutAiApiRequestlogResponse()
Log a response from a provider. Associate with current request if possible.
public
static logResponse(string $function, string $endpoint, array<string|int, mixed> $data[, bool $error = false ]) : SproutAiApiRequest
Use self::$logError() for error responses
Parameters
- $function : string
-
The FUNCTION being ran at call time
- $endpoint : string
-
The provider endpoint we're calling
- $data : array<string|int, mixed>
-
The response data
- $error : bool = false
-
Whether this is an error response. Auto set via self::$logError()
Return values
SproutAiApiRequestsetOverrideClient()
Set the client instance
public
static setOverrideClient(Client $client) : void
Parameters
- $client : Client