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

Поделиться
HTML-код
  • Опубликовано: 31 май 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

Комментарии • 208

  • @mdshahed9031
    @mdshahed9031 3 месяца назад +31

    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 Месяц назад +1

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

  • @philipokposo6265
    @philipokposo6265 4 месяца назад

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

  • @ilriveril
    @ilriveril Год назад +54

    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 11 месяцев назад +2

      can you show us how?

    • @carsonhighfill364
      @carsonhighfill364 8 месяцев назад

      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 7 месяцев назад

      @@carsonhighfill364 Have you figured it out?

    • @ghanshamsharma8973
      @ghanshamsharma8973 7 месяцев назад +1

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

  • @YadavJii-pt2sx
    @YadavJii-pt2sx 3 месяца назад

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

  • @hannibalbianchi1466
    @hannibalbianchi1466 4 месяца назад +2

    Thanks
    Clear explanation and very useful

  • @ChannelYourDenovations
    @ChannelYourDenovations 9 месяцев назад +8

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

  • @KeiraraVT
    @KeiraraVT Год назад +11

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

    • @knkn5049
      @knkn5049 4 месяца назад +1

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

  • @cherry6280
    @cherry6280 3 месяца назад

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

  • @romualdurbanski6111
    @romualdurbanski6111 Год назад +58

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

    • @oceanofanythingshorts
      @oceanofanythingshorts Месяц назад

      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 20 дней назад

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

    • @thetutorialdoctor
      @thetutorialdoctor 19 дней назад

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

  • @josephang3665
    @josephang3665 9 месяцев назад +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?

  • @oceanofanythingshorts
    @oceanofanythingshorts Месяц назад

    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

  • @Explore4code
    @Explore4code 2 месяца назад

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

  • @prajwalkr5777
    @prajwalkr5777 8 месяцев назад

    Thank you it helped us a lot❤❤

  • @pallenda
    @pallenda Год назад +3

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

  • @peektv6570
    @peektv6570 6 месяцев назад +3

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

  • @adinaliana8071
    @adinaliana8071 5 месяцев назад

    It worked for me, thanks

  • @noblethebest9939
    @noblethebest9939 2 месяца назад +3

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

  • @RKAAchannel1
    @RKAAchannel1 3 месяца назад

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

  • @spencerfunk6697
    @spencerfunk6697 3 месяца назад

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

    • @irludure
      @irludure Месяц назад

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

  • @wazz6003
    @wazz6003 Год назад +31

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

  • @Creationish
    @Creationish День назад

    Great video

  • @nancyjhaonepiece
    @nancyjhaonepiece Год назад +3

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

  • @Priyadharshini-yt1us
    @Priyadharshini-yt1us Год назад +1

    Superb🎉

  • @user-hn1ns2dn7h
    @user-hn1ns2dn7h 3 месяца назад

    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.

  • @hirafuyucoding
    @hirafuyucoding Месяц назад

    Thankyou! ❤❤😊

  • @i_fighter29
    @i_fighter29 8 месяцев назад

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

  • @martinsilungwe2725
    @martinsilungwe2725 Год назад

    Thank you very much

  • @nobafan7515
    @nobafan7515 10 месяцев назад +1

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

  • @balogunazeez3458
    @balogunazeez3458 Год назад

    Thank you Federico

  • @gimmernglow7217
    @gimmernglow7217 Месяц назад +1

    How can we train it on large dataset?

  • @lessthanpinochet
    @lessthanpinochet 8 месяцев назад +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 6 месяцев назад

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

    • @_cyan735
      @_cyan735 6 месяцев назад +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 5 месяцев назад

      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.

  • @Jk-ls7ey
    @Jk-ls7ey 6 месяцев назад +1

    Which python framework is used here?

  • @notstating241
    @notstating241 8 месяцев назад

    Can something similar be done using vanilla js?

  • @adityag6022
    @adityag6022 9 месяцев назад

    Thank you sir

  • @croatiaofrandom
    @croatiaofrandom Месяц назад

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

  • @dastfa8402
    @dastfa8402 6 месяцев назад

    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 7 месяцев назад +43

    there's no github link???

    • @battleaxe990
      @battleaxe990 4 месяца назад +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 4 месяца назад +1

      Same

    • @haydenedmunds6004
      @haydenedmunds6004 3 месяца назад

      Yeah where's the link

    • @luascr787
      @luascr787 3 месяца назад

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

    • @patahgaming
      @patahgaming 3 месяца назад

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

  • @maxfedorets7864
    @maxfedorets7864 5 месяцев назад

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

  • @rogerjrperez2872
    @rogerjrperez2872 4 месяца назад

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

  • @costa9083
    @costa9083 7 месяцев назад +2

    no link in the infobox ??

  • @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 ! :)

  • @nelsonvaldivia1430
    @nelsonvaldivia1430 8 месяцев назад +3

    Hi is the source code avaliable?

  • @artistpw
    @artistpw 25 дней назад

    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.

  • @TheHiddenReaveld
    @TheHiddenReaveld 2 месяца назад

    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!

  • @ndunwani
    @ndunwani Месяц назад

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

  • @rishiraj2548
    @rishiraj2548 Год назад

    Thanks

  • @mohammedmudassir123
    @mohammedmudassir123 10 месяцев назад +3

    I can't find the github link

  • @obaidhashmi3965
    @obaidhashmi3965 6 месяцев назад +1

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

  • @CubexDash
    @CubexDash 29 дней назад

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

  • @golinithin8184
    @golinithin8184 2 месяца назад

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

  • @w0ngky
    @w0ngky 7 месяцев назад

    Will this work on Jupiter Notebook?

  • @mohsenghafari7652
    @mohsenghafari7652 2 месяца назад

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

  • @S.H.P.
    @S.H.P. 4 месяца назад

    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?

  • @jeongminyoun5388
    @jeongminyoun5388 2 месяца назад

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

  • @ananthkrish4643
    @ananthkrish4643 9 месяцев назад +1

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

  • @knkn5049
    @knkn5049 4 месяца назад +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...

  • @buddyshub9824
    @buddyshub9824 2 месяца назад +1

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

  • @muskankhan9313
    @muskankhan9313 22 дня назад

    Code is working awesome❤ I have run it

    • @arjunbisht9180
      @arjunbisht9180 21 день назад

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

  • @jasonarendse9075
    @jasonarendse9075 4 месяца назад

    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 3 месяца назад

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

  • @J0nas.nsn7
    @J0nas.nsn7 8 месяцев назад +1

    I can´t find the Github link.

  • @JonathanSandberg-rm9kf
    @JonathanSandberg-rm9kf 10 месяцев назад +12

    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 10 месяцев назад +2

      i belive its not the symble but lilke -->

    • @the_phoenix78
      @the_phoenix78 10 месяцев назад +1

      It doesn’t work for python 3.9 or less

    • @educationheaven2243
      @educationheaven2243 9 месяцев назад

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

    • @user-th9tw5wo3v
      @user-th9tw5wo3v 8 месяцев назад +1

      It is the symbol "->".

    • @AronAyub
      @AronAyub 8 месяцев назад +1

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

  • @kachrooabhishek
    @kachrooabhishek 8 месяцев назад +11

    where is github link for the same ?

  • @AfrinFoodVlog669
    @AfrinFoodVlog669 7 месяцев назад

    Btw can i train this ai with toons of data?

  • @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 8 месяцев назад

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

  • @aviraljain7020
    @aviraljain7020 7 месяцев назад +9

    sir where is the link for github??

  • @vasundharasahu1348
    @vasundharasahu1348 6 месяцев назад

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

  • @fukozrecords6566
    @fukozrecords6566 3 месяца назад

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

    • @maheswarkk1034
      @maheswarkk1034 3 месяца назад

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

  • @wshawnways459
    @wshawnways459 6 месяцев назад +1

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

  • @susguy446
    @susguy446 7 месяцев назад +4

    There is no github link in the description

    • @lawrensla
      @lawrensla 7 месяцев назад +2

      None at all

    • @yogitamore722
      @yogitamore722 7 месяцев назад +1

      Yeah true i needed it asap

  • @arhitro6391
    @arhitro6391 2 месяца назад +1

    What libraries did he use? ChatterBot? NLTK?

    • @Indently
      @Indently  2 месяца назад +1

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

  • @StealthSec-BugBounty
    @StealthSec-BugBounty Год назад +2

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

  • @Oneshot_Gaming16
    @Oneshot_Gaming16 7 месяцев назад +1

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

  • @calebplaysgamesofficial
    @calebplaysgamesofficial День назад

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

  • @dachuandu6539
    @dachuandu6539 5 месяцев назад

    how did you type out the not equal sign?

    • @ilovereading21
      @ilovereading21 Месяц назад

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

  • @thatGuyFean
    @thatGuyFean 6 месяцев назад

    Can you make it learn from the internet though?

  • @roblonoh351
    @roblonoh351 6 месяцев назад +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 6 месяцев назад +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.

  • @hanbaal
    @hanbaal 11 месяцев назад

    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 9 месяцев назад

      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 8 месяцев назад

      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 7 месяцев назад

      I think you have to write file name

  • @kamino3694
    @kamino3694 3 месяца назад

    is it possible to implement this into a discord bot?

  • @kauru4738
    @kauru4738 10 месяцев назад

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

    • @glassesgaming5831
      @glassesgaming5831 9 месяцев назад

      yes, but you'd have to make it asynchronous

  • @martinsilungwe2725
    @martinsilungwe2725 Год назад +1

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

    • @glassesgaming5831
      @glassesgaming5831 9 месяцев назад

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

    • @carsonhighfill364
      @carsonhighfill364 8 месяцев назад +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

  • @vee-creativeways5305
    @vee-creativeways5305 5 дней назад

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

  • @NPC862
    @NPC862 7 месяцев назад

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

  • @shakeyshamey1903
    @shakeyshamey1903 3 месяца назад +1

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

  • @hishamhakeem7999
    @hishamhakeem7999 5 месяцев назад

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

    • @anarcharatic5565
      @anarcharatic5565 5 месяцев назад

      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)

  • @sentexmc
    @sentexmc 3 месяца назад

    cool i searched very long

  • @Chetan_Kingoh
    @Chetan_Kingoh 4 месяца назад

    ≠ how do I get that?

  • @cyber-x3194
    @cyber-x3194 7 месяцев назад

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

  • @mmm55m55m
    @mmm55m55m 8 месяцев назад +1

    can i have the code?

  • @user-bm7di6fw8l
    @user-bm7di6fw8l 4 месяца назад +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

  • @SBRox
    @SBRox 3 месяца назад

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

  • @-Fidelis-
    @-Fidelis- 9 месяцев назад +2

    it says the arrow → is invalid

    • @VYPER_GAMER
      @VYPER_GAMER 8 месяцев назад +1

      not arrow use ->

  • @aquabhi3773
    @aquabhi3773 4 месяца назад

    Can someone send the github link of the whole code

  • @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 11 месяцев назад +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 10 месяцев назад

      @@Indently 😂 true fact

  • @-Fidelis-
    @-Fidelis- 9 месяцев назад +1

    Didn't work for me

  • @troysmastertoys953
    @troysmastertoys953 5 месяцев назад

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

    • @anarcharatic5565
      @anarcharatic5565 5 месяцев назад

      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 21 день назад +1

    github link?

  • @irludure
    @irludure Месяц назад

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

  • @emeraldmonarch3592
    @emeraldmonarch3592 5 месяцев назад

    Couldn't you have it break up the sentence, so "hey there!" Doesn't have a response, but "Hey" does, and looking at the English, "Hey there!" isn't asking anything etc, so "Hey" is what you input, and there is a modifier, and have an answer profile of "greatings" and whenever you see a greeting, you greet back

    • @Indently
      @Indently  5 месяцев назад

      You can do whatever you want, but remember to be just specific enough so that it won’t confuse other sentences for your responses :)

  • @Tadik168
    @Tadik168 6 месяцев назад +1

    Source code pleaseeee

  • @rz41S
    @rz41S 11 месяцев назад +1

    The thumbnail 💀