承接 lingoda/ai-sdk 相关项目开发

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

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

lingoda/ai-sdk

Composer 安装命令:

composer require lingoda/ai-sdk

包简介

Framework-agnostic PHP SDK for AI providers with typed results and platform abstraction

README 文档

README

Framework-agnostic PHP SDK for AI providers with typed results and platform abstraction.

🚀 Quick Start

use Lingoda\AiSdk\Platform;
use Lingoda\AiSdk\Client\OpenAI\OpenAIClientFactory;

// Create client using factory
$client = OpenAIClientFactory::createClient('your-api-key');
$platform = new Platform([$client]);

// Simple ask() method - automatically uses default model
$result = $platform->ask('Hello, AI!');
echo $result->getContent(); // TextResult

// Or specify a specific model
$result = $platform->ask('Hello, AI!', 'gpt-4o-mini');
echo $result->getContent();

// Audio capabilities
$audioResult = $platform->textToSpeech('Hello world', $audioOptions);
$transcription = $platform->transcribeAudio('/path/to/audio.mp3', $options);

📚 Documentation

Guide Description
Installation Setup and Platform basics
Configuration API keys and multi-provider setup
Quick Start Your first AI request
Symfony Integration Bundle configuration and provider-specific platforms
HTTP Clients Advanced HTTP configuration
Logging Debug and monitoring setup
Advanced Usage Complex features and patterns
Security Data protection and sanitization
API Reference Complete API documentation
Audio Speech synthesis and transcription
Interactive Examples Live examples with real APIs

✨ Key Features

  • 🔌 Framework Agnostic - No dependencies on Symfony or other frameworks
  • 🛡️ Security First - Built-in data sanitization and attribute-based protection
  • 🎯 Type Safe - Strongly-typed results and prompt value objects
  • 🌐 Multi-Provider - OpenAI, Anthropic, Gemini support with flexible configuration
  • 🎭 Capabilities - Models declare supported features (vision, tools, audio, streaming, reasoning)
  • ⚡ Performance - Built-in rate limiting and token estimation with exponential backoff
  • 📝 Rich Prompts - Parameterized prompts and conversation management
  • 🎵 Audio Support - Text-to-speech, transcription, and translation with multiple formats
  • 🔄 Streaming - Real-time response streaming support

🏗️ Architecture

Platform → Providers → Models → Clients → AI APIs
    ↓
Results ← Security ← Capabilities ← Response
  • Platform: Main entry point for AI operations
  • Providers: Manage models for each AI service (OpenAI, Anthropic, Gemini)
  • Models: Individual AI models with declared capabilities
  • Clients: Handle API communication with rate limiting
  • Results: Type-safe responses (TextResult, BinaryResult, StreamResult, ObjectResult, ToolCallResult)

🎨 Usage Patterns

Simple Text Generation

$result = $platform->ask('Explain AI');
echo $result->getContent(); // string
print_r($result->getMetadata()); // usage, model info, etc.

Parameterized Prompts

$template = UserPrompt::create('Hello {{name}}, tell me about {{topic}}');
$prompt = $template->withParameters([
    'name' => 'Alice',
    'topic' => 'machine learning'
]);

// Use ask() method with prompt objects
$result = $platform->ask($prompt);

Conversations with Context

$conversation = Conversation::withSystem(
    UserPrompt::create('What is quantum computing?'),
    SystemPrompt::create('You are a helpful physics expert')
);

// ask() method supports Conversation objects
$result = $platform->ask($conversation, 'claude-sonnet-4');

Automatic Data Protection

// Sensitive data is automatically sanitized
$prompt = UserPrompt::create('My email is john@example.com');
// Sent to AI as: "My email is [REDACTED_EMAIL]"
$result = $platform->ask($prompt);

// Disable sanitization if needed
$platform = new Platform([$client], enableSanitization: false);

🔧 Requirements

  • PHP ^8.3
  • PSR-18 HTTP Client (Symfony HTTP Client included)
  • PSR-7 HTTP Messages (nyholm/psr7 included)
  • PSR-3 Logger (optional)

🎵 Audio Features

use Lingoda\AiSdk\Audio\OpenAI\AudioOptions;

// Text-to-Speech
$options = AudioOptions::textToSpeech(
    model: AudioSpeechModel::TTS_1,
    voice: AudioSpeechVoice::NOVA,
    format: AudioSpeechFormat::MP3
);
$audioResult = $platform->textToSpeech('Hello world', $options);
file_put_contents('speech.mp3', $audioResult->getContent());

// Speech-to-Text
$transcription = $platform->transcribeAudio('audio.mp3', $transcriptionOptions);
echo $transcription->getContent(); // "Hello world"

// Translation to English
$translation = $platform->translateAudio('spanish-audio.mp3', $translationOptions);

📦 Installation

composer require lingoda/ai-sdk

🤖 Supported Models

OpenAI Models:

  • GPT-5 series: gpt-5, gpt-5-mini, gpt-5-nano (latest)
  • GPT-4.1 series: gpt-4.1, gpt-4.1-mini, gpt-4.1-nano (1M context)
  • GPT-4o series: gpt-4o, gpt-4o-mini (128K context)
  • Audio models: whisper-1, tts-1, tts-1-hd

Anthropic Models:

  • Claude 4.1: claude-opus-4-1-20250805
  • Claude 4.0: claude-opus-4, claude-sonnet-4
  • Claude 3.7: claude-3-7-sonnet
  • Claude 3.5: claude-3-5-haiku

Google Gemini Models:

  • Gemini 2.5: gemini-2.5-pro, gemini-2.5-flash (1M context)

🚦 Quick Test

Run interactive examples to test the SDK:

# Offline examples (no API keys needed)
php docs/usage-example.php

# With OpenAI
OPENAI_API_KEY=your-key php docs/usage-example.php

# Multiple providers
OPENAI_API_KEY=sk-proj-... \
ANTHROPIC_API_KEY=sk-ant-... \
GEMINI_API_KEY=AIza... \
php docs/usage-example.php

🛠️ Development

# Install dependencies
composer install

# Run tests
vendor/bin/phpunit

# Static analysis
vendor/bin/phpstan analyse

# Code style
vendor/bin/ecs check --fix

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for your changes
  4. Ensure all tests pass
  5. Submit a pull request

📄 License

MIT License. See LICENSE for details.

Get Started: Installation Guide | Try Examples: Interactive Examples | Join Discussion: GitHub Issues

lingoda/ai-sdk 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-27