Comment on page
OpenAI
There are two different ways to use GPT to return the results in JSON format
The first is just specifying that in a prompt, like below:
{
"model":"gpt-3.5-turbo",
"temperature":1,
"messages":[
{
"role":"user",
"content":"Generate 5 startup ideas related to <topic>. Return the result as a JSON array of objects with two parameters, \"idea_name\" and \"idea description\". Make sure the response starts with [ and ], it shouldn't have anything besides the JSON array"
}
]
}
The above will return a response like the below:
{
"id": "chatcmpl-7XGWPIsIEzcZpZdlnTyBKcBLp1TXZ",
"object": "chat.completion",
"created": 1688162689,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "{\n \"idea1\": \"Clown delivery service: A startup that allows customers to hire clowns for various occasions and have them delivered to their desired location.\",\n \"idea2\": \"Clown therapy platform: An online platform that connects certified clown therapists with individuals seeking non-traditional therapy methods for mental health.\",\n \"idea3\": \"Digital clown training platform: A web-based platform that offers comprehensive courses and resources for aspiring clowns to learn and improve their skills remotely.\",\n \"idea4\": \"Clown-themed subscription box: A monthly subscription box service that delivers a curated selection of clown-themed merchandise, costumes, makeup, and accessories to subscribers.\",\n \"idea5\": \"Clown entertainment app: A mobile app that allows users to book professional clowns for parties, events, or performances directly from their smartphones.\"\n}"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 55,
"completion_tokens": 169,
"total_tokens": 224
}
}
As you can see the JSON is returned as the text content of the first message from the assistant. That way works fine but can get messy if you need a complex JSON returned. Starting in June 2023 OpenAI supports requesting a JSON directly, like below:
{
"model": "gpt-3.5-turbo",
"temperature": 1,
"messages": [{
"role": "user",
"content": "Generate 5 startup ideas related to clowns."
}],
"functions": [{
"name": "generate_idea",
"parameters": {
"type": "object",
"properties": {
"results": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"description": {
"type": "string"
}
},
"required": ["name", "description"],
"additionalProperties": false
}
}
},
"required": ["name", "description"]
}
}]
}
Note that you'll need to pass in a JSON Schema object as part of the request, this will tell OpenAI the schema of the JSON that you want returned. You can use ChatGPT to help you generate the JSON Schema object. The result is returned as follows:
{
"id": "chatcmpl-7XGbPfNZrDpY6j6K27TcLJCn5nnFl",
"object": "chat.completion",
"created": 1688162999,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"function_call": {
"name": "generate_idea",
"arguments": "{\n \"results\": [\n {\n \"name\": \"Clown Academy\",\n \"description\": \"An online platform that offers courses and workshops for aspiring clowns to learn various clowning techniques and skills.\"\n },\n {\n \"name\": \"Clown Delivery Service\",\n \"description\": \"A service that allows customers to hire clowns for special occasions and have them delivered to their doorstep.\"\n },\n {\n \"name\": \"Clown Therapy App\",\n \"description\": \"A mobile app that connects individuals in need of therapy with professional clowns who can provide laughter and joy as a form of therapy.\"\n },\n {\n \"name\": \"Clown Party Planning\",\n \"description\": \"A startup that specializes in organizing and planning clown-themed parties, providing entertainment, decorations, and other clown-related services.\"\n },\n {\n \"name\": \"Clown Merchandise Store\",\n \"description\": \"An online store that sells a wide range of clown merchandise, including clothing, accessories, toys, and collectibles.\"\n }\n ]\n}"
}
},
"finish_reason": "function_call"
}
],
"usage": {
"prompt_tokens": 60,
"completion_tokens": 224,
"total_tokens": 284
}
}
As you can see the JSON is now returned in a separate field, under "arguments".
In both of the cases above, the JSON is returned as text, so you won't be able to use it in Bubble directly. However, one easy way to
Last modified 3mo ago