承接 codechap/ai 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

codechap/ai

Composer 安装命令:

composer require codechap/ai

包简介

A generic PHP wrapper for integrating multiple AI services like OpenAI, Anthropic, and xAI.

README 文档

README

Overview

A PHP library that provides a unified interface for interacting with multiple AI services (OpenAI, Anthropic, Mistral, Groq, xAI). Simplifies integration and standardizes interactions across different AI providers while maintaining service-specific features.

Implementations

Service Chat Streaming Tools Vision Caching PDF JSON Live Search
Anthropic
Groq
Mistral
OpenAI
xAI
Google

Requirements

  • PHP 8.2+
  • Composer

Installation

composer require codechap/ai

Basic Usage

OpenAI

use codechap\ai\Ai;
$openai = new ai('openai', $openaiKey);
print $openai
    ->set('temperature', 0)
    ->set('model', 'o3-mini-2025-01-31')
    ->set('systemPrompt', 'You are a helpful assistant from planet earth.')
    ->set('json', true)
    ->set('reasoningEffort', 'low')
    ->query("What is the capital of South Africa? Only return the three in a JSON response.")
    ->one()
    ;

Mistral

use codechap\ai\Ai;
$mistral = new Ai('mistral', $mistralKey);
print $mistral
    ->set('temperature', 0)
    ->set('model', 'o3-mini-2025-01-31')
    ->set('systemPrompt', 'You are a helpful assistant from planet earth.')
    ->set('json', true)
    ->query("What is the capital of South Africa? Only return the three in a JSON response.")
    ->one()
    ;

Groq (Most open source models)

use codechap\ai\Ai;
$groq = new Ai('groq', $groqKey);
print $groq
    ->set('temperature', 0)
    ->set('model', 'deepseek-r1-distill-llama-70b')
    ->set('systemPrompt', 'You are a helpful assistant from planet earth.')
    ->set('stream', false)
    ->query("What is the capital of South Africa?")
    ->one()
    ;
print "\n\n";

Anthropic (Claude)

use codechap\ai\Ai;
$anthropic = new Ai('anthropic', $anthropicKey);
print $anthropic
    ->set('temperature', 0)
    ->set('model', 'claude-3-5-sonnet-20241022')
    ->set('systemPrompt', 'You are a helpful assistant from planet earth.')
    ->set('stream', false)
    ->query("What is the capital of South Africa?")
    ->one()
    ;

xAI (Grok)

use codechap\ai\Ai;
$xai = new Ai('xai', $xaiKey);
print $xai
    ->set('temperature', 0)
    ->set('model', 'grok-2-latest')
    ->set('systemPrompt', 'You are a helpful assistant from planet earth.')
    ->set('stream', false)
    ->query("What is the capital of South Africa?")
    ->one()
    ;

Google (Gemini)

use codechap\ai\Ai;
$google = new Ai('google', $googleKey);
print = $google
    ->set('temperature', 0)
    ->set('systemPrompt', 'You are a helpful assistant from planet earth.')
    ->set('stream', false)
    ->set('json', true)
    ->query("What is the capital of South Africa? Only return the three in a JSON response.")
    ->all()
    ;

Vision Example

$groq = new Ai('groq', $groqKey);
$result = $groq
    ->set('temperature', 0)
    ->set('model', 'meta-llama/llama-4-scout-17b-16e-instruct')
    ->set('systemPrompt', 'You are a helpful assistant from planet earth.')
    ->set('stream', false)
    ->set('json', false)
    ->query(
    [
        [
            'role' => 'user',
            'content' => [
                [
                    'type' => 'image_url',
                    'image_url' => [
                        'url' => 'https://upload.wikimedia.org/wikipedia/commons/f/f2/LPU-v1-die.jpg'
                    ]
                ],
                [
                    'type' => 'text',
                    'text' => 'What is this image about?'
                ]
            ]
        ]
    ])
    ->all()
    ;
print_r($result);

JSON Response Handling

Different AI services handle JSON responses in different ways:

OpenAI

  • Uses native JSON response formatting via the response_format parameter
  • Set json: true to automatically receive properly formatted JSON responses
  • No additional processing needed

Other Services (Anthropic, Mistral, Groq, xAI)

  • JSON responses are handled through post-processing
  • Set json: true to enable JSON extraction and validation
  • Uses the JsonExtractor helper to:
    • Extract JSON from raw responses
    • Handle JSON within markdown code blocks (json ... )
    • Validate JSON structure

Example usage:

// OpenAI (native JSON)
$ai->openai()
   ->set('json', true)
   ->query('Return user data')
   ->one();

// Other services (post-processed JSON)
$ai->anthropic() // or mistral(), groq(), xai()
   ->set('json', true)
   ->query('Return user data')
   ->one();

Scope

This library is focused on text-based chat completions across multiple AI providers.

Feature Status
Chat completions
Streaming Planned
Tool use Planned
Vision
JSON mode
Live search
Caching Planned
PDF input Planned
Image generation

Image generation is intentionally out of scope and will not be added here. It is better suited as a separate project (e.g., an MCP server) due to its fundamentally different request/response patterns.

Contributing

  • Todo

Testing

  • PHPUnit test suite
  • Automatic service discovery testing
  • Error handling verification

Contributing by adding a New Service

To add a new AI service:

  1. Create a new file in src/Services/ following the naming convention

  2. Implement the required methods:

    • __construct(string $apiKey)
    • query(string $prompt): string
  3. The service will be automatically discovered and available through AIWrapper

  4. Run composer test to verify your implementation

codechap/ai 适用场景与选型建议

codechap/ai 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 108 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 01 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 codechap/ai 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-31