You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法调用Language Studio自定义命名实体识别端点求助

问题原因及解决步骤

为什么返回202?

你调用的是异步API端点(analyze-text/jobs),202状态码表示请求已被系统接受,模型正在后台异步处理任务,不会直接返回实体识别结果。Language Studio的UI已经自动帮你完成了轮询等待结果的步骤,所以你能直接看到输出,但手动调用时需要自己完成后续的查询操作。

完整调用流程

  1. 提交异步请求:你当前的代码已经完成这一步,拿到202响应后,需要从响应的headers中提取operation-location字段,这是查询任务结果的专属URL。
  2. 轮询查询结果:使用这个URL发送GET请求,直到返回200状态码,此时响应体中才会包含实体识别的最终结果。

修改后的Python代码

import json
import requests
import time

url = "https://<language_service>.cognitiveservices.azure.com/language/analyze-text/jobs?api-version=2022-10-01-preview"

payload = json.dumps({
  "tasks": [
    {
      "kind": "CustomEntityRecognition",
      "parameters": {
        "projectName": "<project_name>",
        "deploymentName": "<deployment_name>",
        "stringIndexType": "TextElement_v8"
      }
    }
  ],
  "displayName": "CustomTextPortal_CustomEntityRecognition",
  "analysisInput": {
    "documents": [
      {
        "id": "1",
        "text": "<text>",
        "language": "en-us"
      },
      {
        "id": "2",
        "text": "<text>",
        "language": "en-us"
      }
    ]
  }
})
headers = {
  'Content-Type': 'application/json',
  'Ocp-Apim-Subscription-Key': '<key>'
}

# 提交异步任务
response = requests.post(url, headers=headers, data=payload)
if response.status_code == 202:
    # 获取结果查询URL
    result_url = response.headers.get('operation-location')
    # 轮询等待任务完成
    while True:
        result_response = requests.get(result_url, headers=headers)
        if result_response.status_code == 200:
            # 打印格式化后的识别结果
            print(json.dumps(result_response.json(), indent=2))
            break
        elif result_response.status_code == 202:
            # 任务仍在处理,等待后重试
            print("任务处理中,3秒后重试...")
            time.sleep(3)
        else:
            print(f"查询失败,状态码:{result_response.status_code}")
            print(result_response.text)
            break
else:
    print(f"提交任务失败,状态码:{response.status_code}")
    print(response.text)

额外注意事项

  • 确认订阅密钥、项目名称、部署名称与Language Studio中的配置完全一致。
  • 轮询间隔建议设置为3-5秒,避免过于频繁的请求触发API限流。
  • 如果任务长时间未完成,可检查输入文本长度是否超出模型限制,或查看Azure语言服务的监控日志排查问题。

内容的提问来源于stack exchange,提问作者Atinesh Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 19:02:11