如何在Azure AI Search技能集中实现条件逻辑?
Azure技能集条件逻辑实现(针对unknown语言文档执行OCR)
核心实现思路
在Azure认知搜索的技能集中,通过**ConditionalSkill(条件技能)**实现分支逻辑:当语言识别技能输出的语言代码为unknown时,触发OCR技能处理文档图片;若语言已知,则直接使用原文本内容。
完整配置示例
1. 先定义语言识别技能
{ "@odata.type": "#Microsoft.Skills.Text.LanguageDetectionSkill", "name": "LanguageDetection", "context": "/document", "inputs": [ { "name": "text", "source": "/document/content" } ], "outputs": [ { "name": "languageCode", "targetName": "languageCode" } ] }
2. 添加条件分支触发逻辑
{ "@odata.type": "#Microsoft.Skills.Util.ConditionalSkill", "name": "ConditionalOcrTrigger", "context": "/document", "inputs": [ { "name": "condition", "source": "@equals(/document/languageCode, 'unknown')" }, { "name": "whenTrue", "source": "/document/normalized_images/*" }, { "name": "whenFalse", "source": "/document/content" } ], "outputs": [ { "name": "output", "targetName": "contentToProcess" } ] }
3. 配置OCR技能处理分支结果
{ "@odata.type": "#Microsoft.Skills.Vision.OcrSkill", "name": "OcrSkill", "context": "/document", "inputs": [ { "name": "image", "source": "/document/contentToProcess" }, { "name": "detectOrientation", "source": "true" } ], "outputs": [ { "name": "text", "targetName": "ocrText" } ] }
常见错误排查
- 表达式语法错误:必须使用Azure技能集内置的
@equals()函数做比较,不能直接写/document/languageCode == 'unknown'。 - 上下文路径不匹配:确认语言识别技能的输出
languageCode确实在/document上下文下,输入输出的source路径要和技能的context对应。 - 技能顺序错误:技能集按从上到下顺序执行,必须保证语言识别技能在条件技能之前,条件技能在OCR技能之前。
- OCR输入源错误:条件为
true时,输入源必须指向文档的normalized_images(从Blob索引器获取的图片内容),不能用原文本。
可选后续处理
如果需要统一输出格式,可以添加合并技能,根据languageCode决定最终取ocrText还是原content:
{ "@odata.type": "#Microsoft.Skills.Text.MergeSkill", "name": "MergeContent", "context": "/document", "inputs": [ { "name": "itemsToMerge", "source": "@if(equals(/document/languageCode, 'unknown'), /document/ocrText, /document/content)" } ], "outputs": [ { "name": "mergedText", "targetName": "finalContent" } ] }
内容的提问来源于stack exchange,提问作者noobie_
相关产品推荐
相关产品推荐

