Streaming session interface

Updated at:

This document details the request and response parameters for the session interface, along with examples and error codes. To learn about supported concurrency and how to purchase more capacity, see the product billing page. Session concurrency is calculated with the following formula: session concurrency = QPS * tail-packet response latency (s).

Notes

See the Large model session interface integration overview.

Integration example

  1. Web client: integration example

  2. DingTalk bot: integration example

  3. WeChat Customer Service: integration example

  4. WeCom: integration example

  5. Lark bot: integration example

Session API requests

Request URL

https://alime-ws.aliyuncs.com/sse/paas4Json/{AccessToken}/{ChannelId}/{Sign}/{Timestamp}

Request parameters

Parameter

Description

AccessToken

The access token for authentication. Obtain it from the Get connection information for streaming calls API.

ChannelId

The streaming channel. Obtain it from the Get connection information for streaming calls API.

Sign

The signature generated using the signing protocol. For more information, see Session API signature.

Timestamp

The timestamp used to generate the signature. You can use the current system time in milliseconds.

Request body parameters (JSON)

Parameter

Type

Required

Description

messageId

String

Yes

A unique request identifier to ensure idempotence. A UUID is recommended.

action

String

Yes

A fixed value: TongyiBeebotChat.

version

String

Yes

A fixed value: 2022-04-08.

data

List

Yes

For details, see the session request parameters below.

InstanceId

String

Yes

The bot ID. For example: chatbot-cn-dDmF3jcdVf.

Utterance

String

Yes

The user's utterance. The maximum length is 512 characters.

SessionId

String

No

The session ID, used to identify the session and maintain context.

SenderId

String

No

The sender ID, used to identify the user in the current session.

SenderNick

String

No

The nickname of the sender in the current session.

VendorParam

JSON String

No

A JSON string of user-defined passthrough parameters that you can pass to dialog engines. Examples:

  • A single value: {"phone":123456789}

  • An array: {"name":["a","b","c"]}

  • Document labels: {"docLabels":"Label1,Label2"}

  • Multimodal chat:

    {"SYS_IMAGE_ATTACHMENTS":["http://example.com/image1.png", "http://example.com/image2.png"]}

    A list of image URLs. The URLs must be publicly accessible and the images must comply with applicable regulations.

Perspective

Array of String

No

Codes used to retrieve answers from different perspectives under a single knowledge title. Pass this parameter in the format Perspective={codes}. For example: &Perspective=["FZJBY3raWr"].

SandBox

Boolean

No

Specifies the operating environment. The default is the production environment.

  • true: The sandbox environment. For testing only. Using this environment in production may cause instability and is subject to a QPS limit.

  • false: The production environment.

Command

String

No

The command to execute for the request.

TIMEOUT: Triggers the timeout utterance in Conversation Factory.

GUIDEPOST: Triggers the asynchronous service in Conversation Factory. For details, see the asynchronous service user manual.

Response parameters

Important

An HTML response indicates that the URL parameters are incorrect.

Parameter

Type

Description

success

Boolean

Indicates whether the request was successful.

code

String

The error code. This field is returned only when success is false.

message

String

The error message. This field is returned only when success is false.

messageId

String

The ID of the request message.

requestId

String

The unique ID of the request.

data

Array

The response data.

The format of data is as follows:

[
  {
    "type": "JSON_TEXT",
    "value": ""
  }
]

A JSON string containing the detailed session response. See the fields below for its structure.

SessionId

String

The ID of the current session.

MessageId

String

The ID of the response message.

SequenceId

String

The ID of a message segment in the streaming response.

Source

String

The source of the answer. This field can be empty.

TASK_DRIVEN_DIALOGUE: Task-driven dialogue.

STRUCTURAL_KNOWLEDGE: Knowledge from structured data tables.

NON_STRUCTURAL_KNOWLEDGE: Semi-structured or unstructured knowledge.

SYSTEM_ASK_CONFIG: Q&A policy configuration.

HYBRID_KNOWLEDGE: Hybrid knowledge.

StreamEnd

Boolean

Indicates whether the streaming response is complete.

MessageBody

MessageBody

The message body from the large language model.

Type

String

The answer type.

Direct: The response is a direct answer.

Clarify: The response is a clarification question.

DirectMessageBody

DirectMessageBody

The direct answer object from the bot. Returned only when Type is Direct.

SentenceList

Array of Sentence

The answer from the large language model, split into multiple segments. You can render segments as they arrive.

Content

String

A segment of the answer from the large language model.

ReferNumber

Integer

The sequence number of the segment.

ContentType

String

The data type of the answer.

Valid values:

PLAIN_TEXT: Plain text.

RICH_TEXT: Rich text.

HitSystemAskConfig

String

The matched Q&A policy. This field can be empty.

SENSITIVE_WORD: A sensitive word was detected.

NO_ANSWER: No answer was found.

COMMANDS: A command was triggered.

AnswerReference

AnswerReference

The source references for the answer.

ItemList

Array of Item

A list of referenced items. For details, see ItemList Field Details.

Ext

Map<String, Object>

Additional information from various answer sources.

EXTERNAL_FLAGS(when Source is TASK_DRIVEN_DIALOGUE)

Map

Returns the EXTERNAL_FLAGS configured in the flow.

RelatedQuestionList

Array of RelatedQuestion

A list of related questions.

Content

String

The content of the related question.

ClarifyMessageBody

ClarifyMessageBody

The clarification question object from the bot. Returned only when Type is Clarify.

ClarifyContent

String

The full clarification response from the large language model. For example:

"Which school are you asking about: Tsinghua University or Peking University?"

Title

String

The title of the clarification prompt. For example: "Which school are you asking about?"

ClarifyList

Array of Clarify

A list of clarification options for creating clickable follow-up prompts.

For example: "Tsinghua University", "Peking University".

Commands

Map<String, Object>

Contains the parameters of the triggered command. For details, see Commands Field Details.

Item list field descriptions

Parameter

Type

Description

Number

Integer

The sequence number of the reference, starting from 1.

DataSource

String

The data source. Valid values include:

FAQ_DIRECT: A high-confidence direct answer from an FAQ.

FAQ: An FAQ.

DOC: A document.

WEBSITE: A website.

QUARK: Quark.

TABLE: A table.

EXTERNAL_DATA_SOURCE: metadata.

FLOW: A flow.

FUNCTION: An API plugin.

ContentType

String

The data type of the referenced content.

Valid values:

PLAIN_TEXT: plain text.

RICH_TEXT: rich text.

Content

String

When ContentType is PLAIN_TEXT or RICH_TEXT, this field contains the referenced text in the corresponding format.

Title

String

When DataSource is FAQ: The FAQ title.

When DataSource is DOC, WEBSITE, or QUARK: The document title.

When DataSource is TABLE or EXTERNAL_DATA_SOURCE: The data table name.

When DataSource is FLOW: The flow name.

When DataSource is FUNCTION: The API plugin name.

ReferenceExt

Map<String, Object>

Contains reference metadata, which varies by DataSource. For details, see ReferenceExt field descriptions.

ReferenceExt field definitions

If DataSource is DOC, WEBSITE, or QUARK:

Parameter

Type

Description

DocName

String

The document name.

PageUrls

Array of String

The URL of the page containing the referenced content.

PagePos

Array of PageBlockPos

The coordinates of the referenced content.

Meta

JSONString

The metadata of the referenced document.

The value depends on the DataSource.

If DataSource is WEBSITE or QUARK, the value is built-in: {"url":""}.

If DataSource is DOC, the value is user-defined.

PageBlockPos

Parameter

Type

Description

x

Integer

The x-coordinate.

y

Integer

The y-coordinate.

width

Integer

The width of the referenced content.

height

Integer

The height of the referenced content.

pageId

String

The ID of the page containing the content.

Commands field

Built-in system commands

Parameter

Type

Description

Example

Field descriptions

sysToAgent

string

A built-in command that transfers the conversation to an agent.

{
    "skillGroup": "12",
    "ext": "abc",
    "toAgentReason": "MultiTurnNoAnswer"
}

skillGroup: Specifies the skill group for the transfer.

ext: An extension field for custom data.

toAgentReason: Specifies the reason for transferring to an agent. Valid values:

  • MultiTurnNoAnswer: The bot failed to provide a response.

  • RepeatRecommend: The bot repeatedly offered clarifications.

  • RepeatAsk: The user asked the same question repeatedly.

  • HitKeywords: A keyword triggered the transfer.

  • HitEmotion: The user's detected emotion triggered the transfer.

Example

Request example

Example Session API URL:

This link is for reference only; do not use it directly. https://alime-ws.aliyuncs.com/sse/paas4Json/121ccf51-94bf-4035-b7b4-4596ec1c03b4/e8c472e8-ddd8-42c8-b042-a89c7caa346d/d18120b6d9f83953b56340443f75188f/1718179407815

Request Body:

{
  "messageId": "9e268624-1853-4f8b-8bd6-41a7ad818d62",
  "action":"TongyiBeebotChat",
  "version":"2022-04-08",
  "data": [
    {
      "type": "JSON_TEXT",
      "value": "{\"InstanceId\":\"chatbot-cn-dDmF3jcdVf\",\"Utterance\":\"What can you do?\",\"VendorParam\":\"{\\\"docLabels\\\":\\\"30022351214\\\"}\",\"Perspective\":[\"cnfKrDEfFa\",\"deasdSKoipa\"]}"
    }
  ]
}

Response example

{
    "data": [
        {
            "type": "JSON_TEXT",
            "value": "{\"IsAIGC\":true,\"MessageBody\":{\"DirectMessageBody\":{\"AnswerReference\":{\"ItemList\":[]},\"ContentType\":\"RICH_TEXT\",\"RelatedQuestionList\":[],\"SentenceList\":[{\"Content\":\"I can answer your questions as an expert in various fields. No matter what area of expertise you need, I will do my best to help. What specific question can I help you with?\"}]},\"Type\":\"Direct\",\"VoiceConfig\":{\"AsrMaxEndSilence\":\"700\",\"CollectNumber\":false,\"CollectVoice\":false,\"HangUp\":false,\"Interruptible\":false,\"ReplyTimeout\":5}},\"MessageId\":\"7a2c9996-64d4-4f08-8b90-5d78d43f84c7\",\"SequenceId\":\"d8d33d7fe5e244a4a37656a0d80bb003\",\"SessionId\":\"10fc717a-b2ee-4914-86a4-8f1d1f2a827c\",\"Source\":\"HYBRID_KNOWLEDGE\",\"StreamEnd\":false}"
        }
    ],
    "messageId": "9e268624-1853-4f8b-8bd6-41a7ad818d62",
    "requestId": "7a2c9996-64d4-4f08-8b90-5d78d43f84c7",
    "success": true
}

Session response

The unescaped JSON object from the value field in the response example.

{
    "MessageBody": {
        "DirectMessageBody": {
            "AnswerReference": {
                "ItemList": [

                ]
            },
            "ContentType": "RICH_TEXT",
            "RelatedQuestionList": [

            ],
            "SentenceList": [
                {
                    "Content": "I can answer your questions as an expert in a variety of fields. How can I help you?"
                }
            ]
        },
        "Type": "Direct",
        "VoiceConfig": {
            "AsrMaxEndSilence": "700",
            "CollectNumber": false,
            "CollectVoice": false,
            "HangUp": false,
            "Interruptible": false,
            "ReplyTimeout": 5
        }
    },
    "MessageId": "7a2c9996-64d4-4f08-8b90-5d78d43f84c7",
    "SequenceId": "d8d33d7fe5e244a4a37656a0d80bb003",
    "SessionId": "10fc717a-b2ee-4914-86a4-8f1d1f2a827c",
    "Source": "HYBRID_KNOWLEDGE",
    "StreamEnd": false,
    "IsAIGC": true
}

Response examples

Source: TASK_DRIVEN_DIALOGUE
{
    "MessageBody":{
        "Commands":{},
        "DirectMessageBody":{
            "ContentType":"PLAIN_TEXT",
            "Ext":{
                "EXTERNAL_FLAGS":{}
            },
            "SentenceList":[
                {
                    "Content":"It is sunny in Beijing, with a temperature of 8 degrees Celsius and easterly winds up to level 3."
                }
            ]
        },
        "Type":"Direct"
    },
    "MessageId":"7a2cdb51-af56-4ac9-9c6d-108905695ac9",
    "SequenceId":"4861074fe523498ba3f397e876b60105",
    "SessionId":"f3d7e960933411ee96144bea43fe4df3",
    "Source":"TASK_DRIVEN_DIALOGUE",
    "StreamEnd":true
}
Source=NON_STRUCTURAL_KNOWLEDGE
{
    "MessageBody":{
        "DirectMessageBody":{
            "AnswerReference":{
                "ItemList":[
                    {
                        "Content":"Hello",
                        "ContentType":"RICH_TEXT",
                        "DataSource":"FAQ_DIRECT",
                        "Number":1,
                        "ReferenceExt":{
                            "DocName":"FAQ knowledge base"
                        },
                        "Title":"Hello"
                    }
                ]
            },
            "ContentType":"RICH_TEXT",
            "SentenceList":[
                {
                    "Content":"Hello",
                    "ReferNumber":1
                }
            ]
        },
        "Type":"Direct"
    },
    "MessageId":"808a9cfb-2cec-4230-aad1-66c28df6040e",
    "SequenceId":"32a052f098c14769b8b81ac202f38d01",
    "SessionId":"595fa8a0940611eeb59a155f6621f5d6",
    "Source":"NON_STRUCTURAL_KNOWLEDGE",
    "StreamEnd":true
}
Source=SYSTEM_ASK_CONFIG

Triggered exception prompt: HitSystemAskConfig=NO_ANSWER

{
    "MessageBody": {
        "DirectMessageBody": {
            "Ext": {},
            "AnswerReference": {
                "ItemList": []
            },
            "ContentType": "RICH_TEXT",
            "SentenceList": [
                {
                    "Content": "The bot is busy. Please try again later."
                }
            ],
            "HitSystemAskConfig": "NO_ANSWER",
            "RelatedQuestionList": []
        },
        "VoiceConfig": {
            "ReplyTimeout": 5,
            "Interruptible": false,
            "HangUp": false,
            "AsrMaxEndSilence": "700",
            "CollectVoice": false,
            "CollectNumber": false
        },
        "Type": "Direct"
    },
    "MessageId": "6B15B50A-46D0-50DD-8EB4-5E82E931B331",
    "SequenceId": "e140735e71014ff78ee84a2692a231a4",
    "SessionId": "797acf6e-96b7-435b-9156-8a92ccefba1c",
    "Source": "SYSTEM_ASK_CONFIG",
    "StreamEnd": true,
    "IsAIGC": true
}

Triggered the built-in safety prompt: HitSystemAskConfig=SENSITIVE_WORD

{
    "success": true,
    "MessageBody": {
        "DirectMessageBody": {
            "AnswerReference": {
                "ItemList": []
            },
            "ContentType": "PLAIN_TEXT",
            "HitSystemAskConfig": "SENSITIVE_WORD",
            "RelatedQuestionList": [],
            "SentenceList": [
                {
                    "Content": "I cannot answer that question. Please try asking something else."
                }
            ]
        },
        "Type": "Direct"
    },
    "MessageId": "d163a0b1-cce4-41c3-8502-d314f464485e",
    "SequenceId": "b7d96983212340f8ad672aee03e9c28f",
    "SessionId": "c44f27edf48246b5b95c79035cbfac33",
    "Source": "SYSTEM_ASK_CONFIG",
    "StreamEnd": true,
    "IsAIGC": true
}

Command trigger: HitSystemAskConfig=COMMANDS

{
    "success": true,
    "MessageBody": {
      "Commands": {
        "sysToAgent": "{\"skillGroup\":\"\",\"ext\":\"\",\"toAgentReason\":\"HitEmotion\"}"
      },
      "DirectMessageBody": {
        "AnswerReference": {
          "ItemList": []
        },
        "ContentType": "PLAIN_TEXT",
        "HitSystemAskConfig": "COMMANDS",
        "RelatedQuestionList": [],
        "SentenceList": [
          {
            "Content": "Transferring you to an agent."
          }
        ]
      },
      "VoiceConfig": {
        "AsrMaxEndSilence": "700",
        "CollectNumber": false,
        "CollectVoice": false,
        "HangUp": false,
        "Interruptible": false,
        "ReplyTimeout": 5
      },
      "Type": "Direct"
    },
    "MessageId": "5d7db238-7c7e-4adb-b00f-fc22183dbdda",
    "SequenceId": "2be71925e6fe4556bcfcbaaa0456e6d3",
    "SessionId": "a78f6a070a574bb69cea4ee0108b2370",
    "Source": "SYSTEM_ASK_CONFIG",
    "StreamEnd": true,
    "IsAIGC": true
}

Error codes

Important

An HTML response means the URL parameters are invalid.

Error code

HTTP status code

Description

InvalidInstanceId.NotFound

404

The specified robot ID was not found.

InvalidParameter

400

The specified parameter is invalid.

InternalError

500

An internal error occurred.

Rate limiting error

If concurrency exceeds version concurrency, throttling is triggered. For example:

{
    "code": "csQueueBusy",
    "data": [],
    "message": "Queued: The service is busy, please wait.",
    "messageId": "203bee17-c404-4b9c-ba96-cf3873237fa0",
    "requestId": "18f80b3c-ff87-468c-b90f-0d31e4ce34b8",
    "success": false
}