1. Go to this page and download the library: Download mojahed/aiapi 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/ */
mojahed / aiapi example snippets
use Mojahed\AIAPI\AI;
$ai = new AI();
$response = $ai->ask('What is Laravel?');
echo $response->answer; // the answer
echo $response->summary; // updated conversation summary
echo $response->tokensUsed; // total tokens used
echo $response->model; // model that responded
$response->raw; // full raw API response array
use Mojahed\AIAPI\AI;
$response = (new AI())
->setProvider('openrouter')
->setModel('z-ai/glm-5.2')
->setBehaviour('You are an Eduplus school assistant. Reply formally.')
->setKnowledge('/path/to/eduplus-docs.md')
->setSummary($summaryFromDB)
->setMaxTokens(1024)
->setTemperature(0.7)
->ask('How does the attendance system work?');
echo $response->answer;
// Single file
$ai->setKnowledge('/knowledge/eduplus.md');
// Raw string
$ai->setKnowledge('MdsChatBot is a floating chatbot widget...');
// Multiple files
$ai->setKnowledge([
'/knowledge/git.md',
'/knowledge/php.md',
'/knowledge/eduplus.md',
]);
// Load summary from DB
$summary = AiSession::find($sessionId)?->summary ?? '';
$response = (new AI())
->setSummary($summary)
->ask($question);
// Save updated summary to DB
AiSession::updateOrCreate(
['id' => $sessionId],
['summary' => $response->summary]
);
return $response->answer;
// Different providers for different purposes
$fast = (new AI())->setProvider('groq'); // fastest
$smart = (new AI())->setProvider('claude'); // most capable
$cheap = (new AI())->setProvider('deepseek'); // cheapest
$local = (new AI())->setProvider('ollama'); // free, local
use Mojahed\AIAPI\Providers\ProviderInterface;
class MyCustomProvider implements ProviderInterface
{
public function __construct(protected string $apiKey) {}
public function send(array $messages, array $options): array
{
// Make your API call here
// Return the raw response as array
}
public function extractContent(array $raw): string
{
return $raw['my_answer_field'] ?? '';
}
public function extractTokens(array $raw): int
{
return $raw['usage']['total'] ?? 0;
}
public function extractModel(array $raw): string
{
return $raw['model'] ?? 'custom';
}
}
use Mojahed\AIAPI\AI;
public function boot(): void
{
AI::registerProvider('myprovider', MyCustomProvider::class);
}