定制 mozex/anthropic-laravel 二次开发

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

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

mozex/anthropic-laravel

Composer 安装命令:

composer require mozex/anthropic-laravel

包简介

Laravel integration for the Anthropic API: facade, config publishing, install command, testing fakes, messages, streaming, tool use, thinking, and batches.

README 文档

README

Latest Version on Packagist GitHub Checks Workflow Status Docs License Total Downloads

Laravel wrapper for Anthropic PHP, the community-maintained PHP SDK for the Anthropic API. Adds a Facade, a publishable config, an install command, and a testing integration that plugs into the service container.

Read the full documentation at mozex.dev: searchable docs, version requirements, detailed changelog, and more.

Not using Laravel? Use the framework-agnostic Anthropic PHP package directly.

Table of Contents

Support This Project

I maintain this package along with several other open-source PHP packages used by thousands of developers every day.

If my packages save you time or help your business, consider sponsoring my work on GitHub Sponsors. Your support lets me keep these packages updated, respond to issues quickly, and ship new features.

Business sponsors get logo placement in package READMEs. See sponsorship tiers →

Why This Package

Anthropic:: Facade for everything. Anthropic::messages(), Anthropic::models(), Anthropic::batches(), Anthropic::files(), Anthropic::completions(). No client instantiation, no factory setup. The service provider handles it.

Anthropic::fake() in your tests. Swap the real client with a fake, queue responses, and assert exactly which requests were sent. Pairs with Laravel's existing testing idioms like Event::fake() and Queue::fake(). See the testing docs →

use Anthropic\Laravel\Facades\Anthropic;
use Anthropic\Resources\Messages;
use Anthropic\Responses\Messages\CreateResponse;

Anthropic::fake([
    CreateResponse::fake([
        'content' => [['type' => 'text', 'text' => 'Paris is the capital of France.']],
    ]),
]);

// Run your code...

Anthropic::assertSent(Messages::class, function (string $method, array $parameters) {
    return $parameters['model'] === 'claude-sonnet-4-6';
});

One artisan command to set up. php artisan anthropic:install publishes the config file and appends ANTHROPIC_API_KEY= to your .env. You're ready to go.

Forward-compatible. Parameters pass through to the API as-is. When Anthropic ships a new feature, it works in your code the same day. No waiting for a package update.

Full Anthropic API coverage. Messages, streaming, tool use, extended thinking, web search, code execution, citations, token counting, and batch processing. Every feature the API supports is available through the Facade.

Installation

Requires PHP 8.2+ - see all version requirements

composer require mozex/anthropic-laravel

Run the install command:

php artisan anthropic:install

This publishes config/anthropic.php and appends ANTHROPIC_API_KEY= to your .env. Set your key from the Anthropic Console:

ANTHROPIC_API_KEY=sk-ant-...

Quick Start

Use the Anthropic Facade anywhere in your app:

use Anthropic\Laravel\Facades\Anthropic;

$response = Anthropic::messages()->create([
    'model' => 'claude-sonnet-4-6',
    'max_tokens' => 1024,
    'messages' => [
        ['role' => 'user', 'content' => 'Hello!'],
    ],
]);

echo $response->content[0]->text; // Hello! How can I assist you today?

Streaming

Print text as it arrives:

$stream = Anthropic::messages()->createStreamed([
    'model' => 'claude-sonnet-4-6',
    'max_tokens' => 1024,
    'messages' => [
        ['role' => 'user', 'content' => 'Tell me a short story.'],
    ],
]);

foreach ($stream as $response) {
    if ($response->type === 'content_block_delta'
        && $response->delta->type === 'text_delta') {
        echo $response->delta->text;
    }
}

Tool Use

Give Claude tools to call, execute them in your code, send results back:

$response = Anthropic::messages()->create([
    'model' => 'claude-sonnet-4-6',
    'max_tokens' => 1024,
    'tools' => [
        [
            'name' => 'get_weather',
            'description' => 'Get the current weather in a given location',
            'input_schema' => [
                'type' => 'object',
                'properties' => [
                    'location' => ['type' => 'string'],
                ],
                'required' => ['location'],
            ],
        ],
    ],
    'messages' => [
        ['role' => 'user', 'content' => 'What is the weather in San Francisco?'],
    ],
]);

$response->content[1]->name;              // 'get_weather'
$response->content[1]->input['location']; // 'San Francisco'

Extended Thinking

Let Claude reason through complex problems before answering:

$response = Anthropic::messages()->create([
    'model' => 'claude-opus-4-6',
    'max_tokens' => 16000,
    'thinking' => ['type' => 'adaptive'],
    'messages' => [
        ['role' => 'user', 'content' => 'What is the GCD of 1071 and 462?'],
    ],
]);

$response->content[0]->thinking; // 'Using the Euclidean algorithm...'
$response->content[1]->text;     // 'The GCD of 1071 and 462 is 21.'

The full documentation covers every feature: vision and images, web search and code execution, document citations, batch processing, error handling, testing, and more.

Resources

Visit the documentation site for searchable docs auto-updated from this repository.

  • AI Integration: Use this package with AI coding assistants via Context7 and Laravel Boost
  • Requirements: PHP, Laravel, and dependency versions
  • Changelog: Release history with linked pull requests and diffs
  • Contributing: Development setup, code quality, and PR guidelines
  • Questions & Issues: Bug reports, feature requests, and help
  • Security: Report vulnerabilities directly via email

License

The MIT License (MIT). Please see License File for more information.

mozex/anthropic-laravel 适用场景与选型建议

mozex/anthropic-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 340.14k 次下载、GitHub Stars 达 72, 最近一次更新时间为 2024 年 05 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 340.14k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 74
  • 点击次数: 24
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 72
  • Watchers: 3
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-01