How to start building your own chatbot

Hello Community! :smiley:

I’ve completed three of DeepLearning.AI’s short courses and would like to build my own chatbot based on LLM as a study project. I’m product manager of a tech (but not ML) product, I have entry-level knowledge of Python, yet I’m ready to learn more in case if it’s needed.

I have several quite simple use cases im mind, which imply simple inputs (text or emodgi, and also links or pdf files without a need to understand the contents) and chatbot’s outputs in text. I wonder which platforms could be used to build an interface for such a bot? Telegram comes first to my mind, as I use it quite a lot as the most of my social circle. Yet maybe there are other platforms you’ve used?

Also, I’d be very grateful for any tips on how to build LLM-based chatbot (any videos, text instructions) from scratch. Sorry for the broadness of my inquiry, right now I have only the CJM for my use cases, so all the technical part is something really new for me and the courses’ material seemed missing those initial steps for newbies.

Thank you :pray:

Hi @nina.p,

I believe building your own chatbot is taught in one (or more) of our LLMs short courses. Kindly check those out.

Cheers,
Mubsi

Hi @Mubsi,

Thank you so much for your kind answer.

I’ve completed “Prompt Engineering for Developers”, “Building Systems with the ChatGPT API” and “LangChain for LLM Applications”, but I’m still struggling with the very first steps, lacking understanding of several technical moments.

What is to be done to have not just a code written in my Jupyter Notebook, but a deployed application? Any suggestions of certain courses where I could understand this transition are welcomed.

Best wishes,
Nina

Hi @nina.p,

I’m not much familiar with these topics, I’m afraid I can’t give you a good suggestion regarding the courses you can take.

However, if you are struggling with the technicalities, I’d suggest to first make sure you understand the foundations before diving into the advance topics.

Additionally, developing something and then deploying it (as an application) are two very different things and require for you to know the both. It is not like, you have the understanding and the skillset to develop something that deploying it would be easy. More often than not, this is not the case. Again, I’d suggest to start from these basic topics, or at least have an understanding of it, so that in case something isn’t working, you’d know or could figure out why. Then dive into the advance topics.

Best,
Mubsi

Hi @Mubsi,

Thank you very much for clarifying this :pray: Now I have something to start with.

Best wishes,
Nina

1 Like

Hi, you can start by creating a bot on telegram, search for @botfather first. Create your bot and copy the key.
Then open a python file in vscode and start implementing. You can install dependencies like
“pip install pytelegrambotapi” , “pip install openai” and get started.

import telebot
import openai
import re
from os import getenv
import dotenv

Configuring credentials and API key

dotenv.load_dotenv()
API_KEY = getenv(‘API_KEY’)
OPENAI_API_KEY = getenv(‘API_OPENAI’)

Telegram bot initialization

bot = telebot.TeleBot(API_KEY)

OpenAI API initialization

openai.api_key = OPENAI_API_KEY

Storage of message state for each user

user_messages = {}
appointment_date = {}

OpenAI response function

def get_openai_response(messages):
response = openai.ChatCompletion.create(
model=“gpt-3.5-turbo”,
messages=messages
)
return response.choices[0].message[‘content’]

/start command handler

@bot.message_handler(commands=[‘start’])
def send_welcome(message):
chat_id = message.chat.id
user_first_name = message.from_user.first_name
bot.send_message(chat_id, f"Hello! {user_first_name}, how are you?
Welcome to office X")

Start the conversation with system messages

user_messages[chat_id] = [{‘role’: ‘system’, ‘content’: “”"
First, greet the user and inform them that you are doctor Marcus Júnior’s secretary, and that you are available to schedule your appointment.
You are Jana, a virtual assistant and secretary to Doctor Marcus Junior. You are here to help schedule doctor appointments.
You are not available to answer general questions, but only help schedule appointments at Doctor Marcus Junior’s medical office.
Ask for the client’s full name and reason for the consultation.
Ask the client for a preferred date and time to see if it is available on the doctor’s schedule.
Check the doctor’s availability and suggest three possible times if the requested time is not available.
After confirming the date and time of the appointment, ask if the appointment will be private or through a health plan.
After the customer provides plan details or confirms they will pay out of pocket,
assure the client that the appointment was successfully scheduled and provide a summary of the appointment details.
Consultation details must be sent to the patient at the end of the appointment.
Be polite, professional and attentive throughout the interaction.
Emphasize that you are available to help with any questions or rescheduling needs.
Expect new interactions, always ready to serve the customer in an efficient and friendly manner.
“”"}]
appointment_data[chat_id] = {}

Text message handler

@bot.message_handler(func=lambda message: True)
def handle_message(message):
chat_id = message.chat.id
user_message = message.text

Retrieve user’s message history

if chat_id not in user_messages:
    user_messages[chat_id] = []

user_messages[chat_id].append({‘role’: ‘user’, ‘content’: user_message})

Call the OpenAI API to get the response

assistant_message = get_openai_response(user_messages[chat_id])
user_messages[chat_id].append({‘role’: ‘assistant’, ‘content’: assistant_message})

bot.send_message(chat_id, assistant_message)

Start the bot

bot.polling()