Messages
The Message
class in LabsLLM is used to create various types of messages for chat interactions. Each message has a role (such as 'user', 'assistant', or 'system') and content.
Message Types
LabsLLM supports several message types, each serving a different purpose in conversations with AI models:
User Messages
User messages represent inputs from the end user:
$userMessage = Message::user('What is quantum computing?');
Assistant Messages
Assistant messages represent responses from the AI model:
$assistantMessage = Message::assistant('Quantum computing is a type of computing that uses quantum phenomena like superposition and entanglement to perform operations on data.');
You can also create assistant messages with tool calls:
$assistantMessageWithToolCall = Message::assistant(null, [
[
'id' => 'call_12345',
'type' => 'function',
'function' => [
'name' => 'getDate',
'arguments' => '{}'
]
]
]);
2
3
4
5
6
7
8
9
10
System Messages
System messages provide instructions or context to the AI model:
$systemMessage = Message::system('You are a helpful assistant specialized in explaining complex topics in simple terms.');
Tool Messages
Tool messages represent responses from tools that the AI has called:
// Complete tool message with all parameters
$toolMessage = Message::tool(
content: '2024-05-05', // The response content
id: 'call_12345', // The tool call ID
name: 'getDate', // The name of the tool
description: 'Get current date', // Description of what the tool does
arguments: ['format' => 'Y-m-d'], // Arguments passed to the tool
response: '2024-05-05' // The actual response from the tool
);
2
3
4
5
6
7
8
9
Working with Messages
Messages can be used individually or collected in a MessagesBag
for chat conversations:
// Create messages
$assistantMessage::assistant('I\'m doing well! How can I help you today?')
$userMessage = Message::user('Hello, who are you?');
// Add to a MessagesBag
$messages = MessagesBag::create([$systemMessage, $userMessage]);
// Get message details
echo $userMessage->getRole(); // 'user'
echo $userMessage->getContent(); // 'Hello, who are you?'
// Convert to array format
$messageArray = $userMessage->toArray();
2
3
4
5
6
7
8
9
10
11
12
13
Message Structure
Internally, each message has:
role
: Identifies who the message is from ('user', 'assistant', 'system', or 'tool')content
: The text content of the messagetoolCalls
: Optional array of tool calls made by the assistanttoolCallId
: Optional ID that links a tool response to its request
For more information about using messages in conversations, see the MessageBag section.