コンテンツにスキップ
Nibiru docsv0.9.2

チャットプラグイン

単一または複数のターンで行われる、任意のOllama互換エンドポイントに対するチャット完了。

Stable Reading time ~ 1 min Edit on GitHub

チャットプラグインはAIモジュールの最もシンプルな部分です。Ollamaの/api/chatを流暢なビルダー、会話記憶、バックアップモデルへの自動フォールバック、およびワンショットのask()ショートカットでラップしています。

$ai = new \Nibiru\Module\Ai\Ai();
$chat = $ai->chat();
$chat->system('Be terse.'); / optional system prompt
$chat->model('qwen2.5-coder:14b'); / override the configured model
$chat->temperature(0.2); / override config
$chat->maxTokens(512); / override config
$chat->user('Hello'); / append a user message
$chat->assistant('Hi.'); / append an assistant message (rare)
$reply = $chat->complete(); / run the call, return text
$reply = $chat->ask('How are you?'); / = ->user(...)->complete()
$chat->reset(); / clear messages, keep model + system
$chat->history(); / [{role, content}, ]
echo (new \Nibiru\Module\Ai\Ai())
->chat()
->ask('In one sentence, what does Form::create() do?');
$chat = $ai->chat();
$chat->user('Name three Nibiru singletons.');
$singletons = $chat->complete(); / appended to history
$chat->user('What does the second one do?');
$detail = $chat->complete(); / model has full context

モデルとスタイルを個別呼び出しごとに上書きする

Section titled “モデルとスタイルを個別呼び出しごとに上書きする”
$german = $ai->chat()
->system('Answer in German. Be precise.')
->model('qwen2.5-coder:14b')
->temperature(0.1)
->ask('Wie definiere ich einen Controller?');

もし chat.model(例えば nibiru-coder:1.0)が Ollama サーバーに存在しない場合、プラグインは chat.fallback_model(例えば qwen2.5-coder:14b)で呼び出しを再実行します。これにより、アプリケーションは動作し続けますので、微調整の構築中に問題が発生することはありません。

[AI]
chat.model = "nibiru-coder:1.0"
chat.fallback_model = "qwen2.5-coder:14b"

デフォルト: Ollama。Anthropic Claude をバックエンドとして使用するには:

[AI]
chat.provider = "anthropic"
anthropic.api_key = "sk-ant-..."
anthropic.model = "claude-haiku-4-5-20251001"

チャットプラグインはまだフレームワークモジュールにAnthropicトランスポートを含んでいません — 現在、ドキュメントサイトのscripts/lib/providers.mjsパターンが参考となっています。(マイルストーンをご覧ください。)

実践的なパターン: チャットをアクションとして

Section titled “実践的なパターン: チャットをアクションとして”
namespace Nibiru;
use Nibiru\Adapter\Controller;
use Nibiru\Module\Ai\Ai;
class supportController extends Controller
{
public function askAction(): void {
View::forwardToJsonHeader();
$q = trim($this->getPost('question', ''));
if ($q === '') {
View::assign(['data' => ['error' => 'question required']]);
return;
}
$reply = (new Ai())->chat()
->system('You are the Nibiru support assistant. Be brief.')
->ask($q);
View::assign(['data' => ['answer' => $reply]]);
}
public function pageAction(): void {}
public function navigationAction(): void {
JsonNavigation::getInstance()->loadJsonNavigationArray();
}
}

六行プラスボイラープレートで、独自のOllamaをバックにしたAJAX呼び出し可能なAIエンドポイントが完成します。

  • complete()を忘れている。 フラウントビルダーは、complete()またはask()が呼ばれるまで何も実行しません。
  • ユーザーのターン間にassistant()を呼び出す。 これは保存された会話を再生するためのものであり、通常の使用には適していません。
  • 長い会話。 各ターンで完全な履歴が再送信されます。古いコンテキストが必要なくなる場合は、reset()を使用したり、history()をスライスしてトリムします。
  • 温度設定が低すぎる。 0はモデルを硬くします;技術的な回答にとって最適な範囲は0.3–0.5です。