Skip to article frontmatterSkip to article content

Programming your own chatbot

Authors
Affiliations
Leipzig University
Helmholtz Center for Environmental Research - UFZ

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_blablador
prompt("Hi!")
'Hello! How can I help you today?'

Adding memory

Our prompt function does not have memory yet.

prompt("Hi, my name is Robert.")
'Nice to meet you, Robert! How are you doing today? Is there something I can help you with or would you like to chat?'
prompt("What is my name?")
"I'm a large language model, I don't have any information about your personal identity, including your name. Our conversation just started, and I don't retain any information about individual users. If you'd like to share your name with me, I'd be happy to chat with you and learn more about you!"

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! Nice to meet you! Is there something I can help you with or would you like to chat?'
prompt_with_memory("What is my name?")
'Your name is Robert!'

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)