The Chat resource handles multi-turn conversations using a list of messages with specific roles (system, user, assistant).
Key Features:
create(): Generates a response. Supports the tools parameter for function calling.createStreamed(): Streams the chat response. Note: The tools parameter cannot be used with createStreamed.- Tool Calling: When using
tools, the response may contain toolCalls which include the function name and arguments.
// Standard Chat with Tool Calling
$response = $client->chat()->create([
'model' => 'llama3.1',
'messages' => [
['role' => 'user', 'content' => 'What is the weather today in Paris?'],
],
'tools' => [
[
'type' => 'function',
'function' => [
'name' => 'get_current_weather',
'description' => 'Get the current weather',
'parameters' => [
'type' => 'object',
'properties' => [
'location' => ['type' => 'string'],
'format' => ['type' => 'string', 'enum' => ['celsius', 'fahrenheit']],
],
'required' => ['location', 'format'],
],
],
]
]
]);
$toolCall = $response->message->toolCalls[0];
echo $toolCall->function->name;