Skip to main content

Information extraction

This topic describes the syntax of the Tongyi Data Mining information extraction API and provides a request example.

Request syntax

POST /zhiwen-chat/extraction HTTP/1.1

Request parameters

Name

Type

Required

Description

Example value

capabilityType

string

Yes

The capability type.

Bidding document extraction: BID_EXTRACTION. Resume extraction: RESUME_EXTRACTION.

Entity extraction: ENTITY_EXTRACTION.

ENTITY_EXTRACTION

userPrompt

string

Yes

The prompt.

You are an AI assistant that specializes in named entity recognition (NER). Your task is to accurately extract all names of people, places, and organizations from the given text and organize them in the specified dictionary format.

fileIdList

array

Yes

The list of document IDs.

[

"file_zhiwen_XXX"

]

stream

boolean

Yes

Specifies whether to return the response in a stream.

true

Response parameters

Name

Type

Description

output

object

-choices

array

-finishReason

string

Termination reason

-message

object

The message body.

-role

string

The role.

-content

string

The message content.

requestId

string

The request ID.

usage

object

Information about token usage.

-inputTokens

int

The number of input tokens.

-outputTokens

int

The number of output tokens.

-totalTokens

int

The total number of tokens.

Examples

Call example

import os
import requests

'''
API name: Information Extraction
API path: https://dashscope.aliyuncs.com/api/v2/apps/zhiwen-chat/extraction
Environment requirements: Python >= 3.7
'''

url = "https://dashscope.aliyuncs.com/api/v2/apps/zhiwen-chat/extraction"
request_headers = {
    "Authorization": os.environ.get("DASHSCOPE_API_KEY"),  # If you have not configured an environment variable, replace this with your API key.
    "Content-Type": "application/json"
}
# Information extraction
# Bidding document extraction: BID_EXTRACTION. Resume extraction: RESUME_EXTRACTION. Entity extraction: ENTITY_EXTRACTION.
request_body = {
    "capabilityType": "ENTITY_EXTRACTION",
    "userPrompt": "You are an AI assistant that specializes in named entity recognition (NER). Your task is to accurately extract all names of people, places, and organizations from the given text and organize them in the specified dictionary format.",
    "stream": True,
    "fileIdList": [
        "file_zhiwen_XXX"  # Replace this with your document ID.
    ]
}

with requests.post(url, json=request_body, headers=request_headers) as response:
    if request_body["stream"]:
        for line in response.iter_lines():
            if line:
                line = line.decode('utf-8')
                if line.startswith('data:'):
                    print("Received:", line[5:].strip())
    else:
        print("Received:", response.json())

Normal response example

JSON format
{
  "output": {
    "choices": [{
      "finishReason": "stop",
      "message": {
        "role": "assistant",
        "content": "OK. I will extract all names of people, places, and organizations from the given text and organize them in the specified dictionary format: Document information extraction result."
      }
    }]
  },
  "requestId": "60a9c0f4-9414-9c94-81f3-f487f7769c9a",
  "usage": {
    "inputTokens": 100,
    "outputTokens": 100,
    "totalTokens": 200
  }
}

Error codes

For more information, see Error codes - Tongyi Data Mining.
Agent Development
Agent Context
Agent Governance & Evolution
Store
Application Marketplace