Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions examples/toolbox-wikipedia.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use PhpLlm\LlmChain\Bridge\OpenAI\GPT;
use PhpLlm\LlmChain\Bridge\OpenAI\PlatformFactory;
use PhpLlm\LlmChain\Bridge\Anthropic\Claude;
use PhpLlm\LlmChain\Bridge\Anthropic\PlatformFactory;
use PhpLlm\LlmChain\Chain;
use PhpLlm\LlmChain\Chain\Toolbox\ChainProcessor;
use PhpLlm\LlmChain\Chain\Toolbox\Tool\Wikipedia;
Expand All @@ -14,13 +14,13 @@
require_once dirname(__DIR__).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');

if (empty($_ENV['OPENAI_API_KEY'])) {
echo 'Please set the OPENAI_API_KEY environment variable.'.PHP_EOL;
if (empty($_ENV['ANTHROPIC_API_KEY'])) {
echo 'Please set the ANTHROPIC_API_KEY environment variable.'.PHP_EOL;
exit(1);
}

$platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']);
$llm = new GPT(GPT::GPT_4O_MINI);
$platform = PlatformFactory::create($_ENV['ANTHROPIC_API_KEY']);
$llm = new Claude();

$wikipedia = new Wikipedia(HttpClient::create());
$toolbox = Toolbox::create($wikipedia);
Expand Down
12 changes: 7 additions & 5 deletions src/Bridge/Anthropic/ModelHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,10 @@ public function request(Model $model, object|array|string $input, array $options
$options['tool_choice'] = ['type' => 'auto'];
}

$system = $input->getSystemMessage();
$body = array_merge($options, [
$body = [
'model' => $model->getVersion(),
'system' => $system->content,
'messages' => $input->withoutSystemMessage()->jsonSerialize(),
]);
];

$body['messages'] = array_map(static function (MessageInterface $message) {
if ($message instanceof ToolCallMessage) {
Expand Down Expand Up @@ -98,12 +96,16 @@ public function request(Model $model, object|array|string $input, array $options
return $message;
}, $body['messages']);

if ($system = $input->getSystemMessage()) {
$body['system'] = $system->content;
}

return $this->httpClient->request('POST', 'https://api.anthropic.com/v1/messages', [
'headers' => [
'x-api-key' => $this->apiKey,
'anthropic-version' => $this->version,
],
'json' => $body,
'json' => array_merge($options, $body),
]);
}

Expand Down