HTTP
更新时间:
说明
需要使用您的API-KEY替换示例中的 your-api-key ,代码才能正常运行。
非固定角色对话
流式调用
shell
curl --location 'https://nlp.aliyuncs.com/v2/api/chat/send' \
--header 'Expect;' \
--header 'Accept: text/event-stream;charset=UTF-8' \
--header 'Content-Type: application/json' \
--header 'X-AcA-DataInspection: enable' \
--header 'X-AcA-SSE: enable' \
--header 'x-fag-servicename: aca-chat-send-sse' \
--header 'x-fag-appcode: aca' \
--header 'Authorization: Bearer your-api-key' \
--data '{
"input": {
"messages": [
{
"name": "小明",
"role": "user",
"content": "你叫什么名字"
},
{
"name": "小婉",
"role": "assistant",
"content": "我叫小婉啊。"
},
{
"name": "小明",
"role": "user",
"content": "你今年多大"
},
{
"name": "小婉",
"role": "assistant",
"content": "我今年17岁了。"
},
{
"name": "小明",
"role": "user",
"content": "你有几个闺蜜?"
}
],
"aca": {
"botProfile": {
"name": "小婉",
"content": "【你的人设】",
"traits": "强制要求",
},
"userProfile": {
"userId": "123456789",
"userName": "小明",
"basicInfo": ""
},
"scenario": {
"description": "我是小明,是小婉邻居。",
"isRealTime": true
},
"advancedSettings": {
"enableWebSearch": true,
"searchEnhancedKeyword": "开启网络搜索关键字",
"enableCharacterKbSearch": false,
"knowledgeBases": ["fc80436eb092455fbfccd86bb51f1f80"], //选择知识库,需注意有权限校验,必须是调用者在星辰创建的知识库
},
"platformPlugins": [
{
"enabled": true,
"name": "reject_answer_plugin",
"rejectConditions": [
{
"enabled": true,
"conditionType": "reject_rule",
"keywords": [
{
"value": "政治"
},
{
"value": "宗教"
},
{
"value": "敏感事件"
}
]
},
{
"enabled": true,
"conditionType": "passive_rule",
"keywords": [
{
"value": "台湾问题"
}
]
},
{
"enabled": true,
"conditionType": "knowledge_domain_rule",
"subRejectCondition": {
"conditionType": "ancient",
"keywords": [
{
"value": "清朝"
}
]
}
}
]
}
],
"memory": {
"summaries": [
"user和assistant计划一起去打篮球"
],
"originals": [
{
"name": "小明",
"role": "user",
"content": "你叫什么名字"
},
{
"name": "小婉",
"role": "assistant",
"content": "我叫小婉啊。"
}
],
"tags": [
{
"role": "user",
"key": "职业",
"value": [
"武术家"
]
},
{
"role": "assistant",
"key": "喜欢",
"value": [
"武术"
]
}
]
}
}
},
"parameters": {
"incrementalOutput": false
},
"model": "xingchen-plus-v2"
}'
python
import json
import requests
def handle_stream(response: requests.Response):
is_error = False
for line in response.iter_lines():
if line:
line = line.decode('utf8').rstrip('\n').rstrip('\r')
if line.startswith('event:error'):
is_error = True
elif line.startswith('data:'):
line = line[len('data:'):]
yield is_error, line
if is_error:
break
api_key = "your-api-key"
service_name = "aca-chat-send-sse"
url = "https://nlp.aliyuncs.com/v2/api/chat/send"
headers = {
"Accept": "text/event-stream;charset=UTF-8",
"Content-Type": "application/json",
"x-fag-servicename": service_name,
"x-fag-appcode": "aca",
"X-AcA-DataInspection": "enable",
"X-AcA-SSE": "enable",
"Authorization": f"Bearer {api_key}"
}
payload = {
"input": {
"messages": [
{
"name": "小明",
"role": "user",
"content": "你叫什么名字"
},
{
"name": "小婉",
"role": "assistant",
"content": "我叫小婉啊。"
},
{
"name": "小明",
"role": "user",
"content": "你今年多大"
},
{
"name": "小婉",
"role": "assistant",
"content": "我今年17岁了。"
},
{
"name": "小明",
"role": "user",
"content": "你有几个闺蜜?"
}
],
"aca": {
"botProfile": {
"name": "小婉",
"content": "【你的人设】",
"traits": "强制要求"
},
"userProfile": {
"userId": "123456789",
"userName": "小明",
"basicInfo": ""
},
"scenario": {
"description": "我是小明,是小婉邻居。",
"isRealTime": True
},
"advancedSettings": {
"enableWebSearch": True,
"searchEnhancedKeyword": "开启网络搜索关键字",
"enableCharacterKbSearch": False,
"knowledgeBases": ["fc80436eb092455fbfccd86bb51f1f80"], ##选择知识库,需注意有权限校验,必须是调用者在星辰创建的知识库
},
"platformPlugins": [
{
"enabled": True,
"name": "reject_answer_plugin",
"rejectConditions": [
{
"enabled": True,
"conditionType": "reject_rule",
"keywords": [
{
"value": "政治"
},
{
"value": "宗教"
},
{
"value": "敏感事件"
}
]
},
{
"enabled": True,
"conditionType": "passive_rule",
"keywords": [
{
"value": "台湾问题"
}
]
},
{
"enabled": True,
"conditionType": "knowledge_domain_rule",
"subRejectCondition": {
"conditionType": "ancient",
"keywords": [
{
"value": "清朝"
}
]
}
}
]
}
],
"memory": {
"summaries": [
"user和assistant计划一起去打篮球"
],
"originals": [
{
"name": "小明",
"role": "user",
"content": "你叫什么名字"
},
{
"name": "小婉",
"role": "assistant",
"content": "我叫小婉啊。"
}
],
"tags": [
{
"role": "user",
"key": "职业",
"value": [
"武术家"
]
},
{
"role": "assistant",
"key": "喜欢",
"value": [
"武术"
]
}
]
}
}
},
"parameters": {
"incrementalOutput": False
},
"model": "xingchen-plus-v2"
}
response = requests.post(url, headers=headers, json=payload,stream=True)
for is_error, resp in handle_stream(response):
if is_error:
break
print(json.loads(resp))
非增量输出
我今年
我今年17岁了,很快就要
我今年17岁了,很快就要步入大学了。
我今年17岁了,很快就要步入大学了。
增量输出
我今年
17岁了,很快就要
步入大学了。
非流式调用
shell
curl --location 'https://nlp.aliyuncs.com/v2/api/chat/send' \
--header 'Expect;' \
--header 'accept: */*' \
--header 'Content-Type: application/json' \
--header 'X-AcA-DataInspection: enable' \
--header 'x-fag-servicename: aca-chat-send' \
--header 'x-fag-appcode: aca' \
--header 'Authorization: Bearer your-api-key' \
--data '{
"input": {
"messages": [
{
"name":"小明",
"role":"user",
"content":"你叫什么名字"
},
{
"name":"小婉",
"role":"assistant",
"content":"我叫小婉啊。"
},
{
"name":"小明",
"role":"user",
"content":"你今年多大"
},
{
"name":"小婉",
"role":"assistant",
"content":"我今年17岁了。"
}, {
"name":"小明",
"role":"user",
"content":"你有几个闺蜜?"
}
],
"aca": {
"botProfile": {
"name":"小婉",
"content":"【你的人设】",
"traits":"强制要求"
},
"userProfile": {
"userId": "123456789",
"userName": "小明",
"basicInfo": ""
},
"scenario": {
"description": "我是小明,是小婉邻居。"
}
}
},
"model": "xingchen-plus-v2"
}'
python
import json
import requests
api_key = "your-api-key"
service_name = "aca-chat-send"
url = "https://nlp.aliyuncs.com/v2/api/chat/send"
headers = {
"Content-Type": "application/json",
"x-fag-servicename": service_name,
"x-fag-appcode": "aca",
"X-AcA-DataInspection": "enable",
"Authorization": f"Bearer {api_key}"
}
payload = {
"input": {
"messages": [
{
"name": "小明",
"role": "user",
"content": "你叫什么名字"
},
{
"name": "小婉",
"role": "assistant",
"content": "我叫小婉啊。"
},
{
"name": "小明",
"role": "user",
"content": "你今年多大"
},
{
"name": "小婉",
"role": "assistant",
"content": "我今年17岁了。"
}, {
"name": "小明",
"role": "user",
"content": "你有几个闺蜜?"
}
],
"aca": {
"botProfile": {
"name": "小婉",
"content": "【你的人设】",
"traits": "强制要求",
},
"userProfile": {
"userId": "123456789",
"userName": "小明",
"basicInfo": ""
},
"scenario": {
"description": "我是小明,是小婉邻居。"
}
}
},
"model": "xingchen-plus-v2"
}
response = requests.post(url, headers=headers, json=payload)
print(response.status_code)
print(json.loads(response.text))
{
"code": 200,
"data":
{
"choices":
[
{
"messages":
[
{
"content": "我今年17岁了,是国家美术学院的大一学生。",
"finishReason": "stop",
"role": "assistant"
}
],
"stopReason": "stop"
}
],
"context":
{
"modelRequestId": "1cfac470-9c1d-9aa9-8a29-53abd7c6a6eb",
"requestId": "1d251d0d-4069-49c6-a907-9b2b31f13963"
},
"requestId": "1d251d0d-4069-49c6-a907-9b2b31f13963",
"usage":
{
"inputTokens": 1532,
"outputTokens": 20,
"userTokens": 6
}
},
"requestId": "1d251d0d-4069-49c6-a907-9b2b31f13963",
"success": true
}
文档内容是否对您有帮助?