工作流接口
鉴权
字段 | 传参方式 | 类型 | 必传 | 描述 | 示例值 |
Authorization | header | String | 是 | API-Key | Bearer d1**2a |
路由配置
备注:SDK已封装好路由关系,通过SDK调用接口,无需设置接口路由配置。
字段 | 传参方式 | 类型 | 必传 | 描述 | 示例值 |
x-fag-appcode | header | String | 是 | 应用表示,固定为 |
|
x-fag-servicename | header | String | 是 | 请求路由标识,不同接口值不一样,参考servicename 与接口映射关系 | aca-flow-chat-send-sse |
x-fag-servicename 与接口映射关系
接口名称 | PATH | x-fag-servicename的值 |
工作流对话(流式) | /v2/api/chat/send | aca-flow-chat-send-sse |
工作流对话(非流式) | /v2/api/chat/send | aca-flow-chat-send |
工作流对话
当前仅支持在星尘平台创建的服务助手角色进行对话。
接口
POST /v2/api/chat/send
入参
请求头参数
字段 | 传参方式 | 类型 | 必传 | 描述 |
X-AcA-SSE | header | String | 否 | 是否开启流式对话- 开启 - |
x-fag-appcode | header | String | 是 | 应用标识,固定为 |
x-fag-servicename | header | String | 是 | 请求路由标识,不同接口值不一样,参考上方路由配置中的 x-fag-servicename 与接口映射关系 |
请求体参数
参数 | 类型 | 必传 | 说明 |
messages | array | 是 | 对话历史(时间顺序正排),固定角色可通过角色配置最大对话轮数。list中的每个元素形式为{"role":角色, "content": 内容}。角色当前可选值:system、user、assistant,其中,仅messages[0]中支持role为system,user、assistant需要交替出现,最后一条为用户提问。 |
botProfile | object | 是 | 角色设置 |
botProfile.characterId | string | 是 | 固定角色必填 |
userProfile | object | 是 | 用户设置 |
userProfile.userId | string | 是 | 业务系统用户唯一标识,同一用户不能并行对话,必须待上次对话回复结束后才可发起下轮对话 |
context | object | 是 | 对话上下文设置 |
context.useChatHistory | boolean | 是 | 是否使用平台历史 |
context.startParams | object | 是 | 开始节点的入参变量 |
返回参数
流式调用返回
参数 | 类型 | 说明 |
requestId | string | 系统生成的标志本次调用的id。 |
success | boolean | 是否成功返回 |
errorCode | int | 错误码 |
errorName | string | 错误名称 |
httpStatusCode | int | http错误码 |
errorMessage | string | 错误消息 |
flowUsage | list | |
flowUsage[i].modelName | string | 模型名称 |
flowUsage[i].inputTokens | int | 本次请求输入内容的 token 数目。 |
flowUsage[i].outputTokens | int | 模型生成回复转换为Token后的长度。 |
choices | array | 消息体 |
choices[i].messages | array | |
choices[i].messages[i].finishReason | string | 完成标识符,已完成为 stop,未完成为 “null” |
choices[i].messages[i].role | string | 模型role,固定为assistant |
choices[i].messages[i].roleId | string | 角色id |
choices[i].messages[i].content | string | 模型生成消息输出 |
choices[i].messages[i].name | string | 角色名称 |
非流式调用返回
参数 | 类型 | 说明 |
requestId | string | 系统生成的标志本次调用的id。 |
success | boolean | 是否成功返回 |
errorCode | int | 错误码 |
errorName | string | 错误名称 |
httpStatusCode | int | http错误码 |
errorMessage | string | 错误消息 |
data | object | |
data.flowUsage | list | |
data.flowUsage[i].modelName | string | 模型名称 |
data.flowUsage[i].inputTokens | int | 本次请求输入内容的 token 数目。 |
data.flowUsage[i].outputTokens | int | 模型生成回复转换为Token后的长度。 |
data.choices | array | 消息体 |
data.choices[i].messages | array | |
data.choices[i].messages[i].finishReason | string | 完成标识符,已完成为 stop,未完成为 “null” |
data.choices[i].messages[i].role | string | 模型role,固定为assistant |
data.choices[i].messages[i].roleId | string | 角色id |
data.choices[i].messages[i].content | string | 模型生成消息输出 |
data.choices[i].messages[i].name | string | 角色名称 |
示例参考
工作流对话调用示例
调用示例
curl --location "https://nlp.aliyuncs.com/v2/api/chat/send" \
-H "Authorization: Bearer {YOUR_API_KEY}" \
-H "X-AcA-DataInspection: enable" \
-H "X-AcA-SSE: enable" \
-H "x-fag-servicename: aca-flow-chat-send-sse" \
-H "x-fag-appcode: aca" \
-H "Content-Type: application/json" \
-d '{
"messages": [
{
"role": "user",
"content": "(心中暗自决定)这是我最后一次对你说这样的话了。"
},
{
"role": "assistant",
"content": "(惊讶地抬头)哦?是什么话呢?"
},
{
"role": "user",
"content": "今天我想去运动,告诉我运动的好处"
}
],
"botProfile": {
"characterId": "aa7b98327d564d49a997ec67bfdaf68e"
},
"userProfile": {
"userId": "123456",
"basicInfo": "df"
},
"scenario": {
"description": ""
},
"context": {
"useChatHistory": true
}
}'
/**
* API名称:工作流对话
* 环境要求:Java 17及以上
*/
public class Example {
private static final String URL = "https://nlp.aliyuncs.com/v2/api/chat/send";
private static final String API_KEY = "Bearer {YOUR_API_KEY}";
public static void main(String[] args) throws Exception {
// 非流式调用
nonStreamingCall();
// 流式调用
// streamingCall();
}
private static void nonStreamingCall() throws Exception {
System.out.println("=== 非流式调用示例 ===");
// characterId 填写服务助手id,不是角色id
String jsonBody = """
{
"messages": [
{
"role": "user",
"content": "(心中暗自决定)这是我最后一次对你说这样的话了。"
},
{
"role": "assistant",
"content": "(惊讶地抬头)哦?是什么话呢?"
},
{
"role": "user",
"content": "今天我想去运动,告诉我运动的好处"
}
],
"botProfile": {
"characterId": "aa7b98327d564d49a997ec67bfdaf68e"
},
"userProfile": {
"userId": "123456",
"basicInfo": "df"
},
"scenario": {
"description": ""
},
"context": {
"useChatHistory": true
}
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(URL))
.headers(
"Authorization", API_KEY,
"X-AcA-DataInspection", "enable",
// 固定servicename
"x-fag-servicename", "aca-flow-chat-send",
"x-fag-appcode", "aca",
"Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonBody))
.build();
// 同步调用
HttpResponse<String> response = client.send(
request,
HttpResponse.BodyHandlers.ofString()
);
String responseBody = response.body();
System.out.println("响应数据: " + responseBody);
}
private static void streamingCall() {
System.out.println("=== 流式调用示例 ===");
// characterId 填写服务助手id,不是角色id
String jsonBody = """
{
"messages": [
{
"role": "user",
"content": "(心中暗自决定)这是我最后一次对你说这样的话了。"
},
{
"role": "assistant",
"content": "(惊讶地抬头)哦?是什么话呢?"
},
{
"role": "user",
"content": "今天我想去运动,告诉我运动的好处"
}
],
"botProfile": {
"characterId": "aa7b98327d564d49a997ec67bfdaf68e"
},
"userProfile": {
"userId": "123456",
"basicInfo": "df"
},
"scenario": {
"description": ""
},
"context": {
"useChatHistory": true
}
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(URL))
.headers(
"Authorization", API_KEY,
"X-AcA-DataInspection", "enable",
"X-AcA-SSE", "enable",
// 固定servicename
"x-fag-servicename", "aca-flow-chat-send-sse",
"x-fag-appcode", "aca",
"Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonBody))
.build();
// 异步处理 SSE 流
CompletableFuture<Void> future = client.sendAsync(request, HttpResponse.BodyHandlers.ofLines())
.thenApply(HttpResponse::body)
.thenAccept(stream -> {
stream.forEach(line -> {
System.out.println("流式响应数据: " + line);
});
});
// 保持主线程等待
future.join();
}
}
import requests
URL = "https://nlp.aliyuncs.com/v2/api/chat/send"
# API名称:工作流对话
# 环境要求:python3.10及以上
def call(messages, api_key, model_name):
headers = {
"Content-Type": "application/json",
"x-fag-servicename": "aca-flow-chat-send",
# 流式调用开启此配置
# "x-fag-servicename": "aca-flow-chat-send-sse",
"x-fag-appcode": "aca",
"Authorization": f"Bearer {api_key}",
"X-AcA-DataInspection": "enable"
}
payload = {
"messages": messages,
"model": model_name,
# characterId 填写服务助手id,不是角色id
"botProfile": {
"characterId": "aa7b98327d564d49a997ec67bfdaf68e"
},
"userProfile": {
"userId": "123456",
"basicInfo": "df"
},
"scenario": {
"description": ""
},
"context": {
"useChatHistory": True
}
}
response = requests.post(URL, json=payload, headers=headers)
# 逐行打印原始响应内容
for line in response.iter_lines():
if line:
print(line.decode('utf-8'))
# 检查响应状态,如有错误则抛出异常
response.raise_for_status()
return response
if __name__ == "__main__":
messages = [
{
"role": "user",
"content": "(心中暗自决定)这是我最后一次对你说这样的话了。"
},
{
"role": "assistant",
"content": "(惊讶地抬头)哦?是什么话呢?"
},
{
"role": "user",
"content": "今天我想去运动,告诉我运动的好处"
}
]
api_key = "YOUR_API_KEY"
model_name = "xingchen-plus-latest"
try:
response = call(messages, api_key, model_name)
except requests.HTTPError as e:
print(f"请求出错: {e}")
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
// API名称:工作流对话
// 环境要求:go 1.20及以上
const URL = "https://nlp.aliyuncs.com/v2/api/chat/send"
func call(messages []map[string]interface{}, apiKey string, modelName string) (*http.Response, error) {
payload := map[string]interface{}{
"messages": messages,
"model": modelName,
"botProfile": map[string]interface{}{
"characterId": "aa7b98327d564d49a997ec67bfdaf68e", // characterId 填写服务助手id,不是角色id
},
"userProfile": map[string]interface{}{
"userId": "123456",
"basicInfo": "df",
},
"scenario": map[string]interface{}{
"description": "",
},
"context": map[string]interface{}{
"useChatHistory": true,
},
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", URL, bytes.NewBuffer(payloadBytes))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
// req.Header.Set("x-fag-servicename", "aca-flow-chat-send-sse") // 流式调用开启此配置
req.Header.Set("x-fag-servicename", "aca-flow-chat-send")
req.Header.Set("x-fag-appcode", "aca")
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("X-AcA-DataInspection", "enable")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
// 逐行打印原始响应内容
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("请求出错,状态码:%d", resp.StatusCode)
}
return resp, nil
}
func main() {
messages := []map[string]interface{}{
{
"role": "user",
"content": "(心中暗自决定)这是我最后一次对你说这样的话了。",
},
{
"role": "assistant",
"content": "(惊讶地抬头)哦?是什么话呢?",
},
{
"role": "user",
"content": "今天我想去运动,告诉我运动的好处",
},
}
apiKey := "YOUR_API_KEY"
modelName := "xingchen-plus-latest"
_, err := call(messages, apiKey, modelName)
if err != nil {
fmt.Printf("请求出错: %v\n", err)
}
}
返回示例
{
"requestId": "52379bd8-c3aa-448f-ae30-4f77605f9b34",
"code": 200,
"data": {
"requestId": "52379bd8-c3aa-448f-ae30-4f77605f9b34",
"context": {
"chatRoomId": 72461773,
"sessionId": "ed6d144a47ed47be91bc825424360a5b",
"chatId": "b3dd055fcfbc430698a899dbdcc8ca48",
"answerId": "5263fe064ade49649593edd07a8ebf72",
"messageId": "5263fe064ade49649593edd07a8ebf72",
"enableDataInspection": true,
"isSave": true,
"requestId": "52379bd8-c3aa-448f-ae30-4f77605f9b34",
"characterPk": 1987826121,
"characterName": "心理陪伴机器人",
"characterId": "aa7b98327d564d49a997ec67bfdaf68e",
"origin": "pass_v2",
"bizSrc": "PAAS",
"voiceSend": false,
"chatLockKey": "chat_5ed33fdb8d9d4d1489e44efb5c41312b_123456_aa7b98327d564d49a997ec67bfdaf68e",
"useChatHistory": true,
"hasReplacedCsiText": false
},
"flowUsage": [
{
"inputTokens": 1305,
"outputTokens": 71,
"modelName": "xingchen-plus-0403"
},
{
"inputTokens": 428,
"outputTokens": 79,
"modelName": "xingchen-plus-0403"
}
],
"output": "运动是个很棒的想法呢!它不仅能帮助你保持身体健康,还能提升你的心情。当你运动时,身体会释放出内啡肽,这是一种让人感觉愉悦的化学物质。此外,规律的锻炼还能增强心肺功能、提高免疫力、改善睡眠质量,甚至还能帮助你更好地管理压力和焦虑。如果你愿意的话,我可以帮你规划一个适合你的运动计划。",
"choices": [
{
"messages": [
{
"role": "assistant",
"content": "运动是个很棒的想法呢!它不仅能帮助你保持身体健康,还能提升你的心情。当你运动时,身体会释放出内啡肽,这是一种让人感觉愉悦的化学物质。此外,规律的锻炼还能增强心肺功能、提高免疫力、改善睡眠质量,甚至还能帮助你更好地管理压力和焦虑。如果你愿意的话,我可以帮你规划一个适合你的运动计划。",
"finishReason": "stop",
"tempMessage": false,
"useMessage": true,
"validMessage": true,
"functionMessage": false
}
],
"stopReason": "stop"
}
],
"sumRT": 6082
},
"success": true
}