How To Build A Chat Bot That Learns From The User In Python Tutorial

Поділитися
Вставка
  • Опубліковано 8 лип 2024
  • In this video I will be showing you how you can create a chat bot that learns from the user in Python in only 10 minutes. ChatGPT-4 generated most of this, which is pretty cool. There are some parts that can definitely be improved with the Chat Bot, but that's for you to figure out as a bonus homework problem ;)
    ▶ Become job-ready with Python:
    www.indently.io
    ▶ Follow me on Instagram:
    / indentlyreels
    00:00 Intro
    00:24 Demo
    01:30 Source code
    01:57 JSON
    02:41 Imports
    02:56 Loading JSON
    03:35 Saving JSON
    04:27 Finding the best matches
    06:10 Getting answers for questions
    06:52 chat_bot()
    10:17 Running the chat bot
    11:57 Conclusion

КОМЕНТАРІ • 223

  • @mdshahed9031
    @mdshahed9031 5 місяців тому +39

    I am glad that I found this tutorial. I am a 2nd semester student in computer science where I am learning python. While I was searching for something new that I can learn in advance of my class, I found this masterpiece that opened my mind and the way of thinking. I really appreciate your effort and I am gonna suggest my friends to follow this.

    • @sgjnn32134
      @sgjnn32134 3 місяці тому +1

      From what university? same here am a second semester CS student

  • @ilriveril
    @ilriveril Рік тому +55

    So for my bonus homework problem, I changed it to asynchronous to work with a discord bot. I added the ability to have multiple answers in a list and to pick a random answer. I also added an add command that will prompt the user to add a question and answer manually. If the answer is not found, it will create a new entry in the knowledge base. If the answer is found, it will change the string answer to a list and add it, or add to a list if there is one. Thank you so much for this! I just started learning programming a few months ago and the way you explained it helped me out a lot.

    • @radostinivanov8159
      @radostinivanov8159 Рік тому +4

      can you show us how?

    • @carsonhighfill364
      @carsonhighfill364 9 місяців тому

      ive had a hard time getting the import random to work as you wouold want for multiple responses on the answers. but ive only been working on this for 1 hour. probably will get it solved soon

    • @angelomaquiling
      @angelomaquiling 9 місяців тому

      @@carsonhighfill364 Have you figured it out?

    • @ghanshamsharma8973
      @ghanshamsharma8973 8 місяців тому +1

      type: - and then the > : -> rest is his prettier format

  • @philipokposo6265
    @philipokposo6265 5 місяців тому

    Thank you very much @Indently for such a great video. My program is up, running and learning very fast.

  • @ChannelYourDenovations
    @ChannelYourDenovations 10 місяців тому +8

    So, essentially I can make my own tutor. Ducking awesome. I'm gonna teach Marvin so Marvin can teach me.

  • @YadavJii-pt2sx
    @YadavJii-pt2sx 4 місяці тому

    Excellent and versatile project!!! Thank you so much sir 💯

  • @romualdurbanski6111
    @romualdurbanski6111 Рік тому +59

    Please give us the link for GitHub repository for this code example. You miss this.

    • @oceanofanythingshorts
      @oceanofanythingshorts 2 місяці тому

      import json
      from difflib import get_close_matches
      import os
      # create database folder if it doesn't exist
      if not os.path.exists("database"):
      os.makedirs("database")
      def loadKnowledgeBase(filepath: str) -> dict: # Load the knowledge base from a file
      with open(filepath, 'r') as file:
      data: dict = json.load(file)
      return data
      def saveKnowledgeBase(filepath: str, data: dict) -> None: # Save the knowledge base to a file
      with open(filepath, 'w') as file:
      json.dump(data, file, indent=2)
      def findBestMatch(userQuestion: str, questions: list[str]) -> str | None: # Find the best match for the user question
      bestMatch: list = get_close_matches(userQuestion, questions, n=1, cutoff=0.6)
      return bestMatch[0] if bestMatch else None
      def getAnswerForQuestion(question: str, knowledgeBase: dict) -> str | None: # Get the answer for the question
      for q in knowledgeBase["questions"]:
      if q["question"] == question:
      return q["answer"]
      def chat(): # Chat with the bot
      knowledgeBase: dict = loadKnowledgeBase("database\\knowledge_base_0.json")
      while True:
      userInput = input("You: ")
      if userInput == "exit":
      break

      bestMatch: str | None = findBestMatch(userInput, [q["question"] for q in knowledgeBase["questions"]])

      if bestMatch:
      answer: str | None = getAnswerForQuestion(bestMatch, knowledgeBase)
      print(f"Bot: {answer}")

      else:
      print("Bot: I'm sorry, I don't know the answer to that question, can you provide me with the answer?")
      userAnswer: str = input("You: ")
      knowledgeBase["questions"].append({"question": userInput, "answer": userAnswer})
      saveKnowledgeBase("database\\knowledge_base_0.json", knowledgeBase)
      print("Bot: Thank you for the information, I will remember it for next time.")
      if __name__ == "__main__":
      chat()

    • @sagarbhat7317
      @sagarbhat7317 Місяць тому

      Hiya! I wanted to play around, did you find it?

    • @thetutorialdoctor
      @thetutorialdoctor Місяць тому

      I created a repo for this, but my comment never posted.

  • @hannibalbianchi1466
    @hannibalbianchi1466 5 місяців тому +2

    Thanks
    Clear explanation and very useful

  • @KeiraraVT
    @KeiraraVT Рік тому +11

    I like this! Now I can start training some custom chatbots :D

    • @knkn5049
      @knkn5049 5 місяців тому +1

      Tell about your progress, have you moved to conversational pipelines or was able to pull out of here something?

  • @cherry6280
    @cherry6280 4 місяці тому

    You made it very easy to learn, Thank you so much❤❤❤❤❤❤ Masterpiece

  • @wazz6003
    @wazz6003 Рік тому +32

    Chat Bot part 2: How to connect your bot to an ChatBot API :P

  • @prajwalkr5777
    @prajwalkr5777 9 місяців тому

    Thank you it helped us a lot❤❤

  • @josephang3665
    @josephang3665 10 місяців тому +8

    hey there, can I get your code source pls, just simply because I can't find it in the description. However, can I use other languages to train this AI?

  • @Explore4code
    @Explore4code 3 місяці тому

    Amazing tutorial to learn and enjoy to teach own chatbot 🤩🤩

  • @pallenda
    @pallenda Рік тому +3

    ohhh cool! I didn't know about difflib.get_close_matches in default python. Thanks for the inspiration to study it! :)

  • @spencerfunk6697
    @spencerfunk6697 5 місяців тому

    love this im learning computer science and ai/ml and this is perfect

    • @irludure
      @irludure 2 місяці тому

      this isnt really ai, its just fetching from a json file

  • @adinaliana8071
    @adinaliana8071 6 місяців тому

    It worked for me, thanks

  • @RKAAchannel1
    @RKAAchannel1 4 місяці тому

    Thanks for this video.. I am able to make my first bot.

  • @nancyjhaonepiece
    @nancyjhaonepiece Рік тому +3

    U r a life saver for me.....🔥

  • @user-hn1ns2dn7h
    @user-hn1ns2dn7h 4 місяці тому

    Couldnt find my mistakes, nor a github code lol. So, I went to chatgpt and asked if it could help and it caught a spelling error and I forgot a 'f' statement. Otherwise this was a great code and you were very kind to share it, ty.

  • @peektv6570
    @peektv6570 7 місяців тому +3

    Hi there , is it possible to integrate openAI with this code?

  • @balogunazeez3458
    @balogunazeez3458 Рік тому

    Thank you Federico

  • @noblethebest9939
    @noblethebest9939 4 місяці тому +3

    Hello! I couldn't find the repository link for the platform. Can you please provide the link?
    Thank you!

  • @martinsilungwe2725
    @martinsilungwe2725 Рік тому

    Thank you very much

  • @Priyadharshini-yt1us
    @Priyadharshini-yt1us Рік тому +1

    Superb🎉

  • @adityag6022
    @adityag6022 11 місяців тому

    Thank you sir

  • @lessthanpinochet
    @lessthanpinochet 9 місяців тому +7

    Unless I missed something, I never saw you define file_path. How would your script know where to find the json file without defining it first?

    • @thomasgalarneau2928
      @thomasgalarneau2928 7 місяців тому

      I messed around with different file paths and had zero success. But im very new so who knows.

    • @_cyan735
      @_cyan735 7 місяців тому +1

      knowledge_base: dict = load_knowledge_base("knowledge_base.json") just tells the programm that it should load the database from the directory the main file is in, but you can replace "knowledge_base.json" with any filepath you want

    • @lokeshjonakuti437
      @lokeshjonakuti437 6 місяців тому

      The json file is created in the same folder as the main app. when we create any file in the same folder or directory, you dont have to give the path, it will automatically read it.

  • @nobafan7515
    @nobafan7515 11 місяців тому +1

    Is ther a way to impliment cause-effect logic to this?

  • @hirafuyucoding
    @hirafuyucoding 2 місяці тому

    Thankyou! ❤❤😊

  • @notstating241
    @notstating241 9 місяців тому

    Can something similar be done using vanilla js?

  • @gimmernglow7217
    @gimmernglow7217 3 місяці тому +1

    How can we train it on large dataset?

  • @wobblycentaur
    @wobblycentaur Рік тому +10

    you mentioned including a link to the code i am unable to find the link, might you say what it is? thank you ! :)

  • @croatiaofrandom
    @croatiaofrandom 2 місяці тому

    Is there a way to make it on one question pick randomly from few correct answers

  • @Jk-ls7ey
    @Jk-ls7ey 8 місяців тому +1

    Which python framework is used here?

  • @Creationish
    @Creationish Місяць тому

    Great video

  • @i_fighter29
    @i_fighter29 9 місяців тому

    for some reason the save command isn't defined for me is there any reason why

  • @rishiraj2548
    @rishiraj2548 Рік тому

    Thanks

  • @nelsonvaldivia1430
    @nelsonvaldivia1430 9 місяців тому +3

    Hi is the source code avaliable?

  • @maxfedorets7864
    @maxfedorets7864 6 місяців тому

    Is there a way you can connect it to ChatGPT so it can do that for you?

  • @ndunwani
    @ndunwani 2 місяці тому

    Thanks so much, this video really helped a lot for my project. Out of curiosity, what text-editor are you using?

    • @ndunwani
      @ndunwani 2 місяці тому

      My bad, I found it

    • @shane_pro
      @shane_pro 2 місяці тому

      Pycharm

  • @dastfa8402
    @dastfa8402 7 місяців тому

    Hi, may I know how you type =/= non-equal sign in Visual/python or it is a package i have to download in Visual Studio?

  • @abuzargore3762
    @abuzargore3762 8 місяців тому +45

    there's no github link???

    • @battleaxe990
      @battleaxe990 6 місяців тому +17

      Nah but if you wanna put in the effort you can pause here and there and copy down what he did. That's what I did

    • @angelfvrgg
      @angelfvrgg 5 місяців тому +1

      Same

    • @haydenedmunds6004
      @haydenedmunds6004 5 місяців тому

      Yeah where's the link

    • @luascr787
      @luascr787 4 місяці тому

      I don't want copy I just 2an understand it by seeing live code

    • @patahgaming
      @patahgaming 4 місяці тому

      ​@@battleaxe990fedric said there's a repo in description but there's no repo

  • @Mohammed-Alsahli
    @Mohammed-Alsahli Рік тому +14

    Can you please create a video about how to use gui and convert the terminal script into an actual app?

    • @Mercenary1996
      @Mercenary1996 Рік тому +1

      Yes please 🥺

    • @samiranmanna3951
      @samiranmanna3951 Рік тому +4

      Learn the kivy framework. You'll be able to make an actual Android app & build a GUI for it

    • @carsonhighfill364
      @carsonhighfill364 9 місяців тому

      you make the back end like this to run form python. 3.11. then you make a gui using PySimpleGUI.. boom
      Script kiddy

  • @costa9083
    @costa9083 9 місяців тому +2

    no link in the infobox ??

  • @AfrinFoodVlog669
    @AfrinFoodVlog669 8 місяців тому

    Btw can i train this ai with toons of data?

  • @w0ngky
    @w0ngky 9 місяців тому

    Will this work on Jupiter Notebook?

  • @muskankhan9313
    @muskankhan9313 2 місяці тому

    Code is working awesome❤ I have run it

    • @arjunbisht9180
      @arjunbisht9180 Місяць тому

      can you provide me with it's github link? or can you just share the written code with me.
      would be a great help

  • @mohammedmudassir123
    @mohammedmudassir123 11 місяців тому +3

    I can't find the github link

  • @ananthkrish4643
    @ananthkrish4643 10 місяців тому +1

    Can we add database to it or important Wikipedia like module to get answers for random questions??

  • @buddyshub9824
    @buddyshub9824 3 місяці тому +1

    Is this how ai chat bots are made or are there more techniques?

  • @obaidhashmi3965
    @obaidhashmi3965 7 місяців тому +1

    where's the github repository link, can't find it

  • @golinithin8184
    @golinithin8184 3 місяці тому

    Can we use this code as a backend of a chatbot?

  • @DualStyle
    @DualStyle 2 місяці тому

    Is there a way to make this code to work with html. Like make it to work on a website

  • @artistpw
    @artistpw 2 місяці тому

    I'm trying this in visual studio on python 3.12 and getting some errors on the "import long_responses". Just thought I'd mention this.

  • @oceanofanythingshorts
    @oceanofanythingshorts 2 місяці тому

    Really good job man, i implemented this on my ai application, which is now self-training. Thanks for such a beautiful demonstration. really appreciate that

    • @chy2114
      @chy2114 19 днів тому +1

      how did you start building your model/ what do you recommend i start with?

    • @oceanofanythingshorts
      @oceanofanythingshorts 19 днів тому

      ​@@chy2114 you can start with mistral

  • @JonathanSandberg-rm9kf
    @JonathanSandberg-rm9kf Рік тому +13

    Hello! I'm wondering how you did this symbol → valid because when i try to copy paste it i'm present with an error. I'm using PyCharm as my IDE and this symbol get's in the way because it throws an error. Very very nice guide though, will be perfect for my learning project. Would appreciate if i can get some clarity regarding this. Thank you in advance!

    • @Harsh-gh7rx
      @Harsh-gh7rx Рік тому +2

      i belive its not the symble but lilke -->

    • @the_phoenix78
      @the_phoenix78 Рік тому +1

      It doesn’t work for python 3.9 or less

    • @educationheaven2243
      @educationheaven2243 10 місяців тому

      @@the_phoenix78 in spyder i just write -> it worked.

    • @user-th9tw5wo3v
      @user-th9tw5wo3v 10 місяців тому +1

      It is the symbol "->".

    • @AronAyub
      @AronAyub 10 місяців тому +1

      Hi, it's "->" symbol which is indicating a return type of the defined function.

  • @S.H.P.
    @S.H.P. 6 місяців тому

    Wouldnt this glitch kinda bcs ;lets say I wrote "Whats the capital of Italy" and in another chat I wrote "Whats a dish from Italy" wouldnt it say a dish name?

  • @kachrooabhishek
    @kachrooabhishek 9 місяців тому +11

    where is github link for the same ?

  • @TheHiddenReaveld
    @TheHiddenReaveld 4 місяці тому

    My code has no errors, except I only get "Process finished with exit code 0" after running it. If possible, can you please assist on how to fix this. Thank you!

  • @Adster5-Viewer
    @Adster5-Viewer 2 дні тому

    Hey, thank you for the informative video! I cannot find the link to the github so I wrote the code as you had shown. However, when I am running it, the terminal says no such file in directory (with regards to the knowledge_base.json) - "File Not Found". Is there something I am missing, as I had done everything exactly as you had shown.
    I was wondering if it could be the second last line wherein you state if name__ == main... as I do not have a function for that in my VS, so I just typed it from scratch. But it does not select and run the main like yours does, is there an extention I need to download to set the name == main function in VS, thank you!

  • @J0nas.nsn7
    @J0nas.nsn7 9 місяців тому +1

    I can´t find the Github link.

  • @mohsenghafari7652
    @mohsenghafari7652 3 місяці тому

    hi. please help me. how to create custom model from many pdfs in Persian language? tank you.

  • @aviraljain7020
    @aviraljain7020 8 місяців тому +9

    sir where is the link for github??

  • @jasonarendse9075
    @jasonarendse9075 6 місяців тому

    in vs code it doesnt skip a tab and also doesnt show the pre suggested code that you type anyway. did i miss something or is that normal?

    • @Ryoma263
      @Ryoma263 4 місяці тому

      its normal i use vs code i consider pycharm for the weak of mind

  • @jeongminyoun5388
    @jeongminyoun5388 3 місяці тому

    It says that the indentation is not matching what is wrong? Using VScode

  • @martinsilungwe2725
    @martinsilungwe2725 Рік тому +1

    Can you do a video on how we can extract all text from a word document

    • @glassesgaming5831
      @glassesgaming5831 11 місяців тому

      you can just do that by using the in-build open feature in python

    • @carsonhighfill364
      @carsonhighfill364 9 місяців тому +1

      dude you would do a file open and read to import it as a sring then do a .slice() to index the words [ ] and boom

  • @knkn5049
    @knkn5049 5 місяців тому +1

    I dont understand what if i rearranged words in my question, or asked similar thing about other thing?
    I want "conversation", not scripted dialoge...

  • @shakeyshamey1903
    @shakeyshamey1903 4 місяці тому +1

    Can you please teach how to build this in C++ 😊😊😊❤❤❤

  • @sentexmc
    @sentexmc 4 місяці тому

    cool i searched very long

  • @fukozrecords6566
    @fukozrecords6566 5 місяців тому

    Thank you, One quick question. What happens when someone feed knowledge hub with the wrong information..

    • @maheswarkk1034
      @maheswarkk1034 5 місяців тому

      It would output the wrong answer if the question is asked. There is nothing to understand the information given is wrong.

  • @thatGuyFean
    @thatGuyFean 7 місяців тому

    Can you make it learn from the internet though?

  • @proflead
    @proflead 28 днів тому

    Source code could be useful! Thanks :)

  • @roblonoh351
    @roblonoh351 7 місяців тому +6

    Hi! Thank you for your content, this is really great and awesome! Writing the code, I don't get an answer from the bot unless I write 'quit' :/ any idea why it would be that way?

    • @nickthomas8412
      @nickthomas8412 7 місяців тому +3

      I typed it all out from the video, there was a typo in line 16 where he wrote "questions" instead of "question". you might check that.

  • @vasundharasahu1348
    @vasundharasahu1348 7 місяців тому

    Error: _name_ not defined
    How I can fix it..?

  • @kauru4738
    @kauru4738 Рік тому

    im new to coding and i was wondering if this can be used with a discord bot.

    • @glassesgaming5831
      @glassesgaming5831 11 місяців тому

      yes, but you'd have to make it asynchronous

  • @wshawnways459
    @wshawnways459 7 місяців тому +1

    can anyone please tell me where the github link is i cant find it anywhere

  • @user-bm7di6fw8l
    @user-bm7di6fw8l 5 місяців тому +2

    I cannot find the link to Git repository you referred to. Really useful for me as I'm dyslexic and recently started learning programming...... Mainly trial and error, 2 pc one playing your videos, the other playing with the code in anaconda playing "what if", slow going, but getting on surprisingly well LOL

  • @Judstar776
    @Judstar776 12 днів тому

    Nice tutorial, but I'm getting an error. In the function "Find Best Match", I typed "user_question: str, questions: list[str]", but an error is generated saying "Subscript for class "list" will generate runtime exception; enclose type annotation in quotes". I'm currently on Python 3.8.9 64-bit. I am new to this, so maybe I am missing something?

  • @arhitro6391
    @arhitro6391 3 місяці тому +1

    What libraries did he use? ChatterBot? NLTK?

    • @Indently
      @Indently  3 місяці тому +1

      If you watch any % of the video you will find out

  • @NPC862
    @NPC862 8 місяців тому

    thx,but I don't know why that the data base fail

  • @akashnath7999
    @akashnath7999 Рік тому +8

    It's really good one ❤ but can you please tell why we didn't use openai module to make this ? Lots of love from India ❤

    • @Indently
      @Indently  Рік тому +21

      To save money

    • @NnotKnott
      @NnotKnott Рік тому +1

      ​@@Indently 😫👏

    • @CodX710
      @CodX710 Рік тому

      ​@@Indentlyfair enough

    • @WebWizard977
      @WebWizard977 Рік тому +1

      ​@@Indently😂yep I think we should try to build our own chatbot not like using api or library.this way we can make better projects than openai ❤

    • @akashnath7999
      @akashnath7999 11 місяців тому

      @@Indently 😂 true fact

  • @dachuandu6539
    @dachuandu6539 6 місяців тому

    how did you type out the not equal sign?

    • @ilovereading21
      @ilovereading21 3 місяці тому

      It's != on IDE's that aren't PyCharm

  • @susguy446
    @susguy446 9 місяців тому +4

    There is no github link in the description

    • @lawrensla
      @lawrensla 8 місяців тому +2

      None at all

    • @yogitamore722
      @yogitamore722 8 місяців тому +1

      Yeah true i needed it asap

  • @mmm55m55m
    @mmm55m55m 10 місяців тому +1

    can i have the code?

  • @EinzzCookie
    @EinzzCookie Рік тому +2

    Is it possible to manage that the bot searches with beautifulscrape after Answers in the web and learns like that?

    • @EinzzCookie
      @EinzzCookie Рік тому +1

      or make the bot selflearning???

    • @glassesgaming5831
      @glassesgaming5831 11 місяців тому +1

      you mean for all questions including greetings or just for general info questions?

    • @EinzzCookie
      @EinzzCookie 11 місяців тому +1

      @@glassesgaming5831 for all

  • @Oneshot_Gaming16
    @Oneshot_Gaming16 8 місяців тому +1

    I get an error saying, no such file or directory. Pls help

  • @Chetan_Kingoh
    @Chetan_Kingoh 5 місяців тому

    ≠ how do I get that?

  • @rogerjrperez2872
    @rogerjrperez2872 5 місяців тому

    Wait where did you get the chat_bot() ???? In not see anything that you create a class

    • @WafflesssFalling
      @WafflesssFalling Місяць тому

      The chatbot is the code he typed - if you type everything he did in the video, you will have made the chatbot

  • @cyber-x3194
    @cyber-x3194 8 місяців тому

    It's not work on 3.11 don't know why

  • @kamino3694
    @kamino3694 4 місяці тому

    is it possible to implement this into a discord bot?

  • @hishamhakeem7999
    @hishamhakeem7999 6 місяців тому

    It is showing error in line 5(with open ) how can we reslove it

    • @anarcharatic5565
      @anarcharatic5565 6 місяців тому

      Did you do the with open in a function? If so, you have to pass the name as a parameter (assuming you used a variable name instead of the file name directly in the with open)

  • @thebeastcracker3525
    @thebeastcracker3525 5 днів тому

    How to give it sources to automatically learn about this world?

  • @irludure
    @irludure 2 місяці тому

    next step is to install tensorflow and actually make it a real machine learning model

  • @SBRox
    @SBRox 4 місяці тому

    There's no github code in the description I badly needed please :(

  • @aquabhi3773
    @aquabhi3773 5 місяців тому

    Can someone send the github link of the whole code

  • @calebplaysgamesofficial
    @calebplaysgamesofficial Місяць тому

    is this on github??? I think I made a mistake but the video was a little hard to follow.

  • @hanbaal
    @hanbaal Рік тому

    Any Idea why when I copy the code and run it in idle3 I get a syntax error on line 14?
    data: dict = json.load(file) it doesn't like the colon.
    I don't have much idea what I'm doing, I'm not much of a coder.

    • @glassesgaming5831
      @glassesgaming5831 11 місяців тому

      probably doesnt work on an older version of python, try updating python or just remove the colon and dict. specifying a variable's type is nice but not required for it to work

    • @carsonhighfill364
      @carsonhighfill364 9 місяців тому

      You typed it correct maybe you indented it wrong. python is picky about indents. try giveing it a tab or 4 spaces. from the line above

    • @user-sk2ow8op1e
      @user-sk2ow8op1e 8 місяців тому

      I think you have to write file name

  • @troysmastertoys953
    @troysmastertoys953 6 місяців тому

    It says 'r' is not a designated file location?

    • @anarcharatic5565
      @anarcharatic5565 6 місяців тому

      You have to specify the file location first, then put a comma, then put 'r' or something else depending on what you want to with the file

  • @arjunbisht9180
    @arjunbisht9180 Місяць тому +1

    github link?

  • @-Fidelis-
    @-Fidelis- 10 місяців тому +1

    Didn't work for me

  • @vee-creativeways5305
    @vee-creativeways5305 Місяць тому

    Hi! Can you please provide the code for this chatbot!