Quick Start
Basic Usage Example
Full Documentation
- Each request might take longer periods of time to be completed, statement especially valid for the image creation API. Remember to set your request timeout to 120 seconds or greater to ensure that you receive the content of all AI generated content we send.
- If you exceed your monthly API credit count, the API will respond with an error, prompting you that you are rate limited.
- The server is set to the UTC timezone, call counts will be reset monthly based on this time zone.
- Each request will return a JSON string containing the AI generated content, in the ‘result’ field. Also, the responses will contain the remaining API credit count, in the ‘remainingtokens’ field.
Generating AI Text and Images
1. Generating text using the AI writer
API Endpoint:
https://aiomaticapi.com/apis/ai/v1/text
Sample Call:
Result:
API parameters:
- apikey – *required – add your API key for the call – be sure to have a valid subscription for the call to work
- prompt – *required – add the prompt based on which the AI should generate text – for best results, be sure to urlencode the prompt so it is received correctly by the API.
- model – Select the model you wish to use when creating textual content. Available models are: gpt-3.5-turbo-instruct.
- temperature – Advanced parameter (optional). What sampling temperature to use. Higher values means the model will take more risks. Try 0.9 for more creative applications, and 0 (argmax sampling) for ones with a well-defined answer. We generally recommend altering this or top_p but not both.
- top_p – Advanced parameter (optional). An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. We generally recommend altering this or temperature but not both.
- presence_penalty – Advanced parameter (optional). Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model’s likelihood to talk about new topics.
- frequency_penalty – Advanced parameter (optional). Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model’s likelihood to repeat the same line verbatim.
API result:
- remainingtokens – The number of API tokens remaining for your API key.
- finish_reason – Why did the AI stop generating content. The possible values for finish_reason are:
- stop: API returned complete message, or a message terminated by one of the stop sequences provided via the stop parameter
- length: Incomplete model output due to max_tokens parameter or token limit
- content_filter: Omitted content due to a flag from our content filters
- result – The AI generated text.
– In case of error: a JSON with the following content:
- remainingtokens – The number of API calls remaining for your API key. In case of error, this might display 0. In case of errors, the used token count will not be increased.
- error – The error message containing the reason of the failure.
2. ChatGPT/GPT-4 models usage
API Endpoint:
https://aiomaticapi.com/apis/ai/v1/chat
Sample Call:
Result:
API parameters:
- apikey – *required – add your API key for the call – be sure to have a valid subscription for the call to work
- messages – *required – if you make a GET request, simply add the prompt based on which the AI should generate text. If you make a POST request, be sure to add the following array in the POST request, in the “messages” parameter: array(“role” => “user”, “content” => “YOUR_INPUT”). You can add an array of multiple messages, the possible roles are: “user”, “assistant” or “system”. You should add pretraining prompts in the first message, with the role “system”, afterwards add multiple “user” and “assistant” messages in the sent array. If you want to use vision, you need to use a different format in the “messages” parameter (in this case, only POST requests are supported): array(“role” => “user”, “content” => array(array(“type” => “text”, “text” => “YOUR_INPUT”), array(“type” => “image_url”, “image_url” => “IMAGE_URL_FOR_VISION”)))
- model – Select the model you wish to use when creating textual content. Available models are: gpt-3.5-turbo, gpt-3.5-turbo-16k, gpt-3.5-turbo-0125, gpt-3.5-turbo-1106, gpt-4, gpt-4-1106-preview, gpt-4-0125-preview, gpt-4-turbo-preview, gpt-4-turbo, gpt-4-turbo-2024-04-09, gpt-4o, gpt-4o-2024-05-13, gpt-4o-2024-08-06, chatgpt-4o-latest, gpt-4o-mini, gpt-4o-mini-2024-07-18, gpt-4-vision-preview, gpt-4-0314, gpt-4-0613, gpt-4-32k, gpt-4-32k-0314, gpt-4-32k-0613, o1-preview, o1-preview-2024-09-12, o1-mini, o1-mini-2024-09-12, o1, o1-2024-12-17.
- temperature – Advanced parameter (optional). What sampling temperature to use. Higher values means the model will take more risks. Try 0.9 for more creative applications, and 0 (argmax sampling) for ones with a well-defined answer. We generally recommend altering this or top_p but not both.
- top_p – Advanced parameter (optional). An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. We generally recommend altering this or temperature but not both.
- presence_penalty – Advanced parameter (optional). Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model’s likelihood to talk about new topics.
- frequency_penalty – Advanced parameter (optional). Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model’s likelihood to repeat the same line verbatim.
- stream – Advanced parameter (optional). Boolean (true/false) – defines if response streaming mode is enabled. If set, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message.
- tools – An array of function objects, to define the available functions which can be called by the AI. Check required format in the example below.
- tool_choice – “auto” is default. If you want to force the model to call a specific function you can do so by setting tool_choice: {“name”: “
“}. You can also force the model to generate a user-facing message by setting tool_choice: “none”. Note that the default behavior (tool_choice: “auto”) is for the model to decide on its own whether to call a function and if so which function to call.
API result:
- remainingtokens – The number of API tokens remaining for your API key.
- finish_reason – Why did the AI stop generating content. The possible values for finish_reason are:
- stop: API returned complete message, or a message terminated by one of the stop sequences provided via the stop parameter
- length: Incomplete model output due to max_tokens parameter or token limit
- tool_calls: The model decided to call one or multiple functions
- content_filter: Omitted content due to a flag from our content filters
- result – The AI generated text, in the following JSON encoded format: {“role”:”assistant”,”content”:”THE_AI_RESPONSE”}.
– In case of error: a JSON with the following content:
- remainingtokens – The number of API calls remaining for your API key. In case of error, this might display 0. In case of errors, the used token count will not be increased.
- error – The error message containing the reason of the failure.
Extra feature: Function Execution
Prerequisites:
Before you proceed, ensure that you have the following prerequisites in place:
- Latest Version of Turbo or GPT-4:
- To use the Function Calling feature, you need the latest version of AI models (at least gpt-3.5-turbo-1106 or higher). Please verify that your AI model is up-to-date, especially if you are using Azure with deployments of models that might not be the latest ones.
Setting Up Function Calling:
Use the below code structure to enable function calling in AiomaticAPI:
$messages = array(array("role" => "user", "content" => "What's the weather like in Boston?"));
$functions = array(
'type' => 'function',
'function' =>
array(
"name" => "get_current_weather",
"description" => "Get the current weather in a given location",
"parameters" => array(
"type" => "object",
"properties" => array(
"location" => array(
"type" => "string",
"description" => "The city and state, e.g. San Francisco, CA",
),
"unit" => array("type" => "string", "enum" => array("celsius", "fahrenheit")),
),
"required" => array("location"),
),
),
);
$data = array(
"model" => "gpt-3.5-turbo-1106",
"messages" => $messages,
"tools" => $functions,
"tool_choice" => "auto", // auto is default, but we'll be explicit
);
Interpreting results and executing functions based on AI responses:
//check the AI reply, if it contains the tool_calls parameter, execute our function
if (isset($reply->tool_calls) && !empty($reply->tool_calls))
{
foreach($reply->tool_calls as $tool_call)
{
//check if your function name is called by the AI
if ($tool_call->name === 'send_email')
{
//do the stuff from your function, in this case, send an email
$subject = $tool_call->arguments->subject;
$message = $tool_call->arguments->message;
mail("admin@yoursite.com", $subject, $message);
}
}
}
Note that in case a function is called by the AI, a textual response will not be returned by it. So, please handle this case also, you might need to set up a default textual response for chatbots, in case functions are called by the AI.
3. Getting Embeddings data using the API
API Endpoint:
https://aiomaticapi.com/apis/ai/v1/embeddings
Sample Call:
Result:
API parameters:
- apikey – *required – add your API key for the call – be sure to have a valid subscription for the call to work
- input – *required – add the text for which you want to generate the embeddings data.
- model – Select the model you wish to use when creating the embeddings. Available models are: text-embedding-3-small, text-embedding-3-large, text-embedding-ada-002.
API result:
- remainingtokens – The number of API tokens remaining for your API key.
- result – The embeddings data, in the following format: [{“object”:”embedding”,”index”:0,”embedding”:[-0.025966445,0.0040175244,…]}]
– In case of error: a JSON with the following content:
- remainingtokens – The number of API calls remaining for your API key. In case of error, this might display 0. In case of errors, the used token count will not be increased.
- error – The error message containing the reason of the failure.
4. Generating images using the AI
API Endpoint:
https://aiomaticapi.com/apis/ai/v1/image
Sample Call:
Result:
API parameters:
- apikey – *required – add your API key for the call – be sure to have a valid subscription for the call to work.
- prompt – *required – add the prompt based on which the AI should generate an image – for best results, be sure to urlencode the prompt so it is received correctly by the API.
- model – set the image generator model to be used. Supported values are: dall-e-2, dall-e-3, dall-e-3-hd. The default value for the model is: dall-e-2.
- image_size – select the image size which should be generated. Supported values for Dall-E 2 are: 256×256, 512×512, 1024×1024. Supported values for Dall-E 3 and Dall-E 3 HD are: 1024×1024, 1792×1024, 1024×1792.
- style – the style of the generated images. Must be one of vivid or natural. Vivid causes the model to lean towards generating hyper-real and dramatic images. Natural causes the model to produce more natural, less hyper-real looking images. This param is only supported for dall-e-3 or dall-e-3-hd.
API result:
- remainingtokens – The number of API tokens remaining for your API key.
- result – The URL of the AI generated image.
– In case of error: a JSON with the following content:
- remainingtokens – The number of API calls remaining for your API key. In case of error, this might display 0. In case of errors, the used token count will not be increased.
- error – The error message containing the reason of the failure.
What’s next?
There’s a lot more that you can configure and tune in AiomaticAPI to handle the needs of your application. Be sure to read about all the options it exposes and how to get the most out of this API.