PHP code example of debjit / php-ai-hub
1. Go to this page and download the library: Download debjit/php-ai-hub library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
debjit / php-ai-hub example snippets
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,
],
],
];
use Debjit\PhpAiHub\Registry;
$provider = app(Registry::class)->provider(); // default from config
$openai = app(Registry::class)->provider('openai');
$anthropic = app(Registry::class)->provider('anthropic');
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;
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;
$stream = $openai->chat()->stream([
'model' => 'gpt-4o-mini',
'messages' => [/*...*/],
]);
foreach ($stream as $event) {
// handle tokens/chunks
}
use Debjit\PhpAiHub\Registry;
class MyController
{
public function __construct(private Registry $registry) {}
public function __invoke()
{
$ai = $this->registry->provider(); // default
// ...
}
}
'providers' => [
// ...
'fooai' => [
'config' => base_path('config/ai-hub/fooai.php'),
'provider' => \Debjit\PhpAiHub\Stubs\FooAI\Provider::class,
],
],
$foo = app(\Debjit\PhpAiHub\Registry::class)->provider('fooai');
$foo->chat()->create([...]);
bash
composer bash
php artisan vendor:publish --provider="Debjit\PhpAiHub\Installer" --tag=ai-hub-config