Skip to content

Instantly share code, notes, and snippets.

@adrienbrault
Created November 30, 2022 22:01
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/8c2fd8f579cdc794be1a14484904b1a5 to your computer and use it in GitHub Desktop.
Save adrienbrault/8c2fd8f579cdc794be1a14484904b1a5 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 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, that is then used to update the other form fields data. 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: Check out this awesome tutorial on how to get started with OpenAI's GPT-3 completion API in a Symfony 6 web application using PHP 8 features! #OpenAI #GPT3 #Symfony #PHP8

#Tutorial

Introduction

OpenAI's GPT-3 completion API is a powerful tool that can be used to generate text, complete sentences, and provide context-aware suggestions. It is a great addition to any modern web application, and in this tutorial we will learn how to use the latest text-davinci-003 model in a Symfony 6 web application using PHP 8 features.

Prerequisites

Before you begin, you will need the following:

  • A Symfony 6 web application
  • The latest version of the OpenAI SDK (available as a composer package)
  • The latest text-davinci-003 model
  • Guzzle (for making HTTP requests)

Step 1: Create a Symfony Form

The first step is to create a Symfony form with multiple fields. We will use this form to construct our GPT-3 prompt.

<?php

namespace App\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class GPT3FormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('field1', TextType::class)
            ->add('field2', TextType::class)
            ->add('field3', TextType::class)
            ->add('field4', TextType::class);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            // Configure your form options here
        ]);
    }
}

Step 2: Add a Textarea

Next, we will add a textarea to our form. This textarea will be used to construct our GPT-3 prompt.

<?php

namespace App\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class GPT3FormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('field1', TextType::class)
            ->add('field2', TextType::class)
            ->add('field3', TextType::class)
            ->add('field4', TextType::class)
            ->add('gpt3_prompt', TextareaType::class);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            // Configure your form options here
        ]);
    }
}

Step 3: Construct the GPT-3 Prompt

Now that we have our form and textarea, we can construct our GPT-3 prompt. This prompt should contain the values of the other form fields, as well as the desired output format (json).

<?php

$prompt = 'field1: ' . $formData['field1'] . '\n' .
          'field2: ' . $formData['field2'] . '\n' .
          'field3: ' . $formData['field3'] . '\n' .
          'field4: ' . $formData['field4'] . '\n' .
          'output: json';

Step 4: Make the Request

Next, we will use the OpenAPI SDK to make a request to the GPT-3 API. This request should contain our prompt, as well as the text-davinci-003 model.

<?php

use OpenAPI\Client\Api\DefaultApi;

$apiInstance = new DefaultApi();
$prompt = 'field1: ' . $formData['field1'] . '\n' .
          'field2: ' . $formData['field2'] . '\n' .
          'field3: ' . $formData['field3'] . '\n' .
          'field4: ' . $formData['field4'] . '\n' .
          'output: json';

try {
    $result = $apiInstance->gpt3Complete($prompt, 'text-davinci-003');
    $json = $result->getData();
} catch (Exception $e) {
    echo 'Exception when calling DefaultApi->gpt3Complete: ', $e->getMessage(), PHP_EOL;
}

Step 5: Update the Form Data

Finally, we can use the json response to update the form data.

<?php

$formData['field1'] = $json['field1'];
$formData['field2'] = $json['field2'];
$formData['field3'] = $json['field3'];
$formData['field4'] = $json['field4'];

Conclusion

In this tutorial, we learned how to use OpenAI's GPT-3 completion API in a Symfony 6 web application using PHP 8 features. We created a form with multiple fields, added a textarea for constructing our GPT-3 prompt, made the request to the GPT-3 API, and used the response to update the form data. With the power of GPT-3, we can now provide context-aware suggestions in our web applications.

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