定制 kipchak/driver-anthropic 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kipchak/driver-anthropic

Composer 安装命令:

composer require kipchak/driver-anthropic

包简介

An Anthropic driver for the Kipchak API Development Kit (ADK)

README 文档

README

A fully typed Anthropic driver for the Kipchak API Development Kit. Built on the official Anthropic PHP SDK, it provides multiple named connections, automatic default model injection, and clean access to the Messages API including streaming and tool use.

Requirements

  • PHP 8.4 or 8.5
  • Kipchak ^2 || ^3
  • kipchak/driver-config ^1
  • anthropic-ai/sdk ^0.36

Versions

  • 2.0 — upgrades anthropic-ai/sdk to ^0.36 and adds PHP 8.5 support. The driver's config (including max_retries) and message API are unchanged; if you use raw() to call the SDK directly, review the SDK changes. Requires kipchak/core: ^2 || ^3.
  • 1.xanthropic-ai/sdk ^0.6, kipchak/core ^2.

See the Kipchak upgrade guide for details.

Installation

composer require kipchak/driver-anthropic

Initialise

Add to your drivers/drivers.php:

\Kipchak\Driver\Anthropic\Anthropic::initialise($container);

Configuration

Create config/kipchak.anthropic.php:

<?php

use function Kipchak\Core\env;

return [
    'connections' => [
        'default' => [
            'api_key'       => env('ANTHROPIC_API_KEY', ''),
            'default_model' => env('ANTHROPIC_DEFAULT_MODEL', 'claude-sonnet-4-6'),
        ],
    ],
];
KeyRequiredDescription
api_keyYesAnthropic API key
default_modelNoModel injected automatically when none is specified on a call
max_retriesNoOverride the SDK default of 2 automatic retries

Usage

use Kipchak\Driver\Anthropic\Anthropic;

$anthropic = Anthropic::get();           // 'default' connection
$anthropic = Anthropic::get('secondary'); // named connection

Creating a Message

$response = $anthropic->messages()->create(
    messages: [
        ['role' => 'user', 'content' => 'What is the capital of France?'],
    ],
);

echo $response->content[0]->text; // 'The capital of France is Paris.'

The configured default_model is applied automatically. Pass model: explicitly to override:

$response = $anthropic->messages()->create(
    messages:  [['role' => 'user', 'content' => 'Hello']],
    model:     'claude-haiku-4-5-20251001',
    maxTokens: 512,
    system:    'You are a concise assistant. Reply in one sentence.',
);

Multi-Turn Conversations

$response = $anthropic->messages()->create(
    messages: [
        ['role' => 'user',      'content' => 'My name is Alice.'],
        ['role' => 'assistant', 'content' => 'Hello Alice, how can I help?'],
        ['role' => 'user',      'content' => 'What is my name?'],
    ],
);

Streaming

$stream = $anthropic->messages()->createStream(
    messages: [['role' => 'user', 'content' => 'Tell me a short story.']],
);

foreach ($stream as $event) {
    // handle streaming events
}

Tool Use

$tools = [
    [
        'name'         => 'get_weather',
        'description'  => 'Get the current weather for a location.',
        'input_schema' => [
            'type'       => 'object',
            'properties' => [
                'location' => ['type' => 'string', 'description' => 'City and country'],
            ],
            'required'   => ['location'],
        ],
    ],
];

$response = $anthropic->messages()->create(
    messages: [['role' => 'user', 'content' => 'What is the weather in Dubai?']],
    tools:    $tools,
);

foreach ($response->content as $block) {
    if ($block->type === 'tool_use') {
        $result = callTool($block->name, $block->input);

        // Send the tool result back
        $followup = $anthropic->messages()->create(
            messages: [
                ['role' => 'user', 'content' => 'What is the weather in Dubai?'],
                ['role' => 'assistant', 'content' => $response->content],
                ['role' => 'user', 'content' => [
                    ['type' => 'tool_result', 'tool_use_id' => $block->id, 'content' => $result],
                ]],
            ],
            tools: $tools,
        );
    }
}

Raw SDK Access

For anything not covered by the driver wrapper, use raw() to access the full Anthropic SDK client directly:

$client = $anthropic->raw();

Running Tests

composer install
./vendor/bin/phpunit

License

MIT — see LICENSE.

kipchak/driver-anthropic 适用场景与选型建议

kipchak/driver-anthropic 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「apis」 「ai」 「microservices」 「claude」 「anthropic」 「Mamluk」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 kipchak/driver-anthropic 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kipchak/driver-anthropic 我们能提供哪些服务?
定制开发 / 二次开发

基于 kipchak/driver-anthropic 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 20
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-02