#Strict JSON Output Formatter
11 messages · Page 1 of 1 (latest)
How it works:
Input your desired output JSON format
Strict JSON Framework will always return LLM output in your desired JSON format (returns as JSON, not text)
It prompts GPT iterately via self-correcting rule-based error messages, and gets the desired output after a few iterations
If it fails to get it after the iteration limit (default: 2), it outputs an empty JSON
Features:
Free-text generation for output fields
Constrained generation from one element of the list
Constrained generation of the label from "label:description" elements of a list
Flexible header/value generation using angle brackets
Chain of thought prompting by ordering the output fields of the JSON
Multiple input processing using a list as input and outputting a list of JSON
If you're interested, can see more at github at tanchongmin/strictjson
I'm actively developing this so that it can fit my use case better!
Example Usage
Input:
res = strict_output(system_prompt = 'You are a friendly assistant meant to extract information from text',
user_prompt = text,
output_format = {"Summary": "<entity> nibbled on my <entity>", "Entity Caught": ["living", "non-living"],
"<location of injury>": "injury was caused by <entity>"}, verbose = True)
print(res)
Output (JSON format, not text):
{'Summary': 'Fish nibbled on my finger', 'Entity Caught': 'living', 'finger': 'injury was caused by fish'}
Backend:
System prompt:
You are a friendly assistant meant to extract information from text
You are to output the following in json format: {'Summary': '<entity> nibbled on my <entity>', 'Entity Caught': ['living', 'non-living'], '<location of injury>': 'injury was caused by <entity>'}.
Do not put quotation marks or escape character \ in the output fields.
If output field is a list, classify output into the best element of the list.
Any text enclosed by < and > indicates you must generate content to replace it. Example input: Go to <location>, Example output: Go to the garden
Any output key containing < and > indicates you must generate the key name to replace it. Example input: {'<location>': 'description of location'}, Example output: {school: a place for education}
User prompt:
One, two, three, four, five,
Once I caught a fish alive,
Six, seven, eight, nine, ten,
Then I let it go again.
Why did you let it go?
Because it bit my finger so.
Which finger did it bite?
This little finger on my right
GPT response: {"Summary": "Fish nibbled on my finger", "Entity Caught": "living", "finger": "injury was caused by fish"}
^^ If you're doing this on the ChatGPT interface, you can simply copy and paste the System prompt and User prompt together as your prompt.
Customization:
- Replace the "friendly assistant meant to extract information from text" with the role you want GPT to perform
- Replace the text below "User prompt" to your requirements
- Modify the json format to your requirements
- Get the GPT response from ChatGPT
If possible, copy the original request here, because I could not open your file.
Copy and paste this into ChatGPT and replace things in -- x --
System prompt:
You are a -- role you want ChatGPT to do --
You are to output the following in json format: -- your json format here. Output field key names will be maintained, value is to describe what the output field is --
Do not put quotation marks or escape character \ in the output fields.
If output field is a list, classify output into the best element of the list.
Any text enclosed by < and > indicates you must generate content to replace it. Example input: Go to <location>, Example output: Go to the garden
Any output key containing < and > indicates you must generate the key name to replace it. Example input: {'<location>': 'description of location'}, Example output: {'school': 'a place for education'}
User prompt:
-- Your text here --
If you run it on the provided Jupyter notebook with the OpenAI API, benefit is there will be an extra environment feedback loop to ensure the output fields are all present in the GPT ouputted json
But for most use cases, the prompt in ChatGPT is good enough
Looking into this for later. I made the latest version of my #1117763027769503764 prompt using a simple json syntax that seems to help a bit but am wondering what else i can do