9.9.4 OpenAI: ChatGPT, GPTs, DALL.E - caligrafy/caligrafy-quill GitHub Wiki

Artificial Intelligence has made significant strides in the last couple of years especially - unexpectedly - in the creative world. OpenAI has been a major player in bringing generative artificial intelligence to the mass. For those of you who have not heard about generative AI before, it is the ability of the machine to create and produce new ideas from what it already knows. ChatGPT and DALL.E and Midjourney, to name a few, are examples of generative AI that create art (text or images) out of simple sentences provided by a user.

Caligrafy integrates seamlessly with many of the AI models provided by OpenAI to give the opportunity for developers to use them easily and to build next generation applications.

Initializing OpenAI

Step 1: Get an OpenAI Account

The first thing that needs to be done is to sign up for a free OpenAI account. When you sign up, OpenAPI gives you $18 for free to use its models.

Step 2: Get an OpenAI API Key

  • Create a new OpenAI secret key that will allow you to use the OpenAI APIs
  • Copy the API key and paste it into the Caligrafy .env file next to the OPEN_AI_KEY=<paste your key here>

Step 3 (Optional): Example of Caligrafy OpenAI

Caligrafy provides you with an out-of-the-box controller to interface with the Caligrafy OpenAI api. In order for you to explore it:

  • Uncomment the 2 GPT AI routes that are in web.php:

        // GPT AI controller
        // Route::get('/ai', 'AIController');
        // Route::post('/ai', 'AIController@query');
  • Explore the AIController.php that is provided in the Controllers folder in order to see how the api can be used

That's it, you can now start the Caligrafy OpenAI module to create your next big idea!

Understanding Caligrafy OpenAI

Caligrafy OpenAI gives you access to both GPT-3 for natural language processing and DALL.E for image generation. Upon instantiation of the module, you need to specify the model type that you would like to use - 'TEXT' or 'IMAGE'. If you instantiate without specifying, it will go with the 'TEXT' model type.

   
   use Caligrafy\Controller;
   use Caligrafy\OpenAI;

   class YourAIController extends Controller {
            
      public function index()
      {
          // Default is TEXT that uses the GPT-3
          $openai = new OpenAI(); // equivalent to $openai = new OpenAI('TEXT')

          // IMAGE that uses DALL.E
          $openai_image = new OpenAI(IMAGE);

       }
  }

Caligrafy OpenAI provides you with ONE method - delegate(...) that gives you access to all the features that GPT and DALL.E give you.

 
public function delegate($input = '', $outcome = '', $command = ANALYZE, $context = array(), $parameters = array());

// OR

public function delegate(
    input: '',
    outcome: '',
    command: ANALYZE,
    context: array(["role" => "user", "content" => "this is a message" ], []...), // role can be user or system
    parameters: array()
);
  • input: String that describes the information that you want the machine to use when creating something for you. For example: "picture of people shopping" or "tell me the story of a rabbit running away from a fox"

  • outcome: String that helps you scope the prompt of the machine. In other words, it helps give better direction to the machine onto what you are expecting to get. For example: "use second-grader language" or "correct the grammar only"

  • command: A command is the type of reasoning that the machine should have to produce the outcome for you. In other words, what would you like it to do. There are 7 different types of commands that Caligrafy provides you with:

    • CONVERSE: Applies to ‘TEXT’ type only. This is a typical verbose type of conversation where you ask the machine what you want from it using plain language

    • SUMMARIZE: Applies to ‘TEXT’ type only. Command that asks the machine to objectively and factually give you a summary of what you provide in the input.

    • ANALYZE: Applies to ‘TEXT’ type only. Command that asks the machine to interpret and analyze based on the information and data that you provide in the input.

    • CORRECT: Applies to ‘TEXT’ type only. Command that asks the machine to correct what you provide in the input based on what you want it to correct that you specify in the outcome. If you don't specify an outcome, it will correct anything that it interprets as wrong.

    • CLASSIFY: Applies to ‘TEXT’ type only. Command that asks the machine to categorize what you provide in the input based on the categories that you provide in the outcome. If you don't specify an outcome, it will create categories itself based on its understanding of the input.

    • COMPLETE: Applies to ‘TEXT’ type only. Command that asks the machine to build on top and of what you provide in the input and complete it based on its understanding of it. You can use the injection word [insert] in the input to tell the machine where in you want it to add its creation.

    • PREDICT: Applies to ‘TEXT’ type only. Command that asks the machine to follow the trend that you provide in the input and forecast future values or anticipate what comes next. You can specify what you want it to predict in the outcome.

    • RECOMMEND: Applies to ‘TEXT’ type only. Command that asks the machine to give recommendations based on information provided in the ‘input’. The ‘outcome’ can be used to specify what kind of recommendation is expected.

    • CUSTOM: Applies to 'TEXT' type only. You can define any system prompt of your choice.

  • context: If you save all the previous conversation as an exchange between 'system' and 'user', you can feed it back into the context in order to have GPT have a memory to understand the context of the conversation.

  • parameters: Array of attributes that can help you manipulate the different model options for both TEXT (GPT) and IMAGE (DALL.E) models.

    • For TEXT: The controls are model, temperature, response_format, max_tokens, top_p, frequency_penalty etc. GPT-3 is the selected model by default. The model can be changed by adding the "model" => <NAME OF MODEL> to the parameters array. You can learn more about those attributes here.

    • For IMAGE: The controls are prompt, n, size. You can learn more about those attributes here

Using Caligrafy OpenAI

In order to use Caligrafy OpenAI:

  1. Create a controller and import OpenAI.php

       <?php
    
         use Caligrafy\Controller;
         use Caligrafy\OpenAI;
    
         class YourAIController extends Controller {
            // your code goes here
         }
  2. Instantiate an OpenAI object and use the delegate method

         use Caligrafy\Controller;
         use Caligrafy\OpenAI;
    
         class YourAIController extends Controller {
            
             public function index()
             {
                 $openai = new OpenAI();
                 
                 // provide the arguments as explained previously
                 // display the response on the screen
                 dump($openai->delegate(<parameters>));  
             }
         }
⚠️ **GitHub.com Fallback** ⚠️