forked from lucianotonet/groq-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtool-calling-advanced.php
118 lines (103 loc) · 4.23 KB
/
tool-calling-advanced.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
<?php
require __DIR__ . '/_input.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$message = $_POST['message'];
echo "<strong>user :</strong> $message <br>";
/** Example "Calendar" tool */
function calendar_tool($parameters = [])
{
$format = $parameters['format'] ?: 'h:i:s';
return date($format);
}
/** Example “Weather” tool */
function weather_tool($parameters = [])
{
$location = isset($parameters['location']) ? $parameters['location'] : 'New York';
// get weather data from API and return. Faking for demo purposes...
$weather = '30°C, sunny in ' . $location;
return $weather;
}
$messages = [
[
'role' => 'system',
'content' => 'You are a helpful AI assistant. Answer always in a concise way. You shall execute any of "calendar_tool" or "weather_tool" functions to ensure you can respond to the user correctly.'
],
[
'role' => 'user',
'content' => $message
]
];
$tools = [
[
"type" => "function",
"function" => [
"name" => "calendar_tool",
"description" => "Use this function to get the current time in the h:i:s format, or d-m-Y, or any other format supported by the PHP date() function.",
"parameters" => [
"type" => "object",
"properties" => [
"format" => [
"type" => "string",
"description" => "The format of the time to return. Valid options are any format supported by the PHP date() function."
],
],
"required" => ["format"],
"default" => ["format" => "d-m-Y"]
],
]
],
[
"type" => "function",
"function" => [
"name" => "weather_tool",
"description" => "Use this function to get the current weather in a specific location.",
"parameters" => [
"type" => "object",
"properties" => [
"location" => [
"type" => "string",
"description" => "The location for which to get the weather."
],
],
"required" => ["location"],
"default" => ["location" => "New York"]
],
]
]
];
// First inference
$response = $groq->chat()->completions()->create([
'model' => 'mixtral-8x7b-32768', //llama3-70b-8192, llama3-8b-8192, mixtral-8x7b-32768, gemma-7b-it
'messages' => $messages,
"temperature" => 0,
"tool_choice" => "auto",
"tools" => $tools
]);
echo "<strong>assistant:</strong> " . "<br>";
if (isset($response['choices'][0]['message']['tool_calls'])) {
$tool_calls = $response['choices'][0]['message']['tool_calls'];
foreach ($tool_calls as $tool_call) {
if ($tool_call['function']['name']) {
$function_args = json_decode($tool_call['function']['arguments'], true);
echo "<i>> Calling tool...</i>" . "<br>";
$function_response = $tool_call['function']['name']($function_args); // <- call the function
echo "<i>> Building response...</i>" . "<br>";
$messages[] = [
'tool_call_id' => $tool_call['id'],
'role' => 'tool',
'name' => $tool_call['function']['name'],
'content' => $function_response,
];
}
}
// Second inference
$response = $groq->chat()->completions()->create([
'model' => 'mixtral-8x7b-32768',
'messages' => $messages
]);
}
echo "<strong>" . $response['choices'][0]['message']['role'] . ":</strong> " . "<br>";
echo $response['choices'][0]['message']['content'] . "<br>";
} else {
echo "<small>Ask things like \"What's the time?\" or \"What's the weather in London?\".<br/>Results will be mocked for demo purposes.</small><br><br>";
}