Skip to content

Instantly share code, notes, and snippets.

@adrienbrault
Created November 30, 2022 20:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrienbrault/6a0add1a01772fd4267555666a832c20 to your computer and use it in GitHub Desktop.
Save adrienbrault/6a0add1a01772fd4267555666a832c20 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 generate a markdown tutorial.

Getting Started with OpenAI's Completion API in a Symfony 5 Web Application

OpenAI's Completion API is a powerful tool that enables developers to create AI-powered applications. It's used to generate natural language processing (NLP) solutions and can be used in a variety of applications, such as chatbots, text summarization, and text generation.

In this tutorial, we'll demonstrate how to get started with OpenAI's Completion API in a modern Symfony 5 web application with full static typing. We'll use the GPT-3 model to generate a Markdown tutorial.

Prerequisites

Before getting started, you'll need to have the following installed:

Setting up the Client

Once you have the prerequisites installed, you can set up the client in your Symfony 5 web application. In the root directory of your project, run the following command to install the Completion API client:

$ composer require openai/completion-api-client

The installation should install the client and all its dependencies, including the guzzlehttp client and the OpenAI API client.

Configuring the Client

Next, you'll need to configure the client. To do so, create a new file named openai.yaml and add the following code to it:

openai: 
    client: 
        api_key: <YOUR_API_KEY>

Replace <YOUR_API_KEY> with your OpenAI API key.

Writing Your Code

Now, you can write the code to generate the Markdown tutorial. Create a new file named generate_markdown.php and add the following code to it:

<?php

require_once __DIR__.'/vendor/autoload.php';

use OpenAI\Client;

$client = new Client([
    'api_key' => $_ENV['OPENAI_API_KEY'],
]);

$response = $client->completions->create([
    'engine' => 'davinci',
    'prompt' => '# Getting Started with OpenAI\'s Completion API in a Symfony 5 Web Application',
    'max_tokens' => 500,
    'temperature' => 0.9,
]);

$generated_markdown = $response['choices'][0]['text'];

echo $generated_markdown;

The code above will generate a Markdown tutorial with 500 words and a temperature of 0.9.

Running the Code

Finally, you can run the code by executing the following command in the root directory of your project:

$ php generate_markdown.php

The command should output a Markdown tutorial generated by GPT-3.

Conclusion

In this tutorial, we demonstrated how to get started with OpenAI's Completion API in a modern Symfony 5 web application with full static typing. We used the GPT-3 model to generate a Markdown tutorial. With the Completion API, you can create AI-powered applications and generate natural language processing (NLP) solutions.

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