feat: 删除 知识库-终端 关联, 简化 prompt 配置

This commit is contained in:
2026-03-23 15:27:06 +08:00
parent 81a22a2b54
commit 89af7c17f1
23 changed files with 102 additions and 1633 deletions

View File

@@ -1,91 +0,0 @@
<?php
namespace App\Filament\Actions;
use App\Services\PromptTemplateService;
use Filament\Actions\Action;
use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\ViewField;
use Filament\Support\Enums\MaxWidth;
class PreviewPromptAction
{
/**
* 创建预览提示词的Action
*
* @return Action
*/
public static function make(): Action
{
return Action::make('previewPrompt')
->label('预览提示词')
->icon('heroicon-o-eye')
->color('info')
->modalHeading('提示词预览')
->modalDescription('查看变量替换后的实际提示词内容')
->modalWidth(MaxWidth::FourExtraLarge)
->modalSubmitAction(false)
->modalCancelActionLabel('关闭')
->form(function ($record, $livewire) {
$service = app(PromptTemplateService::class);
// 获取当前表单数据
$data = $livewire->data ?? [];
$promptTemplate = $data['prompt']['prompt_template'] ?? '';
if (empty($promptTemplate)) {
return [
Placeholder::make('empty')
->label('')
->content('请先输入提示词模板内容'),
];
}
// 如果是编辑模式,使用记录的终端信息
// 如果是创建模式,使用表单数据创建临时终端对象
if ($record) {
$terminal = $record;
} else {
// 创建临时终端对象用于预览
$terminal = new \App\Models\Terminal([
'name' => $data['name'] ?? '新终端',
'code' => $data['code'] ?? 'TEMP-001',
'station_id' => $data['station_id'] ?? null,
]);
}
// 替换变量
$previewContent = $service->replaceVariables($promptTemplate, $terminal);
// 验证变量
$invalidVariables = $service->validateVariables($promptTemplate);
return [
Placeholder::make('original')
->label('原始模板')
->content(function () use ($promptTemplate) {
return view('filament.components.prompt-preview-original', [
'content' => $promptTemplate,
]);
}),
Placeholder::make('preview')
->label('预览结果')
->content(function () use ($previewContent) {
return view('filament.components.prompt-preview-result', [
'content' => $previewContent,
]);
}),
Placeholder::make('validation')
->label('变量验证')
->content(function () use ($invalidVariables) {
return view('filament.components.prompt-preview-validation', [
'invalidVariables' => $invalidVariables,
]);
})
->visible(fn () => !empty($invalidVariables)),
];
});
}
}

View File

@@ -131,44 +131,6 @@ class TerminalResource extends Resource
->columns(2)
->description('配置终端的语音唤醒能力'),
Forms\Components\Section::make('知识库关联')
->schema([
Forms\Components\Repeater::make('knowledgeBaseAssociations')
->label('关联知识库')
->relationship('knowledgeBases')
->schema([
Forms\Components\Select::make('id')
->label('知识库')
->options(\App\Models\KnowledgeBase::where('status', 'active')->pluck('name', 'id'))
->required()
->searchable()
->distinct()
->disableOptionsWhenSelectedInSiblingRepeaterItems()
->helperText('选择要关联的知识库'),
Forms\Components\TextInput::make('priority')
->label('优先级')
->numeric()
->default(0)
->required()
->minValue(0)
->helperText('数字越小优先级越高0为最高优先级'),
])
->columns(2)
->reorderable()
->reorderableWithButtons()
->addActionLabel('添加知识库')
->reorderableWithDragAndDrop(false)
->itemLabel(
fn(array $state): ?string =>
\App\Models\KnowledgeBase::find($state['id'])?->name ?? '未选择'
)
->collapsed()
->collapsible()
->helperText('可以关联多个知识库,并设置优先级。拖动或使用按钮调整顺序。'),
])
->description('配置终端可以访问的知识库及其优先级'),
Forms\Components\Section::make('指引关联')
->schema([
Forms\Components\Repeater::make('guideAssociations')
@@ -215,17 +177,13 @@ class TerminalResource extends Resource
->label('提示词模板')
->language('markdown')
->fontSize('14px')
->helperText('编辑AI提示词模板支持使用变量如 {user}, {station}, {time} 等')
->helperText('编辑AI提示词模板支持使用占位符 {station_id}, {user}, {time}由HMI端替换')
->placeholderText('请输入AI提示词模板...')
->disablePreview()
->columnSpan(2),
Forms\Components\Grid::make(1)
->schema([
Forms\Components\Placeholder::make('template_selector')
->label('模板库')
->content(fn() => view('filament.components.prompt-template-selector')),
Forms\Components\Placeholder::make('variable_helper')
->label('变量参考')
->content(fn() => view('filament.components.prompt-variable-helper')),
@@ -233,7 +191,7 @@ class TerminalResource extends Resource
->columnSpan(1),
]),
])
->description('配置终端的AI提示词模板用于指导AI助手的行为')
->description('配置终端的AI提示词模板占位符由HMI端替换')
->collapsible(),
Forms\Components\Section::make('状态信息')

View File

@@ -53,23 +53,6 @@ class ViewTerminal extends ViewRecord
->openUrlInNewTab(),
]),
Infolists\Components\Section::make('知识库关联')
->schema([
Infolists\Components\RepeatableEntry::make('knowledgeBases')
->label('关联的知识库')
->schema([
Infolists\Components\TextEntry::make('name')
->label('知识库名称'),
Infolists\Components\TextEntry::make('pivot.priority')
->label('优先级')
->badge()
->color('success'),
])
->columns(2)
->placeholder('未关联任何知识库'),
])
->collapsible(),
Infolists\Components\Section::make('AI提示词配置')
->schema([
Infolists\Components\TextEntry::make('prompt.prompt_template')

View File

@@ -3,7 +3,6 @@
namespace App\Filament\Widgets;
use App\Models\Terminal;
use App\Models\TerminalKnowledgeBase;
use App\Models\TerminalPrompt;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;
@@ -17,25 +16,17 @@ class TerminalStatsWidget extends BaseWidget
// 统计终端数据
$totalTerminals = Terminal::count();
$onlineTerminals = Terminal::where('is_online', true)->count();
// 统计知识库关联
$totalKnowledgeBases = TerminalKnowledgeBase::count();
// 统计提示词
$totalPrompts = TerminalPrompt::count();
return [
Stat::make('终端总数', $totalTerminals)
->description("{$onlineTerminals} 个在线")
->descriptionIcon('heroicon-m-computer-desktop')
->color('primary')
->url(route('filament.admin.resources.terminals.index')),
Stat::make('知识库关联', $totalKnowledgeBases)
->description('终端知识库配置数')
->descriptionIcon('heroicon-m-link')
->color('info'),
Stat::make('提示词配置', $totalPrompts)
->description('终端提示词总数')
->descriptionIcon('heroicon-m-chat-bubble-left-right')

View File

@@ -6,34 +6,26 @@ use App\Http\Controllers\Controller;
use App\Models\Guide;
use App\Models\GuidePage;
use App\Services\KnowledgeContextService;
use App\Services\PromptTemplateService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class TerminalApiController extends Controller
{
public function __construct(
private PromptTemplateService $promptService,
private KnowledgeContextService $knowledgeService,
) {}
/**
* GET /api/terminal/config
* 返回终端配置含渲染后的system prompt
* 返回终端配置
*/
public function config(Request $request): JsonResponse
{
$terminal = $request->attributes->get('terminal');
$terminal->load(['prompt', 'knowledgeBases']);
$terminal->load('prompt');
// 渲染system prompt
$systemPrompt = '';
if ($terminal->prompt && $terminal->prompt->prompt_template) {
$systemPrompt = $this->promptService->replaceVariables(
$terminal->prompt->prompt_template,
$terminal
);
}
// 返回原始提示词模板占位符由HMI端替换
$systemPrompt = $terminal->prompt?->prompt_template ?? '';
// 获取终端关联的已发布指引数量
$guideCount = $terminal->guides()->published()->count();
@@ -56,7 +48,7 @@ class TerminalApiController extends Controller
}
/**
* GET /api/terminal/knowledge?query=xxx
* GET /api/knowledge?query=xxx
* RAG知识搜索由AI tool_call触发
*/
public function knowledge(Request $request): JsonResponse
@@ -65,10 +57,7 @@ class TerminalApiController extends Controller
'query' => 'required|string|max:500',
]);
$terminal = $request->attributes->get('terminal');
$terminal->load('knowledgeBases');
$result = $this->knowledgeService->search($terminal, $request->input('query'));
$result = $this->knowledgeService->search($request->input('query'));
return response()->json($result);
}

View File

@@ -21,19 +21,6 @@ class KnowledgeBase extends Model
'status',
];
/**
* 获取关联的终端
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function terminals()
{
return $this->belongsToMany(Terminal::class, 'terminal_knowledge_bases')
->withPivot('priority')
->withTimestamps()
->orderBy('priority');
}
/**
* 获取知识库下的文档
*

View File

@@ -46,19 +46,6 @@ class Terminal extends Model
];
}
/**
* 获取终端关联的知识库
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function knowledgeBases()
{
return $this->belongsToMany(KnowledgeBase::class, 'terminal_knowledge_bases')
->withPivot('priority')
->withTimestamps()
->orderBy('priority');
}
/**
* 获取终端关联的指引
*

View File

@@ -1,52 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class TerminalKnowledgeBase extends Model
{
/**
* 可批量赋值的属性
*
* @var array<string>
*/
protected $fillable = [
'terminal_id',
'knowledge_base_id',
'priority',
];
/**
* 属性类型转换
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'priority' => 'integer',
];
}
/**
* 获取关联的终端
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function terminal(): BelongsTo
{
return $this->belongsTo(Terminal::class);
}
/**
* 获取关联的知识库
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function knowledgeBase(): BelongsTo
{
return $this->belongsTo(KnowledgeBase::class);
}
}

View File

@@ -2,8 +2,8 @@
namespace App\Services;
use App\Models\Terminal;
use App\Models\Document;
use App\Models\KnowledgeBase;
use Illuminate\Support\Facades\Log;
class KnowledgeContextService
@@ -12,15 +12,14 @@ class KnowledgeContextService
private const TOP_K = 5;
/**
* 搜索终端关联知识库中的文档
* 搜索所有知识库中的文档
*
* @param Terminal $terminal
* @param string $query
* @return array{context: string, sources: array}
*/
public function search(Terminal $terminal, string $query): array
public function search(string $query): array
{
$knowledgeBaseIds = $terminal->knowledgeBases->pluck('id')->toArray();
$knowledgeBaseIds = KnowledgeBase::where('status', 'active')->pluck('id')->toArray();
if (empty($knowledgeBaseIds)) {
return [
@@ -30,11 +29,18 @@ class KnowledgeContextService
}
try {
// 使用 Scout/Meilisearch 原生过滤(与 DocumentSearchService 一致)
$documents = Document::search($query)
->whereIn('knowledge_base_id', $knowledgeBaseIds)
->take(self::TOP_K)
->get();
// 使用 Scout/Meilisearch 搜索并获取排名分数
$rawResults = Document::search($query, function ($meilisearch, $query, $options) use ($knowledgeBaseIds) {
$options['showRankingScore'] = true;
$filter = collect($knowledgeBaseIds)
->map(fn($id) => "knowledge_base_id = {$id}")
->implode(' OR ');
$options['filter'] = $filter;
$options['limit'] = self::TOP_K;
return $meilisearch->search($query, $options);
})->raw();
$hits = $rawResults['hits'] ?? [];
} catch (\Exception $e) {
Log::warning('Knowledge search failed', [
'query' => $query,
@@ -47,17 +53,29 @@ class KnowledgeContextService
];
}
if ($documents->isEmpty()) {
if (empty($hits)) {
return [
'context' => '',
'sources' => [],
];
}
// 取出文档 ID 并加载 Eloquent 模型
$hitIds = collect($hits)->pluck('id')->toArray();
$documents = Document::whereIn('id', $hitIds)->get()->keyBy('id');
// 构建排名分数映射
$rankingScores = collect($hits)->pluck('_rankingScore', 'id');
$context = '';
$sources = [];
foreach ($documents as $document) {
foreach ($hits as $hit) {
$document = $documents->get($hit['id']);
if (!$document) {
continue;
}
$snippet = $this->extractSnippet($document);
if (mb_strlen($context) + mb_strlen($snippet) > self::MAX_CONTEXT_LENGTH) {
@@ -66,9 +84,9 @@ class KnowledgeContextService
$context .= $snippet . "\n\n";
$sources[] = [
'id' => $document->id,
'title' => $document->title,
'knowledge_base' => $document->knowledgeBase?->name,
'id' => 'kb-doc-' . str_pad($document->id, 3, '0', STR_PAD_LEFT),
'relevance' => round($rankingScores->get($document->id, 0), 2),
];
}

View File

@@ -1,168 +0,0 @@
<?php
namespace App\Services;
use App\Models\Terminal;
use Illuminate\Support\Facades\Auth;
class PromptTemplateService
{
/**
* 获取所有可用的模板
*
* @return array
*/
public function getTemplates(): array
{
return config('prompt_templates.templates', []);
}
/**
* 根据ID获取模板
*
* @param string $templateId
* @return array|null
*/
public function getTemplate(string $templateId): ?array
{
$templates = $this->getTemplates();
foreach ($templates as $template) {
if ($template['id'] === $templateId) {
return $template;
}
}
return null;
}
/**
* 应用模板到终端
*
* @param Terminal $terminal
* @param string $templateId
* @return string
*/
public function applyTemplate(Terminal $terminal, string $templateId): string
{
$template = $this->getTemplate($templateId);
if (!$template) {
throw new \InvalidArgumentException("模板不存在: {$templateId}");
}
return $template['content'];
}
/**
* 替换模板中的变量
*
* @param string $template
* @param Terminal $terminal
* @param array $additionalVars
* @return string
*/
public function replaceVariables(string $template, Terminal $terminal, array $additionalVars = []): string
{
$variables = $this->getVariableValues($terminal, $additionalVars);
foreach ($variables as $key => $value) {
// 处理数组类型的变量
if (is_array($value)) {
$value = implode(', ', $value);
}
$template = str_replace('{' . $key . '}', $value, $template);
}
return $template;
}
/**
* 获取变量的实际值
*
* @param Terminal $terminal
* @param array $additionalVars
* @return array
*/
public function getVariableValues(Terminal $terminal, array $additionalVars = []): array
{
$user = Auth::user();
$now = now();
$variables = [
'user' => $user?->name ?? '访客',
'user_id' => $user?->id ?? 0,
'user_role' => $user?->roles?->first()?->name ?? '未知',
'department' => $user?->department ?? '未知部门',
'station' => $terminal->station_id ? "工作站 {$terminal->station_id}" : '未绑定工作站',
'station_id' => $terminal->station_id ?? '未绑定',
'terminal_name' => $terminal->name,
'terminal_code' => $terminal->code,
'time' => $now->format('Y-m-d H:i:s'),
'date' => $now->format('Y-m-d'),
'time_only' => $now->format('H:i:s'),
'shift' => $this->getCurrentShift($now),
'knowledge_bases' => $terminal->knowledgeBases->pluck('name')->toArray(),
'company_name' => config('app.name', '公司名称'),
];
return array_merge($variables, $additionalVars);
}
/**
* 根据时间判断当前班次
*
* @param \Illuminate\Support\Carbon $time
* @return string
*/
protected function getCurrentShift($time): string
{
$hour = $time->hour;
if ($hour >= 8 && $hour < 16) {
return '早班';
} elseif ($hour >= 16 && $hour < 24) {
return '中班';
} else {
return '夜班';
}
}
/**
* 预览模板(替换变量后的结果)
*
* @param string $template
* @param Terminal $terminal
* @return string
*/
public function preview(string $template, Terminal $terminal): string
{
return $this->replaceVariables($template, $terminal);
}
/**
* 验证模板中的变量是否都是有效的
*
* @param string $template
* @return array 返回无效的变量列表
*/
public function validateVariables(string $template): array
{
$validVariables = array_column(config('prompt_variables.variables', []), 'name');
// 提取模板中的所有变量
preg_match_all('/\{([a-z_]+)\}/', $template, $matches);
$usedVariables = $matches[1] ?? [];
// 找出无效的变量
$invalidVariables = array_diff($usedVariables, $validVariables);
return array_values(array_unique($invalidVariables));
}
}