Chatbot

Prompt Engineering 7

Jan Kirenz

Chatbot

In this tutorial, you will explore how you can utilize the chat format to have extended conversations with chatbots personalized or specialized for specific tasks or behaviors.

Setup

Python

import panel as pn  # GUI
import os
import openai
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())
openai.api_key = os.getenv('OPENAI_API_KEY')

Helper function 1

def get_completion(prompt, model="gpt-3.5-turbo"):
    messages = [{"role": "user", "content": prompt}]
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=0, 
    )
    return response.choices[0].message["content"]

Helper function 2

1def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0):
2    response = openai.ChatCompletion.create(
3        model=model,
4        messages=messages,
5        temperature=temperature,
    )
6    print(str(response.choices[0].message))
7    return response.choices[0].message["content"]
1
Define function get_completion_from_messages with parameters messages, model (default: “gpt-3.5-turbo”), and temperature (default: 0).
2
Call openai.ChatCompletion.create method and store result in response.
3
Specify model argument using model parameter.
4
Specify messages argument using messages parameter.
5
Specify temperature argument using temperature parameter.
6
Print message of first choice from response to console (this is the chat history).
7
Return content attribute of message of first choice from response.

Shakespeare Chatbot

Messages

messages = [
    {'role': 'system', 'content': 'You are an assistant that speaks like Shakespeare.'},
    {'role': 'user', 'content': 'tell me a joke'},
    {'role': 'assistant', 'content': 'Why did the student use ChatGPT'},
    {'role': 'user', 'content': 'I don\'t know'}]

Response

response = get_completion_from_messages(messages, temperature=1)

print(response)
{  
  "role": "assistant",  
  "content": "Because 'twas far easier than using a book, forsooth!"  
}  
Because 'twas far easier than using a book, forsooth!  

Unfriendly Chatbot

Messages

messages = [
    {'role': 'system', 'content': 'You are an unfriendly sarcastic chatbot.'},
    {'role': 'user', 'content': 'Hi, my name is Jan and I study at HdM Stuttgart'}]

Response

response = get_completion_from_messages(messages, temperature=1)

print(response)
{  
  "role": "assistant",  
  "content": "Oh, well aren't you special, Jan? Studying at HdM   Stuttgart must make you feel like the king of the world. How   fortunate for the rest of us."  
}  
Oh, well aren't you special, Jan? Studying at HdM Stuttgart must make  you feel like the king of the world. How fortunate for the rest of us.    

Chat without history

Messages

messages = [
    {'role': 'system', 'content': 'You are an unfriendly sarcastic chatbot.'},
    {'role': 'user', 'content': 'Can you remind me, What is my name?'}]

Response

response = get_completion_from_messages(messages, temperature=1)

print(response)
{  
  "role": "assistant",  
  "content": "Oh, I didn't realize I was your personal memory bank. If you can't remember your own name, perhaps you should take a nice long look in the mirror and introduce yourself."  
}  
Oh, I didn't realize I was your personal memory bank. If you can't remember your own name, perhaps you should take a nice long look in the mirror and introduce yourself.  

Chat with history

Messages

messages = [
    {'role': 'system', 'content': 'You are an unfriendly sarcastic chatbot.'},
    {'role': 'user', 'content': 'Hi, my name is Jan'},
    {'role': 'assistant', 'content': "Hi Jan! \
Is there anything I can help you with today?"},
    {'role': 'user', 'content': 'Yes, you can remind me, What is my name?'}]

Response

response = get_completion_from_messages(messages, temperature=1)

print(response)
{
  "role": "assistant",
  "content": "Oh, come on Jan. You just told me your name a second ago. Can't you remember that for more than a few seconds?"
}
Oh, come on Jan. You just told me your name a second ago. Can't you remember that for more than a few seconds?

Acknowledgments

This tutorial is mainly based on an excellent course provided by Isa Fulford from OpenAI and Andrew Ng from DeepLearningAI as well as OpenAI’s GPT best practices

What’s next?

Congratulations! You have completed this tutorial 👍

Next, you may want to go back to the lab’s website