Generator API¶
Note
The generator API
is available in SLU v4.1 or later.
The generator API
enables you to generate a structured response based on JSON-formatted input data.
The generator API
can be helpful whenever you need to extract, aggregate, and present information to the user in a structured, human-readable format. If you have JSON-formatted data, you can use the generator API
to:
Summarize information
Create data digests
Generate reports and so on
To use the generator API
, add the following logic to your dialog script:
Create a
generator
, pass to it the output data format (markdown) and prompt for output data formatting. The prompt’s definition should be human-readable.Use the
generate()
method to generate a response. Thegenerate()
method takes a query, a prompt for the data output, and JSON data against which the query should be run.(Optional) Use the
gather()
method to get the final response. This step is required only if you want to use the obtained response in the dialog logic.
// Create generator
let generator = api.generator_v1({
format: 'markdown',
formatting: 'highlight headers and bold key entities such as names, dates, addresses etc.'
});
// Generate a response
let stream = generator.generate({data: data, "Generate a report with the product details"});
// Get the final response
let answer = await stream.gather();
console.log(answer);
Example of use¶
Let’s assume you have a JSON-formatted list of products. You want the user to be able to retrieve specific product details and view them in an appealing manner in the Alan AI Chat. For this, you can add the following code to your dialog script:
// Save a product list
let productList = [
{
"name": "iPhone 13 Pro",
"min_price":"$999",
"vendor":"Apple",
"spec":"A15 Bionic chip, Super Retina XDR display, ProMotion technology, Triple 12MP camera system, 5G capability, Face ID, Ceramic Shield front cover, and Water and dust resistance.",
"image":"https://www.apple.com/newsroom/images/product/iphone/standard/Apple_iPhone-13-Pro_iPhone-13-Pro-Max_09142021_inline.jpg"
},
{
"name": "Samsung Galaxy S21 Ultra",
"min_price":"$1,199",
"vendor":"Samsung",
"spec":"Exynos 2100 or Snapdragon 888 processor, Dynamic AMOLED 2X display, Quad HD+ resolution, 108MP quad camera system, 5G capability, In-display fingerprint sensor",
"image":"https://image-us.samsung.com/us/smartphones/galaxy-s21/galaxy-s21-5g/v6/models/images/galaxy-s21-plus-5g_models_colors_phantom-violet.png"
}
]
// Create a generator
let generator = api.generator_v1({
format: 'markdown',
formatting: 'add a header and highlight key entities such as name, price, etc, with bold'
});
intent(
`Tell me about $(MODEL iPhone 13 Pro|Samsung Galaxy S21 Ultra), (include|) $(R* .+)`,
async p=> {
let productInfo = productList.find(el => el.name === p.MODEL.value);
let instruct = 'product details: ' + p.R.value;
// Generate and play a response
let stream = generator.generate({data: productInfo, instruct});
p.play(stream);
// Get the final response
let answer = await stream.gather();
console.log(answer.answer);
});
Here, the product details are saved to productList
. After that, you can use the generator API
to retrieve and see the product details:
Create a
generator
with the following parameters:format: 'markdown',
formatting: 'highlight headers and bold key entities such as names, dates, addresses etc.'
Generate a response with the
generate()
method, save it tostream
and play the response to the user.Obtain the final response with the
gather()
method and write it to Alan Studio logs
Now, the user can run the following queries against the products list:
Tell me about Samsung Galaxy S21 Ultra, include its characteristics and image
Tell me about iPhone 13 Pro, show its price and photo
and so on.