Skip to main content

A framework to create chatbots in the easiest way

Project description

Baker

Bot-Maker Baker! Is a framework to create chatbots with Python in the easiest and simplest route, train your chatbot by texting or adding data in XML, JSON or YAML files.

Installation

You can install Baker with pip

pip install baker-python

Usage

Initial Tasks

Using Baker is very easy, all you have to do is to create a YAML, JSON or XML file first. However, files should have been defined in a specific format:

For XML files

<responses>
  <Hello>
    <response>Hello!</response>
    <response>Hi there!</response>
  </Hello>
</responses>

The Hello tag is defining that if the user will write Hello to the chatbot the chatbot will return one of the responses in the response tag.

For JSON files

{
    "Hello": [
        "Hi",
        "Heyy",
        "Hello"
    ]

}

Same goes with JSON files. Even if the response is only one, it must be in a list []. Here, if user inputs Hello then the response must be random in the list.

For YAML files

Hello:
- Hello!
- Hi there!
How are you:
- I am fine
- I am doing good, thanks for asking

Same process is with the YAML files with a bit different syntax and nothing else.

The files can also be empty for example a JSON file can be like this:

{

}

Training

To train the chatbot, use the Trainer class. Here is an example of basic training:

import baker.trainer
import baker.bparser
import baker.chatbot

bot = baker.trainer.Trainer('data.json')

user_input = input("You: ")
response = bot.get_response(user_input)
print("Bot:", response)

# Train the bot with a new response
new_response = input("New response: ")
bot.train_response(user_input, new_response)
print("Bot has been trained with the new response!")

from this route the keyword (user's question) must be already created in the file or else the trainer will not be able to train because the trainer will not find the keyword in the file. For example if you want to train the chatbot for responses of Hello then Hello should be created in the data file.

But with this way to train you can train the chatbot as long you want to with custom keywords (no need to define them in the data file) and their infinite responses:

import baker.trainer

trainer = baker.trainer.Trainer('data.json')
trainer.loop_training()

The data file can either be empty or it can have keywords, pre-defined keyowrds can be trained too.

Parsing

To parse the chatbot to run and test it use the Parser class:

import baker.bparser

def test_chatbot(bot):
    while True:
        user_input = input("You: ")
        if user_input.lower() == "exit":
            print("Testing session ended.")
            break
        response = bot.get_response(user_input)
        print("Bot:", response)

bot = baker.bparser.Parser('data.json')

test_chatbot(bot)

The above code will run the chatbot, but there is anther simpler way to run the chatbot with it's specified name which is to use the Chatbot class:

import baker.trainer
import baker.bparser
import baker.chatbot

trainer = baker.trainer.Trainer('data.json')
parser = baker.bparser.Parser('data.json')
my_chatbot = baker.chatbot.Chatbot("MyChatbot")
my_chatbot.session(trainer, parser)

Parser class has more functions regarding the data file:

  • Exporting responses :
import baker.bparser

response_file_name = "data.json"  
parser_instance = baker.bparser.Parser(response_file_name)  
parser_instance.export_responses(export_file_name="data2.json")
  • Reset resposes
baker.bparser.Parser.reset_responses("A_User_Question")
  • Removing responses:
parser_instance2 = baker.bparser.Parser("data.json")

user_input = "Hello"
response_to_remove = "Heyy"
parser_instance2.remove_response(user_input, response_to_remove)
  • Count responses:
parser_instance3 = baker.bparser.Parser("data.json")
user_input = "Hello"

count = parser_instance3.count_responses(user_input)
print(f"Number of Responses for '{user_input}': {count}")

Keep training your chatbot by texting or adding words in the database and then run it!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

baker_python-2.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file baker_python-2.0-py3-none-any.whl.

File metadata

  • Download URL: baker_python-2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for baker_python-2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ea3ba5a59a246fadf960ae0754221f293b5a55e5852704365692f3b0e478d21c
MD5 7f6464c16fb569767da76034589f82f1
BLAKE2b-256 cb8a2634ec7cac3960290864237754b36801b7416ec6548b6f7598180ed48d4e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page