您可以通过HTTP或SDK的方式来集成百炼应用(智能体、工作流或智能体编排)到您的业务系统中。
前提条件
您可以使用SDK或HTTP接口调用百炼的应用,请您根据您的需求,参考以下方式准备您的计算环境。
调用方式 | 准备条件 |
通过SDK调用 | DashScope SDK提供了Python和Java两个版本,请参考安装SDK,安装最新版SDK。 |
通过HTTP调用 | 如果您需要通过HTTP方式进行调用,需要配置如下:
|
无论使用哪种调用方式,建议您将百炼API-KEY配置到环境变量。
如何使用
单轮会话
您可以参考以下示例代码,通过SDK或HTTP方式调用百炼的应用,体验单轮会话的功能。
Python
请求示例
import os
from http import HTTPStatus
from dashscope import Application
response = Application.call(
# 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
api_key=os.getenv("DASHSCOPE_API_KEY"),
app_id='YOUR_APP_ID',
prompt='你是谁?')
if response.status_code != HTTPStatus.OK:
print(f'request_id={response.request_id}\ncode={response.status_code}\nmessage={response.message}\n')
else:
print(f'request_id={response.request_id}\noutput={response.output}\nusage={response.usage}\n')
响应示例
request_id=e3bba3a5-5784-9592-aa18-03170770de1e
output={"text": "我是来自阿里云的超大规模语言模型,我叫通义千问。
我可以回答各种问题、提供信息、与您进行对话和交流。有什么可以帮助您的吗?",
"finish_reason": "stop", "session_id": "a0606349b3c34c6fad7cf1d6454e0072",
"thoughts": null, "doc_references": null}
usage={"models": [{"model_id": "qwen-max", "input_tokens": 121, "output_tokens": 37}]}
Java
请求示例
package org.example;
// 建议dashscope SDK的版本 >= 2.12.0
import com.alibaba.dashscope.app.*;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
public class Test {
public static void appCall()
throws ApiException, NoApiKeyException, InputRequiredException {
ApplicationParam param = ApplicationParam.builder()
// 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.appId("YOUR_APP_ID")
.prompt("你是谁?")
.build();
Application application = new Application();
ApplicationResult result = application.call(param);
System.out.printf("requestId: %s, text: %s, finishReason: %s\n",
result.getRequestId(), result.getOutput().getText(), result.getOutput().getFinishReason());
if (result.getUsage() != null && result.getUsage().getModels() != null) {
for (ApplicationUsage.ModelUsage usage : result.getUsage().getModels()) {
System.out.printf("modelId: %s, inputTokens: %d, outputTokens: %d\n",
usage.getModelId(), usage.getInputTokens(), usage.getOutputTokens());
}
}
}
public static void main(String[] args) {
try {
appCall();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
throw new RuntimeException(e);
}
System.exit(0);
}
}
响应示例
requestId: 1458c2c0-05aa-9df3-9f99-e36d6a11cfbe,
text: 我是来自阿里云的语言模型,名叫通义千问。我被设计用来帮助用户获得信息、解答问题和进行对话。有什么我可以帮助你的吗?,
finishReason: stop
modelId: qwen-max, inputTokens: 121, outputTokens: 34
curl
请求示例
curl -X POST https://dashscope.aliyuncs.com/api/v1/apps/YOUR_APP_ID/completion \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"input": {
"prompt": "你是谁?"
},
"parameters": {},
"debug": {}
}'
响应示例
{"output":{"finish_reason":"stop",
"session_id":"232ea2e9e6ef448db6b14465c06a9a56",
"text":"我是来自阿里云的超大规模语言模型,我叫通义千问。
我是一个能够回答问题、创作文字,还能表达观点、撰写代码的AI助手。
如果您有任何问题或需要帮助,请随时告诉我,我会尽力为您提供帮助。"},
"usage":{"models":[{"output_tokens":51,"model_id":"qwen-max","input_tokens":121}]},
"request_id":"661c9cad-e59c-9f78-a262-78eff243f151"}%
多轮会话
相比于单轮会话,多轮会话可以让大模型参考历史对话信息,更符合日常交流的场景。
可以参考以下示例代码,上一次调用后返回的session_id
作为下一次调用时的传入参数,实现在下一次调用模型服务时,携带了上一次调用的会话信息。
目前仅智能体应用和对话型工作流应用支持多轮会话功能。
Python
请求示例
import os
from http import HTTPStatus
from dashscope import Application
def call_with_session():
response = Application.call(
# 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
api_key=os.getenv("DASHSCOPE_API_KEY"),
app_id='YOUR_APP_ID',
prompt='我想去新疆')
if response.status_code != HTTPStatus.OK:
print('request_id=%s, code=%s, message=%s\n' % (response.request_id, response.status_code, response.message))
return response
response1 = Application.call(
# 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
api_key=os.getenv("DASHSCOPE_API_KEY"),
app_id='YOUR_APP_ID',
prompt='那边有什么旅游景点或者美食?',
session_id=response.output.session_id)
if response1.status_code != HTTPStatus.OK:
print('request_id=%s, code=%s, message=%s\n' % (response1.request_id, response1.status_code, response1.message))
else:
print('request_id=%s\n output=%s\n usage=%s\n' % (response1.request_id, response1.output, response1.usage))
if __name__ == '__main__':
call_with_session()
响应示例
request_id=a9d3f1b1-6745-981a-af82-9ec58522d78f
output={"text": "新疆不仅旅游景点丰富多样,美食也同样令人向往。
以下是一些不可错过的旅游景点和美食推荐:\n\n### 旅游景点\n\n
1. **赛里木湖**:被誉为“大西洋最后一滴眼泪”,水质清澈,四周雪山环抱,是摄影和休闲的好去处。\n
2. **禾木村**:位于布尔津县,是一个充满原始风貌的图瓦人村落,秋天时金黄的白桦林和炊烟袅袅的木屋构成了一幅幅美丽的画卷。\n
3. **库车大峡谷**(天山神秘大峡谷):红褐色的岩石经过亿万年的风蚀水侵形成了独特的地貌,行走其中仿佛进入另一个世界。\n
4. **塔克拉玛干沙漠**:中国最大的沙漠,可以体验骑骆驼、沙漠探险,观赏壮观的日落与星空。\n
5. **国际大巴扎**(乌鲁木齐):购买手工艺品、品尝新疆特色小吃,体验浓郁的异域风情和商贸氛围。\n\n### 美食\n\n
1. **羊肉串**:新疆的羊肉串以其肉质鲜美、调料独特而闻名,是夜市上不可或缺的美食。\n
2. **大盘鸡**:鸡肉与土豆、青椒等食材炖煮而成,味道鲜美,分量十足,通常搭配宽面食用。\n
3. **手抓饭**:以羊肉、胡萝卜、洋葱等食材和大米一起烹制,饭粒松软,香气四溢。\n
4. **馕**:新疆的传统面食,外皮酥脆,内里柔软,是旅行中的理想干粮。\n
5. **哈密瓜、葡萄**:新疆水果以甜美著称,特别是哈密瓜和各种葡萄品种,夏季时令最佳。\n
6. **酸奶**:自制的新疆酸奶浓厚香醇,常配以蜂蜜或果仁食用,是解腻佳品。\n\n无论是自然风光还是地方美食,新疆都能为旅行者提供难忘的体验。
希望这些建议能帮助您规划一次精彩的旅程!",
"finish_reason": "stop",
"session_id": "443f935a8d80496f94dcfa4a04b7edfa",
"thoughts": null, "doc_references": null}
usage={"models": [{"model_id": "qwen-max",
"input_tokens": 688,
"output_tokens": 423}]}
Java
请求示例
package org.example;
import com.alibaba.dashscope.app.*;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import java.util.Arrays;
import java.util.List;
public class Test {
public static void callWithSession()
throws ApiException, NoApiKeyException, InputRequiredException {
ApplicationParam param = ApplicationParam.builder()
// 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.appId("YOUR_APP_ID")
.prompt("我想去新疆")
.build();
Application application = new Application();
ApplicationResult result = application.call(param);
param.setSessionId(result.getOutput().getSessionId());
param.setPrompt("那边有什么旅游景点或者美食?");
result = application.call(param);
System.out.printf("requestId: %s, text: %s, finishReason: %s\n",
result.getRequestId(), result.getOutput().getText(), result.getOutput().getFinishReason());
}
public static void main(String[] args) {
try {
callWithSession();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.out.printf("Exception: %s", e.getMessage());
}
System.exit(0);
}
}
响应示例
requestId: 440a0772-3c67-922e-8acb-d90840b96217, text: 新疆作为一个旅游资源极为丰富的地区,不仅自然景观壮美,人文景观也极具特色。以下是一些著名的旅游景点及当地不可错过的美食推荐:
### 旅游景点
1. **喀纳斯湖**:被誉为“人间仙境”,以其变幻莫测的湖水颜色、周边的原始森林和神秘的水怪传说而著名。
2. **那拉提草原**:位于伊犁河谷,夏季绿草如茵,野花遍地,是体验哈萨克族游牧文化的绝佳地点。
3. **火焰山**:位于吐鲁番盆地,因《西游记》中的故事而闻名,独特的红色山体在阳光下如同火焰般耀眼。
4. **天山天池**:高山湖泊,四周被雪山环抱,水质清澈,风景如画。
5. **喀什老城**:拥有千年历史的古城,漫步在这里,可以感受到浓郁的维吾尔族风情和历史文化。
6. **塔克拉玛干沙漠**:中国最大的沙漠,体验骑骆驼穿越沙丘,观赏沙漠日落的壮丽景象。
7. **禾木村**:被誉为中国最美的乡村之一,秋季时分,村庄被金黄色的白桦林环绕,美不胜收。
### 美食推荐
1. **羊肉串**:新疆的标志性美食,选用优质羊肉,炭火烤制,香气四溢。
2. **手抓饭**:用羊肉、胡萝卜、洋葱等食材与大米一起烹制,味道鲜美,营养丰富。
3. **大盘鸡**:鸡肉与土豆、辣椒一同炖煮,再配上宽面,色香味俱全。
4. **馕**:新疆的主食之一,外皮酥脆,内里绵软,便于携带,是长途旅行的理想食物。
5. **拉条子**:类似面条,但更粗更有嚼劲,常搭配各种肉类和蔬菜。
6. **烤全羊**:节日和庆典上的重头菜,外皮金黄酥脆,肉质鲜嫩多汁。
7. **酸奶疙瘩**:一种特色奶制品,口感酸甜,营养丰富,是解腻佳品。
8. **哈密瓜、葡萄**:新疆的水果享誉全国,尤其以哈密瓜和葡萄最为出名,香甜可口。
无论是自然风光的探索还是美食的享受,新疆都能给你带来难忘的体验。旅行时不妨尝试融入当地生活,体验更多地道的新疆文化。, finishReason: stop
curl
请求示例(上一轮会话)
curl -X POST https://dashscope.aliyuncs.com/api/v1/apps/YOUR_APP_ID/completion \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"input": {
"prompt": "我想去新疆"
},
"parameters": {},
"debug": {}
}'
响应示例(上一轮会话)
{"output":{"finish_reason":"stop",
"session_id":"d380aeb21d4b4b018f1d7ae9f98de404",
"text":"新疆,全称新疆维吾尔自治区,是中国的一个自治区,位于中国西北部,
是中国面积最大的省级行政区。新疆地域辽阔,自然风光壮丽,拥有丰富的文化和民族多样性,
著名的景点包括天山、喀纳斯湖、吐鲁番的火焰山、库车大峡谷、伊犁草原以及丝绸之路沿线的历史遗迹等。\n\n
如果你计划去新疆旅行,有几点建议:\n\n1. **时间选择**:新疆四季分明,夏季(6月至8月)是旅游旺季,天气适宜,但也是游客最多的时候。春秋两季(4月至5月,9月至10月)景色独特,人少景美,也是不错的选择。冬季寒冷且长,适合滑雪和观赏雪景。\n\n2. **行程规划**:新疆地域广阔,城市之间距离遥远,建议提前规划好行程,考虑租车或参加当地的旅游团。北疆以自然风光为主,如喀纳斯、禾木;南疆则更多展现人文风情,如喀什古城、和田。\n\n3. **安全须知**:新疆部分地区由于特殊的地理和社会环境,可能会有一些安全检查和限制措施,请务必遵守当地规定,携带身份证件,并关注最新的旅行安全提示。\n\n4. **适应时差**:新疆使用乌鲁木齐时间(UTC+6),与北京时间(UTC+8)有2小时时差,注意调整作息。\n\n5. **饮食体验**:新疆美食丰富,如羊肉串、手抓饭、拉条子、馕、哈密瓜等,别具风味,值得尝试。\n\n6. **尊重当地文化**:新疆是一个多民族聚居的地区,旅行中应尊重各民族的风俗习惯和宗教信仰,尤其是参观清真寺等地时,注意着装得体。\n\n记得提前办理好相关旅行证件,查询最新的旅行政策,希望你在新疆有一个难忘的旅程!"},
"usage":{"models":[{"output_tokens":389,"model_id":"qwen-max","input_tokens":51}]},
"request_id":"a1e8cbb2-51dc-9733-a97c-e7ca5a7f0438"}%
请求示例(下一轮会话)
curl -X POST https://dashscope.aliyuncs.com/api/v1/apps/YOUR_APP_ID/completion \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"input": {
"prompt": "那边有什么美食?",
"session_id":"d380aeb21d4b4b018f1d7ae9f98de404"
},
"parameters": {},
"debug": {}
}'
响应示例(下一轮会话)
{"output":{"finish_reason":"stop",
"session_id":"d380aeb21d4b4b018f1d7ae9f98de404",
"text":"新疆是一个美食天堂,因其独特的地理位置和多元的文化交融,孕育出了许多令人垂涎的特色美食。
以下是一些你到了新疆不可错过的地道美食:\n\n
1. **羊肉串**:新疆羊肉串以其大块的肉、香辣的调味和独特的烤制方法而闻名,是夜市和街头最常见的小吃之一。\n\n
2. **大盘鸡**:一道结合了鸡肉、土豆、青椒、宽面等食材的菜肴,味道鲜美,分量十足,是新疆餐饮中的明星菜品。\n\n
3. **手抓饭**:主要由米饭、羊肉、胡萝卜、洋葱等烹饪而成,饭粒松软,肉香四溢,色彩诱人。\n\n
4. **拉条子**:又称拌面,是新疆特色的面食,面条筋道,通常搭配各种蔬菜和牛肉或羊肉的炒菜一起食用。\n\n
5. **馕**:新疆的主食之一,是一种烤制的面饼,外皮酥脆,内里绵软,便于携带和储存,是旅行时的理想食物。\n\n
6. **哈密瓜和葡萄**:新疆因其干燥的气候和充足的日照,盛产甜美的水果,其中哈密瓜和葡萄尤为著名,甜度高,口感绝佳。\n\n
7. **酸奶**:新疆的手工酸奶浓郁而不腻,通常会加入蜂蜜或糖来调节口味,非常受欢迎。\n\n
8. **烤包子**:外表金黄酥脆,内馅多为羊肉和洋葱,是早餐或快餐的好选择。\n\n
9. **喀什噶尔冰激凌**:用传统方式制作的冰激凌,口感醇厚,奶香浓郁,有的还会加入坚果或果仁。\n\n
10. **纳仁**:一种羊肉炖煮后,与手工面片混合的菜肴,是哈萨克族的传统美食。\n\n
这些美食只是新疆众多美味中的冰山一角,到访新疆,你将有机会体验更多地道而丰富的味觉享受。"},
"usage":{"models":[{"output_tokens":411,"model_id":"qwen-max","input_tokens":454}]},
"request_id":"366d21f5-1a4a-9fe5-8ba2-e7955ff8cd23"}%
流式输出
大模型并不是一次性生成最终结果,而是逐步生成中间结果,最终结果由中间结果拼接而成。使用非流式输出方式需要等待模型生成结束后再将生成的中间结果拼接后返回,而流式输出可以实时地将中间结果返回,您可以在模型进行输出的同时进行阅读,减少等待模型回复的时间。
您可以通过DashScope SDK或HTTP方式调用应用,体验流式输出的功能。
完整参数列表参见应用调用API参考。
智能体应用流式输出调用示例如下:
Python
请求示例
import os from http import HTTPStatus from dashscope import Application responses = Application.call( # 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 api_key=os.getenv("DASHSCOPE_API_KEY"), app_id='YOUR_APP_ID', prompt='你是谁?', stream=True, incremental_output=True) for response in responses: if response.status_code != HTTPStatus.OK: print(f'request_id={response.request_id}\ncode={response.status_code}\nmessage={response.message}\n') else: print(f'request_id={response.request_id}\noutput={response.output}\nusage={response.usage}\n')
响应示例
request_id=b63c8c47-f4bd-9fa0-9cfc-f06da91588fe output={"text": "我是", "finish_reason": "null", "session_id": "42ece8924aa842a3b55405b6ddf97429", "thoughts": null, "doc_references": null} usage={"models": [{"model_id": "qwen-max", "input_tokens": 142, "output_tokens": 1}]} request_id=b63c8c47-f4bd-9fa0-9cfc-f06da91588fe output={"text": "来自", "finish_reason": "null", "session_id": "42ece8924aa842a3b55405b6ddf97429", "thoughts": null, "doc_references": null} usage={"models": [{"model_id": "qwen-max", "input_tokens": 142, "output_tokens": 2}]} request_id=b63c8c47-f4bd-9fa0-9cfc-f06da91588fe output={"text": "阿里", "finish_reason": "null", "session_id": "42ece8924aa842a3b55405b6ddf97429", "thoughts": null, "doc_references": null} usage={"models": [{"model_id": "qwen-max", "input_tokens": 142, "output_tokens": 3}]} request_id=b63c8c47-f4bd-9fa0-9cfc-f06da91588fe output={"text": "云的语言模型助手,", "finish_reason": "null", "session_id": "42ece8924aa842a3b55405b6ddf97429", "thoughts": null, "doc_references": null} usage={"models": [{"model_id": "qwen-max", "input_tokens": 142, "output_tokens": 8}]} request_id=b63c8c47-f4bd-9fa0-9cfc-f06da91588fe output={"text": "我叫通义千问。我可以", "finish_reason": "null", "session_id": "42ece8924aa842a3b55405b6ddf97429", "thoughts": null, "doc_references": null} usage={"models": [{"model_id": "qwen-max", "input_tokens": 142, "output_tokens": 16}]} request_id=b63c8c47-f4bd-9fa0-9cfc-f06da91588fe output={"text": "回答各种问题、提供信息、进行", "finish_reason": "null", "session_id": "42ece8924aa842a3b55405b6ddf97429", "thoughts": null, "doc_references": null} usage={"models": [{"model_id": "qwen-max", "input_tokens": 142, "output_tokens": 24}]} request_id=b63c8c47-f4bd-9fa0-9cfc-f06da91588fe output={"text": "对话等任务,很高兴为您服务!", "finish_reason": "null", "session_id": "42ece8924aa842a3b55405b6ddf97429", "thoughts": null, "doc_references": null} usage={"models": [{"model_id": "qwen-max", "input_tokens": 142, "output_tokens": 32}]} request_id=b63c8c47-f4bd-9fa0-9cfc-f06da91588fe output={"text": "有什么可以帮助您的吗?", "finish_reason": "null", "session_id": "42ece8924aa842a3b55405b6ddf97429", "thoughts": null, "doc_references": null} usage={"models": [{"model_id": "qwen-max", "input_tokens": 142, "output_tokens": 37}]} request_id=b63c8c47-f4bd-9fa0-9cfc-f06da91588fe output={"text": "", "finish_reason": "stop", "session_id": "42ece8924aa842a3b55405b6ddf97429", "thoughts": null, "doc_references": null} usage={"models": [{"model_id": "qwen-max", "input_tokens": 142, "output_tokens": 37}]}
Java
请求示例
package org.example; import com.alibaba.dashscope.app.*; import com.alibaba.dashscope.exception.ApiException; import com.alibaba.dashscope.exception.InputRequiredException; import com.alibaba.dashscope.exception.NoApiKeyException; import io.reactivex.Flowable; public class Test { public static void streamCall() throws NoApiKeyException, InputRequiredException { ApplicationParam param = ApplicationParam.builder() // 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 .apiKey(System.getenv("DASHSCOPE_API_KEY")) .appId("YOUR_APP_ID") .prompt("你是谁?") .incrementalOutput(true) .build(); Application application = new Application(); Flowable<ApplicationResult> result = application.streamCall(param); result.blockingForEach(data -> { System.out.printf("requestId: %s, text: %s, finishReason: %s\n", data.getRequestId(), data.getOutput().getText(), data.getOutput().getFinishReason()); }); } public static void main(String[] args) { try { streamCall(); } catch (ApiException | NoApiKeyException | InputRequiredException e) { System.out.printf("Exception: %s", e.getMessage()); } System.exit(0); } }
响应示例
requestId: a36ae034-bc33-91d0-a50f-3f4051211303, text: 我是, finishReason: null requestId: a36ae034-bc33-91d0-a50f-3f4051211303, text: 来自, finishReason: null requestId: a36ae034-bc33-91d0-a50f-3f4051211303, text: 阿里, finishReason: null requestId: a36ae034-bc33-91d0-a50f-3f4051211303, text: 云的超大规模语言, finishReason: null requestId: a36ae034-bc33-91d0-a50f-3f4051211303, text: 模型,我叫通义千问, finishReason: null requestId: a36ae034-bc33-91d0-a50f-3f4051211303, text: 。我可以生成各种类型的文本,如, finishReason: null requestId: a36ae034-bc33-91d0-a50f-3f4051211303, text: 文章、故事、诗歌、故事等, finishReason: null requestId: a36ae034-bc33-91d0-a50f-3f4051211303, text: ,并能够根据不同的场景和需求进行, finishReason: null requestId: a36ae034-bc33-91d0-a50f-3f4051211303, text: 变换和扩展。此外,我还能够, finishReason: null requestId: a36ae034-bc33-91d0-a50f-3f4051211303, text: 回答各种问题,提供帮助和解决方案, finishReason: null requestId: a36ae034-bc33-91d0-a50f-3f4051211303, text: 。如果您有任何问题或需要帮助,请, finishReason: null requestId: a36ae034-bc33-91d0-a50f-3f4051211303, text: 随时告诉我!, finishReason: null requestId: a36ae034-bc33-91d0-a50f-3f4051211303, text: , finishReason: stop
curl
请求示例
curl -X POST https://dashscope.aliyuncs.com/api/v1/apps/YOUR_APP_ID/completion \ --header "Authorization: Bearer $DASHSCOPE_API_KEY" \ --header 'Content-Type: application/json' \ --header 'X-DashScope-SSE: enable' \ --data '{ "input": { "prompt": "你是谁?" }, "parameters": { "incremental_output":true }, "debug": {} }'
响应示例
id:1 event:result :HTTP_STATUS/200 data:{"output":{"session_id":"70ac158ae65f4764b9228a52951f3711","finish_reason":"null","text":"我是"},"usage":{"models":[{"input_tokens":203,"output_tokens":1,"model_id":"qwen-max"}]},"request_id":"f66273ce-1a4d-9107-9c8a-da2a0f7267b5"} id:2 event:result :HTTP_STATUS/200 data:{"output":{"session_id":"70ac158ae65f4764b9228a52951f3711","finish_reason":"null","text":"来自"},"usage":{"models":[{"input_tokens":203,"output_tokens":2,"model_id":"qwen-max"}]},"request_id":"f66273ce-1a4d-9107-9c8a-da2a0f7267b5"} id:3 event:result :HTTP_STATUS/200 data:{"output":{"session_id":"70ac158ae65f4764b9228a52951f3711","finish_reason":"null","text":"阿里"},"usage":{"models":[{"input_tokens":203,"output_tokens":3,"model_id":"qwen-max"}]},"request_id":"f66273ce-1a4d-9107-9c8a-da2a0f7267b5"} id:4 event:result :HTTP_STATUS/200 data:{"output":{"session_id":"70ac158ae65f4764b9228a52951f3711","finish_reason":"null","text":"云"},"usage":{"models":[{"input_tokens":203,"output_tokens":4,"model_id":"qwen-max"}]},"request_id":"f66273ce-1a4d-9107-9c8a-da2a0f7267b5"} id:5 event:result :HTTP_STATUS/200 data:{"output":{"session_id":"70ac158ae65f4764b9228a52951f3711","finish_reason":"null","text":"的超大规模语言"},"usage":{"models":[{"input_tokens":203,"output_tokens":8,"model_id":"qwen-max"}]},"request_id":"f66273ce-1a4d-9107-9c8a-da2a0f7267b5"} id:6 event:result :HTTP_STATUS/200 data:{"output":{"session_id":"70ac158ae65f4764b9228a52951f3711","finish_reason":"null","text":"模型,我叫"},"usage":{"models":[{"input_tokens":203,"output_tokens":12,"model_id":"qwen-max"}]},"request_id":"f66273ce-1a4d-9107-9c8a-da2a0f7267b5"} id:7 event:result :HTTP_STATUS/200 data:{"output":{"session_id":"70ac158ae65f4764b9228a52951f3711","finish_reason":"null","text":"通义千问"},"usage":{"models":[{"input_tokens":203,"output_tokens":16,"model_id":"qwen-max"}]},"request_id":"f66273ce-1a4d-9107-9c8a-da2a0f7267b5"} id:8 event:result :HTTP_STATUS/200 data:{"output":{"session_id":"70ac158ae65f4764b9228a52951f3711","finish_reason":"null","text":"。"},"usage":{"models":[{"input_tokens":203,"output_tokens":17,"model_id":"qwen-max"}]},"request_id":"f66273ce-1a4d-9107-9c8a-da2a0f7267b5"} id:9 event:result :HTTP_STATUS/200 data:{"output":{"session_id":"70ac158ae65f4764b9228a52951f3711","finish_reason":"stop","text":""},"usage":{"models":[{"input_tokens":203,"output_tokens":17,"model_id":"qwen-max"}]},"request_id":"f66273ce-1a4d-9107-9c8a-da2a0f7267b5"}
工作流应用和智能体编排应用的流式输出实现需要设置
has_thoughts
为True。所有节点的流式输出(能流式的会流式输出比如LLM节点,非流式的会同步输出比如文本转换节点)在应用API中的thoughts
部分返回。这样既不会破坏结束节点最终的结果,也能满足用户多变的流式需求。参考下面多节点示例。如图所示已发布的工作流应用配置,对杭州的美食和景点进行流式输出。
通过API调用。
Python
请求示例
import os from http import HTTPStatus from dashscope import Application biz_params = {"city": "杭州"} responses = Application.call(# 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 api_key=os.getenv("DASHSCOPE_API_KEY"), app_id='YOUR_APP_ID', prompt='你好', biz_params=biz_params, stream=True, incremental_output=True, # incremental_output为true开启增量输出,为false关闭增量输出,不填写默认false has_thoughts=True) for response in responses: if response.status_code != HTTPStatus.OK: print(f'request_id={response.request_id}\ncode={response.status_code}\nmessage={response.message}\n') else: print(f'request_id={response.request_id}\noutput={response.output}\nusage={response.usage}\n')
响应示例(开启增量输出)
request_id=0975a13c-68b0-9dab-9177-99c3d3c696f8 output={"text": null, "finish_reason": "null", "session_id": "d6a40e90afc34037b4a81278a7314eae", "thoughts": null, "doc_references": null} usage={"models": null} request_id=0975a13c-68b0-9dab-9177-99c3d3c696f8 output={"text": null, "finish_reason": "null", "session_id": "d6a40e90afc34037b4a81278a7314eae", "thoughts": [{"thought": null, "action_type": null, "response": "{\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"2ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\"\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_Ilo9\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}], "doc_references": null} usage={"models": null} request_id=0975a13c-68b0-9dab-9177-99c3d3c696f8 output={"text": null, "finish_reason": "null", "session_id": "d6a40e90afc34037b4a81278a7314eae", "thoughts": [{"thought": null, "action_type": null, "response": "{\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"2ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\"西湖\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_Ilo9\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}], "doc_references": null} usage={"models": null} request_id=0975a13c-68b0-9dab-9177-99c3d3c696f8 output={"text": null, "finish_reason": "null", "session_id": "d6a40e90afc34037b4a81278a7314eae", "thoughts": [{"thought": null, "action_type": null, "response": "{\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"2ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\"醋鱼\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_Ilo9\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}], "doc_references": null} usage={"models": null} request_id=0975a13c-68b0-9dab-9177-99c3d3c696f8 output={"text": null, "finish_reason": "null", "session_id": "d6a40e90afc34037b4a81278a7314eae", "thoughts": [{"thought": null, "action_type": null, "response": "{\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"2ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\",龙井虾仁\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_Ilo9\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}], "doc_references": null} usage={"models": null} request_id=0975a13c-68b0-9dab-9177-99c3d3c696f8 output={"text": null, "finish_reason": "null", "session_id": "d6a40e90afc34037b4a81278a7314eae", "thoughts": [{"thought": null, "action_type": null, "response": "{\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"2ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\",东坡肉,叫花鸡\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_Ilo9\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}], "doc_references": null} usage={"models": null} request_id=0975a13c-68b0-9dab-9177-99c3d3c696f8 output={"text": null, "finish_reason": "null", "session_id": "d6a40e90afc34037b4a81278a7314eae", "thoughts": [{"thought": null, "action_type": null, "response": "{\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"2ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\",知味小笼\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"success\",\"nodeId\":\"LLM_Ilo9\",\"nodeExecTime\":\"1844ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\"\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_vQDv\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}], "doc_references": null} usage={"models": null} request_id=0975a13c-68b0-9dab-9177-99c3d3c696f8 output={"text": null, "finish_reason": "null", "session_id": "d6a40e90afc34037b4a81278a7314eae", "thoughts": [{"thought": null, "action_type": null, "response": "{\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"2ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\"\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"success\",\"nodeId\":\"LLM_Ilo9\",\"nodeExecTime\":\"1844ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\"西湖,宋\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_vQDv\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}], "doc_references": null} usage={"models": null} request_id=0975a13c-68b0-9dab-9177-99c3d3c696f8 output={"text": null, "finish_reason": "null", "session_id": "d6a40e90afc34037b4a81278a7314eae", "thoughts": [{"thought": null, "action_type": null, "response": "{\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"2ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\"\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"success\",\"nodeId\":\"LLM_Ilo9\",\"nodeExecTime\":\"1844ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\"城,灵隐寺\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_vQDv\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}], "doc_references": null} usage={"models": null} request_id=0975a13c-68b0-9dab-9177-99c3d3c696f8 output={"text": null, "finish_reason": "null", "session_id": "d6a40e90afc34037b4a81278a7314eae", "thoughts": [{"thought": null, "action_type": null, "response": "{\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"2ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\"\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"success\",\"nodeId\":\"LLM_Ilo9\",\"nodeExecTime\":\"1844ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\",西溪国家湿地公园,雷\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_vQDv\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}], "doc_references": null} usage={"models": null} request_id=0975a13c-68b0-9dab-9177-99c3d3c696f8 output={"text": "西湖,宋城,灵隐寺,西溪国家湿地公园,雷峰塔", "finish_reason": "stop", "session_id": "d6a40e90afc34037b4a81278a7314eae", "thoughts": [{"thought": null, "action_type": null, "response": "{\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"2ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\"\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"success\",\"nodeId\":\"LLM_Ilo9\",\"nodeExecTime\":\"1844ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\"峰塔\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"success\",\"nodeId\":\"LLM_vQDv\",\"nodeExecTime\":\"1387ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\"西湖,宋城,灵隐寺,西溪国家湿地公园,雷峰塔\\\"}\",\"nodeType\":\"End\",\"nodeStatus\":\"success\",\"nodeId\":\"End_DrQn7F\",\"nodeExecTime\":\"2ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}], "doc_references": null} usage={"models": null}
响应示例(关闭增量输出)
request_id=3b6b8ee1-a847-9cbc-9b3b-d67dd361f30a output={"text": null, "finish_reason": "null", "session_id": "10073bcd2e714f6c86c789be60b79688", "thoughts": null, "doc_references": null} usage={"models": null} request_id=3b6b8ee1-a847-9cbc-9b3b-d67dd361f30a output={"text": null, "finish_reason": "null", "session_id": "10073bcd2e714f6c86c789be60b79688", "thoughts": [{"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"sys\\\":{\\\"uid\\\":\\\"\\\",\\\"query\\\":\\\"你好\\\",\\\"tenantId\\\":\\\"10317360\\\",\\\"mainAccountUid\\\":\\\"1046927661150576\\\"},\\\"user\\\":{\\\"city\\\":\\\"杭州\\\"}}\",\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"2ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_Ilo9\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}], "doc_references": null} usage={"models": null} request_id=3b6b8ee1-a847-9cbc-9b3b-d67dd361f30a output={"text": null, "finish_reason": "null", "session_id": "10073bcd2e714f6c86c789be60b79688", "thoughts": [{"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"sys\\\":{\\\"uid\\\":\\\"\\\",\\\"query\\\":\\\"你好\\\",\\\"tenantId\\\":\\\"10317360\\\",\\\"mainAccountUid\\\":\\\"1046927661150576\\\"},\\\"user\\\":{\\\"city\\\":\\\"杭州\\\"}}\",\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"2ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\"西湖醋\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_Ilo9\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}], "doc_references": null} usage={"models": null} request_id=3b6b8ee1-a847-9cbc-9b3b-d67dd361f30a output={"text": null, "finish_reason": "null", "session_id": "10073bcd2e714f6c86c789be60b79688", "thoughts": [{"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"sys\\\":{\\\"uid\\\":\\\"\\\",\\\"query\\\":\\\"你好\\\",\\\"tenantId\\\":\\\"10317360\\\",\\\"mainAccountUid\\\":\\\"1046927661150576\\\"},\\\"user\\\":{\\\"city\\\":\\\"杭州\\\"}}\",\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"2ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\"西湖醋鱼\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_Ilo9\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}], "doc_references": null} usage={"models": null} request_id=3b6b8ee1-a847-9cbc-9b3b-d67dd361f30a output={"text": null, "finish_reason": "null", "session_id": "10073bcd2e714f6c86c789be60b79688", "thoughts": [{"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"sys\\\":{\\\"uid\\\":\\\"\\\",\\\"query\\\":\\\"你好\\\",\\\"tenantId\\\":\\\"10317360\\\",\\\"mainAccountUid\\\":\\\"1046927661150576\\\"},\\\"user\\\":{\\\"city\\\":\\\"杭州\\\"}}\",\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"2ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\"西湖醋鱼,龙井虾仁\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_Ilo9\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}], "doc_references": null} usage={"models": null} request_id=3b6b8ee1-a847-9cbc-9b3b-d67dd361f30a output={"text": null, "finish_reason": "null", "session_id": "10073bcd2e714f6c86c789be60b79688", "thoughts": [{"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"sys\\\":{\\\"uid\\\":\\\"\\\",\\\"query\\\":\\\"你好\\\",\\\"tenantId\\\":\\\"10317360\\\",\\\"mainAccountUid\\\":\\\"1046927661150576\\\"},\\\"user\\\":{\\\"city\\\":\\\"杭州\\\"}}\",\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"2ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\"西湖醋鱼,龙井虾仁,东坡肉,叫花鸡\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_Ilo9\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}], "doc_references": null} usage={"models": null} request_id=3b6b8ee1-a847-9cbc-9b3b-d67dd361f30a output={"text": null, "finish_reason": "null", "session_id": "10073bcd2e714f6c86c789be60b79688", "thoughts": [{"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"sys\\\":{\\\"uid\\\":\\\"\\\",\\\"query\\\":\\\"你好\\\",\\\"tenantId\\\":\\\"10317360\\\",\\\"mainAccountUid\\\":\\\"1046927661150576\\\"},\\\"user\\\":{\\\"city\\\":\\\"杭州\\\"}}\",\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"2ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\"西湖醋鱼,龙井虾仁,东坡肉,叫花鸡,宋嫂鱼羹\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"success\",\"nodeId\":\"LLM_Ilo9\",\"nodeExecTime\":\"1532ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_vQDv\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}], "doc_references": null} usage={"models": null} request_id=3b6b8ee1-a847-9cbc-9b3b-d67dd361f30a output={"text": null, "finish_reason": "null", "session_id": "10073bcd2e714f6c86c789be60b79688", "thoughts": [{"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"sys\\\":{\\\"uid\\\":\\\"\\\",\\\"query\\\":\\\"你好\\\",\\\"tenantId\\\":\\\"10317360\\\",\\\"mainAccountUid\\\":\\\"1046927661150576\\\"},\\\"user\\\":{\\\"city\\\":\\\"杭州\\\"}}\",\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"2ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\"西湖醋鱼,龙井虾仁,东坡肉,叫花鸡,宋嫂鱼羹\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"success\",\"nodeId\":\"LLM_Ilo9\",\"nodeExecTime\":\"1532ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\"西湖,灵\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_vQDv\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}], "doc_references": null} usage={"models": null} request_id=3b6b8ee1-a847-9cbc-9b3b-d67dd361f30a output={"text": null, "finish_reason": "null", "session_id": "10073bcd2e714f6c86c789be60b79688", "thoughts": [{"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"sys\\\":{\\\"uid\\\":\\\"\\\",\\\"query\\\":\\\"你好\\\",\\\"tenantId\\\":\\\"10317360\\\",\\\"mainAccountUid\\\":\\\"1046927661150576\\\"},\\\"user\\\":{\\\"city\\\":\\\"杭州\\\"}}\",\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"2ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\"西湖醋鱼,龙井虾仁,东坡肉,叫花鸡,宋嫂鱼羹\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"success\",\"nodeId\":\"LLM_Ilo9\",\"nodeExecTime\":\"1532ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\"西湖,灵隐寺,宋城\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_vQDv\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}], "doc_references": null} usage={"models": null} request_id=3b6b8ee1-a847-9cbc-9b3b-d67dd361f30a output={"text": null, "finish_reason": "null", "session_id": "10073bcd2e714f6c86c789be60b79688", "thoughts": [{"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"sys\\\":{\\\"uid\\\":\\\"\\\",\\\"query\\\":\\\"你好\\\",\\\"tenantId\\\":\\\"10317360\\\",\\\"mainAccountUid\\\":\\\"1046927661150576\\\"},\\\"user\\\":{\\\"city\\\":\\\"杭州\\\"}}\",\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"2ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\"西湖醋鱼,龙井虾仁,东坡肉,叫花鸡,宋嫂鱼羹\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"success\",\"nodeId\":\"LLM_Ilo9\",\"nodeExecTime\":\"1532ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\"西湖,灵隐寺,宋城,西溪国家湿地公园,雷\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_vQDv\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}], "doc_references": null} usage={"models": null} request_id=3b6b8ee1-a847-9cbc-9b3b-d67dd361f30a output={"text": "西湖,灵隐寺,宋城,西溪国家湿地公园,雷峰塔", "finish_reason": "stop", "session_id": "10073bcd2e714f6c86c789be60b79688", "thoughts": [{"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"sys\\\":{\\\"uid\\\":\\\"\\\",\\\"query\\\":\\\"你好\\\",\\\"tenantId\\\":\\\"10317360\\\",\\\"mainAccountUid\\\":\\\"1046927661150576\\\"},\\\"user\\\":{\\\"city\\\":\\\"杭州\\\"}}\",\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"2ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\"西湖醋鱼,龙井虾仁,东坡肉,叫花鸡,宋嫂鱼羹\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"success\",\"nodeId\":\"LLM_Ilo9\",\"nodeExecTime\":\"1532ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"{\\\"result\\\":\\\"西湖,灵隐寺,宋城,西溪国家湿地公园,雷峰塔\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"success\",\"nodeId\":\"LLM_vQDv\",\"nodeExecTime\":\"1217ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}, {"thought": null, "action_type": null, "response": "{\"nodeResult\":\"西湖,灵隐寺,宋城,西溪国家湿地公园,雷峰塔\",\"nodeType\":\"End\",\"nodeStatus\":\"success\",\"nodeId\":\"End_DrQn7F\",\"nodeExecTime\":\"2ms\"}", "action_name": null, "action": null, "action_input_stream": null, "action_input": null, "observation": null}], "doc_references": null} usage={"models": null}
Java
请求示例
package org.example; import com.alibaba.dashscope.app.*; import com.alibaba.dashscope.exception.ApiException; import com.alibaba.dashscope.exception.InputRequiredException; import com.alibaba.dashscope.exception.NoApiKeyException; import com.alibaba.dashscope.utils.JsonUtils; import io.reactivex.Flowable; public class Test { public static void streamCall() throws NoApiKeyException, InputRequiredException { String bizParams = "{\"city\":\"杭州\"}"; ApplicationParam param = ApplicationParam.builder() // 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 .apiKey(System.getenv("DASHSCOPE_API_KEY")) .appId("YOUR_APP_ID") .prompt("你好") .bizParams(JsonUtils.parse(bizParams)) .incrementalOutput(true) .hasThoughts(true) .build(); Application application = new Application(); Flowable<ApplicationResult> result = application.streamCall(param); result.blockingForEach(data -> { System.out.printf("%s\n",data); }); } public static void main(String[] args) { try { streamCall(); } catch (ApiException | NoApiKeyException | InputRequiredException e) { System.out.printf("Exception: %s", e.getMessage()); } System.exit(0); } }
响应示例(开启增量输出)
pplicationResult(requestId=0d49e27c-9e5d-9506-9d77-d09c5022ba14, output=ApplicationOutput(text=null, finishReason=null, sessionId=dda71c1247194c26822ec1dde5a9a4f6, thoughts=null, docReferences=null), usage=ApplicationUsage(models=null)) ApplicationResult(requestId=0d49e27c-9e5d-9506-9d77-d09c5022ba14, output=ApplicationOutput(text=null, finishReason=null, sessionId=dda71c1247194c26822ec1dde5a9a4f6, thoughts=[ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"sys\":{\"uid\":\"\",\"query\":\"你好\",\"tenantId\":\"10317360\",\"mainAccountUid\":\"1046927661150576\"},\"user\":{\"city\":\"杭州\"}}","nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"2ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_Ilo9"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null)], docReferences=null), usage=ApplicationUsage(models=null)) ApplicationResult(requestId=0d49e27c-9e5d-9506-9d77-d09c5022ba14, output=ApplicationOutput(text=null, finishReason=null, sessionId=dda71c1247194c26822ec1dde5a9a4f6, thoughts=[ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"sys\":{\"uid\":\"\",\"query\":\"你好\",\"tenantId\":\"10317360\",\"mainAccountUid\":\"1046927661150576\"},\"user\":{\"city\":\"杭州\"}}","nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"2ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"西湖\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_Ilo9"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null)], docReferences=null), usage=ApplicationUsage(models=null)) ApplicationResult(requestId=0d49e27c-9e5d-9506-9d77-d09c5022ba14, output=ApplicationOutput(text=null, finishReason=null, sessionId=dda71c1247194c26822ec1dde5a9a4f6, thoughts=[ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"sys\":{\"uid\":\"\",\"query\":\"你好\",\"tenantId\":\"10317360\",\"mainAccountUid\":\"1046927661150576\"},\"user\":{\"city\":\"杭州\"}}","nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"2ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"西湖醋鱼\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_Ilo9"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null)], docReferences=null), usage=ApplicationUsage(models=null)) ApplicationResult(requestId=0d49e27c-9e5d-9506-9d77-d09c5022ba14, output=ApplicationOutput(text=null, finishReason=null, sessionId=dda71c1247194c26822ec1dde5a9a4f6, thoughts=[ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"sys\":{\"uid\":\"\",\"query\":\"你好\",\"tenantId\":\"10317360\",\"mainAccountUid\":\"1046927661150576\"},\"user\":{\"city\":\"杭州\"}}","nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"2ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"西湖醋鱼,龙井虾仁\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_Ilo9"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null)], docReferences=null), usage=ApplicationUsage(models=null)) ApplicationResult(requestId=0d49e27c-9e5d-9506-9d77-d09c5022ba14, output=ApplicationOutput(text=null, finishReason=null, sessionId=dda71c1247194c26822ec1dde5a9a4f6, thoughts=[ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"sys\":{\"uid\":\"\",\"query\":\"你好\",\"tenantId\":\"10317360\",\"mainAccountUid\":\"1046927661150576\"},\"user\":{\"city\":\"杭州\"}}","nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"2ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"西湖醋鱼,龙井虾仁,东坡肉,叫花鸡\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_Ilo9"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null)], docReferences=null), usage=ApplicationUsage(models=null)) ApplicationResult(requestId=0d49e27c-9e5d-9506-9d77-d09c5022ba14, output=ApplicationOutput(text=null, finishReason=null, sessionId=dda71c1247194c26822ec1dde5a9a4f6, thoughts=[ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"sys\":{\"uid\":\"\",\"query\":\"你好\",\"tenantId\":\"10317360\",\"mainAccountUid\":\"1046927661150576\"},\"user\":{\"city\":\"杭州\"}}","nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"2ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"西湖醋鱼,龙井虾仁,东坡肉,叫花鸡,知味小笼\"}","nodeType":"LLM","nodeStatus":"success","nodeId":"LLM_Ilo9","nodeExecTime":"2440ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_vQDv"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null)], docReferences=null), usage=ApplicationUsage(models=null)) ApplicationResult(requestId=0d49e27c-9e5d-9506-9d77-d09c5022ba14, output=ApplicationOutput(text=null, finishReason=null, sessionId=dda71c1247194c26822ec1dde5a9a4f6, thoughts=[ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"sys\":{\"uid\":\"\",\"query\":\"你好\",\"tenantId\":\"10317360\",\"mainAccountUid\":\"1046927661150576\"},\"user\":{\"city\":\"杭州\"}}","nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"2ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"西湖醋鱼,龙井虾仁,东坡肉,叫花鸡,知味小笼\"}","nodeType":"LLM","nodeStatus":"success","nodeId":"LLM_Ilo9","nodeExecTime":"2440ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"西湖,\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_vQDv"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null)], docReferences=null), usage=ApplicationUsage(models=null)) ApplicationResult(requestId=0d49e27c-9e5d-9506-9d77-d09c5022ba14, output=ApplicationOutput(text=null, finishReason=null, sessionId=dda71c1247194c26822ec1dde5a9a4f6, thoughts=[ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"sys\":{\"uid\":\"\",\"query\":\"你好\",\"tenantId\":\"10317360\",\"mainAccountUid\":\"1046927661150576\"},\"user\":{\"city\":\"杭州\"}}","nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"2ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"西湖醋鱼,龙井虾仁,东坡肉,叫花鸡,知味小笼\"}","nodeType":"LLM","nodeStatus":"success","nodeId":"LLM_Ilo9","nodeExecTime":"2440ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"西湖,宋\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_vQDv"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null)], docReferences=null), usage=ApplicationUsage(models=null)) ApplicationResult(requestId=0d49e27c-9e5d-9506-9d77-d09c5022ba14, output=ApplicationOutput(text=null, finishReason=null, sessionId=dda71c1247194c26822ec1dde5a9a4f6, thoughts=[ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"sys\":{\"uid\":\"\",\"query\":\"你好\",\"tenantId\":\"10317360\",\"mainAccountUid\":\"1046927661150576\"},\"user\":{\"city\":\"杭州\"}}","nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"2ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"西湖醋鱼,龙井虾仁,东坡肉,叫花鸡,知味小笼\"}","nodeType":"LLM","nodeStatus":"success","nodeId":"LLM_Ilo9","nodeExecTime":"2440ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"西湖,宋城,灵隐寺\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_vQDv"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null)], docReferences=null), usage=ApplicationUsage(models=null)) ApplicationResult(requestId=0d49e27c-9e5d-9506-9d77-d09c5022ba14, output=ApplicationOutput(text=null, finishReason=null, sessionId=dda71c1247194c26822ec1dde5a9a4f6, thoughts=[ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"sys\":{\"uid\":\"\",\"query\":\"你好\",\"tenantId\":\"10317360\",\"mainAccountUid\":\"1046927661150576\"},\"user\":{\"city\":\"杭州\"}}","nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"2ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"西湖醋鱼,龙井虾仁,东坡肉,叫花鸡,知味小笼\"}","nodeType":"LLM","nodeStatus":"success","nodeId":"LLM_Ilo9","nodeExecTime":"2440ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"西湖,宋城,灵隐寺,西溪国家湿地公园,雷峰塔\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_vQDv"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null)], docReferences=null), usage=ApplicationUsage(models=null)) ApplicationResult(requestId=0d49e27c-9e5d-9506-9d77-d09c5022ba14, output=ApplicationOutput(text=西湖,宋城,灵隐寺,西溪国家湿地公园,雷峰塔, finishReason=stop, sessionId=dda71c1247194c26822ec1dde5a9a4f6, thoughts=[ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"sys\":{\"uid\":\"\",\"query\":\"你好\",\"tenantId\":\"10317360\",\"mainAccountUid\":\"1046927661150576\"},\"user\":{\"city\":\"杭州\"}}","nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"2ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"西湖醋鱼,龙井虾仁,东坡肉,叫花鸡,知味小笼\"}","nodeType":"LLM","nodeStatus":"success","nodeId":"LLM_Ilo9","nodeExecTime":"2440ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"西湖,宋城,灵隐寺,西溪国家湿地公园,雷峰塔\"}","nodeType":"LLM","nodeStatus":"success","nodeId":"LLM_vQDv","nodeExecTime":"1349ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"西湖,宋城,灵隐寺,西溪国家湿地公园,雷峰塔","nodeType":"End","nodeStatus":"success","nodeId":"End_DrQn7F","nodeExecTime":"2ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null)], docReferences=null), usage=ApplicationUsage(models=null))
响应示例(关闭增量输出)
ApplicationResult(requestId=b3ed8604-ddcf-908d-b1be-307c6f73c8a4, output=ApplicationOutput(text=null, finishReason=null, sessionId=db9490035d0c4cbb8ee518ecbaa2999b, thoughts=null, docReferences=null), usage=ApplicationUsage(models=null)) ApplicationResult(requestId=b3ed8604-ddcf-908d-b1be-307c6f73c8a4, output=ApplicationOutput(text=null, finishReason=null, sessionId=db9490035d0c4cbb8ee518ecbaa2999b, thoughts=[ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"6ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_Ilo9"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null)], docReferences=null), usage=ApplicationUsage(models=null)) ApplicationResult(requestId=b3ed8604-ddcf-908d-b1be-307c6f73c8a4, output=ApplicationOutput(text=null, finishReason=null, sessionId=db9490035d0c4cbb8ee518ecbaa2999b, thoughts=[ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"6ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"西湖醋\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_Ilo9"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null)], docReferences=null), usage=ApplicationUsage(models=null)) ApplicationResult(requestId=b3ed8604-ddcf-908d-b1be-307c6f73c8a4, output=ApplicationOutput(text=null, finishReason=null, sessionId=db9490035d0c4cbb8ee518ecbaa2999b, thoughts=[ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"6ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"鱼\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_Ilo9"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null)], docReferences=null), usage=ApplicationUsage(models=null)) ApplicationResult(requestId=b3ed8604-ddcf-908d-b1be-307c6f73c8a4, output=ApplicationOutput(text=null, finishReason=null, sessionId=db9490035d0c4cbb8ee518ecbaa2999b, thoughts=[ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"6ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\",东坡肉,\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_Ilo9"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null)], docReferences=null), usage=ApplicationUsage(models=null)) ApplicationResult(requestId=b3ed8604-ddcf-908d-b1be-307c6f73c8a4, output=ApplicationOutput(text=null, finishReason=null, sessionId=db9490035d0c4cbb8ee518ecbaa2999b, thoughts=[ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"6ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"龙井虾仁,叫花鸡\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_Ilo9"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null)], docReferences=null), usage=ApplicationUsage(models=null)) ApplicationResult(requestId=b3ed8604-ddcf-908d-b1be-307c6f73c8a4, output=ApplicationOutput(text=null, finishReason=null, sessionId=db9490035d0c4cbb8ee518ecbaa2999b, thoughts=[ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"6ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\",宋嫂鱼羹\"}","nodeType":"LLM","nodeStatus":"success","nodeId":"LLM_Ilo9","nodeExecTime":"1451ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_vQDv"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null)], docReferences=null), usage=ApplicationUsage(models=null)) ApplicationResult(requestId=b3ed8604-ddcf-908d-b1be-307c6f73c8a4, output=ApplicationOutput(text=null, finishReason=null, sessionId=db9490035d0c4cbb8ee518ecbaa2999b, thoughts=[ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"6ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"\"}","nodeType":"LLM","nodeStatus":"success","nodeId":"LLM_Ilo9","nodeExecTime":"1451ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"西湖\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_vQDv"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null)], docReferences=null), usage=ApplicationUsage(models=null)) ApplicationResult(requestId=b3ed8604-ddcf-908d-b1be-307c6f73c8a4, output=ApplicationOutput(text=null, finishReason=null, sessionId=db9490035d0c4cbb8ee518ecbaa2999b, thoughts=[ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"6ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"\"}","nodeType":"LLM","nodeStatus":"success","nodeId":"LLM_Ilo9","nodeExecTime":"1451ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\",宋\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_vQDv"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null)], docReferences=null), usage=ApplicationUsage(models=null)) ApplicationResult(requestId=b3ed8604-ddcf-908d-b1be-307c6f73c8a4, output=ApplicationOutput(text=null, finishReason=null, sessionId=db9490035d0c4cbb8ee518ecbaa2999b, thoughts=[ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"6ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"\"}","nodeType":"LLM","nodeStatus":"success","nodeId":"LLM_Ilo9","nodeExecTime":"1451ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"城,灵隐寺\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_vQDv"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null)], docReferences=null), usage=ApplicationUsage(models=null)) ApplicationResult(requestId=b3ed8604-ddcf-908d-b1be-307c6f73c8a4, output=ApplicationOutput(text=null, finishReason=null, sessionId=db9490035d0c4cbb8ee518ecbaa2999b, thoughts=[ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"6ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"\"}","nodeType":"LLM","nodeStatus":"success","nodeId":"LLM_Ilo9","nodeExecTime":"1451ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\",西溪国家湿地公园,雷峰塔\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_vQDv"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null)], docReferences=null), usage=ApplicationUsage(models=null)) ApplicationResult(requestId=b3ed8604-ddcf-908d-b1be-307c6f73c8a4, output=ApplicationOutput(text=西湖,宋城,灵隐寺,西溪国家湿地公园,雷峰塔, finishReason=stop, sessionId=db9490035d0c4cbb8ee518ecbaa2999b, thoughts=[ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"6ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"\"}","nodeType":"LLM","nodeStatus":"success","nodeId":"LLM_Ilo9","nodeExecTime":"1451ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"\"}","nodeType":"LLM","nodeStatus":"success","nodeId":"LLM_vQDv","nodeExecTime":"1343ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null), ApplicationOutput.Thought(thought=null, actionType=null, response={"nodeResult":"{\"result\":\"西湖,宋城,灵隐寺,西溪国家湿地公园,雷峰塔\"}","nodeType":"End","nodeStatus":"success","nodeId":"End_DrQn7F","nodeExecTime":"1ms"}, actionName=null, action=null, actionInputStream=null, actionInput=null, observation=null)], docReferences=null), usage=ApplicationUsage(models=null))
curl
请求示例
curl -X POST https://dashscope.aliyuncs.com/api/v1/apps/YOUR_APP_ID/completion \ --header 'X-DashScope-SSE: enable' \ --header "Authorization: Bearer $DASHSCOPE_API_KEY" \ --header 'Content-Type: application/json' \ --data '{ "input": { "prompt": "你好", "biz_params": { "city": "杭州"} }, "parameters": { "has_thoughts": true, "incremental_output": true }, "debug": {} }'
响应示例(开启增量输出)
id:1 event:result :HTTP_STATUS/200 data:{"output":{"session_id":"2d55b40211054ac3b5f05478aea677b5","finish_reason":"null"},"usage":{},"request_id":"a8db9989-8fae-9698-a407-ab362bbb88d8"} id:2 event:result :HTTP_STATUS/200 data:{"output":{"thoughts":[{"response":"{\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"0ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_Ilo9\"}"}],"session_id":"2d55b40211054ac3b5f05478aea677b5","finish_reason":"null"},"usage":{},"request_id":"a8db9989-8fae-9698-a407-ab362bbb88d8"} id:3 event:result :HTTP_STATUS/200 data:{"output":{"thoughts":[{"response":"{\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"0ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"西湖醋\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_Ilo9\"}"}],"session_id":"2d55b40211054ac3b5f05478aea677b5","finish_reason":"null"},"usage":{},"request_id":"a8db9989-8fae-9698-a407-ab362bbb88d8"} id:4 event:result :HTTP_STATUS/200 data:{"output":{"thoughts":[{"response":"{\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"0ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"鱼\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_Ilo9\"}"}],"session_id":"2d55b40211054ac3b5f05478aea677b5","finish_reason":"null"},"usage":{},"request_id":"a8db9989-8fae-9698-a407-ab362bbb88d8"} id:5 event:result :HTTP_STATUS/200 data:{"output":{"thoughts":[{"response":"{\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"0ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\",龙井虾仁\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_Ilo9\"}"}],"session_id":"2d55b40211054ac3b5f05478aea677b5","finish_reason":"null"},"usage":{},"request_id":"a8db9989-8fae-9698-a407-ab362bbb88d8"} id:6 event:result :HTTP_STATUS/200 data:{"output":{"thoughts":[{"response":"{\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"0ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\",东坡肉,叫花鸡\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_Ilo9\"}"}],"session_id":"2d55b40211054ac3b5f05478aea677b5","finish_reason":"null"},"usage":{},"request_id":"a8db9989-8fae-9698-a407-ab362bbb88d8"} id:7 event:result :HTTP_STATUS/200 data:{"output":{"thoughts":[{"response":"{\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"0ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\",知味小笼\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"success\",\"nodeId\":\"LLM_Ilo9\",\"nodeExecTime\":\"1996ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_vQDv\"}"}],"session_id":"2d55b40211054ac3b5f05478aea677b5","finish_reason":"null"},"usage":{},"request_id":"a8db9989-8fae-9698-a407-ab362bbb88d8"} id:8 event:result :HTTP_STATUS/200 data:{"output":{"thoughts":[{"response":"{\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"0ms\"}"},{"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"success\",\"nodeId\":\"LLM_Ilo9\",\"nodeExecTime\":\"1996ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"西湖,宋\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_vQDv\"}"}],"session_id":"2d55b40211054ac3b5f05478aea677b5","finish_reason":"null"},"usage":{},"request_id":"a8db9989-8fae-9698-a407-ab362bbb88d8"} id:9 event:result :HTTP_STATUS/200 data:{"output":{"thoughts":[{"response":"{\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"0ms\"}"},{"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"success\",\"nodeId\":\"LLM_Ilo9\",\"nodeExecTime\":\"1996ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"城,灵隐寺\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_vQDv\"}"}],"session_id":"2d55b40211054ac3b5f05478aea677b5","finish_reason":"null"},"usage":{},"request_id":"a8db9989-8fae-9698-a407-ab362bbb88d8"} id:10 event:result :HTTP_STATUS/200 data:{"output":{"thoughts":[{"response":"{\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"0ms\"}"},{"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"success\",\"nodeId\":\"LLM_Ilo9\",\"nodeExecTime\":\"1996ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\",西溪国家湿地公园,雷\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_vQDv\"}"}],"session_id":"2d55b40211054ac3b5f05478aea677b5","finish_reason":"null"},"usage":{},"request_id":"a8db9989-8fae-9698-a407-ab362bbb88d8"} id:11 event:result :HTTP_STATUS/200 data:{"output":{"thoughts":[{"response":"{\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"0ms\"}"},{"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"success\",\"nodeId\":\"LLM_Ilo9\",\"nodeExecTime\":\"1996ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"峰塔\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"success\",\"nodeId\":\"LLM_vQDv\",\"nodeExecTime\":\"1383ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"西湖,宋城,灵隐寺,西溪国家湿地公园,雷峰塔\\\"}\",\"nodeType\":\"End\",\"nodeStatus\":\"success\",\"nodeId\":\"End_DrQn7F\",\"nodeExecTime\":\"1ms\"}"}],"session_id":"2d55b40211054ac3b5f05478aea677b5","finish_reason":"stop","text":"西湖,宋城,灵隐寺,西溪国家湿地公园,雷峰塔"},"usage":{},"request_id":"a8db9989-8fae-9698-a407-ab362bbb88d8"}
响应示例(关闭增量输出)
id:1 event:result :HTTP_STATUS/200 data:{"output":{"session_id":"ea6819a723684fba9b118b5af1049774","finish_reason":"null"},"usage":{},"request_id":"4172415b-b683-9f1d-b62f-ae3829c8bb11"} id:2 event:result :HTTP_STATUS/200 data:{"output":{"thoughts":[{"response":"{\"nodeResult\":\"{\\\"sys\\\":{\\\"uid\\\":\\\"\\\",\\\"query\\\":\\\"你好\\\",\\\"tenantId\\\":\\\"10317360\\\",\\\"mainAccountUid\\\":\\\"1046927661150576\\\"},\\\"user\\\":{\\\"city\\\":\\\"杭州\\\"}}\",\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"1ms\"}"}, {"response":"{\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_Ilo9\"}"}],"session_id":"ea6819a723684fba9b118b5af1049774","finish_reason":"null"},"usage":{},"request_id":"4172415b-b683-9f1d-b62f-ae3829c8bb11"} id:3 event:result :HTTP_STATUS/200 data:{"output":{"thoughts":[{"response":"{\"nodeResult\":\"{\\\"sys\\\":{\\\"uid\\\":\\\"\\\",\\\"query\\\":\\\"你好\\\",\\\"tenantId\\\":\\\"10317360\\\",\\\"mainAccountUid\\\":\\\"1046927661150576\\\"},\\\"user\\\":{\\\"city\\\":\\\"杭州\\\"}}\",\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"1ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"西湖\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_Ilo9\"}"}],"session_id":"ea6819a723684fba9b118b5af1049774","finish_reason":"null"},"usage":{},"request_id":"4172415b-b683-9f1d-b62f-ae3829c8bb11"} id:4 event:result :HTTP_STATUS/200 data:{"output":{"thoughts":[{"response":"{\"nodeResult\":\"{\\\"sys\\\":{\\\"uid\\\":\\\"\\\",\\\"query\\\":\\\"你好\\\",\\\"tenantId\\\":\\\"10317360\\\",\\\"mainAccountUid\\\":\\\"1046927661150576\\\"},\\\"user\\\":{\\\"city\\\":\\\"杭州\\\"}}\",\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"1ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"西湖醋鱼\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_Ilo9\"}"}],"session_id":"ea6819a723684fba9b118b5af1049774","finish_reason":"null"},"usage":{},"request_id":"4172415b-b683-9f1d-b62f-ae3829c8bb11"} id:5 event:result :HTTP_STATUS/200 data:{"output":{"thoughts":[{"response":"{\"nodeResult\":\"{\\\"sys\\\":{\\\"uid\\\":\\\"\\\",\\\"query\\\":\\\"你好\\\",\\\"tenantId\\\":\\\"10317360\\\",\\\"mainAccountUid\\\":\\\"1046927661150576\\\"},\\\"user\\\":{\\\"city\\\":\\\"杭州\\\"}}\",\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"1ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"西湖醋鱼,东坡肉,\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_Ilo9\"}"}],"session_id":"ea6819a723684fba9b118b5af1049774","finish_reason":"null"},"usage":{},"request_id":"4172415b-b683-9f1d-b62f-ae3829c8bb11"} id:6 event:result :HTTP_STATUS/200 data:{"output":{"thoughts":[{"response":"{\"nodeResult\":\"{\\\"sys\\\":{\\\"uid\\\":\\\"\\\",\\\"query\\\":\\\"你好\\\",\\\"tenantId\\\":\\\"10317360\\\",\\\"mainAccountUid\\\":\\\"1046927661150576\\\"},\\\"user\\\":{\\\"city\\\":\\\"杭州\\\"}}\",\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"1ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"西湖醋鱼,东坡肉,龙井虾仁,叫花鸡\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_Ilo9\"}"}],"session_id":"ea6819a723684fba9b118b5af1049774","finish_reason":"null"},"usage":{},"request_id":"4172415b-b683-9f1d-b62f-ae3829c8bb11"} id:7 event:result :HTTP_STATUS/200 data:{"output":{"thoughts":[{"response":"{\"nodeResult\":\"{\\\"sys\\\":{\\\"uid\\\":\\\"\\\",\\\"query\\\":\\\"你好\\\",\\\"tenantId\\\":\\\"10317360\\\",\\\"mainAccountUid\\\":\\\"1046927661150576\\\"},\\\"user\\\":{\\\"city\\\":\\\"杭州\\\"}}\",\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"1ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"西湖醋鱼,东坡肉,龙井虾仁,叫花鸡,知味小笼\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"success\",\"nodeId\":\"LLM_Ilo9\",\"nodeExecTime\":\"2039ms\"}"}, {"response":"{\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_vQDv\"}"}],"session_id":"ea6819a723684fba9b118b5af1049774","finish_reason":"null"},"usage":{},"request_id":"4172415b-b683-9f1d-b62f-ae3829c8bb11"} id:8 event:result :HTTP_STATUS/200 data:{"output":{"thoughts":[{"response":"{\"nodeResult\":\"{\\\"sys\\\":{\\\"uid\\\":\\\"\\\",\\\"query\\\":\\\"你好\\\",\\\"tenantId\\\":\\\"10317360\\\",\\\"mainAccountUid\\\":\\\"1046927661150576\\\"},\\\"user\\\":{\\\"city\\\":\\\"杭州\\\"}}\",\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"1ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"西湖醋鱼,东坡肉,龙井虾仁,叫花鸡,知味小笼\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"success\",\"nodeId\":\"LLM_Ilo9\",\"nodeExecTime\":\"2039ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"西湖\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_vQDv\"}"}],"session_id":"ea6819a723684fba9b118b5af1049774","finish_reason":"null"},"usage":{},"request_id":"4172415b-b683-9f1d-b62f-ae3829c8bb11"} id:9 event:result :HTTP_STATUS/200 data:{"output":{"thoughts":[{"response":"{\"nodeResult\":\"{\\\"sys\\\":{\\\"uid\\\":\\\"\\\",\\\"query\\\":\\\"你好\\\",\\\"tenantId\\\":\\\"10317360\\\",\\\"mainAccountUid\\\":\\\"1046927661150576\\\"},\\\"user\\\":{\\\"city\\\":\\\"杭州\\\"}}\",\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"1ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"西湖醋鱼,东坡肉,龙井虾仁,叫花鸡,知味小笼\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"success\",\"nodeId\":\"LLM_Ilo9\",\"nodeExecTime\":\"2039ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"西湖,宋\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_vQDv\"}"}],"session_id":"ea6819a723684fba9b118b5af1049774","finish_reason":"null"},"usage":{},"request_id":"4172415b-b683-9f1d-b62f-ae3829c8bb11"} id:10 event:result :HTTP_STATUS/200 data:{"output":{"thoughts":[{"response":"{\"nodeResult\":\"{\\\"sys\\\":{\\\"uid\\\":\\\"\\\",\\\"query\\\":\\\"你好\\\",\\\"tenantId\\\":\\\"10317360\\\",\\\"mainAccountUid\\\":\\\"1046927661150576\\\"},\\\"user\\\":{\\\"city\\\":\\\"杭州\\\"}}\",\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"1ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"西湖醋鱼,东坡肉,龙井虾仁,叫花鸡,知味小笼\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"success\",\"nodeId\":\"LLM_Ilo9\",\"nodeExecTime\":\"2039ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"西湖,宋城,灵隐寺\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_vQDv\"}"}],"session_id":"ea6819a723684fba9b118b5af1049774","finish_reason":"null"},"usage":{},"request_id":"4172415b-b683-9f1d-b62f-ae3829c8bb11"} id:11 event:result :HTTP_STATUS/200 data:{"output":{"thoughts":[{"response":"{\"nodeResult\":\"{\\\"sys\\\":{\\\"uid\\\":\\\"\\\",\\\"query\\\":\\\"你好\\\",\\\"tenantId\\\":\\\"10317360\\\",\\\"mainAccountUid\\\":\\\"1046927661150576\\\"},\\\"user\\\":{\\\"city\\\":\\\"杭州\\\"}}\",\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"1ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"西湖醋鱼,东坡肉,龙井虾仁,叫花鸡,知味小笼\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"success\",\"nodeId\":\"LLM_Ilo9\",\"nodeExecTime\":\"2039ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"西湖,宋城,灵隐寺,西溪国家湿地公园,雷\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_vQDv\"}"}],"session_id":"ea6819a723684fba9b118b5af1049774","finish_reason":"null"},"usage":{},"request_id":"4172415b-b683-9f1d-b62f-ae3829c8bb11"} id:12 event:result :HTTP_STATUS/200 data:{"output":{"thoughts":[{"response":"{\"nodeResult\":\"{\\\"sys\\\":{\\\"uid\\\":\\\"\\\",\\\"query\\\":\\\"你好\\\",\\\"tenantId\\\":\\\"10317360\\\",\\\"mainAccountUid\\\":\\\"1046927661150576\\\"},\\\"user\\\":{\\\"city\\\":\\\"杭州\\\"}}\",\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"1ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"西湖醋鱼,东坡肉,龙井虾仁,叫花鸡,知味小笼\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"success\",\"nodeId\":\"LLM_Ilo9\",\"nodeExecTime\":\"2039ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"西湖,宋城,灵隐寺,西溪国家湿地公园,雷峰塔\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"success\",\"nodeId\":\"LLM_vQDv\",\"nodeExecTime\":\"1222ms\"}"},{"response":"{\"nodeResult\":\"西湖,宋城,灵隐寺,西溪国家湿地公园,雷峰塔\",\"nodeType\":\"End\",\"nodeStatus\":\"success\",\"nodeId\":\"End_DrQn7F\",\"nodeExecTime\":\"1ms\"}"}],"session_id":"ea6819a723684fba9b118b5af1049774","finish_reason":"stop","text":"西湖,宋城,灵隐寺,西溪国家湿地公园,雷峰塔"},"usage":{},"request_id":"4172415b-b683-9f1d-b62f-ae3829c8bb11"}
thoughts
内每一项都是一个节点的执行详情,如下以一个LLM节点结果为例。id:7 event:result :HTTP_STATUS/200 data:{"output":{"thoughts":[{"response":"{ \"nodeType\":\"Start\", \"nodeStatus\":\"success\", \"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"0ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\",知味小笼\\\"}\", \"nodeType\":\"LLM\", \"nodeStatus\":\"success\", \"nodeId\":\"LLM_Ilo9\",\"nodeExecTime\":\"1996ms\"}"}, {"response":"{\"nodeResult\":\"{\\\"result\\\":\\\"\\\"}\", \"nodeType\":\"LLM\", \"nodeStatus\":\"executing\", \"nodeId\":\"LLM_vQDv\"}"}], "session_id":"2d55b40211054ac3b5f05478aea677b5","finish_reason":"null"},"usage":{},"request_id":"a8db9989-8fae-9698-a407-ab362bbb88d8"}
如果用户关注一个LLM节点(以上方LLM_vQDv为例)的流式结果,可以关注每次推送的
thoughts
中nodeId为LLM_vQDv的节点输出。如果节点发生失败,则整个任务也会失败。
自定义参数透传
在应用的调用过程中需要用户透传参数的地方,基本包含两个方面:1.智能体应用的自定义插件的参数和鉴权透传;2.工作流应用和智能体编排应用的自定义输入参数透传。
需要透传的参数都需要使用biz_params
传递。功能详情请参考应用的参数透传。
在工作流应用和智能体编排应用中,支持自定义开始节点的参数。参考下面在工作流应用中查询城市行政区划的例子,在应用中设置变量city,调用时定义String类型的参数
city
进行透传。在智能体应用中,自定义插件的参数透传功能使用固定参数
user_defined_params
传递参数信息,自定义插件的用户级鉴权功能使用固定参数user_defined_tokens
传递鉴权信息,其中固定字段user_token
的值为该插件需要的用户鉴权Token
,参考下面查询寝室公约内容的例子。Python
工作流和编排应用自定义参数透传
请求示例
import os from http import HTTPStatus from dashscope import Application # 工作流和智能体编排应用自定义输入参数透传 biz_params = {"city": "杭州"} response = Application.call( # 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 api_key=os.getenv("DASHSCOPE_API_KEY"), app_id='YOUR_APP_ID', prompt='查询这个城市的行政区域划分', biz_params=biz_params # 传递业务参数 ) if response.status_code != HTTPStatus.OK: print(f'request_id={response.request_id}, code={response.status_code}, message={response.message}') else: print(f'output={response.output}, usage={response.usage}')
响应示例
output={"text": "杭州,简称“杭”,是浙江省的省会城市,位于中国东南部沿海地区,浙江省北部,地处北纬29°11′~30°33′,东经118°21′~120°30′之间。 全市面积16853.57平方千米,辖上城、拱墅、西湖、滨江、萧山、余杭、临平、钱塘、富阳、临安10个区和桐庐、淳安2县,代管建德1个县级市,共有13个区县(市)。 市政府驻地在上城区。截至2022年末,杭州市常住人口为1237.6万人。\n\n 杭州是中国八大古都之一,自秦朝设县治以来已有2200多年历史,曾是吴越国和南宋的都城,因风景秀丽,有“人间天堂”的美誉。市内人文古迹众多,西湖及其周边有大量的自然及人文景观遗迹,具代表性的有西湖文化、良渚文化、丝绸文化、茶文化等。", "finish_reason": "stop", "session_id": "6a3bf88eacda43128f57d87e29323166", "thoughts": null, "doc_references": null}, usage={"models": null}
自定义插件参数透传
请求示例
import os from http import HTTPStatus # 建议dashscope SDK 的版本 >= 1.14.0 from dashscope import Application biz_params = { # 智能体应用的自定义插件输入参数透传,自定义的插件ID替换{your_plugin_code} "user_defined_params": { "{your_plugin_code}": { "article_index": 2}}} response = Application.call( # 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 api_key=os.getenv("DASHSCOPE_API_KEY"), app_id='YOUR_APP_ID', prompt='寝室公约内容', biz_params=biz_params) if response.status_code != HTTPStatus.OK: print('request_id=%s, code=%s, message=%s\n' % (response.request_id, response.status_code, response.message)) else: print('request_id=%s\n output=%s\n usage=%s\n' % (response.request_id, response.output, response.usage))
响应示例
request_id=d97e3c58-7a75-9ea1-9ecf-d5be869b748e output={"text": "寝室公约的第二条内容为:寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。这提醒我们,在寝室生活中要维护良好的人际关系,共同营造一个和谐、积极的生活学习环境。如果您需要了解更多的公约内容,请告诉我具体的序号或继续提问。", "finish_reason": "stop", "session_id": "8ec3fa6c59794ed3ab803b921aa38d7a", "thoughts": null, "doc_references": null} usage={"models": [{"model_id": "qwen-max", "input_tokens": 453, "output_tokens": 107}]}
自定义插件鉴权透传
请求示例
from http import HTTPStatus import os # 建议dashscope SDK 的版本 >= 1.14.0 from dashscope import Application biz_params = { # 智能体应用的自定义插件鉴权透传,自定义的插件ID替换{your_plugin_code},token替换YOUR_TOKEN "user_defined_params": { "{your_plugin_code}": { "article_index": 2}}, "user_defined_tokens": { "{your_plugin_code}": { "user_token": "YOUR_TOKEN"}}} response = Application.call( # 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 api_key=os.getenv("DASHSCOPE_API_KEY"), app_id='YOUR_APP_ID', prompt='寝室公约内容', biz_params=biz_params) if response.status_code != HTTPStatus.OK: print('request_id=%s, code=%s, message=%s\n' % (response.request_id, response.status_code, response.message)) else: print('request_id=%s\n output=%s\n usage=%s\n' % (response.request_id, response.output, response.usage))
响应示例
request_id=e928fe3b-3df3-9a3f-9ea4-884aed3aea3c output={"text": "寝室公约的第二条内容是:“寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。”这强调了寝室成员之间应该建立和谐、支持和尊重彼此的关系。", "finish_reason": "stop", "session_id": "c3b909e48a0b4867a57b482dece9cbb7", "thoughts": null, "doc_references": null} usage={"models": [{"model_id": "qwen-max", "input_tokens": 432, "output_tokens": 82}]}
Java
工作流和编排应用自定义参数透传
请求示例
package org.example; import com.alibaba.dashscope.app.*; import com.alibaba.dashscope.exception.ApiException; import com.alibaba.dashscope.exception.InputRequiredException; import com.alibaba.dashscope.exception.NoApiKeyException; import com.alibaba.dashscope.utils.JsonUtils; import io.reactivex.Flowable; public class Test { public static void streamCall() throws NoApiKeyException, InputRequiredException { String bizParams = "{\"city\":\"杭州\"}"; ApplicationParam param = ApplicationParam.builder() // 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 .apiKey(System.getenv("DASHSCOPE_API_KEY")) .appId("YOUR_APP_ID") .prompt("查询这个城市的行政区域划分") .bizParams(JsonUtils.parse(bizParams)) .build(); Application application = new Application(); Flowable<ApplicationResult> result = application.streamCall(param); result.blockingForEach(data -> { System.out.printf("requestId: %s, text: %s, finishReason: %s\n", data.getRequestId(), data.getOutput().getText(), data.getOutput().getFinishReason()); }); } public static void main(String[] args) { try { streamCall(); } catch (ApiException | NoApiKeyException | InputRequiredException e) { System.out.printf("Exception: %s", e.getMessage()); } System.exit(0); } }
响应示例
requestId: 713ebb6e-8840-9f31-bd58-e7e931ef345d, text: 杭州市下辖10个区、1个县,代管2个县级市, 即上城区、拱墅区、西湖区、滨江区、萧山区、余杭区、临平区、钱塘区、富阳区、临安区、桐庐县、淳安县、建德市、诸暨市。, finishReason: stop
自定义插件参数透传
请求示例
package org.example; import com.alibaba.dashscope.app.*; import com.alibaba.dashscope.exception.ApiException; import com.alibaba.dashscope.exception.InputRequiredException; import com.alibaba.dashscope.exception.NoApiKeyException; import com.alibaba.dashscope.utils.JsonUtils; import io.reactivex.Flowable; public class Test { public static void streamCall() throws NoApiKeyException, InputRequiredException { String bizParams = "{\"user_defined_params\":{\"{your_plugin_code}\":{\"article_index\":2}}}"; ApplicationParam param = ApplicationParam.builder() // 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 .apiKey(System.getenv("DASHSCOPE_API_KEY")) .appId("YOUR_APP_ID") .prompt("寝室公约内容") .bizParams(JsonUtils.parse(bizParams)) .build(); Application application = new Application(); Flowable<ApplicationResult> result = application.streamCall(param); result.blockingForEach(data -> { System.out.printf("requestId: %s, text: %s, finishReason: %s\n", data.getRequestId(), data.getOutput().getText(), data.getOutput().getFinishReason()); }); } public static void main(String[] args) { try { streamCall(); } catch (ApiException | NoApiKeyException | InputRequiredException e) { System.out.printf("Exception: %s", e.getMessage()); } System.exit(0); } }
响应示例
requestId: 48bb400a-d050-9f5c-afbd-c1adb25f6a43, text: 寝, finishReason: null requestId: 48bb400a-d050-9f5c-afbd-c1adb25f6a43, text: 寝室公约的第二条, finishReason: null requestId: 48bb400a-d050-9f5c-afbd-c1adb25f6a43, text: 寝室公约的第二条内容是:寝室成员应当互帮, finishReason: null requestId: 48bb400a-d050-9f5c-afbd-c1adb25f6a43, text: 寝室公约的第二条内容是:寝室成员应当互帮互助、互相关心、互相学习, finishReason: null requestId: 48bb400a-d050-9f5c-afbd-c1adb25f6a43, text: 寝室公约的第二条内容是:寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、, finishReason: null requestId: 48bb400a-d050-9f5c-afbd-c1adb25f6a43, text: 寝室公约的第二条内容是:寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。, finishReason: null requestId: 48bb400a-d050-9f5c-afbd-c1adb25f6a43, text: 寝室公约的第二条内容是:寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。这强调了寝室生活中成员之间和谐, finishReason: null requestId: 48bb400a-d050-9f5c-afbd-c1adb25f6a43, text: 寝室公约的第二条内容是:寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。这强调了寝室生活中成员之间和谐相处与共同进步的重要性。如果您需要, finishReason: null requestId: 48bb400a-d050-9f5c-afbd-c1adb25f6a43, text: 寝室公约的第二条内容是:寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。这强调了寝室生活中成员之间和谐相处与共同进步的重要性。如果您需要了解其他条目,请告诉我序号, finishReason: null requestId: 48bb400a-d050-9f5c-afbd-c1adb25f6a43, text: 寝室公约的第二条内容是:寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。这强调了寝室生活中成员之间和谐相处与共同进步的重要性。如果您需要了解其他条目,请告诉我序号。, finishReason: null requestId: 48bb400a-d050-9f5c-afbd-c1adb25f6a43, text: 寝室公约的第二条内容是:寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。这强调了寝室生活中成员之间和谐相处与共同进步的重要性。如果您需要了解其他条目,请告诉我序号。, finishReason: stop
自定义插件鉴权透传
请求示例
package org.example; import com.alibaba.dashscope.app.*; import com.alibaba.dashscope.exception.ApiException; import com.alibaba.dashscope.exception.InputRequiredException; import com.alibaba.dashscope.exception.NoApiKeyException; import com.alibaba.dashscope.utils.JsonUtils; import io.reactivex.Flowable; public class Test { public static void streamCall() throws NoApiKeyException, InputRequiredException { String bizParams = "{\"user_defined_params\":{\"{your_plugin_code}\":{\"article_index\":2}}," + "\"user_defined_tokens\":{\"{your_plugin_code}\":{\"user_token\":\"YOUR_TOKEN\"}}}"; ApplicationParam param = ApplicationParam.builder() // 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 .apiKey(System.getenv("DASHSCOPE_API_KEY")) .appId("YOUR_APP_ID") .prompt("寝室公约内容") .bizParams(JsonUtils.parse(bizParams)) .build(); Application application = new Application(); Flowable<ApplicationResult> result = application.streamCall(param); result.blockingForEach(data -> { System.out.printf("requestId: %s, text: %s, finishReason: %s\n", data.getRequestId(), data.getOutput().getText(), data.getOutput().getFinishReason()); }); } public static void main(String[] args) { try { streamCall(); } catch (ApiException | NoApiKeyException | InputRequiredException e) { System.out.printf("Exception: %s", e.getMessage()); } System.exit(0); } }
响应示例
requestId: 48bb400a-d050-9f5c-afbd-c1adb25f6a43, text: 寝, finishReason: null requestId: 48bb400a-d050-9f5c-afbd-c1adb25f6a43, text: 寝室公约的第二条, finishReason: null requestId: 48bb400a-d050-9f5c-afbd-c1adb25f6a43, text: 寝室公约的第二条内容是:寝室成员应当互帮, finishReason: null requestId: 48bb400a-d050-9f5c-afbd-c1adb25f6a43, text: 寝室公约的第二条内容是:寝室成员应当互帮互助、互相关心、互相学习, finishReason: null requestId: 48bb400a-d050-9f5c-afbd-c1adb25f6a43, text: 寝室公约的第二条内容是:寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、, finishReason: null requestId: 48bb400a-d050-9f5c-afbd-c1adb25f6a43, text: 寝室公约的第二条内容是:寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。, finishReason: null requestId: 48bb400a-d050-9f5c-afbd-c1adb25f6a43, text: 寝室公约的第二条内容是:寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。这强调了寝室生活中成员之间和谐, finishReason: null requestId: 48bb400a-d050-9f5c-afbd-c1adb25f6a43, text: 寝室公约的第二条内容是:寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。这强调了寝室生活中成员之间和谐相处与共同进步的重要性。如果您需要, finishReason: null requestId: 48bb400a-d050-9f5c-afbd-c1adb25f6a43, text: 寝室公约的第二条内容是:寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。这强调了寝室生活中成员之间和谐相处与共同进步的重要性。如果您需要了解其他条目,请告诉我序号, finishReason: null requestId: 48bb400a-d050-9f5c-afbd-c1adb25f6a43, text: 寝室公约的第二条内容是:寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。这强调了寝室生活中成员之间和谐相处与共同进步的重要性。如果您需要了解其他条目,请告诉我序号。, finishReason: null requestId: 48bb400a-d050-9f5c-afbd-c1adb25f6a43, text: 寝室公约的第二条内容是:寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。这强调了寝室生活中成员之间和谐相处与共同进步的重要性。如果您需要了解其他条目,请告诉我序号。, finishReason: stop
curl
工作流和编排应用自定义参数透传
请求示例
curl -X POST https://dashscope.aliyuncs.com/api/v1/apps/YOUR_APP_ID/completion \ --header "Authorization: Bearer $DASHSCOPE_API_KEY" \ --header 'Content-Type: application/json' \ --data '{ "input": { "prompt": "查询这个城市的行政区域划分", "biz_params": { "city": "杭州"} }, "parameters": {} }'
响应示例
{"output":{"finish_reason":"stop","session_id":"c211219896004b50a1f6f66f2ec5413e", "text":"杭州市下辖10个区、1个县,代管2个县级市,分别为: 上城区、拱墅区、西湖区、滨江区、萧山区、余杭区、临平区、钱塘区、富阳区、临安区、桐庐县、淳安县、建德市、诸暨市。 注意,诸暨市由浙江省直辖、杭州市与绍兴市共同管理。"},"usage":{}, "request_id":"02c3c9e1-7912-9505-91aa-248d04fb1f5d"}
自定义插件参数透传
请求示例
curl -X POST https://dashscope.aliyuncs.com/api/v1/apps/YOUR_APP_ID/completion \ --header "Authorization: Bearer $DASHSCOPE_API_KEY" \ --header 'Content-Type: application/json' \ --data '{ "input": { "prompt": "寝室公约内容", "biz_params": { "user_defined_params": { "{your_plugin_code}": { "article_index": 2 } } } }, "parameters": {}, "debug":{} }'
响应示例
{"output": {"finish_reason":"stop", "session_id":"e151267ffded4fbdb13d91439011d31e", "text":"寝室公约的第二条内容是:“寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。”这意呀着在寝室生活中,大家要彼此支持,共同创造一个和谐、积极向上的生活环境。"}, "usage":{"models":[{"output_tokens":94,"model_id":"qwen-max","input_tokens":453}]}, "request_id":"a39fd2b5-7e2c-983e-84a1-1039f726f18a"}%
自定义插件用户级鉴权透传
请求示例
curl -X POST https://dashscope.aliyuncs.com/api/v1/apps/YOUR_APP_ID/completion \ --header "Authorization: Bearer $DASHSCOPE_API_KEY" \ --header 'Content-Type: application/json' \ --data '{ "input": { "prompt": "寝室公约内容", "biz_params": { "user_defined_params": { "{your_plugin_code}": { "article_index": 2 } }, "user_defined_tokens": { "{your_plugin_code}": { "user_token": "YOUR_TOKEN" } } } }, "parameters": {}, "debug":{} }'
响应示例
{"output":{"finish_reason":"stop", "session_id":"d3b5c3e269dc40479255a7a02df5c630", "text":"寝室公约的第二条内容为:“寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。”这强调了寝室生活中成员之间和谐相处与共同进步的重要性。"}, "usage":{"models":[{"output_tokens":80,"model_id":"qwen-max","input_tokens":432}]}, "request_id":"1f77154c-edc3-9003-b622-816fa2f849cf"}%
长期记忆
长期记忆功能支持您通过API进行调用,具体步骤如下:
在我的应用中打开智能体应用的长期记忆开关并发布应用;
调用CreateMemory接口创建长期记忆体并获取
memoryId
;调用时传入
memoryId
,系统会从用户消息里提取用户偏好信息,并在memoryId下创建MemoryNode作为记忆体内容;再次调用智能体应用时,系统会根据传入的memoryId,自动召回相关记忆体内容,然后将其与当前用户消息一起输入给模型。
目前仅智能体应用支持长期记忆功能。
Python
请求示例(生成记忆体内容)
from http import HTTPStatus
import os
from dashscope import Application
response = Application.call(
# 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
api_key=os.getenv("DASHSCOPE_API_KEY"),
app_id='YOUR_APP_ID',
prompt='用户饮食偏好:面食',
memory_id='YOUR_MEMORY_ID')
if response.status_code != HTTPStatus.OK:
print('request_id=%s, code=%s, message=%s\n' % (response.request_id, response.status_code, response.message))
else:
print('request_id=%s, text=%s, usage=%s\n' % (response.request_id, response.output.text, response.usage))
请求示例
from http import HTTPStatus
import os
from dashscope import Application
response = Application.call(
# 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
api_key=os.getenv("DASHSCOPE_API_KEY"),
app_id='YOUR_APP_ID',
prompt='美食推荐',
memory_id='YOUR_MEMORY_ID')
if response.status_code != HTTPStatus.OK:
print('request_id=%s, code=%s, message=%s\n' % (response.request_id, response.status_code, response.message))
else:
print('request_id=%s, text=%s, usage=%s\n' % (response.request_id, response.output.text, response.usage))
响应示例
request_id=f41a7831-5e60-982b-921d-7aa0465c5a38, text=既然您偏好面食,我为您推荐几款不容错过的面食佳肴:
1. **北京炸酱面**:传统的老北京风味,特色在于那香浓的炸酱,搭配黄瓜丝、豆芽等新鲜蔬菜,面条筋道,味道醇厚。
2. **四川担担面**:辣而不燥,麻中带香,特制的肉末酱汁拌上细薄的面条,每一口都是对味蕾的极致诱惑。
3. **山西刀削面**:以其独特的制作工艺著称,面条宽厚有嚼劲,搭配各种浇头如番茄鸡蛋、牛肉等,风味独特。
4. **陕西凉皮**:虽然严格意义上属于米制品,但其在面食爱好者中也极受欢迎,酸辣爽口,特别适合夏日食用。
5. **兰州拉面**:以“一清二白三红四绿五黄”为特点,汤清而味美,面条可粗可细,搭配牛肉片和香菜,清新不腻。
6. **意大利面**:如果您想尝试国际风味,经典的意式番茄肉酱面或是奶油蘑菇面也是不错的选择,口感丰富,酱料浓郁。
希望这些建议能让您的餐桌增添几分美味!如果您有更具体的口味需求,比如偏爱辣的、清淡的或是想要了解某个特定地区的面食,欢迎告诉我!, usage={"models": [{"model_id": "qwen-max", "input_tokens": 139, "output_tokens": 296}]}
Java
请求示例(生成记忆体内容)
package org.example;
import com.alibaba.dashscope.app.*;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
public class Test {
public static void callWithMemory() throws NoApiKeyException, InputRequiredException {
ApplicationParam param = ApplicationParam.builder()
// 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.appId("YOUR_APP_ID")
.prompt("用户饮食偏好:面食")
.memoryId("YOUR_MEMORY_ID")
.build();
Application application = new Application();
ApplicationResult result = application.call(param);
System.out.printf("requestId: %s, text: %s, finishReason: %s\n",
result.getRequestId(), result.getOutput().getText(), result.getOutput().getFinishReason());
}
public static void main(String[] args) {
try {
callWithMemory();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.out.printf("Exception: %s", e.getMessage());
}
System.exit(0);
}
}
请求示例
package org.example;
import com.alibaba.dashscope.app.*;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
public class Test {
public static void callWithMemory() throws NoApiKeyException, InputRequiredException {
ApplicationParam param = ApplicationParam.builder()
// 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.appId("YOUR_APP_ID")
.prompt("美食推荐")
.memoryId("YOUR_MEMORY_ID")
.build();
Application application = new Application();
ApplicationResult result = application.call(param);
System.out.printf("requestId: %s, text: %s, finishReason: %s\n",
result.getRequestId(), result.getOutput().getText(), result.getOutput().getFinishReason());
}
public static void main(String[] args) {
try {
callWithMemory();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.out.printf("Exception: %s", e.getMessage());
}
System.exit(0);
}
}
响应示例
requestId: 9c4b303c-5951-96b0-af5e-49159e5040f1, text: 既然您喜欢面食,我为您推荐几款不同风格的面食佳肴:
1. **北京炸酱面**:这是老北京的传统面食,特色在于那浓郁的黄豆酱炸酱,搭配黄瓜丝、豆芽等新鲜蔬菜,面条筋道,酱香四溢。
2. **四川担担面**:辣而不燥,麻中带香,细滑的面条拌上特制的芝麻酱和辣椒油,再撒上碎花生和葱花,味道层次丰富。
3. **陕西油泼面**:宽厚的手工扯面,热油一浇,蒜末和辣椒粉的香气立刻被激发出来,简单却极富风味。
4. **山西刀削面**:师傅手工用刀削出的面条,厚薄适中,搭配各种浇头如番茄鸡蛋、牛肉烩菜等,口感独特。
5. **意大利面**:如果您想尝试国际风味,经典的意式面食如意大利肉酱面或海鲜面,搭配浓郁的番茄酱或奶油白酱,也是不错的选择。
6. **日式拉面**:豚骨汤底浓郁,面条弹牙,加上叉烧肉、海苔、笋干等配料,一碗下肚,温暖又满足。
希望您能尝试一下,找到自己最爱的那一款!, finishReason: stop
curl
请求示例(生成记忆体内容)
curl -X POST https://dashscope.aliyuncs.com/api/v1/apps/YOUR_APP_ID/completion \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"input": {
"prompt": "用户饮食偏好:面食",
"memory_id": "YOUR_MEMORY_ID"
},
"parameters": {},
"debug": {}
}'
请求示例
curl -X POST https://dashscope.aliyuncs.com/api/v1/apps/YOUR_APP_ID/completion \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"input": {
"prompt": "美食推荐",
"memory_id": "YOUR_MEMORY_ID"
},
"parameters": {},
"debug": {}
}'
响应示例
{"output":{"finish_reason":"stop",
"session_id":"12677d7f5e5d423ca30db6ff77a4710d",
"text":"考虑到您对面食的偏爱,我为您推荐几种不同风味的面食佳肴:\n\n
1. **北京炸酱面**:经典的北方面食,以黄豆酱和肉酱为主料,搭配黄瓜丝、豆皮等配菜,口感丰富,酱香浓郁。\n\n
2. **四川担担面**:辣味爱好者必试,特制的辣椒油与花生碎、芽菜混合,再加入炒制的猪肉末,麻辣鲜香,十分过瘾。\n\n
3. **山西刀削面**:以其独特的制作工艺著称,面条宽厚有嚼劲,搭配各种浇头如番茄鸡蛋、牛肉等,味道醇厚。\n\n
4. **陕西凉皮**:虽然严格意义上属于小吃,但作为面食的一种变异,其酸辣爽口,搭配面筋、黄瓜丝等,非常适合夏天食用。\n\n
5. **江南阳春面**:简单而精致,清汤搭配细面条,上面撒上葱花、少许猪油,口味清淡却不失鲜美。\n\n
6. **意大利面**:如果想尝试异国风味,可以选择经典的意式番茄肉酱面或是奶油蘑菇意面,体验不同的面条文化。\n\n
希望这些建议能满足您的味蕾,不妨根据自己的口味选择尝试一下!"},
"usage":{"models":[{"output_tokens":269,"model_id":"qwen-max","input_tokens":139}]},
"request_id":"f7792da2-02f6-999c-85f1-a76de85fb99f"}%
异常响应示例
{"code":"InvalidApiKey","message":"Invalid API-key provided.","request_id":"2637fcf9-32b1-9f4e-b0e9-1724d4aea00e"}
知识库过滤
智能体应用开启知识检索增强功能或工作流应用创建知识库节点后,您在调用应用时可以指定要检索的范围。
检索指定的知识库:传入知识库ID。
知识库ID可以在知识索引页面获取,也可以使用调用CreateIndex接口返回的
Data.Id
。目前仅非结构化数据的知识库支持通过API进行创建。
可以是智能体应用已经关联的知识库,也可以是没有关联的知识库。
只会在指定的知识库里检索。例如,网页端的智能体应用里引用了知识库A,而API调用时只指定了知识库B,那么不会检索知识库A,只会检索知识库B。
检索指定的非结构化数据文档:传入文档ID、文档标签tag或文档元数据metadata。
检索指定的文档ID:传入文档ID。
文档ID可以在数据管理页面获取。
可以传入多个文档ID,仅支持已建知识索引的文档。
传入文档ID时,传入文档所属的知识库ID才会生效。
只会在指定的文档里检索。例如:网页端的智能体应用里引用了知识库A,而API调用时指定了文档ID和其所属的知识库B的ID,那么不会检索知识库A的文档,只会检索知识库B的文档。
检索结构化数据文档里的指定数据:传入结构化数据文档的“结构化数据表头+值”的键值对。
指定知识库
Python
请求示例
import os
from http import HTTPStatus
# 建议dashscope SDK 的版本 >= 1.20.11
from dashscope import Application
response = Application.call(
# 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
api_key=os.getenv("DASHSCOPE_API_KEY"),
app_id='YOUR_APP_ID', # 应用ID替换YOUR_APP_ID
prompt='请帮我推荐一款3000元以下的手机',
rag_options={
"pipeline_ids": ["YOUR_PIPELINE_ID1"], # 知识库ID替换YOUR_PIPELINE_ID1
}
)
if response.status_code != HTTPStatus.OK:
print('request_id=%s, code=%s, message=%s\n' % response.request_id, response.status_code, response.message)
else:
print('request_id=%s\n output=%s\n usage=%s\n' % (response.request_id, response.output, response.usage))
响应示例
request_id=9aa5970b-7d08-9f40-a174-ab2db3da6a68
output={"text": "我向您推荐百炼 Zephyr Z9,它是一款轻薄便携型手机,价格区间在3999-4299元,
虽然略微超过了3000元的预算,但考虑到其配置与设计,性价比仍然很高。
这款手机具有以下特点:\n- 轻巧的6.4英寸1080 x 2340像素屏幕,适合单手操作。\n- 配备了6GB RAM和128GB存储空间,满足日常使用和多数应用需求。
\n- 内置4000mAh电池,能够确保一天的正常使用。\n- 拥有30倍数字变焦镜头,方便您捕捉生活中的远距离细节。
\n\n如果您预算严格控制在3000元以内,可能需要等待促销活动或者考虑其他品牌和型号的手机。",
"finish_reason": "stop", "session_id": "b68832563d4a4730b8fe95c4a9ecbddc", "thoughts": null, "doc_references": null}
usage={"models": [{"model_id": "qwen-max", "input_tokens": 1973, "output_tokens": 180}]}
Java
请求示例
package org.example;
import com.alibaba.dashscope.app.*;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import java.util.Collections;
/*建议dashscope SDK 的版本 >= 2.16.8;应用ID替换YOUR_APP_ID,知识库ID替换PIPELINES_ID*/
public class Test {
public static void streamCall() throws NoApiKeyException, InputRequiredException {
ApplicationParam param = ApplicationParam.builder()
// 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.appId("YOUR_APP_ID")
.prompt("请帮我推荐一款3000元左右的手机")
//.images(Collections.singletonList("img_123"))
.ragOptions(RagOptions.builder()
.pipelineIds(Collections.singletonList("PIPELINES_ID"))
.build())
.build();
Application application = new Application();
ApplicationResult result = application.call(param);
System.out.printf("requestId: %s, text: %s, finishReason: %s\n",
result.getRequestId(), result.getOutput().getText(), result.getOutput().getFinishReason());
if (result.getUsage() != null && result.getUsage().getModels() != null) {
for (ApplicationUsage.ModelUsage usage : result.getUsage().getModels()) {
System.out.printf("modelId: %s, inputTokens: %d, outputTokens: %d\n",
usage.getModelId(), usage.getInputTokens(), usage.getOutputTokens());
}
}
}
public static void main(String[] args) {
try {
streamCall();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.out.printf("Exception: %s", e.getMessage());
}
System.exit(0);
}
}
响应示例
requestId: f340053d-0409-9f90-ad51-b3a89e5cfb22,
text: 根据您的预算,我推荐百炼 Zephyr Z9。
这款手机的价格区间为3999-4299元,非常接近您的预算。
它拥有轻巧的6.4英寸1080 x 2340像素屏幕,搭配128GB存储和6GB RAM,适合日常使用,保证了流畅的操作体验。
此外,它配备4000mAh电池,可以确保一天的正常使用,还有30倍数字变焦镜头,方便您捕捉生活中的细节。
虽然略微超过预算,但在性能和价格之间提供了很好的平衡。, finishReason: stop
modelId: qwen-max, inputTokens: 1972, outputTokens: 125
curl
请求示例
curl -X POST https://dashscope.aliyuncs.com/api/v1/apps/{YOUR_APP_ID}/completion \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"input": {
"prompt": "请帮我推荐一款3000元以下的手机"
},
"parameters": {
"rag_options" : {
"pipeline_ids":["YOUR_PIPELINE_ID1"]}
},
"debug": {}
}'
响应示例
{"output":{"finish_reason":"stop","session_id":"d1208af96f9a4d8390e9b29e86f0623c",
"text":"在3000元以下的价格范围内,我向您推荐百炼 Zephyr Z9。
这款手机定价在2499至2799元之间,完美符合您的预算要求。
它拥有轻巧的6.4英寸1080 x 2340像素显示屏,搭配了128GB的存储空间和6GB的RAM,足以应对日常使用中的各种应用程序和多任务处理。
此外,它配备了一块4000mAh的电池,能够确保您一整天的使用无虞,还搭载了30倍数字变焦镜头,方便您捕捉生活中的细节。
综上所述,百炼 Zephyr Z9在性价比、设计与功能上都是一个不错的选择。"},
"usage":{"models":[{"output_tokens":158,"model_id":"qwen-max","input_tokens":1025}]},
"request_id":"eb2d40f7-bede-9d48-88dc-08abdcdd0351"}%
指定非结构化文档
Python
请求示例
import os
from http import HTTPStatus
# 建议dashscope SDK 的版本 >= 1.20.11
from dashscope import Application
response = Application.call(
# 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
api_key=os.getenv("DASHSCOPE_API_KEY"),
app_id='YOUR_APP_ID', # 应用ID替换YOUR_APP_ID
prompt='请帮我推荐一款3000元以下的手机',
rag_options={
"pipeline_ids": ["YOUR_PIPELINE_ID1"], # 知识库ID替换YOUR_PIPELINE_ID1
"file_ids": ["YOUR_FILE_ID1"], # 非结构化文档ID替换YOUR_FILE_ID1
"metadata_filter": {
"name": "张三" # 元数据键值对
},
"tags": ["手机"] # 文档标签
}
)
if response.status_code != HTTPStatus.OK:
print('request_id=%s, code=%s, message=%s\n' % response.request_id, response.status_code, response.message)
else:
print('request_id=%s\n output=%s\n usage=%s\n' % (response.request_id, response.output, response.usage))
响应示例
request_id=fb12950a-43e7-9777-993e-22acc8478f20
output={"text": "我为您推荐百炼 Zephyr Z9,这是一款轻薄便携的手机,价格区间在3999-4299元,虽然略微超过3000元预算,但接近并且特性出色,
您可以关注下是否有促销活动使其价格落入您的预算范围。
其特点包括:\n\n- **屏幕尺寸与分辨率**:配备6.4英寸1080 x 2340像素屏幕,适合单手操作和高清显示。\n-
**内存与存储**:内置6GB RAM和128GB存储空间,满足日常应用和轻度游戏需求。\n-
**电池容量**:拥有4000mAh电池,可确保一天的正常使用。\n-
**相机功能**:配备30倍数字变焦镜头,方便您捕捉生活中的远距离细节。\n
\n如果您的预算严格控制在3000元以内,可能需要考虑其他品牌或等待促销活动。",
"finish_reason": "stop", "session_id": "930fa59ed84a4895a72bcdfe73fe750f", "thoughts": null, "doc_references": null}
usage={"models": [{"model_id": "qwen-max", "input_tokens": 1184, "output_tokens": 197}]}
Java
请求示例
package org.example;
import com.alibaba.dashscope.app.*;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.google.gson.JsonObject;
import java.util.Collections;
/*建议dashscope SDK 的版本 >= 2.16.8;应用ID替换YOUR_APP_ID,知识库ID替换PIPELINES_ID,文件ID替换FILES_ID*/
public class Test {
public static void streamCall() throws NoApiKeyException, InputRequiredException {
JsonObject metadataFilter = new JsonObject();
metadataFilter.addProperty("name", "1");
ApplicationParam param = ApplicationParam.builder()
// 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.appId("YOUR_APP_ID")
.prompt("请帮我推荐一款3000元左右的手机")
.ragOptions(RagOptions.builder()
.tags(Collections.singletonList("手机"))
.pipelineIds(Collections.singletonList("PIPELINES_ID"))
.fileIds(Collections.singletonList("FILES_ID"))
.metadataFilter(metadataFilter)
.build())
.build();
Application application = new Application();
ApplicationResult result = application.call(param);
System.out.printf("requestId: %s, text: %s, finishReason: %s\n",
result.getRequestId(), result.getOutput().getText(), result.getOutput().getFinishReason());
if (result.getUsage() != null && result.getUsage().getModels() != null) {
for (ApplicationUsage.ModelUsage usage : result.getUsage().getModels()) {
System.out.printf("modelId: %s, inputTokens: %d, outputTokens: %d\n",
usage.getModelId(), usage.getInputTokens(), usage.getOutputTokens());
}
}
}
public static void main(String[] args) {
try {
streamCall();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.out.printf("Exception: %s", e.getMessage());
}
System.exit(0);
}
}
响应示例
requestId: b0440b52-2181-9a7d-9372-afe199b622c3,
text: 根据您的预算,我推荐百炼 Zephyr Z9这款手机。
它定价在3999-4299元,配置包括轻巧的6.4英寸1080 x 2340像素屏幕,搭配128GB存储和6GB RAM,非常适合日常使用。
此外,它配备4000mAh电池,可以确保一天的使用无虞,还有30倍数字变焦镜头,既轻薄又不失强大功能,非常符合您的预算要求。
如果您希望在这个价位段内考虑其他品牌或型号,可能需要具体看市场最新情况,但根据目前提供的信息,百炼 Zephyr Z9是最佳选择。, finishReason: stop
modelId: qwen-max, inputTokens: 1183, outputTokens: 146
curl
请求示例
curl -X POST https://dashscope.aliyuncs.com/api/v1/apps/{YOUR_APP_ID}/completion \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"input": {
"prompt": "请帮我推荐一款3000元左右的手机"
},
"parameters": {
"rag_options" : {
"pipeline_ids":["YOUR_PIPELINE_ID1"],
"metadata_filter":{
"name":"张三"},
"tags":"手机"
}
},
"debug": {}
}'
响应示例
{"output":{"finish_reason":"stop","session_id":"f2f114864dd24a458f923aab0ec99a1d",
"text":"根据您的预算,我推荐您考虑“通义 Vivid 7”。它拥有 6.5 英寸 1080 x 2400 像素的全面屏,具备 AI 智能摄影功能,能够让您拍摄出具有专业级色彩与细节的照片。
其硬件配置包括 8GB RAM 和 128GB 存储空间,确保了流畅的操作体验;4500mAh 的电池容量也能较好地满足日常使用需求。
此外,侧面指纹解锁的设计既便捷又安全。参考售价为 2999 至 3299 元之间,符合您的预算范围。"},
"usage":{"models":[{"output_tokens":141,"model_id":"qwen-plus","input_tokens":1610}]},
"request_id":"d815d3d1-8cef-95e2-b895-89fc8d0e0f84"}%
文档ID、文档tag和文档metadata只对非结构知识库生效。当前只有在创建知识库前给文档设置的标签才有效。
指定结构化数据
Python
请求示例
from http import HTTPStatus
import os
# 建议dashscope SDK 的版本 >= 1.20.11
from dashscope import Application
response = Application.call(
# 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
api_key=os.getenv("DASHSCOPE_API_KEY"),
app_id='YOUR_APP_ID', # 应用ID替换YOUR_APP_ID
prompt='请帮我推荐一款3000元以下的手机',
rag_options={
"pipeline_ids": ["YOUR_PIPELINE_ID1"], # 知识库ID替换YOUR_PIPELINE_ID1
"structured_filter": {
"price": "2999" # 元数据键值对,对应结构化数据
}
}
)
if response.status_code != HTTPStatus.OK:
print('request_id=%s, code=%s, message=%s\n' % response.request_id, response.status_code, response.message)
else:
print('request_id=%s\n output=%s\n usage=%s\n' % (response.request_id, response.output, response.usage))
响应示例
request_id=c1db3037-697c-90c8-ad49-bc73a4ea51f4
output={"text": "我推荐您考虑一款名为“百炼”的手机,它的价格为2999元,正好符合您的预算要求。
如果您需要了解更多关于这款手机的详细信息,请告诉我,我会尽力提供帮助。",
"finish_reason": "stop", "session_id": "8fcd2ecd247c4d0fa70360ffa057b943", "thoughts": null, "doc_references": null}
usage={"models": [{"model_id": "qwen-max", "input_tokens": 236, "output_tokens": 44}]}
Java
请求示例
package org.example;
import com.alibaba.dashscope.app.*;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.google.gson.JsonObject;
import java.util.Collections;
/*建议dashscope SDK 的版本 >= 2.16.8;应用ID替换YOUR_APP_ID,知识库ID替换PIPELINES_ID*/
public class Test {
public static void streamCall() throws NoApiKeyException, InputRequiredException {
JsonObject structureFilter = new JsonObject();
structureFilter.addProperty("price", "2999");
ApplicationParam param = ApplicationParam.builder()
// 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.appId("YOUR_APP_ID")
.prompt("请帮我推荐一款3000元左右的手机")
.ragOptions(RagOptions.builder()
.pipelineIds(Collections.singletonList("PIPELINES_ID"))
.structuredFilter(structureFilter)
.build())
.build();
Application application = new Application();
ApplicationResult result = application.call(param);
System.out.printf("requestId: %s, text: %s, finishReason: %s\n",
result.getRequestId(), result.getOutput().getText(), result.getOutput().getFinishReason());
if (result.getUsage() != null && result.getUsage().getModels() != null) {
for (ApplicationUsage.ModelUsage usage : result.getUsage().getModels()) {
System.out.printf("modelId: %s, inputTokens: %d, outputTokens: %d\n",
usage.getModelId(), usage.getInputTokens(), usage.getOutputTokens());
}
}
}
public static void main(String[] args) {
try {
streamCall();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.out.printf("Exception: %s", e.getMessage());
}
System.exit(0);
}
}
响应示例
requestId: dd8eee52-ce28-93cd-aa1a-4bf89908cbef,
text: 我为您推荐一款价格在3000元左右的手机:
- **百炼手机**,价格为2999元。
这款手机符合您的预算要求,不过具体选择时,您可能还需要考虑手机的性能、品牌偏好、操作系统、摄像头质量、电池续航等多方面因素。
如果有更多具体需求,请告诉我,我会为您提供更个性化的建议。, finishReason: stop
modelId: qwen-max, inputTokens: 235, outputTokens: 78
curl
请求示例
curl -X POST https://dashscope.aliyuncs.com/api/v1/apps/YOUR_APP_ID/completion \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"input": {
"prompt": "请帮我推荐一款3000元左右的手机"
},
"parameters": {
"rag_options" : {
"pipeline_ids":["YOUR_PIPELINE_ID1"],
"structured_filter":{
"price":"2999"}
}
},
"debug": {}
}'
响应示例
{"output":{"finish_reason":"stop","session_id":"d6bc4206f9cc4d368d534f8aa4e502bc",
"text":"我为您推荐一款价格接近3000元的手机:\n\n- **百炼手机**,价格为2999元。
\n\n这款手机性价比高,能满足您的预算需求。
如果您需要更多关于这款手机的详细信息或者有其他特定需求(比如摄像头性能、处理器型号等),请告诉我,我会尽力提供更详尽的信息。"},
"usage":{"models":[{"output_tokens":73,"model_id":"qwen-max","input_tokens":235}]},"request_id":"934e1258-219c-9ef1-8982-fc1bcefb8f11"}%
在智能体应用的检索配置中打开展示回答来源开关并发布应用,可在调用的返回结果中查看检索的知识库信息。
输入与输出参数
您可以通过应用调用API参考查看不同调用方式的输入参数与输出参数。
常见问题
运行Java代码示例时,如果出现类似“java: 程序包com.alibaba.dashscope.app不存在”的异常信息,应该怎么处理?
检查导入语句中的类名和包名是否正确。
添加依赖库:如果使用Maven或Gradle进行项目管理,确保DashScope Java SDK依赖库已经添加到
pom.xml
或build.gradle
文件中,且为最新版本。您可以访问Maven获取DashScope Java SDK的最新版本号。<!-- https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dashscope-sdk-java</artifactId> <version>在此处填写最新版本号,例如2.16.4</version> </dependency>
// https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java implementation group: 'com.alibaba', name: 'dashscope-sdk-java', version: '在此处填写最新版本号,例如2.16.4'
升级SDK:旧版本的DashScope Java SDK可能不包含您尝试使用的功能或类。如果您已经添加过依赖库DashScope Java SDK,请确认您所使用的DashScope Java SDK是否为最新版。如果当前版本较低,请将其升级至最新版本。您可以在
pom.xml
或build.gradle
文件中修改DashScope Java SDK的版本为最新版本。<!-- https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dashscope-sdk-java</artifactId> <version>请将此处的版本号修改为最新版本</version> </dependency>
// https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java implementation group: 'com.alibaba', name: 'dashscope-sdk-java', version: '请将此处的版本号修改为最新版本'
重新加载项目使更改生效。
重新运行代码示例。