Chatbot Database in Python

Question:Database help

Repl link:https://replit.com/@hithere40/Mr-ChatBot-mongo?v=1

so I am trying to make it so if the bot doesnt have a responce to what was said it will ask how it should respond and add that to a .json file. it pulls all of its responecs from that .json file. I need help making it upload thejson file to the database and json file to the database and update it when the code is ran, because I want it to keep things said even when preview mode. Any help is very much aprecitated.

You can use the replit database.

Just add a line in your script

from replit import db

and modify the load_responses and save_responses to fetch and save the data from replit db

The responses will be stored in the replit database, and they will persist even when you restart the script.

2 Likes

@hithere40
You could also use YAML as it’s a bit simpler than JSON. Best practice on Replit, though, is to use Replit DB like @WindLother said. Here’s a tutorial on how to use it.

The thing is, he will have to eventually pay for the database.

1 Like

Since when do we have to pay for the Replit database? I’ve never seen that mentioned anywhere.

I think he’s talking about expanding the storage besides the free storage that replit already give to you.

But again, I don’t think anyone will a small project will be consuming all of the free storage, and if that day arrives you will need to spend in resources as you would in any other place lol.

1 Like

I’m pretty sure that you get 50 mb. free, and then you have to pay.

Some terrible “chatbot” I made, I don’t even know if it works or not:

ps. you require a folder named data & a json file named learned in it.

# - feel free to use this code, no credits required. -
import difflib
# import random
import json
import os


class Colors:
    PURPLE = "\033[95m"
    CYAN = "\033[96m"
    DARKCYAN = "\033[36m"
    BLUE = "\033[94m"
    GREEN = "\033[92m"
    YELLOW = "\033[93m"
    RED = "\033[91m"
    BOLD = "\033[1m"
    UNDERLINE = "\033[4m"
    END = "\033[0m"


class SweetrLearn:
    def __init__(self):
        self._learned_data = json.load(open("./data/learned.json", "r"))
        self._appended_data = []

    def _read(self, _question):
        _closest = difflib.get_close_matches(
          _question, 
          self._learned_data.keys(), 
          n=1
        )
        try:
            return f"{Colors.UNDERLINE}{Colors.PURPLE}You: {_question}\n{Colors.UNDERLINE}{Colors.BLUE}Sweetr: {self._learned_data[_closest[0]]}{Colors.END}"

        except:

            return f"{Colors.UNDERLINE}{Colors.PURPLE}You: {_question}\n{Colors.UNDERLINE}{Colors.BLUE}Sweetr: Im sorry, I do not have the answer for this question.{Colors.END}"

    def _teach(self, _question: str, _resp):
        try:
            with open("./data/learned.json", "r+") as _data_file:
                _data = self._learned_data
                _data.append({_question: _resp})
                json.dump(_data, _data_file, indent=3)

                return f"{Colors.UNDERLINE}{Colors.BLUE}Sweetr Training\n\n{Colors.END}{Colors.BOLD}{Colors.GREEN}Successfully trained.{Colors.END}"

        except Exception as e:

            return f"{Colors.UNDERLINE}{Colors.BLUE}Sweetr Training\n\n{Colors.END}{Colors.BOLD}{Colors.RED}Failed to train.\nError: {e}{Colors.END}."


if __name__ == "__main__":
    while True:
        _input = input(f"{Colors.BOLD}{Colors.RED}-> {Colors.END}")
        try:
            os.system("clear")
        except:
            os.system("cls")
        _answer = SweetrLearn()._read(_input)
        if _answer == "Im sorry, I do not have the answer for this question.":
            pass # _inpt = input(f"{Colors.BOLD}{Colors.BLUE}-> {Colors.END}")
        else:
            print(_answer)
        # SweetrLearn()._teach("test", "Hello how can I help you?")

@Sky
It’d be preferable to see what the .json file has to check if it works…

{"hey": "Hey!", "how are you?": "Im fine, how about you?"}

basic.

1 Like