generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-openai-with-LarAgent.php
115 lines (97 loc) · 3.3 KB
/
test-openai-with-LarAgent.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
<?php
require_once __DIR__.'/vendor/autoload.php';
use LarAgent\Drivers\OpenAi\OpenAiDriver;
use LarAgent\History\InMemoryChatHistory;
use LarAgent\LarAgent;
use LarAgent\Message;
use LarAgent\Messages\ToolCallMessage;
use LarAgent\Tool;
// Setup
$yourApiKey = include 'openai-api-key.php';
$driver = new OpenAiDriver(['api_key' => $yourApiKey]);
$chatKey = 'test-chat-history';
$chatHistory = new InMemoryChatHistory($chatKey);
$agent = LarAgent::setup($driver, $chatHistory, [
'model' => 'gpt-4o-mini',
]);
// Sturctured output
$weatherInfoSchema = [
'name' => 'weather_info',
'schema' => [
'type' => 'object',
'properties' => [
'locations' => [
'type' => 'array',
'items' => [
'type' => 'object',
'properties' => [
'city' => ['type' => 'string'],
'weather' => ['type' => 'string'],
],
'required' => ['city', 'weather'],
'additionalProperties' => false,
],
],
],
'required' => ['locations'],
'additionalProperties' => false,
],
'strict' => true,
];
// Create tool
function get_current_weather($location, $unit = 'celsius')
{
// Call the weather API
return 'The weather in '.$location.' is 72 degrees '.$unit;
}
$toolName = 'get_current_weather';
$tool = Tool::create($toolName, 'Get the current weather in a given location');
$tool->addProperty('location', 'string', 'The city and state, e.g. San Francisco, CA')
->addProperty('unit', 'string', 'The unit of temperature', ['celsius', 'fahrenheit'])
->setRequired('location')
->setMetaData(['sent_at' => '2024-01-01'])
// ->setCallback('get_current_weather')
->setCallback(function ($location, $unit = 'fahrenheit') {
// "Call the weather API"
return 'The weather in '.$location.' is 72 degrees '.$unit;
});
$userMessage = Message::user('What\'s the weather like in Boston and Los Angeles? I prefer celsius');
$instuctions = 'You are weather assistant and always respond using celsius. If it provided as fahrenheit, convert it to celsius.';
$agent->setTools([$tool])->structured($weatherInfoSchema)
->withInstructions($instuctions)
->withMessage($userMessage);
$agent->afterToolExecution(function ($agent, $tool, &$result) {
$sentAt = $tool->getMetaData()['sent_at'];
if ($sentAt) {
$result = $result.'. Specify the check date in answer. Checked at '.$sentAt;
}
});
$agent->afterSend(function ($agent, $history, $message) use ($chatKey) {
if ($message instanceof ToolCallMessage) {
// echo $message->getCallId()."\n";
} else {
$usage = $message->getMetadata()['usage'];
echo $usage->totalTokens.' Tokens used in chat: '.$chatKey."\n";
}
});
$response = $agent->run();
// echo $response;
print_r($response);
/* Outputs:
Array
(
[locations] => Array
(
[0] => Array
(
[city] => Boston, MA
[weather] => The weather is 22 degrees Celsius.
)
[1] => Array
(
[city] => Los Angeles, CA
[weather] => The weather is 22 degrees Celsius.
)
)
)
*/