71 lines
3.0 KiB
PHP
71 lines
3.0 KiB
PHP
|
|
<?php
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 文件功能:创建 AI 厂商配置表和 AI 使用日志表
|
|||
|
|
*
|
|||
|
|
* ai_provider_configs — 存储多个 AI 厂商的 API 配置(密钥、端点、模型等)
|
|||
|
|
* ai_usage_logs — 记录每次 AI 接口调用的 token 消耗和响应信息
|
|||
|
|
*
|
|||
|
|
* @author ChatRoom Laravel
|
|||
|
|
*
|
|||
|
|
* @version 1.0.0
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
use Illuminate\Database\Migrations\Migration;
|
|||
|
|
use Illuminate\Database\Schema\Blueprint;
|
|||
|
|
use Illuminate\Support\Facades\Schema;
|
|||
|
|
|
|||
|
|
return new class extends Migration
|
|||
|
|
{
|
|||
|
|
/**
|
|||
|
|
* 执行迁移:创建 AI 相关的两张数据表
|
|||
|
|
*/
|
|||
|
|
public function up(): void
|
|||
|
|
{
|
|||
|
|
// AI 厂商配置表
|
|||
|
|
Schema::create('ai_provider_configs', function (Blueprint $table) {
|
|||
|
|
$table->id();
|
|||
|
|
$table->string('provider', 50)->index()->comment('厂商标识(如 deepseek, qwen, openai)');
|
|||
|
|
$table->string('name', 100)->comment('厂商显示名称');
|
|||
|
|
$table->text('api_key')->comment('API Key(加密存储)');
|
|||
|
|
$table->string('api_endpoint', 255)->comment('API 端点地址');
|
|||
|
|
$table->string('model', 100)->comment('使用的模型名称');
|
|||
|
|
$table->decimal('temperature', 3, 2)->default(0.30)->comment('温度参数');
|
|||
|
|
$table->integer('max_tokens')->default(2048)->comment('最大 Token 数');
|
|||
|
|
$table->boolean('is_enabled')->default(true)->index()->comment('是否启用');
|
|||
|
|
$table->boolean('is_default')->default(false)->comment('是否为默认厂商');
|
|||
|
|
$table->integer('sort_order')->default(0)->comment('排序(故障转移时按此顺序)');
|
|||
|
|
$table->timestamps();
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// AI 使用日志表
|
|||
|
|
Schema::create('ai_usage_logs', function (Blueprint $table) {
|
|||
|
|
$table->id();
|
|||
|
|
$table->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete()->comment('使用者');
|
|||
|
|
$table->string('provider', 50)->comment('AI 厂商标识');
|
|||
|
|
$table->string('model', 100)->comment('使用的模型');
|
|||
|
|
$table->string('action', 50)->default('chatbot')->comment('操作类型');
|
|||
|
|
$table->integer('prompt_tokens')->default(0)->comment('输入 Token 数');
|
|||
|
|
$table->integer('completion_tokens')->default(0)->comment('输出 Token 数');
|
|||
|
|
$table->integer('total_tokens')->default(0)->comment('总 Token 数');
|
|||
|
|
$table->decimal('cost', 10, 6)->default(0)->comment('费用');
|
|||
|
|
$table->integer('response_time_ms')->default(0)->comment('响应时间(毫秒)');
|
|||
|
|
$table->boolean('success')->default(true)->comment('是否成功');
|
|||
|
|
$table->text('error_message')->nullable()->comment('错误信息');
|
|||
|
|
$table->timestamps();
|
|||
|
|
|
|||
|
|
// 索引
|
|||
|
|
$table->index(['provider', 'created_at']);
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 回滚迁移:删除 AI 相关的两张数据表
|
|||
|
|
*/
|
|||
|
|
public function down(): void
|
|||
|
|
{
|
|||
|
|
Schema::dropIfExists('ai_usage_logs');
|
|||
|
|
Schema::dropIfExists('ai_provider_configs');
|
|||
|
|
}
|
|||
|
|
};
|