0

I'm trying to write prompts for chatGPT API. I want it to respond with purely machine readable JSON responses containing information I want.

I want it to appraise a description of a project, and in JSON, specify properties of that appraisal, such as "estimated_hours_of_work". I don't want it to give any text outside of what is requested in JSON format, so my code can evaluate and use the response.

How can I do that? I can't seem to engineer a prompt where the response is purely JSON, it always seems to give extra commentary such as:

Certainly! Here's the appraisal of the text you provided in pure JSON format, without any additional comments or text:

I either want to use gpt-3.5-turbo or gpt-4

Jodes
  • 14,118
  • 26
  • 97
  • 156
  • 1
    Hi. There's a lot more interaction about prompts on reddit (try the OpenAI subreddit). I've seen many dozens of prompts and remember seeing examples similar to what you're requesting. From what I gather, it's a bit of an art at this point. You may get the results you need, and find it doesn't work a few days later. – Mozahler May 15 '23 at 19:53

1 Answers1

1

My experiments with JSON output suggest that response can't blindly be trusted as there is always some "befores" and "afters" (with higher temperatures even more so). On the other hand JSON is always there somewhere in the response text.

Solution:

  1. Ask for JSON, but give a tiny example { colors: ["string"] }
  2. Use extractor function that finds { ... } see Extract JSON from text
  3. Return result.colors

Prompt:

Generate 5 colors depending on users mood.

Return results in JSON format like this: { colors: ["string"] }

Users input:

Happy

Response:

Here are some colors!

{ "colors": ["#FAD02E", "#53A647", "#00BCD4", "#FFC107", "#76FF03"] }

JSON extracted using extractor function:

{ "colors": ["#FAD02E", "#53A647", "#00BCD4", "#FFC107", "#76FF03"] }

Final result:

["#FAD02E", "#53A647", "#00BCD4", "#FFC107", "#76FF03"] 

--

If prompts are complex double-prompting might be required since for content generation requires high temperatures, but valid JSON requires low temperature.

JUBEI
  • 108
  • 1
  • 8