-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
80fd016
commit a669d16
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
use PhpLlm\LlmChain\Bridge\OpenAI\GPT; | ||
use PhpLlm\LlmChain\Bridge\OpenAI\PlatformFactory; | ||
use PhpLlm\LlmChain\Chain; | ||
use PhpLlm\LlmChain\Chain\ToolBox\ChainProcessor; | ||
use PhpLlm\LlmChain\Chain\ToolBox\Tool\Tavily; | ||
use PhpLlm\LlmChain\Chain\ToolBox\ToolAnalyzer; | ||
use PhpLlm\LlmChain\Chain\ToolBox\ToolBox; | ||
use PhpLlm\LlmChain\Model\Message\Message; | ||
use PhpLlm\LlmChain\Model\Message\MessageBag; | ||
use Symfony\Component\Dotenv\Dotenv; | ||
use Symfony\Component\HttpClient\HttpClient; | ||
|
||
require_once dirname(__DIR__).'/vendor/autoload.php'; | ||
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env'); | ||
|
||
if (empty($_ENV['OPENAI_API_KEY']) || empty($_ENV['TAVILY_API_KEY'])) { | ||
echo 'Please set the OPENAI_API_KEY and TAVILY_API_KEY environment variable.'.PHP_EOL; | ||
exit(1); | ||
} | ||
$platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']); | ||
$llm = new GPT(GPT::GPT_4O_MINI); | ||
|
||
$tavily = new Tavily(HttpClient::create(), $_ENV['TAVILY_API_KEY']); | ||
$toolBox = new ToolBox(new ToolAnalyzer(), [$tavily]); | ||
$processor = new ChainProcessor($toolBox); | ||
$chain = new Chain($platform, $llm, [$processor], [$processor]); | ||
|
||
$messages = new MessageBag(Message::ofUser('What was the latest game result of Dallas Cowboys?')); | ||
$response = $chain->call($messages); | ||
|
||
echo $response->getContent().PHP_EOL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Chain\ToolBox\Tool; | ||
|
||
use PhpLlm\LlmChain\Chain\ToolBox\Attribute\AsTool; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
#[AsTool('tavily_search', description: 'search for information on the internet', method: 'search')] | ||
#[AsTool('tavily_extract', description: 'fetch content from a websites', method: 'extract')] | ||
final readonly class Tavily | ||
{ | ||
public function __construct( | ||
private HttpClientInterface $httpClient, | ||
private string $apiKey, | ||
private array $options = ['include_images' => false], | ||
) { | ||
} | ||
|
||
/** | ||
* @param string $query The search query to use | ||
*/ | ||
public function search(string $query): string | ||
{ | ||
$response = $this->httpClient->request('POST', 'https://api.tavily.com/search', [ | ||
'json' => array_merge($this->options, [ | ||
'query' => $query, | ||
'api_key' => $this->apiKey, | ||
]), | ||
]); | ||
|
||
return $response->getContent(); | ||
} | ||
|
||
/** | ||
* TODO: Support list of URLs. | ||
* | ||
* @param string $url URL to fetch information from | ||
*/ | ||
public function extract(string $url): string | ||
{ | ||
$response = $this->httpClient->request('POST', 'https://api.tavily.com/extract', [ | ||
'json' => [ | ||
'urls' => [$url], | ||
'api_key' => $this->apiKey, | ||
], | ||
]); | ||
|
||
return $response->getContent(); | ||
} | ||
} |