Skip to content

feat: add support for Anthropic tool use #209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2025
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
2 changes: 1 addition & 1 deletion src/Bridge/Anthropic/Claude.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ public function supportsStructuredOutput(): bool

public function supportsToolCalling(): bool
{
return false; // it does, but implementation here is still open.
return true;
}
}
63 changes: 62 additions & 1 deletion src/Bridge/Anthropic/ModelHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@

namespace PhpLlm\LlmChain\Bridge\Anthropic;

use PhpLlm\LlmChain\Chain\ToolBox\Metadata;
use PhpLlm\LlmChain\Exception\RuntimeException;
use PhpLlm\LlmChain\Model\Message\AssistantMessage;
use PhpLlm\LlmChain\Model\Message\MessageBagInterface;
use PhpLlm\LlmChain\Model\Message\MessageInterface;
use PhpLlm\LlmChain\Model\Message\ToolCallMessage;
use PhpLlm\LlmChain\Model\Model;
use PhpLlm\LlmChain\Model\Response\ResponseInterface as LlmResponse;
use PhpLlm\LlmChain\Model\Response\StreamResponse;
use PhpLlm\LlmChain\Model\Response\TextResponse;
use PhpLlm\LlmChain\Model\Response\ToolCall;
use PhpLlm\LlmChain\Model\Response\ToolCallResponse;
use PhpLlm\LlmChain\Platform\ModelClient;
use PhpLlm\LlmChain\Platform\ResponseConverter;
use Symfony\Component\HttpClient\Chunk\ServerSentEvent;
Expand Down Expand Up @@ -40,13 +46,58 @@ public function request(Model $model, object|array|string $input, array $options
{
Assert::isInstanceOf($input, MessageBagInterface::class);

if (isset($options['tools'])) {
$tools = $options['tools'];
$options['tools'] = [];
/** @var Metadata $tool */
foreach ($tools as $tool) {
$toolDefinition = [
'name' => $tool->name,
'description' => $tool->description,
'input_schema' => $tool->parameters ?? ['type' => 'object'],
];
$options['tools'][] = $toolDefinition;
}
$options['tool_choice'] = ['type' => 'auto'];
}

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

$body['messages'] = array_map(static function (MessageInterface $message) {
if ($message instanceof ToolCallMessage) {
return [
'role' => 'user',
'content' => [
[
'type' => 'tool_result',
'tool_use_id' => $message->toolCall->id,
'content' => $message->content,
],
],
];
}
if ($message instanceof AssistantMessage && $message->hasToolCalls()) {
return [
'role' => 'assistant',
'content' => array_map(static function (ToolCall $toolCall) {
return [
'type' => 'tool_use',
'id' => $toolCall->id,
'name' => $toolCall->name,
'input' => empty($toolCall->arguments) ? new \stdClass() : $toolCall->arguments,
];
}, $message->toolCalls),
];
}

return $message;
}, $body['messages']);

return $this->httpClient->request('POST', 'https://api.anthropic.com/v1/messages', [
'headers' => [
'x-api-key' => $this->apiKey,
Expand All @@ -72,6 +123,16 @@ public function convert(ResponseInterface $response, array $options = []): LlmRe
throw new RuntimeException('Response content does not contain any text');
}

$toolCalls = [];
foreach ($data['content'] as $content) {
if ('tool_use' === $content['type']) {
$toolCalls[] = new ToolCall($content['id'], $content['name'], $content['input']);
}
}
if (!empty($toolCalls)) {
return new ToolCallResponse(...$toolCalls);
}

return new TextResponse($data['content'][0]['text']);
}

Expand Down