Skip to content

Instantly share code, notes, and snippets.

@adrienbrault
Created November 30, 2022 21:49
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/9f6f7b687af5acf3db9463494f481360 to your computer and use it in GitHub Desktop.
Save adrienbrault/9f6f7b687af5acf3db9463494f481360 to your computer and use it in GitHub Desktop.

Write a markdown tutorial with code examples to get started with OpenAI’s GPT-3 text-davinci-003 completion API in a modern symfony 5 web application with full static typing. 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.

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

"Start using the power of #GPT3 in your #Symfony 5 Web App with this awesome tutorial! Learn how to use OpenAI's GPT-3 Text-Davinci-003 Completion API with full static typing 🔥 https://t.co/SjfhG3dsX5"

Tutorial: Using OpenAI's GPT-3 Text-Davinci-003 Completion API in a Symfony 5 Web App with Full Static Typing

The OpenAI GPT-3 Text-Davinci-003 Completion API is a powerful tool for creating natural language processing applications. This tutorial will show you how to use the GPT-3 API in a modern Symfony 5 web application with full static typing.

Prerequisites

Before you begin, you will need to install the following:

Step 1: Create a Symfony 5 Form with Multiple Fields

Create a new Symfony 5 form, which has multiple fields. For example, the form could have fields for a user’s name, age, and address.

// src/Form/UserFormType.php

namespace App\Form;

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

class UserFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('age')
            ->add('address');
    }

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

Step 2: Add a Textarea for Natural Language Input

Now, add a textarea to the form where the user can enter a natural language description of the information they want to fill the form with. This textarea will be used as the input for GPT-3.

// src/Form/UserFormType.php

namespace App\Form;

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

class UserFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('age')
            ->add('address')
            ->add('natural_language_input', TextareaType::class);
    }

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

Step 3: Install the OpenAI GPT-3 Text-Davinci-003 Completion API SDK

Next, install the OpenAI GPT-3 Text-Davinci-003 Completion API SDK. This will allow us to interact with the API. We will use Composer to install the SDK.

composer require guzzlehttp/guzzle
composer require openai/text-davinci-003

Step 4: Create a Method to Dynamically Build a Completion Prompt

Now, create a method that will dynamically build a completion prompt by using the Symfony form field names and description. This method should take the natural language input from the textarea and use it to build a completion prompt.

// src/Form/UserFormType.php

namespace App\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use OpenAI\TextDavinci\Sdk;

class UserFormType extends AbstractType
{
    private $sdk;

    public function __construct(Sdk $sdk)
    {
        $this->sdk = $sdk;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('age')
            ->add('address')
            ->add('natural_language_input', TextareaType::class);
    }

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

    private function buildCompletionPrompt(string $naturalLanguageInput): string
    {
        $completionPrompt = '';

        // Get the fields from the form
        $fields = $this->form->all();

        foreach ($fields as $field) {
            // Get the field name and description
            $name = $field->getName();
            $description = $field->getDescription();

            // Add the field name and description to the completion prompt
            $completionPrompt .= $name . ' - ' . $description . '\n';
        }

        // Add the natural language input to the completion prompt
        $completionPrompt .= $naturalLanguageInput;

        return $completionPrompt;
    }
}

Step 5: Use the GPT-3 API to Complete the Form

Finally, use the GPT-3 API to complete the form. Pass the completion prompt to the API and use the response to fill the form fields with the appropriate values.

// src/Form/UserFormType.php

namespace App\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use OpenAI\TextDavinci\Sdk;

class UserFormType extends AbstractType
{
    private $sdk;

    public function __construct(Sdk $sdk)
    {
        $this->sdk = $sdk;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('age')
            ->add('address')
            ->add('natural_language_input', TextareaType::class);
    }

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

    public function submit(array $data, FormInterface $form, Request $request)
    {
        $naturalLanguageInput = $data['natural_language_input'];
        $completionPrompt = $this->buildCompletionPrompt($naturalLanguageInput);

        // Use the GPT-3 API to complete the form
        $response = $this->sdk->complete($completionPrompt);

        // Use the response to fill the form fields
        $form->get('name')->setData($response['name']);
        $form->get('age')->setData($response['age']);
        $form->get('address')->setData($response['address']);
    }

    private function buildCompletionPrompt(string $naturalLanguageInput): string
    {
        // Build the completion prompt...
    }
}

Congratulations! You have successfully used the OpenAI GPT-3 Text-Davinci-003 Completion API in a modern Symfony 5 web application with full static typing. Now, your users can fill forms with natural language input.

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