651 lines
23 KiB
PHP
651 lines
23 KiB
PHP
<?php
|
||
|
||
namespace app\api\logic\tcm;
|
||
|
||
use app\common\model\tcm\BloodRecord;
|
||
use app\common\model\tcm\Diagnosis;
|
||
use app\common\service\AiChatService;
|
||
use think\facade\Cache;
|
||
|
||
/**
|
||
* 日常护理 - AI 饮食建议(基于霍大夫升糖指数)
|
||
*/
|
||
class DailyDietAiLogic
|
||
{
|
||
private const CACHE_TTL = 86400;
|
||
|
||
/** 单次 AI 请求超时(秒),避免接口长时间挂起 */
|
||
private const AI_TIMEOUT_SEC = 12;
|
||
|
||
/** 流式 AI 请求超时(秒) */
|
||
private const AI_STREAM_TIMEOUT_SEC = 18;
|
||
|
||
/**
|
||
* 今日推荐饮食(按诊单+日期缓存)
|
||
*
|
||
* @return array{ok:bool,error?:string,data?:array}
|
||
*/
|
||
public static function getDailyRecommend(int $diagnosisId, bool $refresh = false): array
|
||
{
|
||
if ($diagnosisId <= 0) {
|
||
return ['ok' => false, 'error' => '缺少就诊卡'];
|
||
}
|
||
|
||
$cacheKey = self::recommendCacheKey($diagnosisId);
|
||
if (!$refresh) {
|
||
$cached = Cache::get($cacheKey);
|
||
if (is_array($cached) && !empty($cached['breakfast'])) {
|
||
$cached['cached'] = true;
|
||
return ['ok' => true, 'data' => $cached];
|
||
}
|
||
}
|
||
|
||
$context = self::buildPatientContext($diagnosisId);
|
||
if (!$context['ok']) {
|
||
return ['ok' => false, 'error' => $context['error'] ?? '获取档案失败'];
|
||
}
|
||
|
||
$data = null;
|
||
if (AiChatService::isEnabled()) {
|
||
$raw = self::aiDailyRecommend($context['data'], $refresh);
|
||
if (is_array($raw) && !empty($raw['breakfast'])) {
|
||
$data = self::finalizeAiRecommendPlan($raw);
|
||
}
|
||
}
|
||
if (!$data) {
|
||
$tplIdx = self::resolveFallbackTemplateIndex($diagnosisId, $refresh);
|
||
$data = GiKnowledge::fallbackDailyMeals($diagnosisId, $tplIdx);
|
||
$data['disclaimer'] = '按升糖指数与农家饭硬性规则推荐(午餐≥2样蛋白、淀粉不重复)。';
|
||
}
|
||
|
||
$data['date'] = date('Y-m-d');
|
||
$data['cached'] = false;
|
||
Cache::set($cacheKey, $data, self::CACHE_TTL);
|
||
|
||
return ['ok' => true, 'data' => $data];
|
||
}
|
||
|
||
/**
|
||
* 询问某食物能不能吃
|
||
*
|
||
* @return array{ok:bool,error?:string,data?:array}
|
||
*/
|
||
public static function askFood(int $diagnosisId, string $question): array
|
||
{
|
||
$question = trim($question);
|
||
if ($question === '') {
|
||
return ['ok' => false, 'error' => '请输入想咨询的食物'];
|
||
}
|
||
if (mb_strlen($question) > 80) {
|
||
return ['ok' => false, 'error' => '问题过长,请简短描述'];
|
||
}
|
||
|
||
$cacheKey = 'daily_diet_ai_ask:' . md5($diagnosisId . ':' . mb_strtolower($question));
|
||
$cached = Cache::get($cacheKey);
|
||
if (is_array($cached) && !empty($cached['advice'])) {
|
||
$cached['cached'] = true;
|
||
return ['ok' => true, 'data' => $cached];
|
||
}
|
||
|
||
$context = self::buildPatientContext($diagnosisId);
|
||
$data = null;
|
||
|
||
if (AiChatService::isEnabled()) {
|
||
$data = self::aiAskFood($context['data'] ?? [], $question);
|
||
}
|
||
if (!$data) {
|
||
$data = GiKnowledge::fallbackAsk($question);
|
||
}
|
||
|
||
$data['question'] = $question;
|
||
$data['disclaimer'] = '仅供参考,不能替代医嘱。';
|
||
$data['cached'] = false;
|
||
Cache::set($cacheKey, $data, 3600);
|
||
|
||
return ['ok' => true, 'data' => $data];
|
||
}
|
||
|
||
/**
|
||
* 流式:今日饮食推荐(SSE delta + done)
|
||
*
|
||
* @param callable(string, array<string, mixed>):void $emit event: delta|done|error
|
||
*/
|
||
public static function streamDailyRecommend(int $diagnosisId, bool $refresh, callable $emit): void
|
||
{
|
||
if ($diagnosisId <= 0) {
|
||
$emit('error', ['message' => '缺少就诊卡']);
|
||
return;
|
||
}
|
||
|
||
$cacheKey = self::recommendCacheKey($diagnosisId);
|
||
if (!$refresh) {
|
||
$cached = Cache::get($cacheKey);
|
||
if (is_array($cached) && !empty($cached['breakfast'])) {
|
||
$cached['cached'] = true;
|
||
$emit('done', $cached);
|
||
return;
|
||
}
|
||
}
|
||
|
||
$context = self::buildPatientContext($diagnosisId);
|
||
if (!$context['ok']) {
|
||
$emit('error', ['message' => $context['error'] ?? '获取档案失败']);
|
||
return;
|
||
}
|
||
|
||
$data = null;
|
||
$fullText = '';
|
||
|
||
if (AiChatService::isEnabled()) {
|
||
[$system, $user] = self::buildRecommendStreamPrompts($context['data'], $refresh);
|
||
$streamRes = AiChatService::streamChat([
|
||
['role' => 'system', 'content' => $system],
|
||
['role' => 'user', 'content' => $user],
|
||
], static function (string $delta) use (&$fullText, $emit): void {
|
||
$fullText .= $delta;
|
||
$emit('delta', ['text' => $delta, 'buffer' => $fullText]);
|
||
}, [
|
||
'temperature' => $refresh ? 0.78 : 0.35,
|
||
'max_tokens' => 400,
|
||
'timeout' => self::AI_STREAM_TIMEOUT_SEC,
|
||
]);
|
||
|
||
if ($streamRes['ok'] && $fullText !== '') {
|
||
$parsed = self::parseRecommendPlainText($fullText);
|
||
if ($parsed && !empty($parsed['breakfast'])) {
|
||
$data = self::finalizeAiRecommendPlan($parsed);
|
||
}
|
||
}
|
||
}
|
||
|
||
if (!$data) {
|
||
$tplIdx = self::resolveFallbackTemplateIndex($diagnosisId, $refresh);
|
||
$data = GiKnowledge::fallbackDailyMeals($diagnosisId, $tplIdx);
|
||
$data['source'] = 'rule';
|
||
$data['disclaimer'] = '按升糖指数与农家饭硬性规则推荐(午餐≥2样蛋白、淀粉不重复)。';
|
||
}
|
||
|
||
$data['date'] = date('Y-m-d');
|
||
$data['cached'] = false;
|
||
Cache::set($cacheKey, $data, self::CACHE_TTL);
|
||
$emit('done', $data);
|
||
}
|
||
|
||
/**
|
||
* 流式:能不能吃(SSE delta + done)
|
||
*
|
||
* @param callable(string, array<string, mixed>):void $emit event: delta|done|error
|
||
*/
|
||
public static function streamAskFood(int $diagnosisId, string $question, callable $emit): void
|
||
{
|
||
$question = trim($question);
|
||
if ($question === '') {
|
||
$emit('error', ['message' => '请输入想咨询的食物']);
|
||
return;
|
||
}
|
||
if (mb_strlen($question) > 80) {
|
||
$emit('error', ['message' => '问题过长,请简短描述']);
|
||
return;
|
||
}
|
||
|
||
$cacheKey = 'daily_diet_ai_ask:' . md5($diagnosisId . ':' . mb_strtolower($question));
|
||
$cached = Cache::get($cacheKey);
|
||
if (is_array($cached) && !empty($cached['advice'])) {
|
||
$cached['cached'] = true;
|
||
$emit('done', $cached);
|
||
return;
|
||
}
|
||
|
||
$context = self::buildPatientContext($diagnosisId);
|
||
|
||
$data = null;
|
||
$fullText = '';
|
||
|
||
if (AiChatService::isEnabled()) {
|
||
[$system, $user] = self::buildAskStreamPrompts($context['data'] ?? [], $question);
|
||
$streamRes = AiChatService::streamChat([
|
||
['role' => 'system', 'content' => $system],
|
||
['role' => 'user', 'content' => $user],
|
||
], static function (string $delta) use (&$fullText, $emit): void {
|
||
$fullText .= $delta;
|
||
$emit('delta', ['text' => $delta, 'advice' => $fullText]);
|
||
}, [
|
||
'temperature' => 0.3,
|
||
'max_tokens' => 200,
|
||
'timeout' => self::AI_STREAM_TIMEOUT_SEC,
|
||
]);
|
||
|
||
if ($streamRes['ok'] && trim($fullText) !== '') {
|
||
$data = self::finalizeAskFromText($context['data'] ?? [], $question, trim($fullText));
|
||
}
|
||
}
|
||
|
||
if (!$data) {
|
||
$data = GiKnowledge::fallbackAsk($question);
|
||
}
|
||
|
||
$data['question'] = $question;
|
||
$data['disclaimer'] = '仅供参考,不能替代医嘱。';
|
||
$data['cached'] = false;
|
||
Cache::set($cacheKey, $data, 3600);
|
||
$emit('done', $data);
|
||
}
|
||
|
||
private static function recommendCacheKey(int $diagnosisId): string
|
||
{
|
||
return 'daily_diet_ai_rec_v2:' . $diagnosisId . ':' . date('Y-m-d');
|
||
}
|
||
|
||
/** 换一换时轮换规则模板下标(与 GiKnowledge::$mealTemplates 数量一致) */
|
||
private static function resolveFallbackTemplateIndex(int $diagnosisId, bool $refresh): int
|
||
{
|
||
$count = 6;
|
||
$cacheKey = 'daily_diet_ai_tpl_idx:' . $diagnosisId . ':' . date('Y-m-d');
|
||
$defaultIdx = abs(crc32(date('Y-m-d') . ':' . $diagnosisId)) % $count;
|
||
|
||
if (!$refresh) {
|
||
Cache::set($cacheKey, $defaultIdx, self::CACHE_TTL);
|
||
return $defaultIdx;
|
||
}
|
||
|
||
$last = Cache::get($cacheKey);
|
||
if ($last === null || $last === false) {
|
||
$last = $defaultIdx;
|
||
}
|
||
$idx = (((int) $last) + 1) % $count;
|
||
Cache::set($cacheKey, $idx, self::CACHE_TTL);
|
||
|
||
return $idx;
|
||
}
|
||
|
||
/**
|
||
* @return array{ok:bool,error?:string,data?:array}
|
||
*/
|
||
private static function buildPatientContext(int $diagnosisId): array
|
||
{
|
||
$diagnosis = Diagnosis::where('id', $diagnosisId)->where('delete_time', null)->find();
|
||
if (!$diagnosis) {
|
||
return ['ok' => false, 'error' => '就诊卡不存在'];
|
||
}
|
||
|
||
$row = $diagnosis->toArray();
|
||
$gender = (int) ($row['gender'] ?? 0);
|
||
$genderText = $gender === 1 ? '男' : ($gender === 0 ? '女' : '未知');
|
||
|
||
$since = strtotime('-7 days 00:00:00');
|
||
$bloodList = BloodRecord::where('diagnosis_id', $diagnosisId)
|
||
->where('record_date', '>=', $since)
|
||
->where('delete_time', null)
|
||
->order('record_date', 'desc')
|
||
->limit(7)
|
||
->field('record_date,fasting_blood_sugar,postprandial_blood_sugar,other_blood_sugar')
|
||
->select()
|
||
->toArray();
|
||
|
||
$bloodSummary = [];
|
||
foreach ($bloodList as $b) {
|
||
$date = !empty($b['record_date']) ? date('Y-m-d', (int) $b['record_date']) : '';
|
||
$parts = [];
|
||
if ($b['fasting_blood_sugar'] !== null && $b['fasting_blood_sugar'] !== '') {
|
||
$parts[] = '空腹' . $b['fasting_blood_sugar'];
|
||
}
|
||
if ($b['postprandial_blood_sugar'] !== null && $b['postprandial_blood_sugar'] !== '') {
|
||
$parts[] = '餐后' . $b['postprandial_blood_sugar'];
|
||
}
|
||
if ($b['other_blood_sugar'] !== null && $b['other_blood_sugar'] !== '') {
|
||
$parts[] = '其他' . $b['other_blood_sugar'];
|
||
}
|
||
if ($date && $parts) {
|
||
$bloodSummary[] = $date . ':' . implode(',', $parts) . ' mmol/L';
|
||
}
|
||
}
|
||
|
||
return [
|
||
'ok' => true,
|
||
'data' => [
|
||
'patient_name' => (string) ($row['patient_name'] ?? ''),
|
||
'age' => (int) ($row['age'] ?? 0),
|
||
'gender_text' => $genderText,
|
||
'blood_recent' => $bloodSummary,
|
||
'today' => date('Y-m-d'),
|
||
],
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @return array{0:string,1:string}
|
||
*/
|
||
private static function buildRecommendPrompts(array $context, bool $refresh = false): array
|
||
{
|
||
$bloodLine = empty($context['blood_recent'])
|
||
? '近7日无血糖记录'
|
||
: implode(';', $context['blood_recent']);
|
||
|
||
$system = <<<SYS
|
||
你是村里懂糖尿病饮食的大夫助手,依据「霍大夫升糖指数(GI)」给农村老人推荐一日三餐。输出必须严格可执行。
|
||
|
||
硬性规则(违反即不合格):
|
||
1. 午餐至少 2 样蛋白质(鱼/鸡鸭/瘦肉/蛋/豆腐/豆干),且午餐蛋白种类数 > 早餐 > 晚餐。
|
||
2. 淀粉定义:粥/饭/面/饼/窝头/薯/南瓜。每餐最多 1 种淀粉;早中晚 3 种淀粉必须互不相同。
|
||
3. 严禁:白馒头、油条、粘豆包、糯米饭、西瓜、含糖饮料、白稀饭(杂面窝头、玉米碴粥除外)。
|
||
4. 低 GI 优先;农村家常;禁止轻食/沙拉/藜麦等词。
|
||
5. lunch 字段写法:蛋白菜放前面,淀粉(如有)放最后且注明「小半碗/一个」。
|
||
6. 只输出 JSON:
|
||
{"breakfast":"...","lunch":"...","dinner":"...","tips":"...","avoid":["...","..."]}
|
||
SYS;
|
||
|
||
$user = sprintf(
|
||
"患者:%s,%s,%s岁。日期:%s。近7日血糖:%s。\n\nGI知识:\n%s\n\n农家饭参考:\n%s\n\n请输出严格符合硬性规则的三餐 JSON。",
|
||
$context['patient_name'] ?: '大爷大妈',
|
||
$context['gender_text'] ?? '',
|
||
$context['age'] ?? 0,
|
||
$context['today'] ?? date('Y-m-d'),
|
||
$bloodLine,
|
||
GiKnowledge::summaryText(),
|
||
GiKnowledge::ruralMealHints()
|
||
);
|
||
|
||
if ($refresh) {
|
||
$user .= "\n\n用户点了「换一换」,请给一套完全不同的农家三餐,编号" . substr(md5((string) microtime(true)), 0, 8) . '。';
|
||
}
|
||
|
||
return [$system, $user];
|
||
}
|
||
|
||
/**
|
||
* 流式三餐推荐:纯文本格式,便于逐字输出
|
||
*
|
||
* @return array{0:string,1:string}
|
||
*/
|
||
private static function buildRecommendStreamPrompts(array $context, bool $refresh = false): array
|
||
{
|
||
$bloodLine = empty($context['blood_recent'])
|
||
? '近7日无血糖记录'
|
||
: implode(';', $context['blood_recent']);
|
||
|
||
$system = <<<SYS
|
||
你是村里懂糖尿病饮食的大夫助手,依据「霍大夫升糖指数(GI)」给农村老人推荐一日三餐。
|
||
|
||
硬性规则:
|
||
1. 午餐至少 2 样蛋白质(鱼/鸡鸭/瘦肉/蛋/豆腐/豆干),且午餐蛋白 > 早餐 > 晚餐。
|
||
2. 淀粉(粥/饭/面/饼/窝头/薯/南瓜)每餐最多 1 种,早中晚互不相同。
|
||
3. 严禁白馒头、油条、粘豆包、糯米饭、西瓜、含糖饮料。
|
||
4. 农村家常饭,禁止轻食/沙拉/藜麦。
|
||
|
||
严格按下面 5 行格式输出,不要 JSON、不要 markdown、不要多余解释:
|
||
早餐:(具体食物)
|
||
午餐:(具体食物,蛋白放前)
|
||
晚餐:(具体食物)
|
||
提示:(一句土话提醒)
|
||
少碰:(用顿号隔开,如:白馒头、油条)
|
||
SYS;
|
||
|
||
$user = sprintf(
|
||
"患者:%s,%s,%s岁。日期:%s。近7日血糖:%s。\n\nGI摘要:\n%s",
|
||
$context['patient_name'] ?: '大爷大妈',
|
||
$context['gender_text'] ?? '',
|
||
$context['age'] ?? 0,
|
||
$context['today'] ?? date('Y-m-d'),
|
||
$bloodLine,
|
||
mb_substr(GiKnowledge::summaryText(), 0, 380)
|
||
);
|
||
|
||
if ($refresh) {
|
||
$user .= "\n\n用户点了「换一换」,请重新给一套完全不同的农家三餐,编号" . substr(md5((string) microtime(true)), 0, 8) . ',别跟常见模板重复。';
|
||
}
|
||
|
||
return [$system, $user];
|
||
}
|
||
|
||
/**
|
||
* 解析流式三餐纯文本(兼容 JSON 回退)
|
||
*
|
||
* @return array<string, mixed>|null
|
||
*/
|
||
private static function parseRecommendPlainText(string $text): ?array
|
||
{
|
||
$text = trim($text);
|
||
if ($text === '') {
|
||
return null;
|
||
}
|
||
|
||
if ($text[0] === '{') {
|
||
$json = self::parseJsonObject($text);
|
||
if (is_array($json)) {
|
||
return $json;
|
||
}
|
||
}
|
||
|
||
$map = [
|
||
'breakfast' => null,
|
||
'lunch' => null,
|
||
'dinner' => null,
|
||
'tips' => null,
|
||
'avoid' => null,
|
||
];
|
||
|
||
if (preg_match('/早餐[::]\s*(.+)$/mu', $text, $m)) {
|
||
$map['breakfast'] = trim(preg_split('/\R/u', $m[1])[0] ?? $m[1]);
|
||
}
|
||
if (preg_match('/午餐[::]\s*(.+)$/mu', $text, $m)) {
|
||
$map['lunch'] = trim(preg_split('/\R/u', $m[1])[0] ?? $m[1]);
|
||
}
|
||
if (preg_match('/晚餐[::]\s*(.+)$/mu', $text, $m)) {
|
||
$map['dinner'] = trim(preg_split('/\R/u', $m[1])[0] ?? $m[1]);
|
||
}
|
||
if (preg_match('/提示[::]\s*(.+)$/mu', $text, $m)) {
|
||
$map['tips'] = trim(preg_split('/\R/u', $m[1])[0] ?? $m[1]);
|
||
}
|
||
if (preg_match('/少碰[::]\s*(.+)$/mu', $text, $m)) {
|
||
$avoidLine = trim(preg_split('/\R/u', $m[1])[0] ?? $m[1]);
|
||
$parts = preg_split('/[、,,;;\s]+/u', $avoidLine) ?: [];
|
||
$map['avoid'] = array_values(array_filter(array_map('trim', $parts)));
|
||
}
|
||
|
||
if (empty($map['breakfast'])) {
|
||
return null;
|
||
}
|
||
|
||
$out = [
|
||
'breakfast' => (string) $map['breakfast'],
|
||
'lunch' => (string) ($map['lunch'] ?? ''),
|
||
'dinner' => (string) ($map['dinner'] ?? ''),
|
||
'tips' => (string) ($map['tips'] ?? ''),
|
||
'avoid' => is_array($map['avoid']) ? $map['avoid'] : [],
|
||
];
|
||
|
||
return $out;
|
||
}
|
||
|
||
/**
|
||
* 规范化 AI 三餐方案;校验未过也保留 AI 文案(避免流式展示后被规则模板覆盖)
|
||
*
|
||
* @param array<string, mixed> $parsed
|
||
* @return array<string, mixed>
|
||
*/
|
||
private static function finalizeAiRecommendPlan(array $parsed): array
|
||
{
|
||
$checked = DietMealValidator::validateAndNormalize($parsed);
|
||
$data = $checked['plan'];
|
||
$data['source'] = 'ai';
|
||
$data['rules_summary'] = DietMealValidator::metaSummary($checked['meta']);
|
||
if ($checked['ok']) {
|
||
$data['disclaimer'] = 'AI 建议已通过规则校验,仍请结合医嘱与血糖监测。';
|
||
} else {
|
||
$data['validation_warnings'] = $checked['errors'];
|
||
$data['disclaimer'] = 'AI 建议供参考,仍请结合医嘱与血糖监测。';
|
||
}
|
||
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* @return array{0:string,1:string}
|
||
*/
|
||
private static function buildAskStreamPrompts(array $context, string $question): array
|
||
{
|
||
$food = GiKnowledge::extractFoodName($question);
|
||
$hit = GiKnowledge::lookupFood($food);
|
||
$kbHint = $hit
|
||
? "知识库命中:{$hit['food']} → {$hit['level_label']}({$hit['category']})"
|
||
: '知识库未命中,按 GI 原则用农村常识回答';
|
||
|
||
$system = <<<SYS
|
||
你是村里糖尿病饮食顾问,用升糖指数(GI)回答农村老人「能不能吃某东西」。
|
||
用一两句土话直接回答,40字以内,先说能不能吃、再说咋吃/吃多少。不要 JSON、不要 markdown、不要列表。
|
||
SYS;
|
||
|
||
$user = sprintf(
|
||
"老人%s,%s。问:%s\n%s\n\nGI摘要:\n%s",
|
||
$context['patient_name'] ?? '',
|
||
$context['gender_text'] ?? '',
|
||
$question,
|
||
$kbHint,
|
||
GiKnowledge::summaryText()
|
||
);
|
||
|
||
return [$system, $user];
|
||
}
|
||
|
||
/**
|
||
* @return array<string, mixed>
|
||
*/
|
||
private static function finalizeAskFromText(array $context, string $question, string $text): array
|
||
{
|
||
$food = GiKnowledge::extractFoodName($question);
|
||
$hit = GiKnowledge::lookupFood($food);
|
||
|
||
$data = [
|
||
'food' => $food,
|
||
'advice' => $text,
|
||
'source' => 'ai',
|
||
];
|
||
|
||
if ($hit) {
|
||
$data['food'] = $hit['food'];
|
||
$data['level'] = $hit['level'];
|
||
$data['level_label'] = $hit['level_label'];
|
||
$data['portion'] = $hit['level'] === GiKnowledge::LEVEL_HIGH
|
||
? '尽量别吃'
|
||
: ($hit['level'] === GiKnowledge::LEVEL_MEDIUM ? '少量' : '适量');
|
||
} else {
|
||
$data['level'] = GiKnowledge::LEVEL_MEDIUM;
|
||
$data['level_label'] = '中升糖';
|
||
$data['portion'] = '少量';
|
||
}
|
||
|
||
return $data;
|
||
}
|
||
|
||
private static function aiDailyRecommend(array $context, bool $refresh = false): ?array
|
||
{
|
||
[$system, $user] = self::buildRecommendPrompts($context, $refresh);
|
||
|
||
$res = AiChatService::chat([
|
||
['role' => 'system', 'content' => $system],
|
||
['role' => 'user', 'content' => $user],
|
||
], [
|
||
'temperature' => $refresh ? 0.78 : 0.35,
|
||
'max_tokens' => 900,
|
||
'timeout' => self::AI_TIMEOUT_SEC,
|
||
'response_format' => ['type' => 'json_object'],
|
||
]);
|
||
|
||
if (!$res['ok']) {
|
||
return null;
|
||
}
|
||
|
||
$parsed = self::parseJsonObject((string) $res['content']);
|
||
if (!$parsed || empty($parsed['breakfast'])) {
|
||
return null;
|
||
}
|
||
|
||
$parsed['avoid'] = is_array($parsed['avoid'] ?? null) ? $parsed['avoid'] : [];
|
||
|
||
return $parsed;
|
||
}
|
||
|
||
/**
|
||
* @param array $context
|
||
* @return array|null
|
||
*/
|
||
private static function aiAskFood(array $context, string $question): ?array
|
||
{
|
||
$food = GiKnowledge::extractFoodName($question);
|
||
$hit = GiKnowledge::lookupFood($food);
|
||
|
||
$system = <<<SYS
|
||
你是村里糖尿病饮食顾问,用升糖指数(GI)回答农村老人「能不能吃某东西」。
|
||
只输出 JSON:{"food":"食物名","level":"low|medium|high","level_label":"低升糖|中升糖|高升糖","advice":"40字内土话建议","portion":"咋吃、吃多少"}
|
||
level:low≤55,medium 56-69,high≥70。advice 要口语化,别文绉绉。
|
||
SYS;
|
||
|
||
$kbHint = $hit
|
||
? "知识库命中:{$hit['food']} → {$hit['level_label']}({$hit['category']})"
|
||
: '知识库未命中,按 GI 原则用农村常识回答';
|
||
|
||
$user = sprintf(
|
||
"老人%s,%s。问:%s\n%s\n\nGI摘要:\n%s\n\n农家饭原则:\n%s",
|
||
$context['patient_name'] ?? '',
|
||
$context['gender_text'] ?? '',
|
||
$question,
|
||
$kbHint,
|
||
GiKnowledge::summaryText(),
|
||
GiKnowledge::ruralMealHints()
|
||
);
|
||
|
||
$res = AiChatService::chat([
|
||
['role' => 'system', 'content' => $system],
|
||
['role' => 'user', 'content' => $user],
|
||
], [
|
||
'temperature' => 0.3,
|
||
'max_tokens' => 400,
|
||
'timeout' => self::AI_TIMEOUT_SEC,
|
||
'response_format' => ['type' => 'json_object'],
|
||
]);
|
||
|
||
if (!$res['ok']) {
|
||
return null;
|
||
}
|
||
|
||
$parsed = self::parseJsonObject((string) $res['content']);
|
||
if (!$parsed || empty($parsed['advice'])) {
|
||
return null;
|
||
}
|
||
|
||
if ($hit) {
|
||
$parsed['food'] = $hit['food'];
|
||
$parsed['level'] = $hit['level'];
|
||
$parsed['level_label'] = $hit['level_label'];
|
||
if (empty($parsed['portion'])) {
|
||
$parsed['portion'] = $hit['level'] === GiKnowledge::LEVEL_HIGH ? '尽量别吃' : ($hit['level'] === GiKnowledge::LEVEL_MEDIUM ? '少量' : '适量');
|
||
}
|
||
}
|
||
|
||
$parsed['source'] = 'ai';
|
||
if (empty($parsed['food'])) {
|
||
$parsed['food'] = $food;
|
||
}
|
||
|
||
return $parsed;
|
||
}
|
||
|
||
/**
|
||
* @return array<string, mixed>|null
|
||
*/
|
||
private static function parseJsonObject(string $content): ?array
|
||
{
|
||
$content = trim($content);
|
||
if ($content === '') {
|
||
return null;
|
||
}
|
||
if ($content[0] !== '{') {
|
||
if (preg_match('/\{[\s\S]*\}/', $content, $m)) {
|
||
$content = $m[0];
|
||
}
|
||
}
|
||
$decoded = json_decode($content, true);
|
||
return is_array($decoded) ? $decoded : null;
|
||
}
|
||
}
|