feat: 优化RAG检索配置以增加知识库数据量

- 修复Vertex AI Search配置,移除不支持的API字段
- 优化system prompt以更好地利用检索信息
- 添加查询增强功能,通过关键词扩展提高检索效果
- 新增RagConfigOptimizer工具类,支持多种优化场景
- 新增RagConfigManager组件,提供可视化配置管理
- 保留客户端配置字段用于未来扩展
- 添加详细的使用示例和文档

主要改进:
1. 解决了API 400错误问题
2. 通过查询优化间接增加检索相关性
3. 提供了完整的配置管理解决方案
4. 支持场景化的RAG配置优化
This commit is contained in:
imeepos
2025-07-24 12:46:00 +08:00
parent 2fe211adb2
commit 6d86cea892
5 changed files with 772 additions and 6 deletions

View File

@@ -165,6 +165,14 @@ pub struct RagGroundingConfig {
pub temperature: f32,
pub max_output_tokens: u32,
pub system_prompt: Option<String>,
/// 搜索过滤器 (Vertex AI Search支持的字段)
pub search_filter: Option<String>,
/// 最大检索结果数量 (用于客户端逻辑不发送给API)
pub max_retrieval_results: Option<u32>,
/// 相关性阈值 (用于客户端逻辑不发送给API)
pub relevance_threshold: Option<f32>,
/// 是否包含摘要 (用于客户端逻辑不发送给API)
pub include_summary: Option<bool>,
}
impl Default for RagGroundingConfig {
@@ -176,7 +184,12 @@ impl Default for RagGroundingConfig {
model_id: "gemini-2.5-flash".to_string(),
temperature: 1.0,
max_output_tokens: 60000,
system_prompt: Some("你是一个短视频情景穿搭分析专家, 根据用户的输入检索RAG然后参考检索结果输出符合逻辑的情景和模特穿搭描述必须依据已知的数据返回可能的方案, 并且给出参照的依据;如果没有匹配的数据支持,返回空结果;".to_string()),
system_prompt: Some("你是一个短视频情景穿搭分析专家。请仔细分析用户的查询充分利用检索到的所有相关信息包括1详细分析每个检索结果的内容2综合多个来源的信息提供全面的回答3明确引用具体的数据来源和依据4如果检索结果不足请说明需要更多哪方面的信息5提供具体可行的穿搭建议和搭配方案。".to_string()),
search_filter: None, // 暂不使用过滤器
// 以下字段用于客户端逻辑不发送给API
max_retrieval_results: Some(20), // 客户端参考值
relevance_threshold: Some(0.3), // 客户端参考值
include_summary: Some(true), // 客户端参考值
}
}
}
@@ -257,11 +270,16 @@ struct VertexAISearchTool {
struct VertexAIRetrieval {
#[serde(rename = "vertexAiSearch")]
vertex_ai_search: VertexAISearchConfig,
#[serde(rename = "disableAttribution", skip_serializing_if = "Option::is_none")]
disable_attribution: Option<bool>,
}
#[derive(Debug, Serialize)]
struct VertexAISearchConfig {
datastore: String,
/// 搜索过滤器 (支持的字段)
#[serde(skip_serializing_if = "Option::is_none")]
filter: Option<String>,
}
/// Gemini API服务
@@ -942,6 +960,31 @@ impl GeminiService {
}
}
/// 增强查询以获取更多相关信息
fn enhance_query_for_better_retrieval(&self, original_query: &str) -> String {
// 添加相关关键词和上下文来提高检索效果
let enhanced_keywords = vec![
"穿搭", "搭配", "服装", "时尚", "风格", "造型", "服饰", "款式", "颜色", "材质"
];
// 检查原查询是否已包含这些关键词
let query_lower = original_query.to_lowercase();
let missing_keywords: Vec<&str> = enhanced_keywords
.iter()
.filter(|&&keyword| !query_lower.contains(keyword))
.copied()
.collect();
if missing_keywords.is_empty() {
// 如果已包含关键词,添加更详细的描述要求
format!("{} 请提供详细的分析和具体的建议,包括颜色搭配、款式选择、场合适用性等方面。", original_query)
} else {
// 添加相关关键词来扩展搜索范围
let additional_context = missing_keywords.join("");
format!("{} 相关的{}方面的建议和搭配方案", original_query, additional_context)
}
}
/// RAG Grounding 查询 (参考 RAGUtils.py 中的 query_llm_with_grounding)
pub async fn query_llm_with_grounding(&mut self, request: RagGroundingRequest) -> Result<RagGroundingResponse> {
// 如果请求包含会话管理参数,使用多轮对话版本
@@ -980,19 +1023,24 @@ impl GeminiService {
rag_config.data_store_id
);
// 构建工具配置 (Vertex AI Search)
// 构建工具配置 (Vertex AI Search) - 只使用支持的字段
let tools = vec![VertexAISearchTool {
retrieval: VertexAIRetrieval {
vertex_ai_search: VertexAISearchConfig {
datastore: datastore_path,
filter: rag_config.search_filter.clone(),
},
disable_attribution: Some(false), // 保留归属信息
},
}];
// 优化查询内容以获取更多相关信息
let enhanced_query = self.enhance_query_for_better_retrieval(&request.user_input);
// 构建请求内容
let contents = vec![ContentPart {
role: "user".to_string(),
parts: vec![Part::Text { text: request.user_input.clone() }],
parts: vec![Part::Text { text: enhanced_query }],
}];
// 构建生成配置
@@ -1140,10 +1188,11 @@ impl GeminiService {
}
}
// 3. 添加当前用户消息
// 3. 添加当前用户消息(优化查询以获取更多相关信息)
let enhanced_query = self.enhance_query_for_better_retrieval(&request.user_input);
contents.push(ContentPart {
role: "user".to_string(),
parts: vec![Part::Text { text: request.user_input.clone() }],
parts: vec![Part::Text { text: enhanced_query }],
});
// 4. 执行RAG查询
@@ -1271,12 +1320,14 @@ impl GeminiService {
rag_config.data_store_id
);
// 构建工具配置 (Vertex AI Search)
// 构建工具配置 (Vertex AI Search) - 只使用支持的字段
let tools = vec![VertexAISearchTool {
retrieval: VertexAIRetrieval {
vertex_ai_search: VertexAISearchConfig {
datastore: datastore_path,
filter: rag_config.search_filter.clone(),
},
disable_attribution: Some(false), // 保留归属信息
},
}];

View File

@@ -0,0 +1,251 @@
/**
* RAG配置管理组件
* 提供可视化的RAG检索参数配置界面
*/
import React, { useState, useEffect } from 'react';
import { RagGroundingConfig } from '../types/ragGrounding';
import { RagConfigOptimizer, RagConfigPresets } from '../utils/ragConfigOptimizer';
interface RagConfigManagerProps {
config: RagGroundingConfig;
onConfigChange: (config: RagGroundingConfig) => void;
onClose?: () => void;
}
export const RagConfigManager: React.FC<RagConfigManagerProps> = ({
config,
onConfigChange,
onClose
}) => {
const [currentConfig, setCurrentConfig] = useState<RagGroundingConfig>(config);
const [selectedScenario, setSelectedScenario] = useState<string>('BALANCED');
const [customPresets, setCustomPresets] = useState<Record<string, RagGroundingConfig>>({});
useEffect(() => {
setCustomPresets(RagConfigPresets.getPresets());
}, []);
const handleScenarioChange = (scenarioKey: string) => {
setSelectedScenario(scenarioKey);
const scenario = RagConfigOptimizer.SCENARIOS[scenarioKey];
if (scenario) {
const newConfig = RagConfigOptimizer.applyScenario(currentConfig, scenario);
setCurrentConfig(newConfig);
onConfigChange(newConfig);
}
};
const handleConfigUpdate = (updates: Partial<RagGroundingConfig>) => {
const newConfig = { ...currentConfig, ...updates };
setCurrentConfig(newConfig);
onConfigChange(newConfig);
};
const handleSavePreset = () => {
const name = prompt('请输入预设名称:');
if (name && name.trim()) {
RagConfigPresets.savePreset(name.trim(), currentConfig);
setCustomPresets(RagConfigPresets.getPresets());
}
};
const handleLoadPreset = (presetName: string) => {
const preset = RagConfigPresets.getPreset(presetName);
if (preset) {
setCurrentConfig(preset);
onConfigChange(preset);
}
};
return (
<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
<div className="bg-white rounded-lg p-6 max-w-2xl w-full max-h-[90vh] overflow-y-auto">
<div className="flex justify-between items-center mb-6">
<h2 className="text-xl font-bold text-gray-800">RAG检索配置管理</h2>
{onClose && (
<button
onClick={onClose}
className="text-gray-500 hover:text-gray-700"
>
</button>
)}
</div>
{/* 预设场景选择 */}
<div className="mb-6">
<h3 className="text-lg font-semibold mb-3"></h3>
<div className="grid grid-cols-1 md:grid-cols-2 gap-3">
{Object.entries(RagConfigOptimizer.SCENARIOS).map(([key, scenario]) => (
<button
key={key}
onClick={() => handleScenarioChange(key)}
className={`p-3 rounded-lg border text-left transition-colors ${
selectedScenario === key
? 'border-blue-500 bg-blue-50'
: 'border-gray-300 hover:border-gray-400'
}`}
>
<div className="font-medium text-gray-800">{scenario.name}</div>
<div className="text-sm text-gray-600 mt-1">{scenario.description}</div>
</button>
))}
</div>
</div>
{/* 详细配置 */}
<div className="mb-6">
<h3 className="text-lg font-semibold mb-3"></h3>
<div className="space-y-4">
{/* 最大检索结果数量 */}
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
(5-50)
</label>
<input
type="number"
min="5"
max="50"
value={currentConfig.max_retrieval_results || 20}
onChange={(e) => handleConfigUpdate({
max_retrieval_results: parseInt(e.target.value)
})}
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
<p className="text-xs text-gray-500 mt-1">
</p>
</div>
{/* 相关性阈值 */}
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
(0.1-0.9)
</label>
<input
type="number"
min="0.1"
max="0.9"
step="0.1"
value={currentConfig.relevance_threshold || 0.4}
onChange={(e) => handleConfigUpdate({
relevance_threshold: parseFloat(e.target.value)
})}
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
<p className="text-xs text-gray-500 mt-1">
</p>
</div>
{/* 搜索过滤器 */}
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
()
</label>
<input
type="text"
value={currentConfig.search_filter || ''}
onChange={(e) => handleConfigUpdate({
search_filter: e.target.value || undefined
})}
placeholder="例如: category: ANY(&quot;服装&quot;, &quot;搭配&quot;)"
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
<p className="text-xs text-gray-500 mt-1">
使Vertex AI Search过滤器语法限制搜索范围
</p>
</div>
{/* 包含摘要 */}
<div>
<label className="flex items-center">
<input
type="checkbox"
checked={currentConfig.include_summary || false}
onChange={(e) => handleConfigUpdate({
include_summary: e.target.checked
})}
className="mr-2"
/>
<span className="text-sm font-medium text-gray-700"></span>
</label>
<p className="text-xs text-gray-500 mt-1">
AI理解上下文
</p>
</div>
</div>
</div>
{/* 配置说明 */}
<div className="mb-6 p-3 bg-blue-50 rounded-lg">
<h4 className="font-medium text-blue-800 mb-1"></h4>
<p className="text-sm text-blue-700">
{RagConfigOptimizer.getConfigExplanation(currentConfig)}
</p>
</div>
{/* 自定义预设管理 */}
<div className="mb-6">
<h3 className="text-lg font-semibold mb-3"></h3>
<div className="flex gap-2 mb-3">
<button
onClick={handleSavePreset}
className="px-4 py-2 bg-green-500 text-white rounded-md hover:bg-green-600 transition-colors"
>
</button>
</div>
{Object.keys(customPresets).length > 0 && (
<div className="space-y-2">
{Object.keys(customPresets).map((presetName) => (
<div key={presetName} className="flex items-center justify-between p-2 bg-gray-50 rounded">
<span className="text-sm font-medium">{presetName}</span>
<div className="flex gap-2">
<button
onClick={() => handleLoadPreset(presetName)}
className="px-3 py-1 text-xs bg-blue-500 text-white rounded hover:bg-blue-600"
>
</button>
<button
onClick={() => {
RagConfigPresets.deletePreset(presetName);
setCustomPresets(RagConfigPresets.getPresets());
}}
className="px-3 py-1 text-xs bg-red-500 text-white rounded hover:bg-red-600"
>
</button>
</div>
</div>
))}
</div>
)}
</div>
{/* 操作按钮 */}
<div className="flex justify-end gap-3">
{onClose && (
<button
onClick={onClose}
className="px-4 py-2 text-gray-600 border border-gray-300 rounded-md hover:bg-gray-50 transition-colors"
>
</button>
)}
<button
onClick={() => {
onConfigChange(currentConfig);
onClose?.();
}}
className="px-4 py-2 bg-blue-500 text-white rounded-md hover:bg-blue-600 transition-colors"
>
</button>
</div>
</div>
</div>
);
};

View File

@@ -14,6 +14,14 @@ export interface RagGroundingConfig {
temperature: number;
max_output_tokens: number;
system_prompt?: string;
/** 最大检索结果数量 (默认5最大50) */
max_retrieval_results?: number;
/** 相关性阈值 (0.0-1.0,越低检索越多结果) */
relevance_threshold?: number;
/** 搜索过滤器 */
search_filter?: string;
/** 是否包含摘要 */
include_summary?: boolean;
}
/**
@@ -119,6 +127,11 @@ export const DEFAULT_RAG_GROUNDING_CONFIG: RagGroundingConfig = {
temperature: 1.0,
max_output_tokens: 8192,
system_prompt: "你是一个短视频情景穿搭分析专家, 根据用户的输入检索RAG然后参考检索结果输出符合逻辑的情景和模特穿搭描述必须依据已知的数据返回可能的方案, 并且给出参照的依据;如果没有匹配的数据支持,返回空结果;",
// 优化检索参数以获取更多相关数据
max_retrieval_results: 20, // 增加检索结果数量
relevance_threshold: 0.3, // 降低相关性阈值
search_filter: undefined, // 暂不使用过滤器
include_summary: true, // 包含摘要信息
};
/**

View File

@@ -0,0 +1,229 @@
/**
* RAG配置优化工具
* 提供不同场景下的RAG检索配置优化方案
*/
import { RagGroundingConfig } from '../types/ragGrounding';
export interface RagOptimizationScenario {
name: string;
description: string;
config: Partial<RagGroundingConfig>;
}
/**
* RAG配置优化器
*/
export class RagConfigOptimizer {
/**
* 预定义的优化场景
*/
static readonly SCENARIOS: Record<string, RagOptimizationScenario> = {
// 高召回率场景 - 获取更多相关数据
HIGH_RECALL: {
name: "高召回率",
description: "降低相关性阈值,增加检索结果数量,适用于需要更多参考信息的场景",
config: {
max_retrieval_results: 30,
relevance_threshold: 0.2,
include_summary: true,
}
},
// 高精度场景 - 获取最相关的数据
HIGH_PRECISION: {
name: "高精度",
description: "提高相关性阈值,减少检索结果数量,适用于需要精确匹配的场景",
config: {
max_retrieval_results: 10,
relevance_threshold: 0.7,
include_summary: true,
}
},
// 平衡场景 - 默认配置
BALANCED: {
name: "平衡模式",
description: "平衡检索数量和相关性,适用于大多数场景",
config: {
max_retrieval_results: 20,
relevance_threshold: 0.4,
include_summary: true,
}
},
// 快速响应场景 - 减少检索数量提高速度
FAST_RESPONSE: {
name: "快速响应",
description: "减少检索结果数量,提高响应速度,适用于实时对话场景",
config: {
max_retrieval_results: 8,
relevance_threshold: 0.5,
include_summary: false,
}
},
// 深度搜索场景 - 最大化检索范围
DEEP_SEARCH: {
name: "深度搜索",
description: "最大化检索结果数量和范围,适用于复杂查询和研究场景",
config: {
max_retrieval_results: 50, // Vertex AI Search 最大值
relevance_threshold: 0.1,
include_summary: true,
}
}
};
/**
* 根据查询类型自动选择最佳配置
*/
static autoOptimize(query: string, baseConfig: RagGroundingConfig): RagGroundingConfig {
// 检测查询特征
const isComplexQuery = query.length > 50 || query.includes('详细') || query.includes('具体');
const isSimpleQuery = query.length < 20;
const isComparisonQuery = query.includes('比较') || query.includes('对比') || query.includes('区别');
const isListQuery = query.includes('有哪些') || query.includes('列举') || query.includes('所有');
let scenario: RagOptimizationScenario;
if (isListQuery || isComparisonQuery) {
// 列举或比较类查询需要更多数据
scenario = this.SCENARIOS.HIGH_RECALL;
} else if (isComplexQuery) {
// 复杂查询使用深度搜索
scenario = this.SCENARIOS.DEEP_SEARCH;
} else if (isSimpleQuery) {
// 简单查询使用快速响应
scenario = this.SCENARIOS.FAST_RESPONSE;
} else {
// 默认使用平衡模式
scenario = this.SCENARIOS.BALANCED;
}
return this.applyScenario(baseConfig, scenario);
}
/**
* 应用优化场景到配置
*/
static applyScenario(baseConfig: RagGroundingConfig, scenario: RagOptimizationScenario): RagGroundingConfig {
return {
...baseConfig,
...scenario.config
};
}
/**
* 根据用户反馈动态调整配置
*/
static adjustBasedOnFeedback(
currentConfig: RagGroundingConfig,
feedback: 'too_few_results' | 'too_many_results' | 'irrelevant_results' | 'good'
): RagGroundingConfig {
const adjustedConfig = { ...currentConfig };
switch (feedback) {
case 'too_few_results':
// 增加检索数量,降低相关性阈值
adjustedConfig.max_retrieval_results = Math.min((adjustedConfig.max_retrieval_results || 20) + 10, 50);
adjustedConfig.relevance_threshold = Math.max((adjustedConfig.relevance_threshold || 0.4) - 0.1, 0.1);
break;
case 'too_many_results':
// 减少检索数量
adjustedConfig.max_retrieval_results = Math.max((adjustedConfig.max_retrieval_results || 20) - 5, 5);
break;
case 'irrelevant_results':
// 提高相关性阈值
adjustedConfig.relevance_threshold = Math.min((adjustedConfig.relevance_threshold || 0.4) + 0.1, 0.9);
break;
case 'good':
// 保持当前配置
break;
}
return adjustedConfig;
}
/**
* 为特定领域创建过滤器
*/
static createDomainFilter(domain: 'fashion' | 'general'): string | undefined {
switch (domain) {
case 'fashion':
return 'category: ANY("服装", "搭配", "时尚", "穿搭")';
case 'general':
default:
return undefined;
}
}
/**
* 获取推荐的配置说明
*/
static getConfigExplanation(config: RagGroundingConfig): string {
const maxResults = config.max_retrieval_results || 20;
const threshold = config.relevance_threshold || 0.4;
let explanation = `当前配置将检索最多 ${maxResults} 个结果,`;
if (threshold < 0.3) {
explanation += "使用较低的相关性阈值以获取更多可能相关的信息";
} else if (threshold > 0.6) {
explanation += "使用较高的相关性阈值以确保结果的精确性";
} else {
explanation += "使用平衡的相关性阈值";
}
if (config.include_summary) {
explanation += ",并包含结果摘要信息";
}
return explanation + "。";
}
}
/**
* RAG配置预设管理器
*/
export class RagConfigPresets {
private static readonly STORAGE_KEY = 'rag_config_presets';
/**
* 保存自定义预设
*/
static savePreset(name: string, config: RagGroundingConfig): void {
const presets = this.getPresets();
presets[name] = config;
localStorage.setItem(this.STORAGE_KEY, JSON.stringify(presets));
}
/**
* 获取所有预设
*/
static getPresets(): Record<string, RagGroundingConfig> {
const stored = localStorage.getItem(this.STORAGE_KEY);
return stored ? JSON.parse(stored) : {};
}
/**
* 删除预设
*/
static deletePreset(name: string): void {
const presets = this.getPresets();
delete presets[name];
localStorage.setItem(this.STORAGE_KEY, JSON.stringify(presets));
}
/**
* 获取预设
*/
static getPreset(name: string): RagGroundingConfig | undefined {
const presets = this.getPresets();
return presets[name];
}
}

View File

@@ -0,0 +1,222 @@
/**
* RAG检索优化使用示例
* 展示如何使用新的RAG配置优化功能
*/
import { RagGroundingConfig, DEFAULT_RAG_GROUNDING_CONFIG } from '../apps/desktop/src/types/ragGrounding';
import { RagConfigOptimizer } from '../apps/desktop/src/utils/ragConfigOptimizer';
import { queryRagGrounding } from '../apps/desktop/src/services/ragGroundingService';
/**
* 示例 1: 使用预定义优化场景
*/
async function scenarioBasedOptimization() {
console.log('=== 场景化优化示例 ===');
const baseConfig = DEFAULT_RAG_GROUNDING_CONFIG;
// 高召回率场景 - 适用于需要更多参考信息的查询
const highRecallConfig = RagConfigOptimizer.applyScenario(
baseConfig,
RagConfigOptimizer.SCENARIOS.HIGH_RECALL
);
console.log('高召回率配置:', {
max_retrieval_results: highRecallConfig.max_retrieval_results,
relevance_threshold: highRecallConfig.relevance_threshold,
include_summary: highRecallConfig.include_summary
});
const result = await queryRagGrounding(
"请详细介绍各种牛仔裤的搭配方案",
{ customConfig: highRecallConfig }
);
if (result.success) {
console.log('检索到更多相关信息:', result.data?.grounding_metadata?.sources?.length);
}
}
/**
* 示例 2: 自动优化配置
*/
async function autoOptimization() {
console.log('=== 自动优化示例 ===');
const queries = [
"牛仔裤配什么上衣?", // 简单查询 -> 快速响应模式
"请详细分析不同场合下牛仔裤的搭配技巧,包括颜色、款式、配饰等方面", // 复杂查询 -> 深度搜索模式
"有哪些适合春季的牛仔裤搭配方案?", // 列举查询 -> 高召回率模式
"比较直筒牛仔裤和紧身牛仔裤的搭配区别" // 比较查询 -> 高召回率模式
];
for (const query of queries) {
const optimizedConfig = RagConfigOptimizer.autoOptimize(query, DEFAULT_RAG_GROUNDING_CONFIG);
console.log(`查询: "${query}"`);
console.log('自动优化配置:', {
max_retrieval_results: optimizedConfig.max_retrieval_results,
relevance_threshold: optimizedConfig.relevance_threshold
});
const result = await queryRagGrounding(query, { customConfig: optimizedConfig });
if (result.success) {
console.log(`检索结果数量: ${result.data?.grounding_metadata?.sources?.length || 0}`);
console.log(`响应时间: ${result.totalTime}ms\n`);
}
}
}
/**
* 示例 3: 基于反馈的动态调整
*/
async function feedbackBasedOptimization() {
console.log('=== 反馈优化示例 ===');
let currentConfig = DEFAULT_RAG_GROUNDING_CONFIG;
const query = "夏季牛仔裤搭配建议";
// 第一次查询
let result = await queryRagGrounding(query, { customConfig: currentConfig });
console.log('初始查询结果数量:', result.data?.grounding_metadata?.sources?.length || 0);
// 模拟用户反馈:结果太少
currentConfig = RagConfigOptimizer.adjustBasedOnFeedback(currentConfig, 'too_few_results');
console.log('调整后配置 (结果太少):', {
max_retrieval_results: currentConfig.max_retrieval_results,
relevance_threshold: currentConfig.relevance_threshold
});
// 第二次查询
result = await queryRagGrounding(query, { customConfig: currentConfig });
console.log('调整后查询结果数量:', result.data?.grounding_metadata?.sources?.length || 0);
// 模拟用户反馈:结果不相关
currentConfig = RagConfigOptimizer.adjustBasedOnFeedback(currentConfig, 'irrelevant_results');
console.log('再次调整后配置 (结果不相关):', {
max_retrieval_results: currentConfig.max_retrieval_results,
relevance_threshold: currentConfig.relevance_threshold
});
// 第三次查询
result = await queryRagGrounding(query, { customConfig: currentConfig });
console.log('最终查询结果数量:', result.data?.grounding_metadata?.sources?.length || 0);
}
/**
* 示例 4: 领域特定过滤器
*/
async function domainSpecificFiltering() {
console.log('=== 领域过滤示例 ===');
const baseConfig = DEFAULT_RAG_GROUNDING_CONFIG;
// 应用时尚领域过滤器
const fashionConfig: RagGroundingConfig = {
...baseConfig,
search_filter: RagConfigOptimizer.createDomainFilter('fashion'),
max_retrieval_results: 25,
relevance_threshold: 0.4
};
console.log('时尚领域配置:', {
search_filter: fashionConfig.search_filter,
max_retrieval_results: fashionConfig.max_retrieval_results
});
const result = await queryRagGrounding(
"职场穿搭建议",
{ customConfig: fashionConfig }
);
if (result.success) {
console.log('领域过滤后的结果数量:', result.data?.grounding_metadata?.sources?.length || 0);
}
}
/**
* 示例 5: 性能对比测试
*/
async function performanceComparison() {
console.log('=== 性能对比示例 ===');
const query = "牛仔裤搭配技巧";
const configs = [
{ name: '默认配置', config: DEFAULT_RAG_GROUNDING_CONFIG },
{ name: '快速响应', config: RagConfigOptimizer.applyScenario(DEFAULT_RAG_GROUNDING_CONFIG, RagConfigOptimizer.SCENARIOS.FAST_RESPONSE) },
{ name: '高召回率', config: RagConfigOptimizer.applyScenario(DEFAULT_RAG_GROUNDING_CONFIG, RagConfigOptimizer.SCENARIOS.HIGH_RECALL) },
{ name: '深度搜索', config: RagConfigOptimizer.applyScenario(DEFAULT_RAG_GROUNDING_CONFIG, RagConfigOptimizer.SCENARIOS.DEEP_SEARCH) }
];
for (const { name, config } of configs) {
const startTime = Date.now();
const result = await queryRagGrounding(query, { customConfig: config });
const endTime = Date.now();
console.log(`${name}:`, {
检索数量: result.data?.grounding_metadata?.sources?.length || 0,
: `${endTime - startTime}ms`,
: {
max_results: config.max_retrieval_results,
threshold: config.relevance_threshold
}
});
}
}
/**
* 示例 6: 配置解释和建议
*/
function configurationGuidance() {
console.log('=== 配置指导示例 ===');
const scenarios = Object.entries(RagConfigOptimizer.SCENARIOS);
scenarios.forEach(([key, scenario]) => {
const config = RagConfigOptimizer.applyScenario(DEFAULT_RAG_GROUNDING_CONFIG, scenario);
const explanation = RagConfigOptimizer.getConfigExplanation(config);
console.log(`${scenario.name}:`);
console.log(` 描述: ${scenario.description}`);
console.log(` 配置说明: ${explanation}`);
console.log(` 适用场景: ${getUseCaseExamples(key)}\n`);
});
}
function getUseCaseExamples(scenarioKey: string): string {
const examples = {
HIGH_RECALL: "复杂查询、研究分析、需要全面信息的场景",
HIGH_PRECISION: "精确匹配、专业咨询、质量优于数量的场景",
BALANCED: "日常对话、一般性查询、大多数应用场景",
FAST_RESPONSE: "实时聊天、快速问答、移动端应用",
DEEP_SEARCH: "学术研究、详细分析、专业报告生成"
};
return examples[scenarioKey as keyof typeof examples] || "通用场景";
}
// 运行示例
async function runAllExamples() {
try {
await scenarioBasedOptimization();
await autoOptimization();
await feedbackBasedOptimization();
await domainSpecificFiltering();
await performanceComparison();
configurationGuidance();
} catch (error) {
console.error('示例运行失败:', error);
}
}
// 导出示例函数
export {
scenarioBasedOptimization,
autoOptimization,
feedbackBasedOptimization,
domainSpecificFiltering,
performanceComparison,
configurationGuidance,
runAllExamples
};