oscarweijman / ai-client
Een moderne PHP AI Client
v1.1.0
2025-03-14 14:11 UTC
Requires
- php: ^8.1
- guzzlehttp/guzzle: *
Requires (Dev)
- mockery/mockery: ^1.5
- pestphp/pest: ^2.0
- pestphp/pest-plugin: ^2.1
- phpstan/phpstan: ^1.10
- vlucas/phpdotenv: ^5.5
README
Een moderne PHP client voor AI API's zoals OpenAI en DeepSeek.
Installatie
composer require oscarweijman/ai-client
Gebruik
Client initialiseren
use OscarWeijman\AIClient\AIClientFactory; // Maak een OpenAI client $openaiClient = AIClientFactory::create('openai', 'jouw-api-key'); // Maak een DeepSeek client $deepseekClient = AIClientFactory::create('deepseek', 'jouw-api-key');
Text Completion
$result = $openaiClient->completion('Wat is de hoofdstad van Nederland?', [ 'max_tokens' => 100, 'temperature' => 0.7, ]); echo $result['content'];
Chat Completion
$result = $openaiClient->chatCompletion([ ['role' => 'system', 'content' => 'Je bent een behulpzame assistent.'], ['role' => 'user', 'content' => 'Wat is de hoofdstad van Nederland?'], ], [ 'max_tokens' => 100, 'temperature' => 0.7, ]); echo $result['content'];
Streaming Chat Completion (v1.1.0+)
$messages = [ ['role' => 'system', 'content' => 'Je bent een behulpzame assistent.'], ['role' => 'user', 'content' => 'Schrijf een kort verhaal over een robot die leert programmeren.'], ]; // Callback functie die wordt aangeroepen voor elk stukje van de streaming response $callback = function ($chunk) { echo $chunk['content']; flush(); // Zorg ervoor dat de output direct wordt weergegeven }; // Voer de streaming chat completion uit $openaiClient->streamingChatCompletion($messages, $callback, [ 'model' => 'gpt-3.5-turbo', 'temperature' => 0.7, ]);
Response formaat
Alle API responses worden gestandaardiseerd naar het volgende formaat:
[ 'provider' => 'openai', // of 'deepseek' 'content' => 'De inhoud van het antwoord', 'raw_response' => [], // De originele API response 'finish_reason' => 'stop', // De reden waarom de generatie is gestopt 'model' => 'gpt-3.5-turbo', // Het gebruikte model 'usage' => [ 'prompt_tokens' => 10, 'completion_tokens' => 20, 'total_tokens' => 30, ], ]
Error Handling
De bibliotheek gooit AIClientException
bij fouten:
use OscarWeijman\AIClient\Exceptions\AIClientException; try { $result = $client->chatCompletion($messages); } catch (AIClientException $e) { echo "Error: " . $e->getMessage(); }
Tests uitvoeren
Unit en Feature tests
composer test
Of met de Pest CLI:
./vendor/bin/pest --exclude-group=integration
Integratie tests met echte API's
- Kopieer het
.env.example
bestand naar.env
- Vul je API keys in
- Zet
ENABLE_API_TESTS=true
- Voer de tests uit:
composer test-integration
Of met de Pest CLI:
./vendor/bin/pest tests/Integration
Statische analyse
composer analyse
GitHub Actions
Dit project gebruikt GitHub Actions voor automatische tests en statische analyse:
- tests.yml: Voert unit en feature tests uit op verschillende PHP versies
- integration-tests.yml: Voert integratie tests uit met echte API's (handmatig te triggeren)
- static-analysis.yml: Voert PHPStan analyse uit
Licentie
Dit project is gelicenseerd onder de MIT licentie - zie het LICENSE bestand voor details.