-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfunction_calling.php
executable file
·277 lines (243 loc) · 7.11 KB
/
function_calling.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use Partitech\PhpMistral\Messages;
use Partitech\PhpMistral\MistralClient;
use Partitech\PhpMistral\MistralClientException;
use Partitech\PhpMistral\Tools\FunctionTool;
use Partitech\PhpMistral\Tools\Parameter;
use Partitech\PhpMistral\Tools\Tool;
$model = 'open-mistral-nemo';
$temperature = 0.3;
// export MISTRAL_API_KEY=
$apiKey = getenv('MISTRAL_API_KEY');
$client = new MistralClient(apiKey: $apiKey);
// Assuming we have the following data
$data = [
"transactionId" => [
'T1001',
'T1002',
'T1003',
'T1004',
'T1005'
],
"customerId" => [
'C001',
'C002',
'C003',
'C002',
'C001'
],
"paymentAmount" => [
125.50,
89.99,
120.00,
54.30,
210.20
],
"paymentDate" => [
'2021-10-05',
'2021-10-06',
'2021-10-07',
'2021-10-05',
'2021-10-08'
],
"paymentStatus" => [
'Paid',
'Unpaid',
'Paid',
'Paid',
'Pending'
]
];
/**
* This function retrieves the payment status of a transaction id.
*/
$retrievePaymentStatus = function ($values) use ($data): array|string {
$transactionId = $values['transactionId'];
if(!in_array($transactionId, $data['transactionId'])) {
return ['error' => 'error - transaction id not found.'];
}
$key = array_search($transactionId, $data['transactionId']);
return ['status' => $data['paymentStatus'][$key]];
};
/**
* This function retrieves the payment date of a transaction id.
*/
$retrievePaymentDate = function ($values) use ($data) : array|string {
$transactionId = $values['transactionId'];
if(!in_array($transactionId, $data['transactionId'])) {
return ['error' => 'error - transaction id not found.'];
}
$key = array_search($transactionId, $data['transactionId']);
return ['status' => $data['paymentDate'][$key]];
};
$namesToFunctions = [
"retrievePaymentStatus" => $retrievePaymentStatus(...),
"retrievePaymentDate" => $retrievePaymentDate(...)
];
// Create the tools definition
$transactionIdParam = new Parameter(
type: Parameter::STRING_TYPE,
name: 'transactionId',
description: 'The transaction id.',
required: true
);
$retrievePaymentStatusFunction = new FunctionTool(
name: 'retrievePaymentStatus',
description: 'Get payment status of a transaction id',
parameters: [
$transactionIdParam
]
);
$retrievePaymentDateFunction = new FunctionTool(
name: 'retrievePaymentDate',
description: 'Get payment date of a transaction id',
parameters: [
$transactionIdParam
]
);
$tools = [
new Tool('function', $retrievePaymentStatusFunction),
new Tool('function', $retrievePaymentDateFunction),
];
// $tools should be encoded as json by HttpClient.
// $jsonTools = json_encode($tools, JSON_PRETTY_PRINT);
//[
// {
// "type": "function",
// "function": {
// "name": "retrievePaymentStatus",
// "description": "Get payment status of a transaction id",
// "parameters": {
// "type": "object",
// "required": [
// "transactionId"
// ],
// "properties": {
// "transactionId": {
// "type": "string",
// "description": "The transaction id."
// }
// }
// }
// }
// },
// {
// "type": "function",
// "function": {
// "name": "retrievePaymentDate",
// "description": "Get payment date of a transaction id",
// "parameters": {
// "type": "object",
// "required": [
// "transactionId"
// ],
// "properties": {
// "transactionId": {
// "type": "string",
// "description": "The transaction id."
// }
// }
// }
// }
// }
//]
$messages = new Messages();
$messages->addUserMessage(content: "What's the status of my transaction?");
try {
$chatResponse = $client->chat(
messages: $messages,
params: [
'temperature' => $temperature,
'model' => $model,
'tools' => $tools,
'tool_choice' => MistralClient::TOOL_CHOICE_AUTO
]
);
} catch (MistralClientException $e) {
echo $e->getMessage();
exit(1);
}
// print_r($chatResponse->getMessage());
// will output :
// I'd be happy to help you with that!
// However, I'll need a bit more information to provide a useful response.
// Could you please tell me the type of transaction you're referring to? It could be a bank transaction,
// a purchase from an online store, a cryptocurrency transfer, or something else.
// Additionally, if you have any specific details like a transaction ID, that would be very helpful.
// Please note that I can't access personal data or account details, but I can guide you on how to check the status.
// Push response to history
$messages->addAssistantMessage(content: $chatResponse->getMessage());
// Add customer response
$messages->addUserMessage(content: 'My transaction ID is T1001.');
try {
$chatResponse = $client->chat(
messages: $messages,
params: [
'temperature' => $temperature,
'model' => $model,
'tools' => $tools,
'tool_choice' => MistralClient::TOOL_CHOICE_AUTO
]
);
} catch (MistralClientException $e) {
echo $e->getMessage();
exit(1);
}
$toolCallResponse = $chatResponse->getToolCalls();
$toolCall = $toolCallResponse[0];
$functionName = $toolCall['function']['name'];
$functionParams = $toolCall['function']['arguments'];
// Call the proper function
$functionResult = $namesToFunctions[$functionName]($functionParams);
print_r($toolCall);
//Array
//(
// [id] => 1b9Ds90lR
// [function] => Array
// (
// [name] => retrievePaymentStatus
// [arguments] => Array
// (
// [transactionId] => T1001
// )
//
// )
//
//)
print_r($functionResult);
// Array
// (
// [status] => Paid
// )
print_r($functionParams);
//Array
//(
// [transactionId] => T1001
//)
// Add the last assistant message to messages history
$messages->addAssistantMessage(
content: $chatResponse->getMessage(),
toolCalls: $chatResponse->getToolCalls()
);
// Add the tool message to query mistral for a message
$messages->addToolMessage(
name: $toolCall['function']['name'],
content: $namesToFunctions[$functionName]($functionParams),
toolCallId: $toolCall['id']
);
try {
$chatResponse = $client->chat(
messages: $messages,
params: [
'model' => $model,
'tools' => $tools,
'tool_choice' => MistralClient::TOOL_CHOICE_AUTO
]
);
} catch (MistralClientException $e) {
echo $e->getMessage();
exit(1);
}
print_r($chatResponse->getMessage());
// The status of your transaction with ID T1001 is 'Paid'. Is there anything else you need help with?