Skip to content

Commit f8eed58

Browse files
authored
feat: add tavily tool support (#204)
1 parent a467199 commit f8eed58

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

.env

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ OPENROUTER_KEY=
2727
# For using SerpApi (tool)
2828
SERP_API_KEY=
2929

30+
# For using Tavily (tool)
31+
TAVILY_API_KEY=
32+
3033
# For using MongoDB Atlas (store)
3134
MONGODB_URI=
3235

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ $eventDispatcher->addListener(ToolCallsExecuted::class, function (ToolCallsExecu
251251

252252
1. **Clock Tool**: [toolbox-clock.php](examples/toolbox-clock.php)
253253
1. **SerpAPI Tool**: [toolbox-serpapi.php](examples/toolbox-serpapi.php)
254+
1. **Tavily Tool**: [toolbox-tavily.php](examples/toolbox-tavily.php)
254255
1. **Weather Tool with Event Listener**: [toolbox-weather-event.php](examples/toolbox-weather-event.php)
255256
1. **Wikipedia Tool**: [toolbox-wikipedia.php](examples/toolbox-wikipedia.php)
256257
1. **YouTube Transcriber Tool**: [toolbox-youtube.php](examples/toolbox-youtube.php) (with streaming)

examples/toolbox-tavily.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use PhpLlm\LlmChain\Bridge\OpenAI\GPT;
4+
use PhpLlm\LlmChain\Bridge\OpenAI\PlatformFactory;
5+
use PhpLlm\LlmChain\Chain;
6+
use PhpLlm\LlmChain\Chain\ToolBox\ChainProcessor;
7+
use PhpLlm\LlmChain\Chain\ToolBox\Tool\Tavily;
8+
use PhpLlm\LlmChain\Chain\ToolBox\ToolAnalyzer;
9+
use PhpLlm\LlmChain\Chain\ToolBox\ToolBox;
10+
use PhpLlm\LlmChain\Model\Message\Message;
11+
use PhpLlm\LlmChain\Model\Message\MessageBag;
12+
use Symfony\Component\Dotenv\Dotenv;
13+
use Symfony\Component\HttpClient\HttpClient;
14+
15+
require_once dirname(__DIR__).'/vendor/autoload.php';
16+
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
17+
18+
if (empty($_ENV['OPENAI_API_KEY']) || empty($_ENV['TAVILY_API_KEY'])) {
19+
echo 'Please set the OPENAI_API_KEY and TAVILY_API_KEY environment variable.'.PHP_EOL;
20+
exit(1);
21+
}
22+
$platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']);
23+
$llm = new GPT(GPT::GPT_4O_MINI);
24+
25+
$tavily = new Tavily(HttpClient::create(), $_ENV['TAVILY_API_KEY']);
26+
$toolBox = new ToolBox(new ToolAnalyzer(), [$tavily]);
27+
$processor = new ChainProcessor($toolBox);
28+
$chain = new Chain($platform, $llm, [$processor], [$processor]);
29+
30+
$messages = new MessageBag(Message::ofUser('What was the latest game result of Dallas Cowboys?'));
31+
$response = $chain->call($messages);
32+
33+
echo $response->getContent().PHP_EOL;

src/Chain/ToolBox/Tool/Tavily.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpLlm\LlmChain\Chain\ToolBox\Tool;
6+
7+
use PhpLlm\LlmChain\Chain\ToolBox\Attribute\AsTool;
8+
use Symfony\Contracts\HttpClient\HttpClientInterface;
9+
10+
/**
11+
* Tool integration of tavily.com.
12+
*/
13+
#[AsTool('tavily_search', description: 'search for information on the internet', method: 'search')]
14+
#[AsTool('tavily_extract', description: 'fetch content from a website', method: 'extract')]
15+
final readonly class Tavily
16+
{
17+
/**
18+
* @param array<string, string|string[]|int|bool> $options
19+
*/
20+
public function __construct(
21+
private HttpClientInterface $httpClient,
22+
private string $apiKey,
23+
private array $options = ['include_images' => false],
24+
) {
25+
}
26+
27+
/**
28+
* @param string $query The search query to use
29+
*/
30+
public function search(string $query): string
31+
{
32+
$response = $this->httpClient->request('POST', 'https://api.tavily.com/search', [
33+
'json' => array_merge($this->options, [
34+
'query' => $query,
35+
'api_key' => $this->apiKey,
36+
]),
37+
]);
38+
39+
return $response->getContent();
40+
}
41+
42+
/**
43+
* TODO: Support list of URLs.
44+
*
45+
* @param string $url URL to fetch information from
46+
*/
47+
public function extract(string $url): string
48+
{
49+
$response = $this->httpClient->request('POST', 'https://api.tavily.com/extract', [
50+
'json' => [
51+
'urls' => [$url],
52+
'api_key' => $this->apiKey,
53+
],
54+
]);
55+
56+
return $response->getContent();
57+
}
58+
}

0 commit comments

Comments
 (0)