Python / Salesforce Platform APIs / Generate Response Based on Prompt Template
Back to Collection Items
import sys
import chilkat2
# This example assumes the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.
http = chilkat2.Http()
# Use this online tool to generate code from sample JSON: Generate Code to Create JSON
# The following JSON is sent in the request body.
# {
# "isPreview": "false",
# "inputParams": {
# "valueMap": {}
# },
# "additionalConfig": {
# "numGenerations": 1,
# "temperature": 0,
# "frequencyPenalty": 0.0,
# "presencePenalty": 0.0,
# "additionalParameters": {},
# "applicationName": "PromptBuilderPreview"
# }
# }
json = chilkat2.JsonObject()
json.UpdateString("isPreview","false")
json.UpdateNewObject("inputParams.valueMap")
json.UpdateInt("additionalConfig.numGenerations",1)
json.UpdateInt("additionalConfig.temperature",0)
json.UpdateNumber("additionalConfig.frequencyPenalty","0.0")
json.UpdateNumber("additionalConfig.presencePenalty","0.0")
json.UpdateNewObject("additionalConfig.additionalParameters")
json.UpdateString("additionalConfig.applicationName","PromptBuilderPreview")
# Adds the "Authorization: Bearer <access_token>" header.
http.AuthToken = "<access_token>"
# resp is a CkHttpResponse
resp = http.PostJson3("https://domain.com/services/data/v{{version}}/einstein/prompt-templates/:PROMPT_TEMPLATE_API_NAME/generations","application/json",json)
if (http.LastMethodSuccess == False):
print(http.LastErrorText)
sys.exit()
sbResponseBody = chilkat2.StringBuilder()
resp.GetBodySb(sbResponseBody)
jResp = chilkat2.JsonObject()
jResp.LoadSb(sbResponseBody)
jResp.EmitCompact = False
print("Response Body:")
print(jResp.Emit())
respStatusCode = resp.StatusCode
print("Response Status Code = " + str(respStatusCode))
if (respStatusCode >= 400):
print("Response Header:")
print(resp.Header)
print("Failed.")
sys.exit()
# Sample JSON response:
# (Sample code for parsing the JSON response is shown below)
# {
# "generations": [
# {
# "parameters": "{finish_reason=stop, index=0, logprobs=null}",
# "responseId": "893db990-7acb-4845-ad65-54d82606ca65",
# "text": "{\n "twitter": "๐ Exciting new listing! Explore our luxurious property, [Provide:{PROPERTY NAME}], featuring [Provide:{BEDS}] beds, [Provide:{BATHS}] baths, and an asking price of [Provide:{ASKING PRICE}]. Click the link for a sneak peek! [Provide:{PICTURE}]",\n "linkedin": "๐๐ Don't miss out on this incredible opportunity! Discover the stunning property, [Provide:{PROPERTY NAME}], offering [Provide:{BEDS}] beds, [Provide:{BATHS}] baths, and an asking price of [Provide:{ASKING PRICE}]. Click the link below to see more! [Provide:{PICTURE}]",\n "blockkit": {\n "blocks": [\n {\n "type": "section",\n "text": {\n "type": "mrkdwn",\n "text": "*Luxury Property Alert!* \n\n๐๐๐\n\nIntroducing [Provide:{PROPERTY NAME}], a magnificent home with [Provide:{BEDS}] beds and [Provide:{BATHS}] baths. Priced at [Provide:{ASKING PRICE}]."\n },\n "accessory": {\n "type": "image",\n "image_url": "[Provide:{PICTURE}]",\n "alt_text": "Luxury Property"\n }\n },\n {\n "type": "divider"\n },\n {\n "type": "actions",\n "elements": [\n {\n "type": "button",\n "text": {\n "type": "plain_text",\n "text": "View Property",\n "emoji": true\n },\n "url": "https://d1q000001ewauuaq-dev-ed.develop.lightning.force.com/[Provide:{RECORD ID}]"\n }\n ]\n }\n ]\n }\n}"
# }
# ],
# "parameters": null,
# "prompt": "You're the community manager for Dreamhouse, a real estate agency that sells luxury properties. \nCreate social media posts for twitter, linkedin and slack (block kit format) describing the property.\n\nInstructions:\n"""\nUse clear, concise, and straightforward language using the active voice and strictly avoiding the use of filler words and phrases and redundant language.\nMake sure the response is valid JSON.\nWhen you generate the posts, include the name of the property, [Provide:{PROPERTY NAME}], and explain the characteristics of the house, such as [Provide:{BATHS}], [Provide:{BEDS}]and[Provide:{ASKING PRICE}]. Also include a link to the picture of the property, [Provide:{PICTURE}]. \n\nThe twitter post should include emojis.\nThe linkedin post should include several emojis and bullets, and also have a special output formatting: Text render environment only supports Unicode and emoji. Use symbols from Unicodeโs Mathematical Alphanumeric Symbols block liberally to produce facsimiles of bold, italics, line separation, and other formatting. Examples for a sample sentence:\n\nitalics sans: ๐๐ฉ๐ฆ ๐๐ถ๐ช๐ค๐ฌ ๐๐ณ๐ฐ๐ธ๐ฏ ๐๐ฐ๐น ๐๐ถ๐ฎ๐ฑ๐ฆ๐ฅ ๐๐ท๐ฆ๐ณ ๐ต๐ฉ๐ฆ ๐ญ๐ข๐ป๐บ ๐ฅ๐ฐ๐จ.\nbold sans: ๐ง๐ต๐ฒ ๐ค๐๐ถ๐ฐ๐ธ ๐๐ฟ๐ผ๐๐ป ๐๐ผ๐
๐๐๐บ๐ฝ๐ฒ๐ฑ ๐ข๐๐ฒ๐ฟ ๐๐ต๐ฒ ๐น๐ฎ๐๐ ๐ฑ๐ผ๐ด.\nbold italic sans: ๐๐๐ ๐๐ช๐๐๐ ๐ฝ๐ง๐ค๐ฌ๐ฃ ๐๐ค๐ญ ๐
๐ช๐ข๐ฅ๐๐ ๐๐ซ๐๐ง ๐ฉ๐๐ ๐ก๐๐ฏ๐ฎ ๐๐ค๐.\nitalics serif: ๐โ๐ ๐๐ข๐๐๐ ๐ต๐๐๐ค๐ ๐น๐๐ฅ ๐ฝ๐ข๐๐๐๐ ๐๐ฃ๐๐ ๐กโ๐ ๐๐๐ง๐ฆ ๐๐๐.\n\nThe block kit code should be valid block kit code.\nWhen you generate the block kit code: \n- Add several sections. \n- Include the name of the property, [Provide:{PROPERTY NAME}], and explain the characteristics of the house, such as [Provide:{BATHS}], [Provide:{BEDS}] and [Provide:{ASKING PRICE}]. \n- Include a the picture of the property, which image_url is [Provide:{PICTURE}]. \n- Include a button which url points to https://d1q000001ewauuaq-dev-ed.develop.lightning.force.com/[Provide:{RECORD ID}]\n- Include emoticons so the post is more visual.\n\nExample of block kit code:\n\n{\n "blocks": [\n {\n "type": "section",\n "text": {\n "type": "mrkdwn",\n "text": "Hello, Assistant to the Regional Manager Dwight! *Michael Scott* wants to know where you'd like to take the Paper Company investors to dinner tonight.\n\n *Please select a restaurant:*"\n }\n },\n {\n "type": "divider"\n },\n {\n "type": "section",\n "text": {\n "type": "mrkdwn",\n "text": "*Farmhouse Thai Cuisine*\n:star::star::star::star: 1528 reviews\n They do have some vegan options, like the roti and curry, plus they have a ton of salad stuff and noodles can be ordered without meat!! They have something for everyone here"\n },\n "accessory": {\n "type": "image",\n "image_url": "https://s3-media3.fl.yelpcdn.com/bphoto/c7ed05m9lC2EmA3Aruue7A/o.jpg",\n "alt_text": "alt text for image"\n }\n },\n {\n "type": "section",\n "text": {\n "type": "mrkdwn",\n "text": "*Kin Khao*\n:star::star::star::star: 1638 reviews\n The sticky rice also goes wonderfully with the caramelized pork belly, which is absolutely melt-in-your-mouth and so soft."\n },\n "accessory": {\n "type": "image",\n "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/korel-1YjNtFtJlMTaC26A/o.jpg",\n "alt_text": "alt text for image"\n }\n },\n {\n "type": "section",\n "text": {\n "type": "mrkdwn",\n "text": "*Ler Ros*\n:star::star::star::star: 2082 reviews\n I would really recommend theย Yum Koh Moo Yang - Spicy lime dressing and roasted quick marinated pork shoulder, basil leaves, chili & rice powder."\n },\n "accessory": {\n "type": "image",\n "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/DawwNigKJ2ckPeDeDM7jAg/o.jpg",\n "alt_text": "alt text for image"\n }\n },\n {\n "type": "divider"\n },\n {\n "type": "actions",\n "elements": [\n {\n "type": "button",\n "text": {\n "type": "plain_text",\n "text": "Farmhouse",\n "emoji": true\n },\n "value": "click_me_123"\n },\n {\n "type": "button",\n "text": {\n "type": "plain_text",\n "text": "Kin Khao",\n "emoji": true\n },\n "value": "click_me_123",\n "url": "https://google.com"\n },\n {\n "type": "button",\n "text": {\n "type": "plain_text",\n "text": "Ler Ros",\n "emoji": true\n },\n "value": "click_me_123",\n "url": "https://google.com"\n }\n ]\n }\n ]\n}\n\nThe response should have the next format:\n{\n "twitter": [here goes the twitter post, that should have less than 280 characters],\n "linkedin": [here goes the linkedin post, that should have between 1500 and 2000 characters], \n "blockkit": [here goes the block kit post],ย \n}\n\n"""\n\nNow generate the posts.\n\n\n",
# "promptTemplateDevName": "Generate_Social_Media_Posts",
# "requestId": "chatcmpl-97KqPl1TEiOgMdnKKPQ9aVs7rPJGr"
# }
# Sample code for parsing the JSON response...
# Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code
parameters = jResp.StringOf("parameters")
prompt = jResp.StringOf("prompt")
promptTemplateDevName = jResp.StringOf("promptTemplateDevName")
requestId = jResp.StringOf("requestId")
i = 0
count_i = jResp.SizeOfArray("generations")
while i < count_i :
jResp.I = i
parameters = jResp.StringOf("generations[i].parameters")
responseId = jResp.StringOf("generations[i].responseId")
text = jResp.StringOf("generations[i].text")
i = i + 1
Curl Command
curl -X POST
-H "Authorization: Bearer <access_token>"
-d '{
"isPreview": "false",
"inputParams": {
"valueMap": {
<INPUT_PARAMS>
}
},
"additionalConfig": {
"numGenerations": 1,
"temperature": 0,
"frequencyPenalty": 0.0,
"presencePenalty": 0.0,
"additionalParameters": {},
"applicationName": "PromptBuilderPreview"
}
}'
https://domain.com/services/data/v{{version}}/einstein/prompt-templates/:PROMPT_TEMPLATE_API_NAME/generations
Postman Collection Item JSON
{
"name": "Generate Response Based on Prompt Template",
"request": {
"method": "POST",
"header": [
],
"body": {
"mode": "raw",
"raw": "{\n \"isPreview\": \"false\",\n \"inputParams\": {\n \"valueMap\": {\n <INPUT_PARAMS>\n }\n },\n \"additionalConfig\": {\n \"numGenerations\": 1,\n \"temperature\": 0,\n \"frequencyPenalty\": 0.0,\n \"presencePenalty\": 0.0,\n \"additionalParameters\": {},\n \"applicationName\": \"PromptBuilderPreview\"\n }\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{_endpoint}}/services/data/v{{version}}/einstein/prompt-templates/:PROMPT_TEMPLATE_API_NAME/generations",
"host": [
"{{_endpoint}}"
],
"path": [
"services",
"data",
"v{{version}}",
"einstein",
"prompt-templates",
":PROMPT_TEMPLATE_API_NAME",
"generations"
],
"variable": [
{
"key": "PROMPT_TEMPLATE_API_NAME",
"value": "",
"description": "API name of the prompt template to invoke"
}
]
}
},
"response": [
{
"name": "Generate Response Based on Prompt Template",
"originalRequest": {
"method": "POST",
"header": [
],
"body": {
"mode": "raw",
"raw": "{\n \"isPreview\": \"false\",\n \"inputParams\": {\n \"valueMap\": {\n \"Input:Property\": {\n \"value\": {\n \"id\": \"a011Q00001EQYXOQA5\"\n }\n }\n }\n },\n \"additionalConfig\": {\n \"numGenerations\": 1,\n \"temperature\": 0,\n \"frequencyPenalty\": 0.0,\n \"presencePenalty\": 0.0,\n \"additionalParameters\": {},\n \"applicationName\": \"PromptBuilderPreview\"\n }\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{_endpoint}}/services/data/v{{version}}/einstein/prompt-templates/Generate_Social_Media_Posts/generations",
"host": [
"{{_endpoint}}"
],
"path": [
"services",
"data",
"v{{version}}",
"einstein",
"prompt-templates",
"Generate_Social_Media_Posts",
"generations"
]
}
},
"status": "Created",
"code": 201,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Date",
"value": "Wed, 27 Mar 2024 10:30:55 GMT"
},
{
"key": "Content-Type",
"value": "application/json;charset=UTF-8"
},
{
"key": "Transfer-Encoding",
"value": "chunked"
},
{
"key": "Connection",
"value": "keep-alive"
},
{
"key": "Set-Cookie",
"value": "CookieConsentPolicy=0:1; path=/; expires=Thu, 27-Mar-2025 10:30:46 GMT; Max-Age=31536000; secure"
},
{
"key": "Set-Cookie",
"value": "LSKey-c$CookieConsentPolicy=0:1; path=/; expires=Thu, 27-Mar-2025 10:30:46 GMT; Max-Age=31536000; secure"
},
{
"key": "Set-Cookie",
"value": "BrowserId=EmiKg-wlEe6gDIWbBKrQ-g; domain=.salesforce.com; path=/; expires=Thu, 27-Mar-2025 10:30:46 GMT; Max-Age=31536000"
},
{
"key": "Strict-Transport-Security",
"value": "max-age=63072000; includeSubDomains"
},
{
"key": "X-Content-Type-Options",
"value": "nosniff"
},
{
"key": "X-Robots-Tag",
"value": "none"
},
{
"key": "Cache-Control",
"value": "no-cache,must-revalidate,max-age=0,no-store,private"
},
{
"key": "Origin-Trial",
"value": "AqlAE64ET63tVSana3qdVkfkPAgyUhY8GwcehUlpqv067CevOpumeNUlx9YouLkBxJ0CT+EwIb8/SiNbF2NGvwYAAABfeyJvcmlnaW4iOiJodHRwczovL3NhbGVzZm9yY2UuY29tOjQ0MyIsImZlYXR1cmUiOiJUcGNkIiwiZXhwaXJ5IjoxNzM1MzQzOTk5LCJpc1N1YmRvbWFpbiI6dHJ1ZX0="
},
{
"key": "Vary",
"value": "Accept-Encoding"
},
{
"key": "Content-Encoding",
"value": "gzip"
},
{
"key": "Server",
"value": "sfdcedge"
},
{
"key": "X-SFDC-Request-Id",
"value": "7070edfcd5d02a0d047af20b8dccf83e"
}
],
"cookie": [
],
"body": "{\n \"generations\": [\n {\n \"parameters\": \"{finish_reason=stop, index=0, logprobs=null}\",\n \"responseId\": \"893db990-7acb-4845-ad65-54d82606ca65\",\n \"text\": \"{\\n "twitter": "🌟 Exciting new listing! Explore our luxurious property, [Provide:{PROPERTY NAME}], featuring [Provide:{BEDS}] beds, [Provide:{BATHS}] baths, and an asking price of [Provide:{ASKING PRICE}]. Click the link for a sneak peek! [Provide:{PICTURE}]",\\n "linkedin": "🌟🌟 Don't miss out on this incredible opportunity! Discover the stunning property, [Provide:{PROPERTY NAME}], offering [Provide:{BEDS}] beds, [Provide:{BATHS}] baths, and an asking price of [Provide:{ASKING PRICE}]. Click the link below to see more! [Provide:{PICTURE}]",\\n "blockkit": {\\n "blocks": [\\n {\\n "type": "section",\\n "text": {\\n "type": "mrkdwn",\\n "text": "*Luxury Property Alert!* \n\n🌟🌟🌟\n\nIntroducing [Provide:{PROPERTY NAME}], a magnificent home with [Provide:{BEDS}] beds and [Provide:{BATHS}] baths. Priced at [Provide:{ASKING PRICE}]."\\n },\\n "accessory": {\\n "type": "image",\\n "image_url": "[Provide:{PICTURE}]",\\n "alt_text": "Luxury Property"\\n }\\n },\\n {\\n "type": "divider"\\n },\\n {\\n "type": "actions",\\n "elements": [\\n {\\n "type": "button",\\n "text": {\\n "type": "plain_text",\\n "text": "View Property",\\n "emoji": true\\n },\\n "url": "https://d1q000001ewauuaq-dev-ed.develop.lightning.force.com/[Provide:{RECORD ID}]"\\n }\\n ]\\n }\\n ]\\n }\\n}\"\n }\n ],\n \"parameters\": null,\n \"prompt\": \"You're the community manager for Dreamhouse, a real estate agency that sells luxury properties. \\nCreate social media posts for twitter, linkedin and slack (block kit format) describing the property.\\n\\nInstructions:\\n"""\\nUse clear, concise, and straightforward language using the active voice and strictly avoiding the use of filler words and phrases and redundant language.\\nMake sure the response is valid JSON.\\nWhen you generate the posts, include the name of the property, [Provide:{PROPERTY NAME}], and explain the characteristics of the house, such as [Provide:{BATHS}], [Provide:{BEDS}]and[Provide:{ASKING PRICE}]. Also include a link to the picture of the property, [Provide:{PICTURE}]. \\n\\nThe twitter post should include emojis.\\nThe linkedin post should include several emojis and bullets, and also have a special output formatting: Text render environment only supports Unicode and emoji. Use symbols from Unicodeโs Mathematical Alphanumeric Symbols block liberally to produce facsimiles of bold, italics, line separation, and other formatting. Examples for a sample sentence:\\n\\nitalics sans: 𝘛𝘩𝘦 𝘘𝘶𝘪𝘤𝘬 𝘉𝘳𝘰𝘸𝘯 𝘍𝘰𝘹 𝘑𝘶𝘮𝘱𝘦𝘥 𝘖𝘷𝘦𝘳 𝘵𝘩𝘦 𝘭𝘢𝘻𝘺 𝘥𝘰𝘨.\\nbold sans: 𝗧𝗵𝗲 𝗤𝘂𝗶𝗰𝗸 𝗕𝗿𝗼𝘄𝗻 𝗙𝗼𝘅 𝗝𝘂𝗺𝗽𝗲𝗱 𝗢𝘃𝗲𝗿 𝘁𝗵𝗲 𝗹𝗮𝘇𝘆 𝗱𝗼𝗴.\\nbold italic sans: 𝙏𝙝𝙚 𝙌𝙪𝙞𝙘𝙠 𝘽𝙧𝙤𝙬𝙣 𝙁𝙤𝙭 𝙅𝙪𝙢𝙥𝙚𝙙 𝙊𝙫𝙚𝙧 𝙩𝙝𝙚 𝙡𝙖𝙯𝙮 𝙙𝙤𝙜.\\nitalics serif: 𝑇โ𝑒 𝑄𝑢𝑖𝑐𝑘 𝐵𝑟𝑜𝑤𝑛 𝐹𝑜𝑥 𝐽𝑢𝑚𝑝𝑒𝑑 𝑂𝑣𝑒𝑟 𝑡โ𝑒 𝑙𝑎𝑧𝑦 𝑑𝑜𝑔.\\n\\nThe block kit code should be valid block kit code.\\nWhen you generate the block kit code: \\n- Add several sections. \\n- Include the name of the property, [Provide:{PROPERTY NAME}], and explain the characteristics of the house, such as [Provide:{BATHS}], [Provide:{BEDS}] and [Provide:{ASKING PRICE}]. \\n- Include a the picture of the property, which image_url is [Provide:{PICTURE}]. \\n- Include a button which url points to https://d1q000001ewauuaq-dev-ed.develop.lightning.force.com/[Provide:{RECORD ID}]\\n- Include emoticons so the post is more visual.\\n\\nExample of block kit code:\\n\\n{\\n "blocks": [\\n {\\n "type": "section",\\n "text": {\\n "type": "mrkdwn",\\n "text": "Hello, Assistant to the Regional Manager Dwight! *Michael Scott* wants to know where you'd like to take the Paper Company investors to dinner tonight.\n\n *Please select a restaurant:*"\\n }\\n },\\n {\\n "type": "divider"\\n },\\n {\\n "type": "section",\\n "text": {\\n "type": "mrkdwn",\\n "text": "*Farmhouse Thai Cuisine*\n:star::star::star::star: 1528 reviews\n They do have some vegan options, like the roti and curry, plus they have a ton of salad stuff and noodles can be ordered without meat!! They have something for everyone here"\\n },\\n "accessory": {\\n "type": "image",\\n "image_url": "https://s3-media3.fl.yelpcdn.com/bphoto/c7ed05m9lC2EmA3Aruue7A/o.jpg",\\n "alt_text": "alt text for image"\\n }\\n },\\n {\\n "type": "section",\\n "text": {\\n "type": "mrkdwn",\\n "text": "*Kin Khao*\n:star::star::star::star: 1638 reviews\n The sticky rice also goes wonderfully with the caramelized pork belly, which is absolutely melt-in-your-mouth and so soft."\\n },\\n "accessory": {\\n "type": "image",\\n "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/korel-1YjNtFtJlMTaC26A/o.jpg",\\n "alt_text": "alt text for image"\\n }\\n },\\n {\\n "type": "section",\\n "text": {\\n "type": "mrkdwn",\\n "text": "*Ler Ros*\n:star::star::star::star: 2082 reviews\n I would really recommend the Yum Koh Moo Yang - Spicy lime dressing and roasted quick marinated pork shoulder, basil leaves, chili & rice powder."\\n },\\n "accessory": {\\n "type": "image",\\n "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/DawwNigKJ2ckPeDeDM7jAg/o.jpg",\\n "alt_text": "alt text for image"\\n }\\n },\\n {\\n "type": "divider"\\n },\\n {\\n "type": "actions",\\n "elements": [\\n {\\n "type": "button",\\n "text": {\\n "type": "plain_text",\\n "text": "Farmhouse",\\n "emoji": true\\n },\\n "value": "click_me_123"\\n },\\n {\\n "type": "button",\\n "text": {\\n "type": "plain_text",\\n "text": "Kin Khao",\\n "emoji": true\\n },\\n "value": "click_me_123",\\n "url": "https://google.com"\\n },\\n {\\n "type": "button",\\n "text": {\\n "type": "plain_text",\\n "text": "Ler Ros",\\n "emoji": true\\n },\\n "value": "click_me_123",\\n "url": "https://google.com"\\n }\\n ]\\n }\\n ]\\n}\\n\\nThe response should have the next format:\\n{\\n "twitter": [here goes the twitter post, that should have less than 280 characters],\\n "linkedin": [here goes the linkedin post, that should have between 1500 and 2000 characters], \\n "blockkit": [here goes the block kit post], \\n}\\n\\n"""\\n\\nNow generate the posts.\\n\\n\\n\",\n \"promptTemplateDevName\": \"Generate_Social_Media_Posts\",\n \"requestId\": \"chatcmpl-97KqPl1TEiOgMdnKKPQ9aVs7rPJGr\"\n}"
}
]
}