Skip to content

Instantly share code, notes, and snippets.

@adrienbrault
Created November 30, 2022 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrienbrault/2168d0176c959847d7be3e9d775d250d to your computer and use it in GitHub Desktop.
Save adrienbrault/2168d0176c959847d7be3e9d775d250d to your computer and use it in GitHub Desktop.

Write a markdown tutorial with code examples to get started with OpenAI’s GPT-3 completion API, using the latest text-davinci-003 model, in a modern symfony 6 web application using PHP 8 features. The main example is about having GPT-3 fill a symfony form (with multiple fields) initial data using a textarea where the user can enter a natural language description of the information. The code dynamically builds a completion prompt by using the symfony form field names and description. The code uses a composer package for the openapi sdk that is uses guzzle. The code examples only include changes to the code and not entire files.

Before the tutorial write a viral tweet to share the tutorial.

"Want to get started with OpenAI's GPT-3 completion API in your #symfony 6 web app? Check out this tutorial on how to use the latest text-davinci-003 model and PHP 8 features to have GPT-3 fill in your forms! #php #programming #development"

Tutorial: Using OpenAI's GPT-3 Completion API in a Symfony 6 Web Application

This tutorial will show you how to use the latest text-davinci-003 model of OpenAI's GPT-3 Completion API to dynamically fill a Symfony 6 web application form with natural language data using PHP 8 features.

Step 1: Install OpenAI SDK

Install the OpenAI SDK using Composer by running the following command.

composer require openai/sdk

Step 2: Create a GPT-3 Client

Create a GPT-3 client object using the OpenAI SDK with Guzzle.

$client = new OpenAI\Client('<YOUR_API_KEY>', new \GuzzleHttp\Client());

Step 3: Build the Completion Prompt

Build the completion prompt using the Symfony form field names and descriptions.

$prompt = 
  'Please enter a natural language description of the form fields: '.
  implode(', ', array_map(
    function ($field) {
      return $field['name'].' ('.$field['description'].')';
    },
    $form->getFields()
  ));

Step 4: Make the GPT-3 Completion Request

Make a GPT-3 completion request with the prompt and the natural language data.

$response = $client->completions->create(
  $prompt,
  [
    'max_tokens' => 500,
    'temperature' => 0.7,
    'top_p' => 0.9,
    'n' => 3,
    'stream' => false,
    'logprobs' => null,
    'stop' => '\n'
  ],
  ['text' => $text]
);

Step 5: Parse the GPT-3 Response

Parse the GPT-3 response into an array of field name and value pairs.

$data = array_reduce(
  $response->choices,
  function ($data, $choice) {
    foreach (explode("\n", trim($choice->text)) as $line) {
      [$name, $value] = explode(':', trim($line), 2);
      $data[$name] = $value;
    }
    return $data;
  },
  []
);

Step 6: Fill the Symfony Form

Fill the Symfony form with the data from the GPT-3 response.

$form->submit($data);
if ($form->isSubmitted() && $form->isValid()) {
  // Do something with the form data
}

And that's it! You now have a working form completion application using OpenAI's GPT-3 Completion API in a Symfony 6 web application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment