Skip to content

Instantly share code, notes, and snippets.

@adrienbrault
Last active November 10, 2023 19:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrienbrault/673edf2db79db61ad316d0245c36b5ac to your computer and use it in GitHub Desktop.
Save adrienbrault/673edf2db79db61ad316d0245c36b5ac to your computer and use it in GitHub Desktop.
A tutorial generated by GPT-3, about how to use GPT-3 in a Symfony application with a real use case!

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 exercise is about a symfony form (with multiple fields) to which we add a textarea that is used to construct a GPT-3 prompt, that will return a json of the form data to use, which is then used to update the other form fields data. The GPT-3 prompt is constructed using the form fields names and their description. The code uses a composer package for the openapi sdk that is uses guzzle.

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

Tweet:

Learn how to use OpenAI's GPT-3 completion API in a modern Symfony 6 web application with this step-by-step tutorial! #Symfony #OpenAI #GPT3 #PHP #WebDev

#Markdown Tutorial

Introduction

In this tutorial, we will learn how to use the OpenAI GPT-3 completion API in a modern Symfony 6 web application using PHP 8 features. We will use the latest text-davinci-003 model to build a Symfony form with multiple fields and add a textarea that is used to construct a GPT-3 prompt. This prompt will return a JSON of the form data to use, which is then used to update the other form fields data.

Prerequisites

Before we start, make sure you have the following installed and configured:

Step 1: Setting up the Symfony Form

We will start by setting up a Symfony form with multiple fields. This form will contain fields such as name, address, email, etc.

To create the form, we will use the FormBuilder class. This class allows us to easily create and configure forms.

use Symfony\Component\Form\FormBuilder;

$builder = new FormBuilder();

$builder
    ->add('name', TextType::class)
    ->add('address', TextType::class)
    ->add('email', EmailType::class)
    // ...
;

Step 2: Adding the GPT-3 Textarea

Next, we will add a textarea to the form. This textarea will be used to construct a GPT-3 prompt that will return a JSON of the form data to use. The GPT-3 prompt will be constructed using the form fields names and their description.

To add the textarea, we will use the FormBuilder class again.

$builder
    ->add('gpt3_textarea', TextareaType::class, [
        'label' => 'GPT-3 Textarea',
        'required' => false
    ])
;

Step 3: Installing the OpenAPI SDK

Now that we have the form set up, we can install the OpenAPI SDK. This SDK will allow us to interact with the OpenAI GPT-3 completion API.

We will use the Guzzle package to install the SDK. To install it, run the following command in your terminal:

composer require guzzlehttp/guzzle

Step 4: Constructing the GPT-3 Prompt

Now that we have the SDK installed, we can construct the GPT-3 prompt. This prompt will be constructed using the form fields names and their description.

To construct the prompt, we will use the GuzzleHttp\Client class. This class allows us to make requests to the OpenAI API.

use GuzzleHttp\Client;

$client = new Client();

$prompt = 'Please fill out the following fields: ';

foreach ($form->getFields() as $field) {
    $prompt .= $field->getName() . ' (' . $field->getDescription() . '), ';
}

$prompt = rtrim($prompt, ', ');

Step 5: Making the Request

Now that we have the prompt constructed, we can make the request to the OpenAI API. The API will return a JSON of the form data to use.

To make the request, we will use the GuzzleHttp\Client class again.

$response = $client->request('POST', 'https://api.openai.com/v1/engines/davinci/completions', [
    'headers' => [
        'Authorization' => 'Bearer {your_api_key}'
    ],
    'json' => [
        'prompt' => $prompt,
        'temperature' => 0.9
    ]
]);

$data = json_decode($response->getBody(), true);

Step 6: Updating the Form Data

Finally, we can use the JSON data returned from the OpenAI API to update the form fields.

To update the form fields, we will use the FormBuilder class again.

foreach ($data as $fieldName => $fieldValue) {
    $builder->get($fieldName)->setData($fieldValue);
}

Conclusion

In this tutorial, we learned how to use the OpenAI GPT-3 completion API in a modern Symfony 6 web application using PHP 8 features. We used the latest text-davinci-003 model to build a Symfony form with multiple fields and added a textarea that is used to construct a GPT-3 prompt. This prompt returned a JSON of the form data to use, which was then used to update the other form fields data.

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