-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleSerper.php
More file actions
55 lines (45 loc) · 1.35 KB
/
GoogleSerper.php
File metadata and controls
55 lines (45 loc) · 1.35 KB
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
<?php
declare(strict_types=1);
namespace Cortex\Tools;
use SensitiveParameter;
use Cortex\LLM\Data\ToolCall;
use Cortex\JsonSchema\SchemaFactory;
use Illuminate\Support\Facades\Http;
use Cortex\JsonSchema\Types\ObjectSchema;
class GoogleSerper extends AbstractTool
{
public function __construct(
#[SensitiveParameter]
protected string $apiKey,
) {}
public function name(): string
{
return 'web_search';
}
public function description(): string
{
return 'Useful for when you need to search the web for information.';
}
public function schema(): ObjectSchema
{
return SchemaFactory::object()
->properties(
SchemaFactory::string('query')->description('The search query.'),
);
}
/**
* @param ToolCall|array<int, mixed> $toolCall
*/
public function invoke(ToolCall|array $toolCall = []): string
{
$arguments = $this->getArguments($toolCall);
$searchQuery = $arguments['query'];
$response = Http::post('https://google.serper.dev/search', [
'q' => $searchQuery,
])
->withHeader('x-api-key', $this->apiKey)
->withHeader('Content-Type', 'application/json');
// @phpstan-ignore method.notFound
return $response->collect('knowledgeGraph')->toJson();
}
}