PHPackages                             debjit/php-ai-hub - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. debjit/php-ai-hub

ActiveComposer-plugin[API Development](/categories/api)

debjit/php-ai-hub
=================

AI HTTP Service Installer (no-SDK) for Laravel: copy/paste provider stubs via Composer plugin events with JSON registry.

v0.1.1(9mo ago)020MITPHPPHP ^8.2 || ^8.3

Since Aug 2Pushed 9mo agoCompare

[ Source](https://github.com/debjit/php-ai-hub)[ Packagist](https://packagist.org/packages/debjit/php-ai-hub)[ RSS](/packages/debjit-php-ai-hub/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (4)Used By (0)

PHP AI Hub (Laravel) — Use AI providers without their SDKs
==========================================================

[](#php-ai-hub-laravel--use-ai-providers-without-their-sdks)

A lightweight Laravel package that provides a unified way to call AI providers (like OpenAI and Anthropic) using simple HTTP connectors and provider stubs — no vendor SDKs required.

This package focuses on:

- Simple configuration per provider
- Minimal surface area to call common AI endpoints (e.g., chat/completions)
- Extensibility to add new providers without pulling their full SDKs

Requirements
------------

[](#requirements)

- PHP 8.2+
- Laravel 10 or 11
- Composer

Installation
------------

[](#installation)

```
composer require debjit/php-ai-hub
```

Laravel package discovery should auto-register the service provider.

Publish Configuration
---------------------

[](#publish-configuration)

```
php artisan vendor:publish --provider="Debjit\PhpAiHub\Installer" --tag=ai-hub-config
```

This will publish:

- `config/ai-hub.php` — main hub config
- `config/ai-hub/openai.php` — OpenAI-specific config
- `config/ai-hub/anthropic.php` — Anthropic-specific config

If these files already exist, review and merge as needed.

Environment Variables
---------------------

[](#environment-variables)

Set the required keys for the providers you intend to use.

OpenAI:

```
OPENAI_API_KEY=sk-...
OPENAI_BASE_URL=https://api.openai.com
OPENAI_ORG_ID=org_...(optional)
OPENAI_PROJECT_ID=proj_...(optional)

```

Anthropic:

```
ANTHROPIC_API_KEY=sk-ant-...
ANTHROPIC_BASE_URL=https://api.anthropic.com
ANTHROPIC_VERSION=2023-06-01

```

You can override the base URLs to route through proxies/gateways if needed.

Configuration Overview
----------------------

[](#configuration-overview)

Main hub configuration: `config/ai-hub.php`

- Controls default provider and maps provider identifiers to resolver classes and stub implementations.

Example (high-level):

```
return [
    'default' => env('AI_HUB_DEFAULT_PROVIDER', 'openai'),

    'providers' => [
        'openai' => [
            'config' => base_path('config/ai-hub/openai.php'),
            'provider' => \Debjit\PhpAiHub\Stubs\OpenAI\Provider::class,
        ],
        'anthropic' => [
            'config' => base_path('config/ai-hub/anthropic.php'),
            'provider' => \Debjit\PhpAiHub\Stubs\Anthropic\Provider::class,
        ],
    ],
];
```

Per-provider configuration:

- `config/ai-hub/openai.php` contains base URL, auth header strategy, default model, timeouts, etc.
- `config/ai-hub/anthropic.php` contains base URL, version header, default model, timeouts, etc.

How config is resolved:

- `Debjit\PhpAiHub\Support\ConfigResolver` reads the main hub config, then loads the targeted provider config file.
- Environment variables are read via Laravel `env()` in those configs.
- Missing or invalid configurations will throw descriptive exceptions early.

Core Concepts
-------------

[](#core-concepts)

- Registry: `Debjit\PhpAiHub\Registry`
    - Entry point to get a provider instance by key (e.g., "openai", "anthropic") or the default.
- Provider Stub: `Debjit\PhpAiHub\Stubs\{Provider}\Provider`
    - Facade-like API that exposes high-level operations (e.g., chat).
- Client/Connector: `Debjit\PhpAiHub\Stubs\{Provider}\Connectors\HttpConnector`
    - Responsible for HTTP requests to the provider with proper headers, endpoints, and payload shape.

You don’t install SDKs for each provider: we model the minimal HTTP required.

Quick Start
-----------

[](#quick-start)

Resolve the default provider (configured via `AI_HUB_DEFAULT_PROVIDER` or `config/ai-hub.php`):

```
use Debjit\PhpAiHub\Registry;

$provider = app(Registry::class)->provider(); // default from config
```

Or explicitly:

```
$openai = app(Registry::class)->provider('openai');
$anthropic = app(Registry::class)->provider('anthropic');
```

### Example: OpenAI Chat Completion

[](#example-openai-chat-completion)

```
use Debjit\PhpAiHub\Registry;

$openai = app(Registry::class)->provider('openai');

$response = $openai->chat()->create([
    'model' => 'gpt-4o-mini',
    'messages' => [
        ['role' => 'system', 'content' => 'You are a helpful assistant.'],
        ['role' => 'user', 'content' => 'Write a haiku about Laravel.'],
    ],
    'temperature' => 0.7,
]);

$content = $response['choices'][0]['message']['content'] ?? null;
```

Notes:

- The stub normalizes the request to OpenAI’s `/v1/chat/completions`.
- Auth header `Authorization: Bearer {OPENAI_API_KEY}` is added by the connector.

### Example: Anthropic Messages

[](#example-anthropic-messages)

```
use Debjit\PhpAiHub\Registry;

$anthropic = app(Registry::class)->provider('anthropic');

$response = $anthropic->chat()->create([
    'model' => 'claude-3-5-sonnet-20240620',
    'messages' => [
        ['role' => 'user', 'content' => 'Summarize Laravel service providers.'],
    ],
    'max_tokens' => 512,
]);

$content = $response['content'][0]['text'] ?? null;
```

Notes:

- The stub targets Anthropic’s `/v1/messages` with headers:
    - `x-api-key: {ANTHROPIC_API_KEY}`
    - `anthropic-version: {ANTHROPIC_VERSION}`

### Streaming (if supported)

[](#streaming-if-supported)

If a provider supports streaming, the stub may expose a streaming method:

```
$stream = $openai->chat()->stream([
    'model' => 'gpt-4o-mini',
    'messages' => [/*...*/],
]);

foreach ($stream as $event) {
    // handle tokens/chunks
}
```

Refer to the specific provider stub for supported features.

Service Container Usage
-----------------------

[](#service-container-usage)

You can type-hint the registry wherever needed:

```
use Debjit\PhpAiHub\Registry;

class MyController
{
    public function __construct(private Registry $registry) {}

    public function __invoke()
    {
        $ai = $this->registry->provider(); // default
        // ...
    }
}
```

Adding a New Provider
---------------------

[](#adding-a-new-provider)

1. Create a provider config: `config/ai-hub/{provider}.php`

    - Base URL, auth headers, defaults, timeouts/retries.
2. Implement a connector:

    - Example: `src/Stubs/FooAI/Connectors/HttpConnector.php`
    - Use Laravel HTTP client, apply headers and base URL from config.
    - Provide methods like `chatCompletions(array $payload): array`.
3. Implement a provider stub:

    - Example: `src/Stubs/FooAI/Provider.php`
    - Expose simplified methods, like `chat()->create($payload)` that internally calls the connector.
    - Keep the public API consistent with existing providers when possible.
4. Register in main config:

    - In `config/ai-hub.php` add the mapping: ```
        'providers' => [
            // ...
            'fooai' => [
                'config' => base_path('config/ai-hub/fooai.php'),
                'provider' => \Debjit\PhpAiHub\Stubs\FooAI\Provider::class,
            ],
        ],
        ```
5. Use it:

    ```
    $foo = app(\Debjit\PhpAiHub\Registry::class)->provider('fooai');
    $foo->chat()->create([...]);
    ```

Guidelines:

- Do not use the provider’s official SDK. Prefer HTTP with minimal dependencies.
- Normalize payloads where possible to keep your app code portable between providers.

Error Handling &amp; Troubleshooting
------------------------------------

[](#error-handling--troubleshooting)

- Missing API key or base URL

    - Ensure `.env` variables are set and the provider config reads them via `env()`.
- Invalid provider key

    - Confirm the provider key exists in `config/ai-hub.php` under `providers`.
- HTTP errors (4xx/5xx)

    - The connector will return the response or throw exceptions based on your implementation. Check your timeouts and retry logic in the provider config.
- Config resolution issues

    - `Debjit\PhpAiHub\Support\ConfigResolver` loads `config/ai-hub.php`, validates provider entry, and then loads the provider’s own config. If paths or classes are wrong, you’ll get descriptive exceptions.

Testing
-------

[](#testing)

- Mock the connector classes and assert payload shapes.
- For integration tests, set fake API keys and use Laravel’s HTTP fake to simulate provider responses.

Security
--------

[](#security)

- Never log raw API keys or entire request/response bodies containing sensitive data.
- Prefer setting keys in `.env` and referencing via config files.

Versioning
----------

[](#versioning)

Follow semantic versioning (SemVer). Breaking changes will bump the major version.

License
-------

[](#license)

MIT License. See LICENSE file.

Credits
-------

[](#credits)

Crafted for teams that prefer HTTP-first integrations with AI providers in Laravel without heavyweight SDKs.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance56

Moderate activity, may be stable

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

289d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/42a2c645a4e7520b873bc4930cf94d031111396a1753cf7e72b8f15cfa1f8bcb?d=identicon)[debjit](/maintainers/debjit)

---

Top Contributors

[![debjit](https://avatars.githubusercontent.com/u/565922?v=4)](https://github.com/debjit "debjit (4 commits)")

---

Tags

laravelsdkaicomposer-pluginno-sdk

### Embed Badge

![Health badge](/badges/debjit-php-ai-hub/health.svg)

```
[![Health](https://phpackages.com/badges/debjit-php-ai-hub/health.svg)](https://phpackages.com/packages/debjit-php-ai-hub)
```

###  Alternatives

[gemini-api-php/laravel

Gemini API client for Laravel

8915.7k](/packages/gemini-api-php-laravel)[helgesverre/mistral

Laravel Client for the Mistral.ai API

5213.5k1](/packages/helgesverre-mistral)[claude-php/claude-php-sdk-laravel

Laravel integration for the Claude PHP SDK - Anthropic Claude API

5010.8k](/packages/claude-php-claude-php-sdk-laravel)[octw/aramex

A Library to integrate with Aramex APIs

2925.2k](/packages/octw-aramex)[wxm/pdd-sdk

拼多多 SDK 封装, 调用简单、语义化增强。支持 Laravel/Lumen。

154.7k](/packages/wxm-pdd-sdk)[thehocinesaad/stability-laravel

Stability Laravel is a comprehensive Laravel API client designed for seamless interaction with the Stability AI API.

111.3k](/packages/thehocinesaad-stability-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
