buildfire.ai.conversation
This feature is designed to empower plugin developers, enabling them to engage in conversations with AI. It will be helpful for generating content for plugins, available for both widget and control sides.
Requirements
Widget
<head>
<script src="../../../scripts/buildfire.min.js"></script>
<script src="../../../scripts/buildfire/services/ai/ai.js"></script>
</head>
Control
<head>
<script src="../../../../scripts/buildfire.min.js"></script>
<script src="../../../../scripts/buildfire/services/ai/ai.js"></script>
</head>
Conversation
conversation()
new buildfire.ai.conversation()
Conversation class constructor method, which initializes the chat conversation with AI.
More Examples
let conversation = new buildfire.ai.conversation();
Methods
conversation.systemSays()
conversation.systemSays(content)
Conversations with AI will be role-based. There are two types of roles: system
and user
. Ideally you should start the conversation by using the systemSays()
method. The content of this method will be sent as system
role.
Use the system
role to provide context about the application for which you want to generate a response. Additionally, you can use it to add conditions or specify response requirements.
By doing this, you are helping the AI understand how to behave while replying to your conversation and providing more accurate responses.
let conversation = new buildfire.ai.conversation();
let systemMessage = "You are an expert in furniture market locations, only generate up to ten locations.";
conversation.systemSays(systemMessage);
Arguments
Name | Type | Required | Description | Default |
---|---|---|---|---|
content | string | yes | Message from system to be sent to AI as system role. |
You can use userSays()
& systemSays()
methods multiple times for same conversation, regardless the calling order. Each time you call any of them we will append the content to your conversation query.
conversation.userSays()
conversation.userSays(content)
Append extra info as user
role by providing a clear description of the desired AI output. Specify the content you aim to generate through AI.
let conversation = new buildfire.ai.conversation();
let userMessage = "Please generate a list of top furniture markets locations based in USA, please generate dummy data if you were not able to generate real ones.";
conversation.userSays(userMessage);
Arguments
Name | Type | Required | Description | Default |
---|---|---|---|---|
content | string | yes | Message from user to be sent to AI as user role. |
conversation.fetchTextResponse()
conversation.fetchTextResponse(params, callback)
Once you have prepared your conversation using userSays()
and/or systemSays()
, retrieve the AI API response as a string by invoking the fetchTextResponse()
method.
let conversation = new buildfire.ai.conversation();
let systemMessage = "You are an expert in furniture market locations, only generate up to ten locations.";
conversation.systemSays(systemMessage);
let userMessage = "Please generate a list of top furniture markets locations based in USA, please generate dummy data if you were not able to generate real ones.";
conversation.userSays(userMessage);
console.log('fetching response...');
conversation.fetchTextResponse({}, (err, res) => {
console.log(err, res);
/* Sample response captured from AI:
err: null,
res:
{
"data": "1. High Point, North Carolina\n2. Las Vegas, Nevada\n3. Chicago, Illinois\n4. Dallas, Texas\n5. Atlanta, Georgia\n6. New York City, New York\n7. Los Angeles, California\n8. San Francisco, California\n9. Miami, Florida\n10. Seattle, Washington",
"responseType": "string"
}
*/
});
Arguments
Name | Type | Required | Description | Default |
---|---|---|---|---|
params | object | yes | Params object. | |
callback | function | yes | Callback function to get the response. |
params
Reserved for future usage. can be passed as empty object {}
.
callback(err, res)
Called when the response is retrieved from the AI API.
Name | Type | Description |
---|---|---|
err | object | error object, null when operation is successful. |
res | object | Response of AI conversation. |
err
Name | Type | Description |
---|---|---|
code | string | Status error code. |
message | string | Error message. |
error | object | Error object, could be empty. |
res
Name | Type | Description |
---|---|---|
data | string | Content generated by AI API. |
responseType | string | Specifies type of generated content. Always will be string . |
conversation.fetchJsonResponse()
conversation.fetchJsonResponse(params, callback)
After preparing your conversation using userSays()
and/or systemSays()
, retrieve the AI API response in Json format by utilizing the fetchJsonResponse()
method. Attach a Json object to obtain the AI response matched with your desired Json template.
It is recommended to start conversations with systemSays()
, then use userSays()
for precise responses. Enhance result accuracy with this approach.
let conversation = new buildfire.ai.conversation();
let systemMessage = "You are an expert in furniture market locations, only generate up to three locations.";
conversation.systemSays(systemMessage);
let userMessage = "Please generate a list of top furniture markets locations based in USA, please generate dummy data if you were not able to generate real ones.";
conversation.userSays(userMessage);
conversation.userSays("please attach their lat, lng, and review.");
let jsonTemplate = {
"furnitureMarkets": [
{ "name": "", "lat": "", "lng": "", "review": 0 }
]
};
console.log('fetching response...');
conversation.fetchJsonResponse({jsonTemplate}, (err, res) => {
console.log(err, res);
/* Sample response captured from AI:
err: null,
res:
{
"data":
{
"furnitureMarkets": [
{
"name": "Furniture City",
"lat": "40.7128",
"lng": "-74.0060",
"review": 4.5
},
{
"name": "Home Furnishings Mart",
"lat": "34.0522",
"lng": "-118.2437",
"review": 4.2
},
{
"name": "Furniture World",
"lat": "41.8781",
"lng": "-87.6298",
"review": 4.7
}
]
},
"responseType": "object"
}
*/
});
Arguments
Name | Type | Required | Description | Default |
---|---|---|---|---|
params | object | yes | Params object. | |
callback | function | yes | Callback function to get the response. |
params
Name | Type | Description |
---|---|---|
jsonTemplate | object | Json object that represents the template response. |
callback(err, res)
Called when the response is retrieved from the AI API.
Name | Type | Description |
---|---|---|
err | object | error object, null when operation is successful. |
res | object | Response of AI conversation. |
err
Name | Type | Description |
---|---|---|
code | string | Status error code. |
message | string | Error message. |
error | object | Error object, could be empty. |
res
Name | Type | Description |
---|---|---|
data | object or array | Content generated by AI, you can check response type to get the type of data . |
responseType | string | Specifies type of generated content. Will be object or array only, based on your jsonTemplate . |
conversation.clear()
conversation.clear()
Clear conversation messages. both userSays
and systemSays
.
let conversation = new buildfire.ai.conversation();
let userMessage = "Please generate a list of top furniture markets locations based in USA, please generate dummy data if you were not able to generate real ones.";
conversation.userSays(userMessage);
conversation.clear(); //By executing conversation.clear(), the conversation will be reset to its initial state, as if it were newly instantiated."
Error Status Codes
Errors returned by calling fetchTextResponse()
and fetchJsonResponse()
will have one of these codes:
Code | Description |
---|---|
validationError | Missing required parameter. |
serviceUnreachable | Error by AI service provider. |
noSuggestionsFound | I got no suggestion for you! |
suggestionTooBig | Suggestion too big, try to narrow down your query. |
invalidAppId | Invalid app. |
unknownError | Any other unknown error. |
Enhance Query
Enhance your query by adding extra info for AI adaptation.
let userMessage = userMessage + " please generate dummy data if you were not able to generate real ones"
Test your conversation with ChatGPT and seek its help to improve your prompt.
// Bad Content:
let userMessage = "Give me some places."
// Improved Content By ChatGpt:
let userMessage = "Can you generate a list of unique and interesting locations for a weekend getaway in Europe?"
Examples
let conversation = new buildfire.ai.conversation();
let systemMessage = "You are an app that is typically used for scheduling various types of events. Please generate a list of up to three events for me.";
conversation.systemSays(systemMessage);
let userMessage = "Please generate a list of events shows in Las Vegas this month, please generate dummy data if you were not able to generate real ones.";
conversation.userSays(userMessage);
let jsonTemplate = {
"events": [
{
"title": "",
"startDate": "",
"price":"",
"address": {
"title":"",
"lat":"",
"lng":""
},
"eventDays": [
{
"dayName":"",
"date":"",
"startsAt":" PM/AM",
"endsAt":" PM/AM"
}
]
}
]
};
console.log('fetching response...');
conversation.fetchJsonResponse({jsonTemplate}, (err, res) => {
console.log(err, res);
/* Sample response captured from AI:
err: null,
res:
{
"data":
{
"events": [
{
"title":"Cirque du Soleil: Mystère",
"startDate":"2022-10-15",
"price":"$75 - $150",
"address": {
"title":"Mystère Theater, Treasure Island",
"lat":"36.1245",
"lng":"-115.1729"
},
"eventDays": [
{
"dayName":"Saturday",
"date":"2022-10-15",
"startsAt":"7:00 PM",
"endsAt":"9:00 PM"
},
{
"dayName":"Sunday",
"date":"2022-10-16",
"startsAt":"5:00 PM",
"endsAt":"7:00 PM"
}
]
},
{
"title":"Magic Mike Live",
"startDate":"2022-10-20",
"price":"$99 - $250",
"address": {
"title":"Theater at Sahara Las Vegas",
"lat":"36.1420",
"lng":"-115.1565"
},
"eventDays": [
{
"dayName":"Thursday",
"date":"2022-10-20",
"startsAt":"8:00 PM",
"endsAt":"10:00 PM"
},
{
"dayName":"Friday",
"date":"2022-10-21",
"startsAt":"9:00 PM",
"endsAt":"11:00 PM"
}
]
},
{
"title":"Concert: Imagine Dragons",
"startDate":"2022-10-25",
"price":"$120 - $300",
"address": {
"title":"T-Mobile Arena",
"lat":"36.1021",
"lng":"-115.1784"
},
"eventDays": [
{
"dayName":"Tuesday",
"date":"2022-10-25",
"startsAt":"7:30 PM",
"endsAt":"10:30 PM"
}
]
}
]
},
"responseType":"object"
}
*/
});