droath/nextus-ai
Composer 安装命令:
composer require droath/nextus-ai
包简介
Nextus AI is a unified Laravel client library for multiple LLM providers (OpenAI, Anthropic, Perplexity), providing seamless backend integration for AI-powered applications.
README 文档
README
Nextus AI is a unified Laravel client library designed for multiple LLM providers, including OpenAI, Anthropic Claude, and Perplexity. This package offers a consistent interface for working with Large Language Models, featuring agent coordination, tool execution, and flexible memory strategies.
Features
- Multi-Provider Support: Seamlessly switch between OpenAI, Anthropic Claude, and Perplexity with a unified API
- Driver Pattern Architecture: Clean abstraction layer for LLM provider integrations
- Agent System: Coordinate multiple AI agents with parallel, sequential, or router strategies
- Tool Integration: Define and execute tools that LLMs can call during conversations
- Memory Strategies: Flexible memory management with database, session, or null strategies
- Streaming Support: Real-time streaming responses from compatible providers
- Testing Helpers: Built-in fake clients and testing utilities
- Laravel Integration: Native Laravel service provider with auto-discovery
Requirements
- PHP 8.3 or higher
- Laravel 11.x or higher
- Valid API keys for desired LLM providers
Installation
Install the package via Composer:
composer require droath/nextus-ai
The service provider will be automatically discovered by Laravel.
Configuration
Publish the configuration file:
php artisan vendor:publish --tag="nextus-ai-config"
This will create config/nextus-ai.php where you can configure your LLM
providers.
Environment Variables
Add your API keys to your .env file:
# OpenAI Configuration OPENAI_API_KEY=your-openai-api-key OPENAI_ORGANIZATION=your-org-id # Optional # Anthropic Claude Configuration CLAUDE_API_KEY=your-claude-api-key # Perplexity Configuration PERPLEXITY_API_KEY=your-perplexity-api-key
Database Migrations
If you plan to use the database memory strategy or agent memory features, run the migrations:
php artisan migrate
Usage
Basic Chat Example
use Droath\NextusAi\Facades\NextusAiClient; use Droath\NextusAi\Drivers\Enums\LlmProvider; use Droath\NextusAi\Messages\UserMessage; // Using OpenAI with Message objects (recommended) $response = NextusAiClient::driver(LlmProvider::OPENAI) ->chat() ->withMessages([ UserMessage::make('What is Laravel?') ])(); echo $response->getMessage(); // Alternative: Using array syntax (also supported) $response = NextusAiClient::driver(LlmProvider::OPENAI) ->chat() ->withMessages([ ['role' => 'user', 'content' => 'What is Laravel?'] ])();
Using Different Providers
use Droath\NextusAi\Facades\NextusAiClient; use Droath\NextusAi\Messages\UserMessage; use Droath\NextusAi\Messages\SystemMessage; use Droath\NextusAi\Drivers\Enums\LlmProvider; // Using Claude with Message objects $driver = NextusAiClient::driver(LlmProvider::CLAUDE); $claudeResponse = $driver->chat() ->withModel('claude-3-5-sonnet-20241022') ->withMessages([ SystemMessage::make('You are a helpful programming assistant.'), UserMessage::make('Explain async programming') ])(); // Using Perplexity $driver = NextusAiClient::driver(LlmProvider::PERPLEXITY); $perplexityResponse = $driver->chat() ->withMessages([ UserMessage::make('Latest news on AI developments') ])();
Message Classes
The package provides dedicated message classes for better type safety and structure:
use Droath\NextusAi\Facades\NextusAiClient; use Droath\NextusAi\Drivers\Enums\LlmProvider; use Droath\NextusAi\Messages\UserMessage; use Droath\NextusAi\Messages\SystemMessage; use Droath\NextusAi\Messages\AssistantMessage; // Create messages using the make() method $systemMessage = SystemMessage::make('You are a helpful assistant.'); $userMessage = UserMessage::make('Hello, how are you?'); // Send conversation with multiple messages $driver = NextusAiClient::driver(LlmProvider::OPENAI); $response = $driver->chat() ->withMessages([ SystemMessage::make('You are a helpful assistant specialized in Laravel.'), UserMessage::make('What are the new features in Laravel 11?'), ])(); // UserMessage supports context for additional metadata $messageWithContext = UserMessage::make( 'Analyze this code', context: 'This is a Laravel controller with CRUD operations' );
Available Message Classes:
UserMessage- Messages from the userSystemMessage- System instructions/promptsAssistantMessage- Messages from the AI assistant
Working with Agents
Create intelligent agents that can coordinate tasks:
use Droath\NextusAi\Agents\Agent; use Droath\NextusAi\Agents\AgentCoordinator; use Droath\NextusAi\Agents\Enums\AgentStrategy; // Create specialized agents $researchAgent = Agent::make() ->setSystemPrompt('You are a research assistant specialized in gathering information.'); $writerAgent = Agent::make() ->setSystemPrompt('You are a content writer who creates engaging articles.'); // Coordinate agents with a strategy $coordinator = AgentCoordinator::make( 'Create a blog post about Laravel', [$researchAgent, $writerAgent], AgentStrategy::SEQUENTIAL ); $result = $coordinator->run($resource);
Using Tools
Define tools that LLMs can invoke:
use Droath\NextusAi\Facades\NextusAiClient; use Droath\NextusAi\Drivers\Enums\LlmProvider; use Droath\NextusAi\Messages\UserMessage; use Droath\NextusAi\Tools\Tool; use Droath\NextusAi\Tools\ToolProperty; $weatherTool = Tool::make('get_weather') ->describe('Get the current weather for a location') ->using(function (array $arguments) { // Your tool implementation here $location = $arguments['location']; $unit = $arguments['unit'] ?? 'fahrenheit'; // Example: fetch weather from an API return "The current weather in {$location} is 72 degrees {$unit}."; }) ->withProperties([ ToolProperty::make('location', 'string') ->describe('The city and state, e.g. San Francisco, CA') ->required(), ToolProperty::make('unit', 'string') ->describe('Temperature unit: celsius or fahrenheit') ->withEnums(['celsius', 'fahrenheit']), ]); $driver = NextusAiClient::driver(LlmProvider::OPENAI); $response = $driver->chat() ->withTools([$weatherTool]) ->withMessages([ UserMessage::make('What is the weather in San Francisco?') ])(); // If the LLM decides to use the tool, it will be automatically executed // and the response will include the tool's output
Memory Strategies
Use memory to maintain context across interactions:
use Droath\NextusAi\Memory\MemoryDefinition; use Droath\NextusAi\Memory\MemoryStrategyFactory; use Droath\NextusAi\Agents\Agent; // Database memory strategy $memoryDefinition = new MemoryDefinition('database', [ 'connection' => 'mysql', 'table' => 'llm_agent_memory' ]); $factory = new MemoryStrategyFactory($memoryDefinition); $memory = $factory->createInstance(); $agent = Agent::make() ->setSystemPrompt('You remember previous conversations') ->setMemory($memory); // Store information $memory->set('user_preference', 'prefers concise answers'); // Retrieve later $preference = $memory->get('user_preference');
Streaming Responses
Get real-time streaming responses using callbacks:
use Droath\NextusAi\Facades\NextusAiClient; use Droath\NextusAi\Messages\UserMessage; use Droath\NextusAi\Drivers\Enums\LlmProvider; use Droath\NextusAi\Responses\NextusAiResponseMessage; $driver = NextusAiClient::driver(LlmProvider::OPENAI); $streamOutput = ''; $chat = $driver->chat() ->withModel('gpt-4') ->withMessages([ UserMessage::make('Write a long story') ]) ->usingStream( function (string $chunk, bool $initialized) use (&$streamOutput) { // Process each chunk as it arrives echo $chunk; $streamOutput .= $chunk; }, function (NextusAiResponseMessage $response) { // Called when streaming is complete echo "\n\nStreaming finished!"; } ); // Execute the chat request $response = $chat();
Supported LLM Providers
OpenAI
- Models: GPT-4, GPT-4 Turbo, GPT-3.5 Turbo, and more
- Features: Chat, embeddings, function calling, streaming
- Configuration: API key, organization ID (optional), base URL (optional)
Anthropic Claude
- Models: Claude 3.5 Sonnet, Claude 3 Opus, Claude 3 Haiku
- Features: Chat, function calling, streaming
- Configuration: API key
Perplexity
- Models: Various Perplexity models
- Features: Chat with web search capabilities
- Configuration: API key
Advanced Features
Agent Coordination Strategies
- Parallel: Execute multiple agents simultaneously
- Sequential: Execute agents one after another, passing results
- Router: Intelligently route requests to the most appropriate agent
Memory Strategies
- Database Strategy: Persistent storage using Laravel's database
- Session Strategy: Session-based temporary storage
- Null Strategy: No memory persistence (stateless)
Custom Drivers
Extend the package to support additional LLM providers by implementing the driver interface:
use Droath\NextusAi\Drivers\NextusAiDriver; class CustomDriver extends NextusAiDriver { // Implement required methods }
Testing
Run the test suite:
composer test
Run code style checks:
vendor/bin/pint
Run static analysis:
vendor/bin/phpstan analyse
Testing Your Application
Use the built-in fake client for testing:
use Droath\NextusAi\Facades\NextusAi; use Droath\NextusAi\Responses\NextusAiResponseMessage; NextusAi::fake( responseCallback: fn() => NextusAiResponseMessage::fromString('Fake response') ); // Your test code here
Console Commands
Memory Cleanup
Clean up expired memory entries:
# Perform cleanup php artisan nextus-ai:memory:cleanup # Dry run to see what would be cleaned php artisan nextus-ai:memory:cleanup --dry-run
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
droath/nextus-ai 适用场景与选型建议
droath/nextus-ai 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 10 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「openai」 「llm」 「claude」 「anthropic」 「perplexity」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 droath/nextus-ai 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 droath/nextus-ai 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 droath/nextus-ai 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP 8.0+ OpenAI API client with fully typed/documented requests+responses models, guzzle and symfony/http-client support and async/parallel requests.
Estimate AI API costs before making expensive calls
A powerful package that seamlessly integrates OpenAI's advanced AI capabilities into your Laravel applications. This package offers quick setup and intuitive configuration to leverage AI models for chat, embeddings, and more.
AI discussion summaries for Flarum with real-time streaming.
Alfabank REST API integration
Official Azure OpenAI provider for the PHP AI SDK.
统计信息
- 总下载量: 13
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-31