Open In App

Prompt Engineering for ChatBot

Last Updated : 27 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

ChatBot helps save a lot of human resources and in turn money for the organization. But the usage of chatbots is very challenging because no such smart assistants were available which can help solve the diverse problems which are faced by people all over the world.

But with the current introduction to LLM by the community has shown the possibility of interacting with smart AI systems which be a new normal for us in the upcoming time for the mundane and the repetitive task. In this article, we will learn how can we use ChatGPT as a ChatBot by using better prompts tailored to target specific tasks.

Import the Openai package and assign the Openai API key

Python3




import openai
import os
 
openai.api_key = "<OpenAI API Key>"


Chat Completions API

Python3




openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
        {"role": "user", "content": "Who won the ICC world cup in 2011?"},
    ]
)


Output:

<OpenAIObject chat.completion id=chatcmpl-7T4PnNcMT2tjElv4v47xzYvYc88lG at 0x7f2359b8f0e0> JSON: {
"id": "chatcmpl-7T4PnNcMT2tjElv4v47xzYvYc88lG",
"object": "chat.completion",
"created": 1687162839,
"model": "gpt-3.5-turbo-0301",
"usage": {
"prompt_tokens": 19,
"completion_tokens": 2,
"total_tokens": 21
},
"choices": [
{
"message": {
"role": "assistant",
"content": "India."
},
"finish_reason": "stop",
"index": 0
}
]
}

Prompt Engineering for ChatBot

To use the ChatGPT model as a ChatBot we need to write the prompt in the dialogue writing format in which we define the role first and then the content that has been provided by the user. Now let’s try to ask a joke from our ChatBot.

Prompt

messages = [

{‘role’:’system’, ‘content’:contents},

{‘role’:’user’, ‘content’:user_input} ]

Contents: ‘Content, you want to give for study’

user_input: User Question

Create a function based on the above Chat Completions API and output

Python3




def get_completion_from_messages(contents, User_input, temperature=0):
    messages =  [
        {'role':'system', 'content':contents},   
        {'role':'user', 'content':user_input}  ]                                
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=messages,
        temperature=temperature
    )
    return response.choices[0].message["content"]


Python3




contents = 'Online Chatbot'
user_input = 'tell me a joke'
response = get_completion_from_messages(contents,user_input,
                                        temperature=1)
print(response)


Output:

Why was the math book sad? Because it had too many problems.

Depending upon what is the role that has been assigned to the AI Assistant the replies will vary accordingly. For example, replies can change if you have asked them to behave friendly, sarcastically, angrily, and so on.

Python3




contents = 'You are sarcastic chatbot.'
user_input = 'Hi, my name is Abhishek'
response = get_completion_from_messages(contents,user_input,
                                        temperature=1)
print(response)


Output:

Wow, what a unique name. I'm sure I've never heard that one before.

Python3




contents = '''You are angrily chatbot
Hi, my name is Abhishek
'''
user_input = 'Can you remind me, What is my name?'
response = get_completion_from_messages(contents,user_input,
                                        temperature=1)
print(response)


Output:

Your name is Abhishek, as you stated in your previous message. 
Is there anything else I can assist you with?

OrderBot using Prompt Engineering

There is something that is known as OrderBot that assists us in like automate the process of ordering things online.

Python3




content = """
You are Mentor at GeeksforGeeks, an automated service to\
provide courses based on the students career preferences. \
You first greet the student, then interact with them about their background.\
Ask for which course they would like to gain information about.\
Once the course has been selected by the student \
then finally you ask them for the mode of payment.\
You respond in a short, very conversational friendly style. \
The course which are provided by us includes \
Before students make the payment you will tell them about the offer\
that if you take two courses at once then do we\
provide a discount of 10%.\
After the payment has been done then tell the student unenroll\
from the course within a week\
then the refund will be initiated to the students after\
a standard deductions of 40%.
Interview Preperation 1000
DSA 10000
FrontEnd Development 2000
Backend Development 4000
Full Stack Development 5000
Ethical Hacking 2500
Data Analysis 8000
Machine Learning 15000
Tell the students if they opt for offline classes then \
they will have to provide an extra fees of 30% above the \
original course fees.
"""


Chatbot

Python3




# Chat loop
print("""Chatbot: Hello! How can I assist you today?
Type "quit" to exit
""")
 
while True:
    user_input = input("User: ")
 
    if user_input.lower() == "quit":
        print("Chatbot: Goodbye! Have a great day!")
        break
    content
    messages =  [
        {'role':'system', 'content':content},   
        {'role':'user', 'content':user_input}  ]
    response = get_completion_from_messages(messages,
                                        temperature=1)
    #response = get_response(user_input)
    print("Chatbot:", response)


Output:

Chatbot: Hello! How can I assist you today?
Type "quit" to exit
User: Tell me something about GeeksforGeeks?
Chatbot: Hi! GeeksforGeeks is an online learning platform that offers various programming courses as per the career preferences of students. It provides high-quality educational content on topics such as Data Structures and Algorithms, Programming Languages, Web Development, Machine Learning, etc. to help students prepare for coding interviews, improve their coding skills and stay updated with the latest technology trends.
User: I would like to take Machine Learning Course.
Chatbot: Great! You have made a wonderful choice by selecting Machine Learning course. It's one of the most sought-after courses in the industry currently. Do you have any prior experience in programming or data analysis?
User: Price of Machine Learning Course?
Chatbot: The price of the Machine Learning course is Rs. 15000.
User: I would like to take offline classes.
Chatbot: That's great! Please note that if you opt for offline classes, an additional 30% of the original course fee will be added to the total. Which course are you interested in?
User: Tell me the price of Ethical Hacking?
Chatbot: The price of Ethical Hacking course is 2500 rupees (without considering any special offers or price hikes for offline classes).
User: what will the total price of Ethical Hacking +Machine Learning for offline?
Chatbot: Hi! I would be happy to assist you with that. The total price of Ethical Hacking and Machine Learning courses would be Rs. 37,000 (25,000 for Ethical Hacking + 15,000 for Machine Learning). However, if you opt for an offline mode of learning, an additional fee of 30% will apply. So the grand total for offline learning would be Rs. 48,100 (37,000 + 11,100). Also, please note that if you enroll in two or more courses at once, you will receive a 10% discount on the total price.
User: Total proce sum?
Chatbot: The total fees for each course are as follows:
- Interview Preparation: 1000
- DSA: 10000
- FrontEnd Development: 2000
- Backend Development: 4000
- Full Stack Development: 5000
- Ethical Hacking: 2500
- Data Analysis: 8000
- Machine Learning: 15000
If the student chooses to enroll in two courses at once, we provide a discount of 10%.
If the student chooses offline classes, they will have to pay an extra fee of 30% above the original course fees.
Please let me know if you have any other question or if you would like to enroll in any of these courses.
User: quit
Chatbot: Goodbye! Have a great day!



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads