Building a Child-Safe AI Chatbot: A Comprehensive Guide

Piotr Krosniak
5 min readAug 20, 2024

--

In today’s digitally connected world, children are increasingly accessing online platforms at a younger age. According to research published in the Journal of Pediatrics, children as young as two years old are engaging with digital devices, and by the time they reach adolescence, digital communication becomes a central part of their social interaction and learning. However, this early and extensive exposure to digital platforms also raises significant concerns regarding their safety online.

A child-safe chatbot is crucial in this context because it can serve as a protective intermediary between young users and the vast, often unfiltered, information available online. Research published in Computers in Human Behavior highlights the importance of creating digital environments that are not only informative but also developmentally appropriate for children. The study stresses that without proper safeguards, children are vulnerable to encountering inappropriate content, cyberbullying, and other online risks.

Moreover, the role of AI in moderating content and guiding interactions has been recognized as a key factor in protecting young users. A study in the Journal of Information Technology explored how AI-driven systems, like chatbots, can be designed to promote safe online behavior among children. The study concluded that AI can effectively filter harmful content and provide real-time guidance during interactions, helping to mitigate risks associated with unsupervised internet use.

The chatbot described in this article leverages AI to ensure that conversations remain age-appropriate, informative, and protective against potentially harmful content. By providing thoughtful, guided responses and suggesting follow-up questions, the chatbot not only engages children in a constructive dialogue but also steers the conversation towards topics that are safe and educational. This approach is supported by findings in the International Journal of Child-Computer Interaction, which emphasizes the importance of interactive technologies that promote positive engagement and learning outcomes in children.

In summary, a child-safe chatbot is an essential tool in the digital age, where children’s online safety is a growing concern. By combining AI with carefully curated content, such chatbots can create a safe, educational, and supportive environment for young users, helping them navigate the complexities of the digital world safely.

Project Overview

The chatbot is designed with the following goals:

  1. Safe Interactions: The chatbot is tailored to ensure that interactions remain within the bounds of safe and constructive dialogue.
  2. Guided Conversations: It provides follow-up questions that steer the conversation towards educational and helpful topics.
  3. Cost-Efficiency: By carefully managing API usage, the chatbot minimizes the costs associated with token usage in AI models.

Setting Up the Environment

To build this chatbot, you’ll need a Python environment with the following libraries:

  • gradio: For creating the web interface.
  • openai: To access OpenAI's GPT models.
  • tiktoken: For encoding and counting tokens.
  • pandas: For data manipulation (optional, but useful).

The Code Walkthrough

Importing Necessary Libraries

First, let’s start by importing the required libraries and setting up the OpenAI API key.

import gradio as gr
import openai
import os
import tiktoken
import pandas as pd

# Set your OpenAI API key
openai.api_key = os.getenv('OPENAI_API_KEY')

Here, gradio is used to create the user interface, while openai is used to access the GPT-3.5 model. The tiktoken library is essential for counting tokens, which helps in managing the cost of API calls.

Token Management

Managing the number of tokens is crucial for both cost-efficiency and performance. The following function calculates the number of tokens used in a message:

def num_tokens_from_messages(messages, model="gpt-3.5-turbo"):
encoding = tiktoken.encoding_for_model(model)
num_tokens = 0
for message in messages:
num_tokens += 4 # Base tokens for each message
for key, value in message.items():
num_tokens += len(encoding.encode(value))
if key == "name":
num_tokens += 1
num_tokens += 2 # Tokens for the assistant's reply
return num_tokens

This function is vital for keeping track of token usage, ensuring that the chatbot remains within budget constraints.

Chat Initialization

The chatbot starts by initializing the conversation with an introductory question. This is done using the initialize_chat function:

def initialize_chat(initial_question):
chat_history = [(None, initial_question)]
response, follow_up_questions, token_info = generate_response(initial_question, 0)
chat_history.append((None, response))

examples_state = [[q.strip()] for q in follow_up_questions.split('\n') if q.strip()]

return chat_history, follow_up_questions, token_info, examples_state

This function sets up the initial state of the chat, including the history, token information, and suggested follow-up questions.

Generating Responses

Generating responses that are both relevant and safe is the core of this chatbot. The generate_response function handles this:

def generate_response(prompt, token_count=0):
messages = [
{"role": "system", "content": "You are a friendly and helpful chatbot."},
{"role": "user", "content": prompt}
]

try:
input_tokens = num_tokens_from_messages(messages, model="gpt-3.5-turbo")

response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
max_tokens=150,
temperature=0.7,
)

output_text = response.choices[0].message['content'].strip()
output_tokens = response.usage['completion_tokens']

follow_up_prompt = f"Based on the following response, suggest three follow-up questions that a young person should ask in first person: {output_text}"
follow_up_messages = [
{"role": "system", "content": "You are a friendly and helpful chatbot."},
{"role": "user", "content": follow_up_prompt}
]
follow_up_response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=follow_up_messages,
max_tokens=100,
temperature=0.7,
)

follow_up_questions = follow_up_response.choices[0].message['content'].strip().split('\n')
follow_up_questions = "\n".join(follow_up_questions)

total_input_tokens = input_tokens + num_tokens_from_messages(follow_up_messages, model="gpt-3.5-turbo")
total_output_tokens = output_tokens + follow_up_response.usage['completion_tokens']

input_cost = total_input_tokens * INPUT_COST_PER_TOKEN
output_cost = total_output_tokens * OUTPUT_COST_PER_TOKEN
total_cost = input_cost + output_cost

new_response = output_text + "\n\nTopics: " + "Topic analysis not available"
token_info = f"### Token Usage:\n\n* Input Tokens: {total_input_tokens}\n* Output Tokens: {total_output_tokens}\n* Total Cost: ${total_cost:.4f}"
except Exception as e:
new_response = f"Error generating response: {e}"
follow_up_questions = []
token_info = "### Token Usage:\n\n* Input Tokens: 0\n* Output Tokens: 0\n* Total Cost: $0.0000"

return new_response, follow_up_questions, token_info

This function ensures that the responses are safe and relevant by incorporating a system message that sets the tone for the chatbot. The follow-up questions guide the user towards productive and safe topics.

Creating the User Interface

Finally, the user interface is built using Gradio, a user-friendly tool for creating web interfaces in Python:

with gr.Blocks(css=css) as demo:
examples_state = gr.State([])
chat_history = gr.State([])
token_info = gr.State("")
follow_up_questions_md = gr.State("")

with gr.Row():
with gr.Column(scale=1):
gr.Markdown(
"""
# Child-Safe Chatbot Project
Welcome to a chatbot designed with children's safety in mind. Start by typing your question below.
"""
)
token_info_display = gr.Markdown(value="")
follow_up_questions_display = gr.Markdown(value="")
initial_question_input = gr.Textbox(placeholder="Type your initial question here...")
initialize_button = gr.Button("Initialize Chat")

question_examples = gr.Examples(
examples=questions,
inputs=initial_question_input,
label="Example Questions"
)

with gr.Column(scale=1, elem_id="chat-container"):
chatbot = gr.Chatbot(value=[], elem_id="chatbot")

with gr.Row():
txt = gr.Textbox(scale=4, show_label=False, placeholder="Select question...")
btn = gr.Button("Submit")

btn.click(
fn=process_response,
inputs=[txt, chat_history, gr.State(0), examples_state],
outputs=[chatbot, token_info_display, follow_up_questions_display, examples_state]
)
examples_component = gr.Examples(
examples=examples_state.value,
inputs=[txt],
label="Suggested Questions"
)

chatbot.like(print_like_dislike, None, None)

initialize_button.click(
fn=initialize_chat,
inputs=[initial_question_input],
outputs=[chat_history, follow_up_questions_display, token_info_display, examples_state]
).then(
fn=lambda chat_history: chat_history,
inputs=[chat_history],
outputs=[chatbot]
)

if __name__ == "__main__":
demo.launch(share=False)

This interface is simple yet effective, providing a safe and engaging environment for young users to interact with the chatbot.

Conclusion

Building a child-safe chatbot is a critical step towards creating a safer internet for children. By leveraging advanced AI models like GPT-3.5 and tools like Gradio, developers can create chatbots that are not only engaging but also prioritize the safety and well-being of young users. The provided code offers a solid foundation for building such a chatbot, with customization options to suit various needs.

By implementing these practices, you contribute to a more secure and positive online experience for children.

Here is also implemented chatbot on Hugging Face Space https://huggingface.co/spaces/peterkros/child-safe-chatbot

--

--

Piotr Krosniak
Piotr Krosniak

Written by Piotr Krosniak

DataScience and #GIS specialist. Love #triathlon #dataviz and #opensource tech. Dad of two work in UNICEF

No responses yet