In addition to the other good answers here, I just wanted to share some thoughts that might help you or others reading this to better control the format of the AI's outputs.
I have seen varying results with the AI sometimes not returning the specified format.
Generally with OpenAi's endpoints I have found myself making small tweaks to the prompts/request, running it, and repeating that process until I get closer to my desired output and format.
Things you can try, that seem to improve the chances of it returning the desired format more consistently:
1. In addition to saying "format as an HTML table" or "return as a JSON array", give it an example of the desired output.
For example:
return as a JSON object in this format:
{
"country" : "Brazil",
"population: 213,429,381
}
Or
return the results as an HTML table. Use the following code snippet as an example of the format:
<table>
<tr>
<th>Country</th>
<th>Population</th>
</tr>
<tr>
<td>Brazil</td>
<td>213,429,381</td>
</tr>
</table>
2. Try using a different model.
There are definitely differences I have seen between the gpt-3.5-turbo
and gpt-4
models.
3. Try setting the 'temperature' property to a lower value.
This should reduce variance of the output (for better or for worse depending on what you are needing).
4. Explore using Functions.
With the chat endpoint, they now have the ability to send up a function for the model to call. You can specify arguments for the functions, where you can say
functions : [{
name: "make a table",
description: "creates a table in valid HTML table format, that includes table headers",
parameters: {
type: "object",
properties: {
nameThisWhateverYouWantRawHTML: {
type: "string",
description: "the tabular data in HTML format: <table>...</table>"
}
}
}
}
]
Then in your prompt or chat messages include some instruction to trigger that function call.
5. Set the n
property in the request greater than 1.
This will generate multiple options to choose from in the response. If the first option is in an invalid format, use the next one.
6. Implement a retry strategy in your code.
If a request ever returns an invalid format, run it again.