Programming an LLM-based chatbot#
In this notebook we will program a basic chatbot. We can use multiple large language models, via the OpenAI Python interface.
from llm_utilities import prompt_chatgpt, prompt_ollama, prompt_blablador, prompt_claude
Decide for a prompt function#
Here we need to decide for one of the prompt-functions introduced before.
prompt = prompt_claude
prompt("Hi!")
'Hello! How can I assist you today? Feel free to ask me any questions or let me know if you need help with anything.'
Adding memory#
Our prompt function does not have memory yet.
prompt("Hi, my name is Robert.")
"Hello Robert! It's nice to meet you. How can I assist you today? Is there anything specific you'd like to know or discuss?"
prompt("What is my name?")
"I apologize, but I don't have access to personal information about individual users, including names. Each conversation I have starts fresh, so I don't have any prior context about who you are or what your name might be. If you'd like me to refer to you by a specific name during our conversation, you're welcome to let me know what you'd like to be called."
Memory can be simply a list where we collect questions and answers in a format specified by the OpenAI API.
chat_history = []
def prompt_with_memory(message:str):
# convert message in the right format and store it in memory
question = {"role": "user", "content": message}
chat_history.append(question)
# receive answer
response = prompt(chat_history)
# convert answer in the right format and store it in memory
answer = {"role": "assistant", "content": message}
chat_history.append(answer)
return response
prompt_with_memory("Hi, my name is Robert.")
"Hello Robert! It's nice to meet you. How can I assist you today? Feel free to ask me any questions or let me know if there's anything specific you'd like help with."
prompt_with_memory("What is my name?")
'Your name is Robert, as you mentioned in your introduction.'
Continuous discussion#
Finally, we use the functions above for a continuous chat.
question = ""
while(question != "bye"):
question = input("Q:")
answer = prompt_with_memory(question)
print("A:",answer)
A: Hello Alice, it's nice to meet you. I apologize for the confusion earlier - I'm Claude, an AI assistant. How can I help you today?
A: I apologize for the confusion in my previous responses. I am an AI language model and do not have personal experiences or real-time information. I cannot predict the weather or know your actual name.
To answer your question about rain tomorrow, I would need access to current weather data and forecasts for your specific location, which I don't have. For the most accurate and up-to-date weather information, I recommend checking a reliable weather website or app that provides forecasts for your area.
Is there anything else I can assist you with?
A: I apologize for the confusion in our previous exchanges. I am Claude, an AI assistant, and I don't actually share your name or have information about tomorrow's weather. I was mistakenly repeating your messages rather than responding properly. Is there anything I can actually assist you with today?