feat(Capture): 报告识别注入 Vision OCR 参考文本,提升 2B 多模态数字准确率
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -31,12 +31,38 @@ nonisolated enum VLPrompts {
|
||||
|
||||
/// VL 模型不知"今天"是哪天,且 few-shot 示例里写死了日期,
|
||||
/// 必须把当天日期显式注入 prompt,模型在无报告日期时才会用对正确的回退值。
|
||||
static func reportExtraction(today: Date = .now) -> String {
|
||||
/// ocrText 非空时把 Vision OCR 的结果作为参考文本注入 —— Vision 抄数字比
|
||||
/// 2B 多模态读密集小字稳;版面与表格结构仍以图片为准。
|
||||
static func reportExtraction(today: Date = .now, ocrText: String = "") -> String {
|
||||
let f = DateFormatter()
|
||||
f.locale = Locale(identifier: "en_US_POSIX")
|
||||
f.dateFormat = "yyyy-MM-dd"
|
||||
let todayStr = f.string(from: today)
|
||||
return reportExtractionTemplate.replacingOccurrences(of: "{{TODAY}}", with: todayStr)
|
||||
let ocrSection: String
|
||||
if ocrText.isEmpty {
|
||||
ocrSection = ""
|
||||
} else {
|
||||
ocrSection = """
|
||||
|
||||
|
||||
OCR 参考文本(系统对同一报告做文字识别的结果,可能有错字、串行或漏行;版面与表格结构以图片为准,但数值、小数点以 OCR 文字更可靠):
|
||||
\(clipOCR(ocrText))
|
||||
|
||||
"""
|
||||
}
|
||||
return reportExtractionTemplate
|
||||
.replacingOccurrences(of: "{{TODAY}}", with: todayStr)
|
||||
.replacingOccurrences(of: "{{OCR_SECTION}}", with: ocrSection)
|
||||
}
|
||||
|
||||
/// OCR 文本截断:限制进入 prompt 的体量(2B 模型上下文有限)。截到最后一个完整行。
|
||||
static func clipOCR(_ text: String, limit: Int = 1800) -> String {
|
||||
guard text.count > limit else { return text }
|
||||
let clipped = String(text.prefix(limit))
|
||||
if let lastNewline = clipped.lastIndex(of: "\n") {
|
||||
return String(clipped[..<lastNewline]) + "\n(后续内容过长已截断)"
|
||||
}
|
||||
return clipped + "\n(后续内容过长已截断)"
|
||||
}
|
||||
|
||||
private static let reportExtractionTemplate: String = #"""
|
||||
@@ -84,7 +110,7 @@ JSON schema(严格):
|
||||
输入: 一份春季体检,3 项可读
|
||||
输出:
|
||||
{"title":"春季年度体检","type":"checkup","report_date":"2026-04-12","institution":"协和医院","page_count":1,"summary":"血脂偏高、其他正常","indicators":[{"name":"低密度脂蛋白","value":"3.84","unit":"mmol/L","range":"< 3.40","status":"high","source_page":1,"source_box":[0.12,0.31,0.76,0.07]},{"name":"谷丙转氨酶","value":"32","unit":"U/L","range":"9 - 50","status":"normal","source_page":1,"source_box":[0.12,0.39,0.76,0.07]},{"name":"空腹血糖","value":"5.2","unit":"mmol/L","range":"3.9 - 6.1","status":"normal","source_page":1,"source_box":[0.12,0.47,0.76,0.07]}]}
|
||||
|
||||
{{OCR_SECTION}}
|
||||
现在请识别图片并输出 JSON:
|
||||
"""#
|
||||
|
||||
|
||||
Reference in New Issue
Block a user