Often when I try to generate code with OpenAI's text-davinci-003 model, the output is lead by a introductory sentence.
In an instance where I instructed the code to list all the variables in a PHP file using JSON, the returned result starts with:
The JSON array would look like this:
...followed by indeed a JSON array with the desired result — so that's great. However, the introductory line makes it hard for me to parse the JSON.
Question: Is there a way to instruct text-davinci-003 not to include a introductory sentence and "get right to it"?
You can do this in two ways:
Prompt Design
This was a prompt I tested in the OpenAI playground.
The following is PHP code. Return only a JSON response with a list of all the variables and their values. The variables will be the key and the values the JSON values.
[Code]
<?php
$name = "John";
$age = 25;
$location = "New York";
echo "My name is $name, I am $age years old and I live in $location.";
?>
[JSON]
GPT returned the following response:
{"name": "John", "age": 25, "location": "New York"}
One-shot training
This is where you provide a single example in the prompt with example code and an example output. This primes GPT to provide the answer in the format you like.
The following is PHP code. Return only a JSON response with a list of all the variables and their values. The variables will be the key and the values the JSON values.
[Example Code]
<?php
$name = "John";
$age = 25;
$location = "New York";
echo "My name is $name, I am $age years old and I live in $location.";
?>
[Example JSON]
{"name": "John", "age": 25, "location": "New York"}
[Code]
<?php
$name = "John";
$age = 30;
echo "My name is $name and I am $age years old.";
?>
[JSON]
Make sure to use the OpenAI playground to test your prompts and get the response you want before adding it to your code.