HTTP
更新时间:
流式调用
shell
curl --location 'https://nlp.aliyuncs.com/v2/api/chat/send' \
--header 'accept: */*' \
--header 'Content-Type: application/json' \
--header 'X-DashScope-DataInspection: enable' \
--header 'X-DashScope-SSE: enable' \
--header 'x-fag-servicename: aca-chat-send-sse' \
--header 'x-fag-appcode: aca' \
--header 'Authorization: Bearer {your-api-key}' \
--data '{
"input": {
"messages": [
{
"role": "user",
"content": "你好"
},
{
"role": "assistant",
"content": "你好呀"
},
{
"role": "user",
"content": "今天我们出去玩吧?"
}
],
"aca": {
"botProfile": {
"enableOpenLine": false,
//角色名必填
"name": "海梦",
"type": "assistant",
"customTemplateId": "8474991ea7cb4bee8f254ac93237cf79",
"params": {
"characterName": "海梦",
"characterSex": "女",
"characterBirthday": "5月20日"
}
},
"userProfile": {
//userId 可以自定义
"userId": "123456789",
"basicInfo": ""
},
"scenario": {
"description": ""
}
}
}
}'
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}"
}
"input": {
"messages": [
{
"role": "user",
"content": "你好"
},
{
"role": "assistant",
"content": "你好呀"
},
{
"role": "user",
"content": "今天我们出去玩吧?"
}
],
"aca": {
"botProfile": {
"name": "海梦",
"type": "assistant",
"customTemplateId": "8474991ea7cb4bee8f254ac93237cf79",
"params": {
"characterName": "海梦",
"characterSex": "女",
"characterBirthday": "5月20日"
}
},
"userProfile": {
"userId": "123456789",
"basicInfo": ""
},
"scenario": {
"description": ""
}
}
},
"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": [
{
"role": "user",
"content": "你好"
},
{
"role": "assistant",
"content": "你好呀"
},
{
"role": "user",
"content": "今天我们出去玩吧?"
}
],
"aca": {
"botProfile": {
"enableOpenLine": false,
//角色名必填
"name": "海梦",
"type": "assistant",
"customTemplateId": "8474991ea7cb4bee8f254ac93237cf79",
"params": {
"characterName": "海梦",
"characterSex": "女",
"characterBirthday": "5月20日"
}
},
"userProfile": {
//userId 可以自定义
"userId": "123456789",
"basicInfo": ""
},
"scenario": {
"description": ""
}
}
}
}'
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": [
{
"role": "user",
"content": "你好"
},
{
"role": "assistant",
"content": "你好呀"
},
{
"role": "user",
"content": "今天我们出去玩吧?"
}
],
"aca": {
"botProfile": {
"name": "海梦",
"type": "assistant",
"customTemplateId": "8474991ea7cb4bee8f254ac93237cf79",
"params": {
"characterName": "海梦",
"characterSex": "女",
"characterBirthday": "5月20日"
}
},
"userProfile": {
"userId": "123456789",
"basicInfo": ""
},
"scenario": {
"description": ""
}
}
},
"model": "xingchen-plus"
}
response = requests.post(url, headers=headers, json=payload)
print(response.status_code)
print(json.loads(response.text))
{'requestId': 'cc4f154735a1906739ab057a81892e63', 'code': 200, 'data': {'requestId': 'cc4f154735a1906739ab057a81892e63', 'choices': [{'messages': [{'role': 'assistant', 'content': '听起来很不错,我们可以去找一些有趣的地方拍照。', 'finishReason': 'stop', 'functionMessage': False, 'validMessage': True}], 'stopReason': 'stop'}], 'usage': {'outputTokens': 10, 'inputTokens': 489, 'userTokens': 9, 'pluginTokens': 0}, 'context': {'answerId': '45a5e041937841bc83549387d8580b31', 'enableDataInspection': True, 'isSave': False, 'requestId': 'cc4f154735a1906739ab057a81892e63', 'modelRequestId': 'ae15196c-f5e5-9196-88c5-38b9b2bb041b', 'modelName': 'xingchen-plus', 'origin': 'pass_v2', 'bizSrc': 'PAAS', 'ext': 'chat', 'isRegenerate': False, 'bizUserId': '123456789', 'usePlatformChatHistory': True, 'chatLockKey': 'chat_3ec1527c744c47708189d95a335b3fff_123456789_null_1716342894244', 'resultCount': 1}, 'stop': True}, 'success': True}
文档内容是否对您有帮助?