Create Chatbot from file
- Python
- Golang
The Sarufi Python SDK allows you to create a chatbot from a file where you can define your intents, utterances and responses separately.
You can either use the YAML or JSON format to define your chatbot. You can find the sample JSON file here.
You should choose between YAML or JSON based on your business needs and preferences. We will be using YAML in this tutorial because it is more readable.
Create Chatbot from file
The Sarufi Python SDK allows you to create a chatbot from a file where you can define your intents, utterances, and responses separately.
You can either use the YAML or JSON format to define your chatbot. You can find the sample YAML file here and the sample JSON file here.
To create a chatbot from the YAML file, use the following code snippet:
- YAML
- JSON
greetings:
- Hi
- Hello
- Howdy
- Hey
- Good morning
- Good evening
- Good afternoon
- Good day
- Good night
goodbye:
- Bye
- Goodbye
- See you later
- See you soon
- Talk to you later
- Talk to you soon
- Talk to you
- See you
- Have a nice day
- Have a nice evening
- Have a nice night
- Have a nice morning
thanks:
- Thanks
- Thank you
- Thank you so much
- Thanks a lot
- Thanks a bunch
- Thanks a ton
- Thanks a million
- Thanks a zillion
{
"greetings": [
"Hi",
"Hello",
"Howdy",
"Hey",
"Good morning",
"Good evening",
"Good afternoon",
"Good day",
"Good night"
],
"goodbye": [
"Bye",
"Goodbye",
"See you later",
"See you soon",
"Talk to you later",
"Talk to you soon",
"Talk to you",
"See you",
"Have a nice day",
"Have a nice evening",
"Have a nice night",
"Have a nice morning"
],
"thanks": [
"Thanks",
"Thank you",
"Thank you so much",
"Thanks a lot",
"Thanks a bunch",
"Thanks a ton",
"Thanks a million",
"Thanks a zillion"
]
}
If you are not familiar with YAML or JSON, you can learn more about them here and here, respectively.
Create dialog file
Here is a sample file with dialog defined in YAML format. You can find the sample YAML file here.
- YAML
- JSON
greetings:
message:
- Hi, how can I help you?
next_state: end
goodbye:
message:
- Bye
- See you soon
next_state: end
thanks:
message:
- You are welcome
next_state: end
{
"greetings": {
"message": ["Hi, how can I help you?"],
"next_state": "end"
},
"goodbye": {
"message": ["Bye", "See you soon"],
"next_state": "end"
},
"thanks": {
"message": ["You are welcome"],
"next_state": "end"
}
}
Create a description YAML file
Here is a sample file with description defined in YAML format. You can find the sample YAML file here.
- YAML
- JSON
name: Pizza Bot
description: A chatbot that helps you order pizza
industry: Food
{
"name": "Pizza Bot",
"description": "A chatbot that helps you order pizza",
"industry": "Food"
}
Create a chatbot from YAML files
You can create a chatbot from YAML files using the following code.
>>> from sarufi import Sarufi
>>> sarufi = Sarufi(api_key='your API KEY')
>>> response = sarufi.create_from_file(
... intents="data/intents.yaml",
... flow="data/flows.yaml",
... metadata="data/metadata.yaml",
... )
You can also update a chatbot from YAML files using the following code.
>>> from sarufi import Sarufi
>>> sarufi = Sarufi(api_key='your API KEY')
>>> response = sarufi.update_from_file(
... intents="data/intents.yaml",
... flow="data/flows.yaml",
... metadata="data/metadata.yaml",
... id="xxx",
... )
You can use the same code to create or update a chatbot from JSON files. The only difference is the file extension.
What you have learned
In this tutorial, you have learned how to create a chatbot from YAML files using the Sarufi Python SDK.
Sarufi Golang SDK allows you to create intents and flows from json
files.
Create Intents From JSON File
Lets say you have an intents.json
file with the following content:
{
"greetings": [
"Hi",
"Hello",
"Mambo",
"Hola",
"Salut"
],
"goodbye": [
"Bye",
"Goodbye",
"See you later",
"Adios",
"Au revoir"
],
"thanks": [
"You're welcome",
"No problem",
"My pleasure",
"Anytime",
"No problem"
],
"confused": [
"I'm sorry, I don't understand",
"I'm not sure I understand",
"I'm sorry, can you repeat that?",
"I'm sorry, what was that?",
"I'm sorry, I didn't understand that"
]
}
You can simply include the name of the file with the function bot.CreateIntents("intents.json")
:
if err := example_bot.CreateIntents("intents.json"); err != nil {
log.Fatal(err)
}
// For changes to take effect
if err = app.UpdateBot(example_bot); err != nil {
log.Fatal(err)
}
Create Flows From JSON Files
You can similarly create flows from json files. Assuming you have an flows.json
files as below:
{
"greetings": {
"message": [
"Hello, how can I help you?"
],
"next_state": "end"
},
"goodbye": {
"message": [
"Goodbye, hope to see you again soon"
],
"next_state": "end"
},
"thanks": {
"message": [
"You're welcome"
],
"next_state": "end"
},
"confused": {
"message": [
"I'm sorry, I didn't understand you"
],
"next_state": "end"
}
}
Then you can create a new flow as follows:
if err := example_bot.CreateFlows("flows.json"); err != nil {
log.Fatal(err)
}
// For changes to take effect
if err = app.UpdateBot(example_bot); err != nil {
log.Fatal(err)
}
What you have learned
In this tutorial, you have learned how to create intents and flows from JSON files using Sarufi Golang SDK.