Skip to content

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:

php
$userMessage = Message::user('What is quantum computing?');

Assistant Messages

Assistant messages represent responses from the AI model:

php
$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:

php
$assistantMessageWithToolCall = Message::assistant(null, [
    [
        'id' => 'call_12345',
        'type' => 'function',
        'function' => [
            'name' => 'getDate',
            'arguments' => '{}'
        ]
    ]
]);

System Messages

System messages provide instructions or context to the AI model:

php
$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:

php
// 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
);

Working with Messages

Messages can be used individually or collected in a MessagesBag for chat conversations:

php
// 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();

Message Structure

Internally, each message has:

  • role: Identifies who the message is from ('user', 'assistant', 'system', or 'tool')
  • content: The text content of the message
  • toolCalls: Optional array of tool calls made by the assistant
  • toolCallId: Optional ID that links a tool response to its request

For more information about using messages in conversations, see the MessageBag section.

Released under the MIT License.