AI Logo
AI Exporter Hub
Tips & Tricks

Building Custom Chatbots with Large Language Models

J
Jack
June 12, 2025
prompts
Building Custom Chatbots with Large Language Models

Building Custom Chatbots with Large Language Models

One of the most exciting aspects of using large language models is that you can build custom chatbots with a moderate amount of effort. ChatGPT is a web interface that allows you to interact with large language models through conversations, providing an interactive chat interface. But here’s a cool feature: you can also use large language models to build custom chatbots, such as an AI customer service agent or AI order taker for a restaurant.

In this video, you’ll learn how to do this yourself. I’ll first describe the components of the OpenAI ChatCompletions format in more detail, and then you’ll build a chatbot from scratch. Let’s get started. First, we’ll set up the OpenAI Python package as usual. Chat models like ChatGPT are trained to take a series of messages as input and return a model-generated message as output. While the chat format is designed to facilitate multi-turn conversations, we’ve seen in previous videos that it’s equally useful for single tasks without dialogue.

Setting Up and Defining Helper Functions

First, we’ll set up the OpenAI Python package as usual. Chat models (like ChatGPT) are trained to take a series of messages as input and return a model-generated message as output. While the chat format is designed for multi-turn conversations, we’ve seen in previous videos that it’s also useful for single tasks without dialogue.

Next, we define two helper functions:

  1. getCompletion function: This is the function we’ve used in all previous videos. Although we provide a prompt, internally, the function wraps this prompt into a “user message”-like structure. This is because ChatGPT is a chat model, trained to take a list of messages as input (where user messages are inputs and assistant messages are outputs).
  2. generateResponse function: This function takes a user message and generates a corresponding assistant message from the ChatGPT model. Together, these two functions allow us to interact with the AIGPT model and generate conversations.

Message Lists and the Role of System Messages

In this video, we’ll use a different approach instead of inputting a single prompt and getting a completion: we’ll pass in a list of messages from different roles. Here’s an example of a message list: The first message is a system message providing overall instructions, followed by a conversation between the user and the assistant. This conversation can continue indefinitely. If you’ve used ChatGPT’s web interface, your messages are “user messages,” and ChatGPT’s responses are “assistant messages.”

System messages help set the assistant’s behavior and role, serving as high-level instructions for the conversation. You can think of them as “whispering” guidelines to the assistant without the user noticing. As a user, you might not know what’s in ChatGPT’s system messages—and that’s intentional. The benefit of system messages is that they allow developers to frame the conversation without making the instructions part of the dialogue itself, guiding the assistant’s responses subtly.

Example: A Shakespearean Assistant

Let’s test this with a system message that says, “You are an assistant who speaks like Shakespeare.” Using a higher temperature (for more creative responses), we can influence the assistant’s tone naturally without explicit prompts in the dialogue.

For example, the message list could be:

  • System message: “You are an assistant who speaks like Shakespeare.”
  • User message: “Tell me a joke.”
  • Assistant message (history): “Why did the chicken cross the road?”
  • User message: “I know not.” Running this, the response might be: “To reach the other side, fair lady—pray, forgive me, ‘tis a timeless classic.”

Example: Context Understanding in a Friendly Chatbot

  • Without context:
  • With context:

Building the OrderBot

Now, let’s build your own chatbot: the OrderBot, designed to take pizza orders. We’ll use an automated approach to collect user prompts and assistant responses.

Helper Function for Context Management

First, define a helper function to collect user messages and append them to a context list, which is passed to the model each time. The model’s responses are also added to the context, allowing it to maintain conversation history.

System Message Setup

The system message for OrderBot includes:

  • “You are OrderBot, an automated service for collecting pizza orders. Greet customers, take orders, ask if they want pickup or delivery. Wait until the full order is collected, summarize it, and confirm if they want to add anything else. For delivery, ask for the address. Finally, process payment. Clarify all options, add-ons, and specifications to uniquely identify items from the menu. Respond briefly, in a very conversational and friendly tone. Menu: [list of pizzas, prices, sides, etc.].”

Conversation Example

  • User: “Hi, I’d like to order a pizza.”
  • Assistant: “Great! Which type of pizza would you like? We have pepperoni, cheese, and eggplant.”
  • User: “Medium eggplant pizza, please.”
  • Assistant: “Any toppings or sides? We have fries (small/large), drinks, etc.”
  • User: “Small fries and a Coke.”
  • Assistant: “Got it! That’s a medium eggplant pizza, small fries, and a Coke. Pickup or delivery?”

Generating a JSON Summary

Add a system message to generate a JSON summary of the order, including item details and total price. Use a lower temperature for predictable, structured output (e.g., for integrating with an order system):

json

{
“pizza”: “medium eggplant”,
“sides”: [“small fries”],
“drinks”: [“Coke”],
“total_price”: “$25.99”
}

Wrapping Up

You’ve now built your own OrderBot! Feel free to customize it by tweaking the system message—you can change the bot’s behavior, make it mimic different personas, or adapt it to other domains. The key is to use context (message history) and system messages to guide the model’s responses effectively.

分享

深度思考

Want to read more?

Explore our collection of guides and tutorials.

View All Articles