AI - Direct Access

<< Click to Display Table of Contents >>

Navigation:  3. Script Language > AI - Artificial Intelligence Commands > AIC. - Artificial Intelligence Command >

AI - Direct Access

 

The Commands of this Section give you direct Control over the results that are returned from the Open AI-API.


In the OpenAI API, when you make a request to one of its endpoints, the response you receive is structured in a format known as JSON (JavaScript Object Notation).
JSON is a widely-used data interchange format that is both human-readable and easy for machines to parse and generate.

 

clip0660

Generated using Stable Diffusion "Neural Network".

 

Here’s a brief overview of JSON:

 

JSON is composed of key-value pairs.

Each key is a string, and the value can be a string, number, boolean, null, array, or another JSON object.

JSON objects are enclosed in curly braces {}.

Arrays are ordered lists and are enclosed in square brackets [].

Key-value pairs within objects are separated by commas.

 


Now, let's talk about the OpenAI API response*:

 

The OpenAI API typically returns a JSON structure that contains various pieces of information in response to your request. For instance, if you are using the completion endpoint, the response might contain generated text, model information, and other metadata.

 

Here is an example of a JSON structure that you might receive from the completion endpoint, it contains two alternative results:

 

{

  "id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7",

  "object": "text_completion",

  "created": 1589478378,

  "model": "text-davinci-003",

  "choices": [

    {

      "text": "\n\nThis is the first Choice",

      "index": 0,

      "logprobs": null,

      "finish_reason": "length"

    }

 {

      "text": "\n\nThis is the second choice",

      "index": 1,

      "logprobs": null,

      "finish_reason": "length"

    }

  ],

  "usage": {

    "prompt_tokens": 5,

    "completion_tokens": 7,

    "total_tokens": 12

  }

}


The AIC. Command also provides direct access commands which allow you to directly access specific fields within the JSON structure.

For example, you can directly access the text, role and content fields if they are part of the response.

 

For this you can just call the command

AIC.Get Role|<index>|$$RET

and you get the role-output of the last call directly in the SPR-Variable. Ready to use.

 

Hereby, the AIC.-Commands takes care of escaping and unescaping the JSON structure for you.

 

Why is Escaping Necessary?

JSON data is structured with specific characters such as curly braces {}, square brackets [], double quotes ", and colons :.

However, the actual data you are working with might also contain these characters.

For example, if you have a string that contains double quotes, it could be misinterpreted as the end of a string in the JSON structure.

 

Escaping is the process of converting characters that have special meaning in JSON syntax into a format that can be included in a JSON string without breaking the structure.

For example, the double quote character " is escaped as \". This tells the JSON parser that this character is part of the data and should not be treated as a control character.

 

Why is Unescaping Necessary?

Unescaping is the reverse process of escaping.
When you retrieve data from a JSON structure, you want it in its original form, not in the escaped form. Unescaping converts the escaped characters back into their original form.

Automatic Escaping and Unescaping in AIC Commands

In the context of using the OpenAI API with AIC (AI Commands), the escaping and unescaping processes are handled automatically.

This means that you don't have to worry about manually replacing special characters with their escaped equivalents when sending data, or converting them back when retrieving data.

 

This automation is highly beneficial as it:

Simplifies the Code: You don’t have to write additional code for escaping and unescaping, making your code cleaner and easier to maintain.

Prevents Errors: Automatically handling escaping and unescaping helps in preventing syntax errors in the JSON structure which could be caused by special characters.

Saves Time: It saves development time as you can focus on the core functionality rather than the intricacies of data formatting.

 

By handling these processes automatically, the AIC commands ensure a smooth and error-free interaction with the JSON data returned by the OpenAI API.


 

 

*More detailled Info see here: Open AI API Reference