PHP code example of developer-unijaya / laravel-ai-chatbox
1. Go to this page and download the library: Download developer-unijaya/laravel-ai-chatbox 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/ */
developer-unijaya / laravel-ai-chatbox example snippets
// config/ai-chatbox.php
'frontend' => 'vue', // Vue 3 widget (default)
'frontend' => 'blade', // Vanilla JS, no framework
'frontend' => 'livewire', // Alpine.js via Livewire
'frontend' => 'none', // API + config only
use DeveloperUnijaya\AiChatbox\AI;
// Use the active provider (resolves to AI_CHATBOX_ACTIVE_PROVIDER)
$reply = AI::chat('Summarise this document: ...');
// Use a specific named provider
$reply = AI::provider('openai')->chat('Translate to French: ...');
$reply = AI::provider('lmstudio')->chat('Write a test for this function...');
$reply = AI::provider('ollama')->chat('What is the capital of France?');
$reply = AI::provider('openai')
->withModel('gpt-4o-mini')
->withTemperature(0.2)
->withSystemPrompt('You are a JSON-only responder. Return only valid JSON.')
->withMaxTokens(512)
->withTimeout(60)
->chat($prompt);
// Pass a callback — tokens are emitted synchronously
AI::provider('openai')->stream($prompt, [], function (string $token) {
echo $token;
ob_flush(); flush();
});
// Or receive a Closure to invoke later
$reader = AI::provider('default')->stream($prompt);
$reader(fn(string $token) => print($token));
// config/ai-chatbox.php — default
'rag_no_context_prompt' => "No relevant knowledge-base entries were found for this question. "
. "If the user is asking for information or a factual answer, do not answer from general knowledge — "
. "reply exactly: \"I don't have that information in my knowledge base.\" "
. "You may still respond naturally to greetings, thanks, and small talk.",
use DeveloperUnijaya\AiChatbox\Orchestration\Contracts\ToolInterface;
use Illuminate\Http\Request;
class GetOrderStatusTool implements ToolInterface
{
public function name(): string
{
return 'get_order_status';
}
public function description(): string
{
return 'Look up the current status of a customer order by its order number.';
}
public function parameters(): array
{
return [
'type' => 'object',
'properties' => [
'order_id' => ['type' => 'string', 'description' => 'The order number, e.g. "A-10234".'],
],
'
use DeveloperUnijaya\AiChatbox\Orchestration\ToolRegistry;
public function boot(): void
{
$this->app->make(ToolRegistry::class)->register(new GetOrderStatusTool());
}
use DeveloperUnijaya\AiChatbox\Engine\Contracts\AiEngineInterface;
use DeveloperUnijaya\AiChatbox\Engine\Exceptions\AiEngineException;
class AnthropicEngine implements AiEngineInterface
{
public function validateConfig(array $options): void
{
if (empty($options['api_token'])) {
throw new AiEngineException('E03', 'API token missing', 500);
}
}
public function complete(array $messages, array $options = []): string
{
// Call Anthropic Messages API, return the reply as a plain string
}
public function stream(array $messages, array $options, callable $onToken): string
{
// Call $onToken('word') per token, return the full assembled reply
}
public function beginStream(array $messages, array $options): \Closure
{
// Open the HTTP connection before response()->stream() starts
// Return a closure: fn(callable $onToken): string
$this->validateConfig($options);
return function (callable $onToken): string {
// read stream, call $onToken per token, return full reply
};
}
}
use DeveloperUnijaya\AiChatbox\Engine\Contracts\AiEngineInterface;
$this->app->bind(AiEngineInterface::class, AnthropicEngine::class);
use DeveloperUnijaya\AiChatbox\Memory\Contracts\ConversationRepositoryInterface;
class RedisConversationRepository implements ConversationRepositoryInterface
{
public function getHistory(string $threadId): array
{
return json_decode(Redis::get("chat:{$threadId}") ?? '[]', true);
}
public function saveHistory(string $threadId, array $history): void
{
Redis::set("chat:{$threadId}", json_encode($history));
}
public function trimToLimit(string $threadId, int $maxPairs): void
{
$history = $this->getHistory($threadId);
$this->saveHistory($threadId, array_slice($history, -($maxPairs * 2)));
}
public function clear(string $threadId): void
{
Redis::del("chat:{$threadId}");
}
}
use DeveloperUnijaya\AiChatbox\Memory\Contracts\ConversationRepositoryInterface;
$this->app->bind(ConversationRepositoryInterface::class, RedisConversationRepository::class);
use DeveloperUnijaya\AiChatbox\Orchestration\Contracts\ToolInterface;
class GetOrderStatusTool implements ToolInterface { /* name/description/parameters/authorize/handle */ }