How to train model to always return response in specific JSON format

I want my model to always return result in json format.
I tried training model with following structure:
{
“prompt”: “Some text: . \n Some Text 2: \n ”,
“completion”: “{
“data”: [{
“data1”: “Add Product”,
“data2”: “Verify total amount is updated when product is added”,
“data3”: [“Opportunity with no products added”],
“data4”: [“Add a product to Opportunity”, “Verify total amount is updated”],
“data5”: “Product name, Price, Quantity”,
“data6”: “Total amount should be updated with product details”
},
{
“data1”: “Add Product”,
“data2”: “Verify total amount is updated when product is added”,
“data3”: [“Opportunity with no products added”],
“data4”: [“Add a product to Opportunity”, “Verify total amount is updated”],
“data5”: “Product name, Price, Quantity”,
“data6”: “Total amount should be updated with product details”
}
]”
}

but I am not getting the desired result, need help to understand how I can train model to always return a json response.

hey, did u find a solution? I’m facing the same thing

Remember that GPT is an autocompletion engine based on its training data.

So, you want to follow common practices. Better yet, you should start by giving it your desired JSON schema, and then ask it to modify/improve it to fit better into what it believes is correct. Dall-E is a great representation of this. If you prompt it correctly you can get beautiful, in-depth and consistent artwork. If your prompt is confusing, or lacks understanding, you get clip art mashed together in Microsoft Paint.

In the case of OP: their JSON structure makes no sense. Ideally your result should look something like this. Enough that it’s not too in-depth, but the fundamentals are there and can be understood.

{
  "product": {
    // Product Name
    "name": string,
    // Product cost (per square foot)
    "cost": number,
    // Stock availability
    "inventory": number,
    // Is it discontinued?
    "isDiscontinued": boolean,
  }
}

Then, you can refine it using GPT. I used Typescript as I believe it’s the best for consistency with GPT.

/**
 * @typedef {Object} Product
 * 
 * @property {string} name - Product Name
 * @property {number} cost - Product cost (per square foot)
 * @property {number} inventory - Stock availability
 * @property {boolean} isDiscontinued - Is it discontinued?
 */
type Product = {
    "name": string,
    "cost": number,
    "inventory": number,
    "isDiscontinued": boolean,
  }

5 samples produced

/**
 * @type {Product}
 */
const product1 = {
  name: 'Wooden Flooring',
  cost: 10.5,
  inventory: 150,
  isDiscontinued: false,
}

/**
 * @type {Product}
 */
const product2 = {
  name: 'Marble Tiles',
  cost: 20.0,
  inventory: 100,
  isDiscontinued: false,
}

/**
 * @type {Product}
 */
const product3 = {
  name: 'Laminate Flooring',
  cost: 5.0,
  inventory: 300,
  isDiscontinued: true,
}

/**
 * @type {Product}
 */
const product4 = {
  name: 'Porcelain Tiles',
  cost: 15.0,
  inventory: 200,
  isDiscontinued: false,
}

/**
 * @type {Product}
 */
const product5 = {
  name: 'Hardwood Flooring',
  cost: 25.0,
  inventory: 50,
  isDiscontinued: true,
}

GPT wants to produce consistent JSON. But if your structure is confusing, or downright wrong, it will drift and produce errors.

Also note: it assumed I am selling flooring, possibly as a result of the price per square foot…

3 Likes