Today's

길을 나서지 않으면 그 길에서 만날 수 있는 사람을 만날 수 없다

파이썬 스크립트

오늘은 간단한 채봇을 하나 구현해 봅니다. (feat Python)

Billcorea 2023. 3. 5. 13:55
반응형
오늘은 python 코드를 하나 작성해 보았습니다.   

심플한 챗봇 놀이를 하기 위해서 말입니다.  그런데 말입니다. 이런 코드를 사람이 아닌 챗봇이 만들어 주었다고 하면 믿을까요?

import random

# define some basic responses
greetings = ["hi", "hello", "hey"]
goodbyes = ["bye", "goodbye", "see you later"]
thanks = ["thanks", "thank you", "thanks a lot"]
compliments = ["nice", "great", "awesome", "cool"]

# define a function to generate a response
def generate_response(user_input):
    # clean up the user input
    cleaned_input = user_input.lower().strip()

    # check for a greeting
    for word in greetings:
        if word in cleaned_input:
            return "Hi there!"

    # check for a goodbye
    for word in goodbyes:
        if word in cleaned_input:
            return "Goodbye!"

    # check for a thank you
    for word in thanks:
        if word in cleaned_input:
            return "You're welcome!"

    # check for a compliment
    for word in compliments:
        if word in cleaned_input:
            return "Thank you, I try my best!"

    # if no match was found, generate a random response
    responses = ["I'm sorry, I didn't understand that.",
                 "Can you please rephrase that?",
                 "I'm not sure what you mean.",
                 "Let's talk about something else."]
    return random.choice(responses)

# main loop
print("Hello, I'm a chatbot. What can I help you with?")
while True:
    user_input = input("> ")
    response = generate_response(user_input)
    print(response)

open ai gpt에게 챗을 이용해서 나는 심심하니 간단한 챗봇 코드를 구현해 달라고 했더니 이런 코드를 만들어 주었습니다.
이렇게 작성된 코드를 이용해서 간단한 놀이을 해 볼 수 도 있지 않을까 하는 생각이 드네요.
 
재미 있을 것 같지 않나요?   다음 링크를 타고 들어가서 여러분도 챗봇을 통해 간단한 이야기를 만들어 보세요.  다만, 이 챗봇은 영어로 말해 주는 것을 좋아합니다. 이유는 외국계 이니까요.
 
https://chat.openai.com
 
오늘도 그럼 즐거운 코딩 하시길 바랍니다. 
 

반응형