Skip to content

Instantly share code, notes, and snippets.

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

Getting started with OpenAI's Completion API in Symfony 5

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.

Prerequisites

Before beginning, you will need:

  • A modern Symfony 5 web application
  • An OpenAI API key
  • Composer installed

Installation

  1. Install the OpenAI SDK via Composer:

    composer require openai/openai-sdk
    
  2. 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.

Usage

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);
    }
}

Conclusion

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.

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