AI 模型配置
本文介绍如何配置和选择 AI 模型,以及不同模型的特点和使用场景。
模型概览
OpenAI 模型
| 模型 | 能力 | 速度 | 成本 | 适用场景 |
|---|---|---|---|---|
| gpt-4 | ⭐⭐⭐⭐⭐ | ⭐⭐ | ⭐⭐⭐⭐⭐ | 高质量内容、复杂任务 |
| gpt-4-turbo | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐⭐⭐ | 长文本、复杂推理 |
| gpt-3.5-turbo | ⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ | ⭐⭐ | 日常内容生成 |
通义千问模型
| 模型 | 能力 | 速度 | 成本 | 适用场景 |
|---|---|---|---|---|
| qwen-max | ⭐⭐⭐⭐⭐ | ⭐⭐ | ⭐⭐⭐⭐ | 高质量创作 |
| qwen-plus | ⭐⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐⭐ | 专业内容 |
| qwen-turbo | ⭐⭐⭐ | ⭐⭐⭐⭐⭐ | ⭐⭐ | 快速生成 |
文心一言模型
| 模型 | 能力 | 速度 | 成本 | 适用场景 |
|---|---|---|---|---|
| ernie-bot-4 | ⭐⭐⭐⭐⭐ | ⭐⭐ | ⭐⭐⭐⭐ | 专业写作 |
| ernie-bot | ⭐⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐⭐ | 通用创作 |
| ernie-bot-turbo | ⭐⭐⭐ | ⭐⭐⭐⭐⭐ | ⭐⭐ | 快速生成 |
模型配置
基础配置
在 config/ai.php 中配置模型:
return [
'models' => [
'openai' => [
'gpt-4' => [
'name' => 'GPT-4',
'max_tokens' => 8192,
'temperature' => 0.7,
'cost_per_1k_tokens' => 0.03,
],
'gpt-3.5-turbo' => [
'name' => 'GPT-3.5 Turbo',
'max_tokens' => 4096,
'temperature' => 0.7,
'cost_per_1k_tokens' => 0.002,
],
],
'qwen' => [
'qwen-max' => [
'name' => '通义千问 Max',
'max_tokens' => 8192,
],
'qwen-plus' => [
'name' => '通义千问 Plus',
'max_tokens' => 6144,
],
],
],
];
模型参数
max_tokens
生成内容的最大长度:
'max_tokens' => 2000, // 约 1500 汉字
temperature
控制创造性(0-1):
0.0-0.3: 精确、一致性高,适合事实性内容0.4-0.7: 平衡,适合大多数场景0.8-1.0: 创造性强,适合创意写作
'temperature' => 0.7,
top_p
采样阈值(0-1):
'top_p' => 0.9, // 保留概率最高的 90% 词汇
frequency_penalty
频率惩罚(-2.0 到 2.0):
'frequency_penalty' => 0.5, // 减少重复词语
presence_penalty
存在惩罚(-2.0 到 2.0):
'presence_penalty' => 0.5, // 鼓励话题多样性
模型选择策略
根据内容类型选择
新闻资讯
[
'model' => 'gpt-3.5-turbo',
'temperature' => 0.3, // 事实准确
]
创意文案
[
'model' => 'gpt-4',
'temperature' => 0.8, // 创造性强
]
技术文档
[
'model' => 'gpt-4',
'temperature' => 0.2, // 精确专业
]
营销内容
[
'model' => 'qwen-plus',
'temperature' => 0.7, // 吸引力和准确性平衡
]
根据长度选择
短文本(< 500字)
- gpt-3.5-turbo
- qwen-turbo
- ernie-bot-turbo
中等文本(500-2000字)
- gpt-4
- qwen-plus
- ernie-bot
长文本(> 2000字)
- gpt-4-turbo
- qwen-max
- ernie-bot-4
根据预算选择
经济型
'default_model' => 'gpt-3.5-turbo', // 性价比高
标准型
'default_model' => 'qwen-plus', // 平衡性能和成本
高端型
'default_model' => 'gpt-4', // 最高质量
动态模型选择
根据用户等级
class AIService
{
public function selectModel($user)
{
$models = [
'free' => 'gpt-3.5-turbo',
'basic' => 'qwen-plus',
'premium' => 'gpt-4',
];
return $models[$user->level] ?? 'gpt-3.5-turbo';
}
}
根据内容长度
public function selectModelByLength($targetLength)
{
if ($targetLength < 500) {
return 'gpt-3.5-turbo';
} elseif ($targetLength < 2000) {
return 'qwen-plus';
} else {
return 'gpt-4-turbo';
}
}
根据任务复杂度
public function selectModelByComplexity($task)
{
$complexityMap = [
'simple' => 'gpt-3.5-turbo',
'medium' => 'qwen-plus',
'complex' => 'gpt-4',
];
$complexity = $this->analyzeComplexity($task);
return $complexityMap[$complexity];
}
模型微调
OpenAI Fine-tuning
// 准备训练数据
$trainingData = [
['prompt' => '写一篇关于...', 'completion' => '...'],
// 更多示例
];
// 创建微调任务
$fineTune = $ai->createFineTune([
'training_file' => $fileId,
'model' => 'gpt-3.5-turbo',
]);
// 使用微调模型
$content = $ai->generate([
'model' => 'ft:gpt-3.5-turbo:xxx',
'prompt' => '...',
]);
提示词模板
自定义模型行为:
'templates' => [
'news' => [
'system' => '你是一名专业的新闻编辑,擅长撰写客观、准确的新闻报道。',
'temperature' => 0.3,
],
'creative' => [
'system' => '你是一名富有创意的文案作家,擅长创作吸引人的营销内容。',
'temperature' => 0.8,
],
],
模型性能对比
质量测试
测试不同模型生成同一主题的文章:
php think ai:benchmark \
--models=gpt-4,gpt-3.5-turbo,qwen-plus \
--prompt="写一篇关于人工智能的文章" \
--count=10
速度测试
php think ai:speed-test \
--models=all \
--iterations=100
成本分析
查看不同模型的成本:
php think ai:cost-analysis \
--period=30d
模型切换
平滑切换
实现模型降级策略:
class AIService
{
protected $fallbackModels = [
'gpt-4' => 'gpt-3.5-turbo',
'qwen-max' => 'qwen-plus',
];
public function generateWithFallback($params)
{
$model = $params['model'];
try {
return $this->generate($params);
} catch (QuotaExceededException $e) {
// 切换到降级模型
if (isset($this->fallbackModels[$model])) {
$params['model'] = $this->fallbackModels[$model];
return $this->generate($params);
}
throw $e;
}
}
}
A/B 测试
对比不同模型效果:
class ABTestService
{
public function test()
{
$users = User::random(1000);
foreach ($users as $user) {
$model = $user->id % 2 == 0 ? 'gpt-4' : 'gpt-3.5-turbo';
$user->assigned_model = $model;
$user->save();
}
}
public function analyze()
{
// 分析用户满意度、内容质量等指标
}
}
监控与优化
使用统计
后台查看各模型使用情况:
- 请求次数
- Token 消耗
- 平均响应时间
- 成功率
成本优化
'optimization' => [
// 缓存相似请求
'cache_enabled' => true,
'cache_ttl' => 3600,
// Token 限制
'max_tokens_per_request' => 2000,
// 批量处理
'batch_size' => 10,
],
质量监控
// 记录生成质量评分
Log::info('AI 生成完成', [
'model' => $model,
'quality_score' => $this->evaluateQuality($content),
'tokens_used' => $tokensUsed,
'response_time' => $responseTime,
]);
最佳实践
模型选择原则
- 优先选择性价比高的模型
- 根据具体场景调整参数
- 定期评估和优化
- 设置合理的降级策略
参数调优
// 事实性内容
'temperature' => 0.2,
'top_p' => 0.9,
'frequency_penalty' => 0,
'presence_penalty' => 0,
// 创意内容
'temperature' => 0.8,
'top_p' => 0.95,
'frequency_penalty' => 0.5,
'presence_penalty' => 0.5,
成本控制
'limits' => [
'daily_budget' => 100, // 每日预算(元)
'max_tokens_per_user' => 10000, // 单用户限制
'alert_threshold' => 80, // 预警阈值(%)
],
故障排查
模型不可用
// 检查模型状态
$status = $ai->checkModelStatus('gpt-4');
if (!$status['available']) {
Log::warning('模型不可用', [
'model' => 'gpt-4',
'reason' => $status['reason'],
]);
// 切换到备用模型
$model = $this->fallbackModels['gpt-4'];
}
质量问题
如果生成质量不佳:
- 调整 temperature(降低)
- 优化提示词
- 增加示例
- 切换到更强的模型
速度问题
如果响应太慢:
- 减少 max_tokens
- 使用更快的模型
- 启用缓存
- 批量处理
