Lesson 5
Managing Multiple Chat Sessions with OpenAI in PHP
Managing Multiple Chat Sessions with OpenAI

Welcome to the next step in your journey of creating a chatbot with OpenAI! In the previous lessons, you learned how to send messages to OpenAI's language model, explored model parameters, maintained conversation history, and personalized AI behavior with system prompts. Now, we will focus on managing multiple chat sessions. This is crucial for applications where you need to handle several conversations simultaneously, such as customer service chatbots. By the end of this lesson, you will be able to create and manage multiple chat sessions using OpenAI's API, setting the stage for more complex interactions.

Creating Unique Chat Sessions

In a chatbot application, each conversation should be treated as a separate session. To achieve this, we use unique identifiers for each chat session. This ensures that messages and responses are correctly associated with their respective sessions. In our code example, we use PHP's uniqid() function to generate a unique identifier for each chat session. When a new chat session is created, a unique chat_id is generated, and an empty conversation history is initialized.

php
1<?php 2 3// Store all active chat sessions 4$chat_sessions = []; 5 6// Define a common system prompt for all conversations 7$system_prompt = [ 8 "role" => "system", 9 "content" => "You are a friendly and efficient customer service attendant eager to assist customers with their inquiries and concerns." 10]; 11 12// Create a new chat session with a unique identifier 13function create_chat() { 14 global $chat_sessions, $system_prompt; 15 $chat_id = uniqid(); // Create unique session identifier 16 $chat_sessions[$chat_id] = []; // Initialize empty conversation history 17 $chat_sessions[$chat_id][] = $system_prompt; // Add system prompt to conversation history 18 return $chat_id; 19}

In our example, we store conversation history in an array called $chat_sessions, where each key is a unique chat_id. When a user sends a message, it is added to the conversation history, ensuring that the AI has access to the full context when generating a response. This approach helps create a seamless and coherent interaction between the user and the AI.

Sending Messages and Receiving Responses

Once a chat session is established, you can send messages and receive responses from the OpenAI model. It's important to maintain the context by sending the full conversation history to the model. In our code example, we use the send_message function to handle this process. The function takes a chat_id and a user_message as inputs, adds the message to the conversation history, and requests a response from the AI. The response is then processed and added to the conversation history, ensuring continuity in the interaction.

php
1<?php 2require 'vendor/autoload.php'; 3 4use Dotenv\Dotenv; 5 6// Load environment variables 7$dotenv = Dotenv::createImmutable(__DIR__); 8$dotenv->load(); 9 10// Fetch API Key 11$apiKey = $_ENV['OPENAI_API_KEY'] ?? getenv('OPENAI_API_KEY'); 12$baseUrl = $_ENV['OPENAI_BASE_URI'] ?? getenv('OPENAI_BASE_URI'); 13 14$client = \OpenAI::factory() 15 ->withApiKey($apiKey) 16 ->withBaseUri($baseUrl) 17 ->make(); 18 19function send_message($chat_id, $user_message) { 20 global $chat_sessions, $client; 21 22 // Verify chat session exists 23 if (!isset($chat_sessions[$chat_id])) { 24 throw new Exception("Chat session not found!"); 25 } 26 27 // Add user's message to history 28 $chat_sessions[$chat_id][] = ["role" => "user", "content" => $user_message]; 29 30 // Get AI response using conversation history 31 $response = $client->chat()->create([ 32 'model' => 'gpt-4', 33 'messages' => $chat_sessions[$chat_id], 34 ]); 35 36 $answer = trim($response['choices'][0]['message']['content']); 37 38 // Add AI's response to history 39 $chat_sessions[$chat_id][] = ["role" => "assistant", "content" => $answer]; 40 41 // Return AI's response 42 return $answer; 43}
Handling Multiple Chat Sessions

Managing multiple chat sessions simultaneously is a crucial feature for advanced chatbot applications. By using unique identifiers, you can create and interact with different chat sessions independently, ensuring that each conversation remains distinct and contextually accurate. Below, we demonstrate this by initiating a first session and sending messages to it.

php
1<?php 2 3// Create the first chat and send messages 4$chat_id1 = create_chat(); 5echo "Chat 1, First Message: " . send_message($chat_id1, "I'm having trouble with my recent order. Can you help me track it?") . "\n"; 6echo "Chat 1, Follow-up Message: " . send_message($chat_id1, "It was supposed to arrive yesterday but hasn't. What should I do next?") . "\n";

Output for the first chat session:

Plain text
1Chat 1, First Message: Sure, I can help with that. Could you please provide your order number? 2Chat 1, Follow-up Message: I recommend checking with the delivery service for any updates. If there's no information, please contact our support team for further assistance.

Now, let's create a second chat session and interact with it.

php
1<?php 2 3// Create the second chat and send messages 4$chat_id2 = create_chat(); 5echo "Chat 2, First Message: " . send_message($chat_id2, "I'm interested in upgrading my membership. What are the benefits?") . "\n"; 6echo "Chat 2, Follow-up Message: " . send_message($chat_id2, "Could you guide me through the upgrade process?") . "\n";

Output for the second chat session:

Plain text
1Chat 2, First Message: Upgrading your membership offers benefits such as exclusive discounts, early access to new features, and priority customer support. 2Chat 2, Follow-up Message: Certainly! To upgrade, please visit your account settings and select the 'Upgrade Membership' option. Follow the prompts to complete the process.

This approach not only maintains the integrity of each conversation but also enhances scalability, making it ideal for applications like customer support where multiple interactions occur simultaneously. By keeping conversations separate, you can provide a more efficient and effective service to each user.

Summary and Preparation for Practice

In this lesson, you learned how to manage multiple chat sessions using OpenAI's API. We covered creating unique chat sessions, maintaining conversation history, and handling multiple interactions simultaneously. These skills are essential for building scalable chatbot applications that can handle numerous conversations at once. As you move on to the practice exercises, I encourage you to apply what you've learned by creating and managing chat sessions independently. This hands-on practice will reinforce your understanding and prepare you for more advanced chatbot development. Keep up the great work, and enjoy the journey of creating your chatbot with OpenAI!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.