Skip to content

Instantly share code, notes, and snippets.

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

Write a markdown tutorial with code examples to get started with OpenAI’s 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.

Tweet: "Learn how to use OpenAI's completion API in a modern Symfony 5 web app with full static typing. #symfony #openAI #gpt3"

Markdown Tutorial for OpenAI's Completion API in a Symfony 5 Web Application

In this tutorial, we will discuss how to get started with OpenAI's completion API in a modern Symfony 5 web application with full static typing. We will be focusing on building a form that utilizes OpenAI's completion API to fill in initial data from a textarea where a user can enter a natural language description of the information. We will be using a composer package for the OpenAPI SDK that uses Guzzle.

Prerequisites

  • Familiarity with Symfony 5
  • Familiarity with OpenAI's Completion API
  • A composer package for the OpenAPI SDK

Step 1: Install the OpenAPI SDK

First, we need to install the OpenAPI SDK. This can be done using composer:

composer require guzzlehttp/guzzle

Once the package is installed, we can now use it in our Symfony 5 application.

Step 2: Create the Form

Next, we need to create the form that we will be using with the OpenAPI SDK. This form will have multiple fields and will have a textarea where a user can enter a natural language description of the information that they want to fill in the form fields.

Here is an example of a Symfony 5 form that we can use:

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextAreaType;

class OpenAIFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class)
            ->add('description', TextAreaType::class)
            ->add('location', TextType::class)
            ->add('openAI', TextAreaType::class);
    }
}

Step 3: Dynamically Build the Completion Prompt

Now that we have our form created, we need to dynamically build the completion prompt by using the Symfony form field names and description. To do this, we can use the OpenAPI SDK.

First, we will create an OpenAPI client:

$client = new OpenApi\Client('https://api.openai.com/v1/engines/davinci/completions');

Next, we will create a prompt by iterating over the form fields:

$prompt = '';
foreach ($form->all() as $field) {
    $prompt .= $field->getName() . ': ' . $field->getDescription() . '\n';
}

Finally, we will use the OpenAPI SDK to make the completion request and get the response from the OpenAI's completion API:

$response = $client->completions->create([
    'prompt' => $prompt,
    'temperature' => 0.9
]);

$completions = $response->body->choices;

Step 4: Populate the Form with the Completion Response

Now that we have our completion response, we can use it to populate the form fields with the initial data.

We can do this by iterating over the form fields and setting the initial data with the corresponding completion response:

foreach ($form->all() as $field) {
    $field->setData($completions[$field->getName()]);
}

Conclusion

In this tutorial, we discussed how to get started with OpenAI's completion API in a modern Symfony 5 web application with full static typing. We focused on building a form that utilizes OpenAI's completion API to fill in initial data from a textarea where a user can enter a natural language description of the information. We used a composer package for the OpenAPI SDK that uses Guzzle. Finally, we discussed how to dynamically build a completion prompt and how to populate the form with the completion response.

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