Python – Endless chat update, plus ChatGPT version.

This simple code project is designed for the new python coder, it’s easy to read and can be greatly improved.

Jan 2023 Made a couple small updates to this, I am thinking of adding some input parsing to get more custom replies. I have also included a ChatGPT ‘how can I improve this code’ prompt reply at bottom which is done much better in the while loop.

Update I played around with ChatGPT and it advised me of a sentiment system to more customize responses, I settled on the ‘vadersentiment’ module as it worked best in my tests. This analyzes the input you give it for positive, neutral, and negative so you can then choose a more appropriate endlesschat response. Most likely would use the neutral responses for my silly replies, then the others.

Something I made as a Python rookie to chat with you, it’s very limited but could be greatly enhanced. You could be a total rookie with Python and code this simple script in 1-2 hours of study. Main things you will learn or need to learn are listed below.

  1. Importing modules, in this case the random module
  2. Creating variables like myChat = myChat + 1
  3. Creating a list with strings of text like our mySlang = [‘text’, ‘in quotes’, ‘are strings’]
  4. While loop ours is, while True:
  5. if elif else, this is one of the most used things in programming to make conditionals
  6. Using input(‘Some message indicating you need input: ‘)
  7. Using print(‘Stuff to print to screen’) and using print(f’strings’)
# Simple endless chat v1.1 script by Ryan Burleson
# Use 'stop' to end the chat.

# random module is included with Python, still need to import
import random
mySlang = [
    'hell nah!',
    'too boring...',
    'who df you?',
    'how much you paying?',
    'maybe later!',
    'fine, sup brah!',
    'its about time',
    'lets hit the club first..',
    'always the same shit...'
    'let\'s get to the good stuff'
]
myChat = 0
while True:
    myChat = myChat + 1
    if myChat == 1:
        chat = input('What you wanna chat about? ')
    elif chat == 'stop':
        quit(f'Ok STOPPING this conversation, chatted {myChat} times.')
    elif chat == '':
        quit(f'Looks like you didn\'t enter anything, ending... Chatted {myChat} times.')
    elif myChat > 1:
        chat = input('Now what you want? ')
    # This needs to always be checked at end to print replies so if is used
    # reason for this is so the script doesn't reply to stop or blank data.
    if chat != 'stop' and chat != '':
        # Here you could instead send 'chat' to another 
        # function to analyze it for improved replies.
        print(str.capitalize(chat), random.choice(mySlang))

Results, me typing stupid shit and no real checking of string input so its just dumb. This is like many web bots you see on reddit or other places online. Chatting with the script is like some people I know, clueless.

=============== RESTART: endLessChat.py ===============
What you wanna chat about?Fine ass babes
Ok we can talk about: Fine ass babes
Fine ass babes hell nah!
Now what you want?Where they at?
Where they at? its about time
Now what you want?Gime their numbers
Gime their numbers how much you paying?
Now what you want?Bro you drunk?
Bro you drunk? how much you paying?
Now what you want?Nothing
Nothing hell nah!
Now what you want?

ChatGPT version update below

Note the new ‘weights’ added to the code, I’ll paste why those are used from ChatGPT itself:

The weights in the code are used to specify the probability of each response being selected from the list of possible responses. The weights are values between 0 and 1 that indicate the proportion of each response relative to the others. In this code, the weights are specified as values in the responses dictionary.

For example, if the response “hell nah!” has a weight of 0.1, it means that there’s a 10% chance that this response will be selected when calling random.choices(). The sum of all weights should equal to 1. In this code, the function random.choices is used to select one response randomly based on the specified weights.

BUT…

There is an error in ChatGPT’s code, the weight = 11 so I entered this into the prompt and here is what it said:

me: but the weights equal 11 in the code, I meant 1.1

ChatGPT: You are correct, the weights in the code do not sum up to 1. This is a mistake in the code. The sum of weights should be equal to 1 as weights represent probabilities. To correct this, you can either divide each weight by the sum of all weights, or set each weight to the same value (e.g. 1/len(responses)) to distribute the probability evenly among all responses.

So it had a few more issues, the weight system was too hard coded so it reverted to a list again with new dynamic weight system and the final if statement could just be an else. So after about 4 code updates the final version is below which is an improvement and I learned about 4 new things, profit win win.

ChatGPT writes working code, but it MUST be checked!

# ChatGPT final update
# The response weights are dynamic and I really do not understand them myself or
# if this even works properly. If you do write a comment. Typically when using wieghts
# you are selecting items you want to appear more or less often, think of a slot machine.
import random

responses = [
    'hell nah!',
    'too boring...',
    'who df you?',
    'how much you paying?',
    'maybe later!',
    'fine, sup brah!',
    'its about time',
    'lets hit the club first...',
    'always the same shit...',
    'let\'s get to the good stuff',
]

response_weights = [1 for _ in responses]
total_weights = sum(response_weights)
response_weights = [weight/total_weights for weight in response_weights]

myChat = 0
while True:
    myChat += 1
    chat = input("What you wanna chat about? " if myChat == 1 else "Now what you want? ")

    if chat == 'stop':
        print(f'Ok STOPPING this conversation, chatted {myChat} times.')
        break
    elif chat == '':
        print(f'Looks like you didn\'t enter anything, ending... Chatted {myChat} times.')
        break
    else:
        reply = random.choices(responses, weights=response_weights, k=1)[0]
        print(str.capitalize(chat), reply)

So after this I will make another post on endLessChat v1.2 now with a sentiment system and maybe more, this allows more custom responses depending on what the user inputs.

Leave a Comment