forked from lucianotonet/groq-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtool-calling.php
128 lines (114 loc) · 4.4 KB
/
tool-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
<?php
require __DIR__ . '/_input.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$message = $_POST['message'];
echo "<strong>user:</strong><br>";
echo "$message<br>";
function getNbaScore($teamName)
{
// Example dummy function hard coded to return the score of an NBA game
if (strpos(strtolower($teamName), 'warriors') !== false) {
return json_encode([
"game_id" => "401585601",
"status" => 'Final',
"home_team" => "Los Angeles Lakers",
"home_team_score" => 121,
"away_team" => "Golden State Warriors",
"away_team_score" => 128
]);
} elseif (strpos(strtolower($teamName), 'lakers') !== false) {
return json_encode([
"game_id" => "401585601",
"status" => 'Final',
"home_team" => "Los Angeles Lakers",
"home_team_score" => 121,
"away_team" => "Golden State Warriors",
"away_team_score" => 128
]);
} elseif (strpos(strtolower($teamName), 'nuggets') !== false) {
return json_encode([
"game_id" => "401585577",
"status" => 'Final',
"home_team" => "Miami Heat",
"home_team_score" => 88,
"away_team" => "Denver Nuggets",
"away_team_score" => 100
]);
} elseif (strpos(strtolower($teamName), 'heat') !== false) {
return json_encode([
"game_id" => "401585577",
"status" => 'Final',
"home_team" => "Miami Heat",
"home_team_score" => 88,
"away_team" => "Denver Nuggets",
"away_team_score" => 100
]);
} else {
return json_encode([
"team_name" => $teamName,
"score" => "unknown"
]);
}
}
$messages = [
[
'role' => 'system',
'content' => "You shall call the function 'getNbaScore' to answer questions around NBA game scores. Include the team and their opponent in your response."
],
[
'role' => 'user',
'content' => $message
]
];
$tools = [
[
'type' => 'function',
'function' => [
'name' => 'getNbaScore',
'description' => 'Get the score for a given NBA game',
'parameters' => [
'type' => 'object',
'properties' => [
"team_name" => [
"type" => "string",
"description" => "The name of the NBA team (e.g. 'Golden State Warriors')",
]
],
"required" => ["team_name"],
],
],
]
];
$response = $groq->chat()->completions()->create([
'model' => 'mixtral-8x7b-32768', // mixtral-8x7b-32768, gemma-7b-it
'messages' => $messages,
"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'] == 'getNbaScore') {
$function_args = json_decode($tool_call['function']['arguments'], true);
echo "<i>> Calling tool...</i>" . "<br>";
$function_response = getNbaScore($function_args['team_name']);
echo "<i>> Building response...</i>" . "<br>";
$messages[] = [
'tool_call_id' => $tool_call['id'],
'role' => 'tool',
'name' => 'getNbaScore',
'content' => $function_response,
];
}
}
$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 about NBA game scores.<br/>Results will be mocked for demo purposes.</small><br><br>";
}