Skip to content

Commit a4600b8

Browse files
Merge pull request #19 from GeraudBourdin/main
Add tool Message type and update the function call example
2 parents a85d6d2 + 1d7c29b commit a4600b8

File tree

3 files changed

+129
-14
lines changed

3 files changed

+129
-14
lines changed

examples/function_calling.php

100644100755
+58-5
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
use Partitech\PhpMistral\Tools\Parameter;
99
use Partitech\PhpMistral\Tools\Tool;
1010

11-
$model = 'mistral-small-latest';
11+
$model = 'mistral-large-latest';
1212

13-
// export MISTRAL_API_KEY=your_api_key
13+
// export MISTRAL_API_KEY=
1414
$apiKey = getenv('MISTRAL_API_KEY');
1515
$client = new MistralClient(apiKey: $apiKey);
1616

@@ -207,15 +207,68 @@
207207
exit(1);
208208
}
209209

210-
$toolCall = $chatResponse->getToolCalls();
211-
$functionName = $toolCall[0]['function']['name'];
212-
$functionParams = $toolCall[0]['function']['arguments'];
210+
$toolCallResponse = $chatResponse->getToolCalls();
211+
$toolCall = $toolCallResponse[0];
212+
$functionName = $toolCall['function']['name'];
213+
$functionParams = $toolCall['function']['arguments'];
213214

214215
// Call the proper function
215216
$functionResult = $namesToFunctions[$functionName]($functionParams);
216217

218+
print_r($toolCall);
219+
//Array
220+
//(
221+
// [id] => 1b9Ds90lR
222+
// [function] => Array
223+
// (
224+
// [name] => retrievePaymentStatus
225+
// [arguments] => Array
226+
// (
227+
// [transactionId] => T1001
228+
// )
229+
//
230+
// )
231+
//
232+
//)
233+
217234
print_r($functionResult);
218235
// Array
219236
// (
220237
// [status] => Paid
221238
// )
239+
240+
print_r($functionParams);
241+
//Array
242+
//(
243+
// [transactionId] => T1001
244+
//)
245+
246+
// Add the last assistant message to messages history
247+
$messages->addAssistantMessage(
248+
content: $chatResponse->getMessage(),
249+
toolCalls: $chatResponse->getToolCalls()
250+
);
251+
252+
// Add the tool message to query mistral for a message
253+
$messages->addToolMessage(
254+
name: $toolCall['function']['name'],
255+
content: $namesToFunctions[$functionName]($functionParams),
256+
toolCallId: $toolCall['id']
257+
);
258+
259+
try {
260+
$chatResponse = $client->chat(
261+
messages: $messages,
262+
params: [
263+
'model' => $model,
264+
'tools' => $tools,
265+
'tool_choice' => MistralClient::TOOL_CHOICE_AUTO
266+
]
267+
);
268+
} catch (MistralClientException $e) {
269+
echo $e->getMessage();
270+
exit(1);
271+
}
272+
273+
print_r($chatResponse->getMessage());
274+
// The status of your transaction with ID T1001 is 'Paid'. Is there anything else you need help with?

src/Message.php

+58-8
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
class Message
66
{
77
private ?string $role = null;
8-
private ?string $content = null;
8+
private null|string|array $content = null;
99
private ?string $chunk = null;
1010
private ?array $toolCalls = null;
11+
private ?string $toolCallId = null;
12+
private ?string $name = null;
1113

1214
/**
1315
* @return ?string
@@ -26,17 +28,17 @@ public function setRole(string $role): void
2628
}
2729

2830
/**
29-
* @return ?string
31+
* @return array|string|null
3032
*/
31-
public function getContent(): ?string
33+
public function getContent(): null|array|string
3234
{
3335
return $this->content;
3436
}
3537

3638
/**
37-
* @param string $content
39+
* @param string|array $content
3840
*/
39-
public function setContent(string $content): void
41+
public function setContent(string|array $content): void
4042
{
4143
$this->content = $content;
4244
}
@@ -69,18 +71,37 @@ public function getChunk(): string
6971

7072
public function toArray(): array
7173
{
72-
return [
74+
$payLoad = [
7375
'role' => $this->getRole(),
7476
'content' => $this->getContent()
7577
];
78+
79+
if($this->getRole() === 'tool') {
80+
$payLoad['content'] = json_encode($this->getContent());
81+
$payLoad['name'] = $this->getName();
82+
$payLoad['tool_call_id'] = $this->getToolCallId();
83+
}
84+
85+
if ($this->getRole() === 'assistant' && !is_null($this->getToolCalls())){
86+
$payLoad['tool_calls'] = $this->getToolCalls(true);
87+
}
88+
89+
return $payLoad;
7690
}
7791

7892
/**
93+
* @param bool|null $payload
7994
* @return array|null
8095
*/
81-
public function getToolCalls(): ?array
96+
public function getToolCalls(?bool $payload = false): ?array
8297
{
83-
return $this->toolCalls;
98+
$response = $this->toolCalls;
99+
if($payload){
100+
foreach($response as &$toolCall){
101+
$toolCall['function']['arguments'] = json_encode($toolCall['function']['arguments']);
102+
}
103+
}
104+
return $response;
84105
}
85106

86107
/**
@@ -89,11 +110,40 @@ public function getToolCalls(): ?array
89110
*/
90111
public function setToolCalls(?array $toolCalls): Message
91112
{
113+
if(null === $toolCalls){
114+
return $this;
115+
}
116+
92117
foreach($toolCalls as &$toolCall) {
118+
if(is_array($toolCall['function']['arguments'])){
119+
continue;
120+
}
93121
$toolCall['function']['arguments'] = json_decode($toolCall['function']['arguments'], true);
94122
}
95123

96124
$this->toolCalls = $toolCalls;
97125
return $this;
98126
}
127+
128+
public function setName(?string $name): Message
129+
{
130+
$this->name = $name;
131+
return $this;
132+
}
133+
134+
public function getName(): ?string
135+
{
136+
return $this->name;
137+
}
138+
139+
public function setToolCallId(?string $toolCallId): Message
140+
{
141+
$this->toolCallId = $toolCallId;
142+
return $this;
143+
}
144+
145+
public function getToolCallId(): ?string
146+
{
147+
return $this->toolCallId;
148+
}
99149
}

src/Messages.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,23 @@ public function addUserMessage(string $content): self
112112
return $this;
113113
}
114114

115-
public function addAssistantMessage(string $content): self
115+
public function addToolMessage(string $name, array $content, string $toolCallId): self
116+
{
117+
$message = new Message();
118+
$message->setRole('tool');
119+
$message->setContent($content);
120+
$message->setName($name);
121+
$message->setToolCallId($toolCallId);
122+
$this->addMessage($message);
123+
return $this;
124+
}
125+
126+
public function addAssistantMessage(string $content, null|array $toolCalls = null): self
116127
{
117128
$message = new Message();
118129
$message->setRole('assistant');
119130
$message->setContent($content);
131+
$message->setToolCalls($toolCalls);
120132
$this->addMessage($message);
121133
return $this;
122134
}

0 commit comments

Comments
 (0)