Flarum的扩展关于AI(ChatGPT)的有两个,一个是 ChatGPT,一个是 AI-Tookit。
因为ChatGPT不能支持Mention,所以我选用了 AI-Toolkit.
安装方法如下
首先,Composer安装插件
composer require flarum/ai-toolkit:"dev-main"
安装完成后,后台启用。
后台默认只有两个设置项,key 和组织,我们使用的是自定义api,所以我把自定义api写到了 组织那里。。
我使用的模型是 gpt-4o-mini,所以修改 Flarum\Ai\Agent\Model 文件 为如下
<?php
namespace Flarum\Ai\Agent;
enum Model: string
{
case gpt_4 = 'gpt-4';
case gpt_3_5_turbo = 'gpt-3.5-turbo';
case gpt_4o_mini = 'gpt-4o-mini';
}
Ai的Boot.php是使用的默认官方api地址,我改为了如下
<?php
namespace Flarum\Ai;
use Flarum\Ai\Agent\Collection;
use Flarum\Ai\Interaction\Feature;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Container\Container;
use OpenAI;
class Boot
{
static private bool $booted = false;
public function __invoke(Container $container): void
{
if (static::$booted) return;
/** @var SettingsRepositoryInterface $settings */
$settings = $container->make(SettingsRepositoryInterface::class);
$apiKey = $settings->get('flarum-ai-toolkit.openai-api-key');
$organisation = $settings->get('flarum-ai-toolkit.openai-api-organisation');
if ($apiKey) {
$client = OpenAI::factory()
->withApiKey($apiKey)
->withBaseUri($organisation) // default: api.openai.com/v1
->make();
$container->singleton('blomstra-ai-client', fn() => $client);
Agent::setClient($client);
}
// Class to help identify what we can do.
$container->singleton(Feature::class);
// Class to help centrally register any agents.
$container->singleton(Collection::class);
static::$booted = true;
}
}
如代码,这里偷懒使用了之前填写的组织为url地址。。。。。。。 
有一个问题是, openai-php 这个sdk会检查 header 里边的 requestid,但是我的api里边没有requestid,所以修改代码为如下
$requestId = $headers['x-oneapi-request-id'][0];
我不知道这个为什么是 x-oneapi-request-id
,反正我的是这个就传这个了。
然后修改 flarum根目录的 extend.php,增加以下代码
return [
(new \Flarum\Ai\Extend\Ai(
// unique identifier
key: 'tttttttttttt',
// username or user Id of User to represent
represents: 'Ass',
// Chat GPT Model to use. Either \Flarum\Ai\Agent\Model::gpt_3_5_turbo or \Flarum\Ai\Agent\Model::gpt_4
model: \Flarum\Ai\Agent\Model::gpt_4o_mini,
// Discussion Id of discussion that contains the instructions
instructions: 7
))
// Chain the call to assign authorizations/permissions
->authorize()
// The tag slug where this authorization applies
->in('ai')
// What the Ai can do, full list is in the documentation/readme.
->can(
replyToPosts: true,
respondToMentions: true
)
// Conclude this autorization
->activate()
// Chain another authorization after activate() that applies to this Ai
->authorize()
->in('another-tag')
->can(respondToMentions: true)
->activate(),
];