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.
-
-
Save adrienbrault/7990a634d7680a4a10c1c748ebac515a to your computer and use it in GitHub Desktop.
OpenAI's Completion API provides an efficient way to automatically complete forms with natural language input, using the power of GPT-3. This tutorial will guide you through the process of incorporating OpenAI's Completion API into a modern Symfony 5 web application with full static typing.
Before beginning, you will need:
- A modern Symfony 5 web application
- An OpenAI API key
- Composer installed
-
Install the OpenAI SDK via Composer:
composer require openai/openai-sdk
-
Create a
config/packages/openai.yaml
file and add the following configuration:openai: api_key: your-api-key
Replace
your-api-key
with your OpenAI API key.
To use the OpenAI Completion API in your Symfony 5 application, you will need to create a completion prompt using the form field names and descriptions. The following example shows how to dynamically generate a completion prompt from a Symfony Form:
$form = $this->createForm(MyFormType::class);
$prompt = 'Please fill in the following fields: ';
$prompt .= implode(', ', array_map(function(FormInterface $field) {
return sprintf('%s (%s)', $field->getName(), $field->getDescription());
}, $form->all()));
$completion = $this->openai->completions->create([
'prompt' => $prompt
]);
Now that the completion prompt has been generated, you can render the form with a textarea for the user to enter their natural language description. The following example shows how to render the form with a textarea for the OpenAI Completion API:
{{ form_start(form) }}
{{ form_row(form.description, { 'attr': { 'placeholder': completion.prompt } }) }}
{{ form_row(form.submit) }}
{{ form_end(form) }}
Finally, when the form is submitted, the Completion API can be used to fill the form with the user's natural language description. The following example shows how to use the Completion API to fill the form with the user's natural language description:
$form->handleRequest($request);
if ($form->isSubmitted()) {
$completion = $this->openai->completions->create([
'prompt' => $prompt,
'completion_request' => $form->get('description')->getData()
]);
foreach ($completion->choices as $choice) {
$form->get($choice->value)->setData($choice->text);
}
}
In this tutorial, you learned how to incorporate OpenAI's Completion API into a modern Symfony 5 web application with full static typing. You installed the OpenAI SDK via Composer, configured the API key, and used the API to dynamically generate completion prompts from a Symfony Form. You also rendered the form with a textarea for the user to enter their natural language description and used the Completion API to fill the form with the user's natural language description.