You can integrate Alibaba Cloud Model Studio agent applications into your business systems using the DashScope SDK (a unified API for Alibaba Cloud model services) or HTTP requests.
Prerequisites
Before you begin, complete the following three steps to set up your development environment.-
Obtain credentials
- API key: Go to the API Key Management page and create and obtain an API key.
- Alibaba Cloud Model Studio application ID: Go to the Application Management page, create an agent application, and copy its APP_ID from the application card.
-
Install the DashScope SDK
Skip this step if you use HTTP APIs.
Select and run the installation command for your programming language.- Python
- Java
Run the following command to install or upgrade the DashScope Python SDK:Copy# 使用此命令,将SDK安装到您的Python 3环境中 python3 -m pip install -U dashscopeRun the following command to add the Java SDK dependency. Replacethe-latest-versionwith the latest version number. For the latest version number, visit the DashScope Java SDK page.- XML
- Gradle
Run the following command to add the Java SDK dependency.- Add the dependency
pom.xmlfile under the<dependencies>section:Copy<dependency> <groupId>com.alibaba</groupId> <artifactId>dashscope-sdk-java</artifactId> <!-- 请将 'the-latest-version' 替换为最新版本号:https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java --> <version>the-latest-version</version> </dependency>- Update the project
pom.xmlfile. Your IDE (such as IntelliJ IDEA or Eclipse) usually detects the change automatically and prompts you to reload Maven dependencies.If no prompt appears, you can do one of the following:- Manually reload or update the Maven project in your IDE.
- Alternatively, run the following command in the project root directory:
mvn clean install.
-
Add the dependency
In your
build.gradlefile, add the following dependency to thedependenciesblock:
Copydependencies { // 请将 'the-latest-version' 替换为最新版本号:https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java implementation group: 'com.alibaba', name: 'dashscope-sdk-java', version: 'the-latest-version' }-
Synchronize the project
Save the
build.gradlefile. Your IDE (such as IntelliJ IDEA) usually displays an icon. Click it to synchronize the Gradle project. Alternatively, you can force a dependency refresh from the command line in the project root directory:
Copy./gradlew build --refresh-dependencies - Configure environment variables (recommended) To protect your credentials and avoid hard-coding them in your code, we recommend that you configure the API key as an environment variable. The SDK reads the API key from this variable automatically.
Quick start
- Python
- Java
- HTTP
import os
import dashscope
from dashscope import DashScopeError
client = dashscope.Client(
# 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
api_key=os.getenv("DASHSCOPE_API_KEY"))
try:
response = client.apps.completions.create(
app_id='APP_ID',# 替换为实际的应用 ID
prompt='你是谁?')
print(response.text)
except DashScopeError as e:
print(f'request_id={e.request_id}')
print(f'code={e.code}')
print(f'message={e.message}')
print(f'请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code')
我是阿里云开发的一款超大规模语言模型,我叫千问。我被设计用来帮助用户生成各种类型的文本,如文章、故事、诗歌、故事等,并能根据不同的场景和需求进行调整和优化。此外,我还能够回答各种问题,提供信息和解释,辅助学习和研究。如果您有任何需要,欢迎随时向我提问!
// 建议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 Main {
public static void appCall()
throws ApiException, NoApiKeyException, InputRequiredException {
ApplicationParam param = ApplicationParam.builder()
// 若没有配置环境变量,可用百炼API Key将下行替换为:.apiKey("sk-xxx")。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.appId("APP_ID")
.prompt("你是谁?")
.build();
Application application = new Application();
ApplicationResult result = application.call(param);
System.out.printf("text: %s\n",
result.getOutput().getText());
}
public static void main(String[] args) {
try {
appCall();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.err.println("message:"+e.getMessage());
System.out.println("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code");
}
System.exit(0);
}
}
text: 我是阿里云开发的一款超大规模语言模型,我叫千问。
- curl
- PHP
- Node.js
- C#
- Go
curl -X POST https://dashscope.aliyuncs.com/api/v1/apps/APP_ID/completion \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"input": {
"prompt": "你是谁?"
},
"parameters": {},
"debug": {}
}'
Replace YOUR_APP_ID with the actual application ID.Sample response
{"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"}%
<?php
# 若没有配置环境变量,可用百炼API Key将下行替换为:$api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
$api_key = getenv("DASHSCOPE_API_KEY");
$application_id = 'APP_ID'; // 替换为实际的应用 ID
$url = "https://dashscope.aliyuncs.com/api/v1/apps/$application_id/completion";
// 构造请求数据
$data = [
"input" => [
'prompt' => '你是谁?'
]
];
// 将数据编码为 JSON
$dataString = json_encode($data);
// 检查 json_encode 是否成功
if (json_last_error() !== JSON_ERROR_NONE) {
die("JSON encoding failed with error: " . json_last_error_msg());
}
// 初始化 cURL 对话
$ch = curl_init($url);
// 设置 cURL 选项
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
]);
// 执行请求
$response = curl_exec($ch);
// 检查 cURL 执行是否成功
if ($response === false) {
die("cURL Error: " . curl_error($ch));
}
// 获取 HTTP 状态码
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// 关闭 cURL 对话
curl_close($ch);
// 解码响应数据
$response_data = json_decode($response, true);
// 处理响应
if ($status_code == 200) {
if (isset($response_data['output']['text'])) {
echo "{$response_data['output']['text']}\n";
} else {
echo "No text in response.\n";
}}
else {
if (isset($response_data['request_id'])) {
echo "request_id={$response_data['request_id']}\n";}
echo "code={$status_code}\n";
if (isset($response_data['message'])) {
echo "message={$response_data['message']}\n";}
else {
echo "message=Unknown error\n";}
}
?>
我是来自阿里云的超大规模语言模型,我叫千问。
npm install axios
const axios = require('axios');
async function callDashScope() {
// 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey='sk-xxx'。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
const apiKey = process.env.DASHSCOPE_API_KEY;
const appId = 'APP_ID';// 替换为实际的应用 ID
const url = `https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`;
const data = {
input: {
prompt: "你是谁?"
},
parameters: {},
debug: {}
};
try {
const response = await axios.post(url, data, {
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
}
});
if (response.status === 200) {
console.log(`${response.data.output.text}`);
} else {
console.log(`request_id=${response.headers['request_id']}`);
console.log(`code=${response.status}`);
console.log(`message=${response.data.message}`);
}
} catch (error) {
console.error(`Error calling DashScope: ${error.message}`);
if (error.response) {
console.error(`Response status: ${error.response.status}`);
console.error(`Response data: ${JSON.stringify(error.response.data, null, 2)}`);
}
}
}
callDashScope();
我是来自阿里云的大规模语言模型,我叫千问。
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
//若没有配置环境变量,可用百炼API Key将下行替换为:apiKey="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
string apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY") ?? throw new InvalidOperationException("DASHSCOPE_API_KEY environment variable is not set.");
string appId = "APP_ID"; // 替换为实际的应用ID
string url = $"https://dashscope.aliyuncs.com/api/v1/apps/{appId}/completion";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
string jsonContent = @"{
""input"": {
""prompt"": ""你是谁?""
},
""parameters"": {},
""debug"": {}
}";
HttpContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
try
{
HttpResponseMessage response = await client.PostAsync(url, content);
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine("Request successful:");
Console.WriteLine(responseBody);
}
else
{
Console.WriteLine($"Request failed with status code: {response.StatusCode}");
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error calling DashScope: {ex.Message}");
}
}
}
}
{
"output": {
"finish_reason": "stop",
"session_id": "c274e14a58d9492f9baeffdc003a97c5",
"text": "我是阿里云开发的一款超大规模语言模型,我叫千问。我被设计用来帮助用户生成各种类型的文本,如文章、故事、诗歌、故事等,并能根据不同的场景和需求进行变换和创新。此外,我还能够回答各种问题,提供信息和解释,帮助用户解决问题和获取知识。如果你有任何问题或需要帮助,请随时告诉我!"
},
"usage": {
"models": [
{
"output_tokens": 79,
"model_id": "qwen-plus",
"input_tokens": 74
}
]
},
"request_id": "5c4b86b1-cd2d-9847-8d00-3fba8f187bc6"
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)
func main() {
// 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey := "sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
apiKey := os.Getenv("DASHSCOPE_API_KEY")
appId := "APP_ID" // 替换为实际的应用 ID
if apiKey == "" {
fmt.Println("请确保设置了DASHSCOPE_API_KEY。")
return
}
url := fmt.Sprintf("https://dashscope.aliyuncs.com/api/v1/apps/%s/completion", appId)
// 创建请求体
requestBody := map[string]interface{}{
"input": map[string]string{
"prompt": "你是谁?",
},
"parameters": map[string]interface{}{},
"debug": map[string]interface{}{},
}
jsonData, err := json.Marshal(requestBody)
if err != nil {
fmt.Printf("Failed to marshal JSON: %v\n", err)
return
}
// 创建 HTTP POST 请求
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
fmt.Printf("Failed to create request: %v\n", err)
return
}
// 设置请求头
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
// 发送请求
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Failed to send request: %v\n", err)
return
}
defer resp.Body.Close()
// 读取响应
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Failed to read response: %v\n", err)
return
}
// 处理响应
if resp.StatusCode == http.StatusOK {
fmt.Println("Request successful:")
fmt.Println(string(body))
} else {
fmt.Printf("Request failed with status code: %d\n", resp.StatusCode)
fmt.Println(string(body))
}
}
{
"output": {
"finish_reason": "stop",
"session_id": "6105c965c31b40958a43dc93c28c7a59",
"text": "我是千问,由阿里云开发的AI助手。我被设计用来回答各种问题、提供信息和与用户进行对话。有什么我可以帮助你的吗?"
},
"usage": {
"models": [
{
"output_tokens": 36,
"model_id": "qwen-plus",
"input_tokens": 74
}
]
},
"request_id": "f97ee37d-0f9c-9b93-b6bf-bd263a232bf9"
}
Core features
Multi-turn conversation
Unlike single-turn conversations, multi-turn conversations enable large language models to reference historical information, resulting in more natural and realistic interactions.| Cloud storage | Self-managed |
|---|---|
When you pass the session_id, the system automatically loads stored conversation history from the cloud and combines it with new instructions to generate context. | You must maintain a messages array to manually record and pass conversation history and new instructions for each turn. |
Required parameters:
| Required parameter: messages arrayOptional parameter: prompt
Copy |
Priority rule: If both session_id and messages are passed, messages takes precedence. The session_id is ignored. | |
- Cloud storage
- Self-managed
- Python
- Java
- HTTP
import os
import dashscope
from dashscope import DashScopeError
def call_with_session():
client = dashscope.Client(
# 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
api_key=os.getenv("DASHSCOPE_API_KEY"))
try:
response = client.apps.completions.create(
app_id='YOUR_APP_ID', # 替换为实际的应用 ID
prompt='你是谁?')
responseNext = client.apps.completions.create(
app_id='YOUR_APP_ID', # 替换为实际的应用 ID
prompt='你有什么技能?',
session_id=response.session_id) # 上一轮response的session_id
print('%s\n session_id=%s\n' % (responseNext.text, responseNext.session_id))
# print('%s\n' % (responseNext.usage))
except DashScopeError as e:
print(f'request_id={e.request_id}')
print(f'code={e.code}')
print(f'message={e.message}')
print(f'请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code')
if __name__ == '__main__':
call_with_session()
我具备多种技能,可以协助你完成各种任务。以下是一些主要的技能:
1. **信息查询**:提供天气、新闻、历史事实、科学知识等各种信息。
2. **语言处理**:翻译文本、纠正语法错误、生成文章和故事。
3. **技术问题解答**:解答编程、软件使用、技术故障排除等问题。
4. **学习辅导**:帮助解答数学、物理、化学等学科的问题。
5. **生活建议**:提供健康、饮食、旅行、购物等方面的建议。
6. **娱乐互动**:讲笑话、玩文字游戏、进行简单的聊天互动。
7. **日程管理**:提醒重要日期、安排日程、设置提醒。
8. **数据分析**:解释数据图表、提供数据分析建议。
9. **情感支持**:倾听你的感受、提供安慰和支持。
如果你有具体的需求或问题,可以直接告诉我,我会尽力帮助你!
session_id=98ceb3ca0c4e4b05a20a00f913050b42
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 Main {
public static void callWithSession()
throws ApiException, NoApiKeyException, InputRequiredException {
ApplicationParam param = ApplicationParam.builder()
// 若没有配置环境变量,可用百炼API Key将下行替换为:.apiKey("sk-xxx")。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
// 替换为实际的应用 ID
.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("%s\n session_id: %s\n",
result.getOutput().getText(), result.getOutput().getSessionId());
}
public static void main(String[] args) {
try {
callWithSession();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.out.printf("Exception: %s", e.getMessage());
System.out.println("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code");
}
System.exit(0);
}
}
我具备多项技能,可以为您提供多种帮助。以下是一些主要的技能:
1. **多语言理解与生成**:我能理解和生成包括中文、英文在内的多种语言文本。
2. **信息检索与综合**:我可以根据您的问题查找相关信息并进行整理和总结。
3. **写作辅助**:无论是撰写文章、报告还是创意写作,我都能提供支持。
4. **编程助手**:对于程序员来说,我可以帮助解答编程相关的问题,提供代码示例等。
5. **教育辅导**:在学习过程中遇到困难时,我可以作为助手提供帮助,涵盖从数学到历史等多个学科领域。
6. **生活建议**:关于健康饮食、旅行规划等方面的问题我也能给出一些建议。
7. **情感交流**:虽然我是AI,但我努力以一种温暖和支持的方式与您交流。
如果您有任何具体需求或想要进一步了解某个方面的内容,请随时告诉我!
session_id: f2e94a980a34424fa25be45a7048d77c
- curl
- PHP
- Node.js
- C#
- Go
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": "4f8ef7233dc641aba496cb201fa59f8c",
"text": "我是通义千问,由阿里云开发的AI助手。我被设计用来回答各种问题、提供信息和与用户进行对话。有什么我可以帮助你的吗?"
},
"usage": {
"models": [
{
"output_tokens": 36,
"model_id": "qwen-plus",
"input_tokens": 75
}
]
},
"request_id": "e571b14a-423f-9278-8d1e-d86c418801e0"
}
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":"4f8ef7233dc641aba496cb201fa59f8c"
},
"parameters": {},
"debug": {}
}'
{
"output": {
"finish_reason": "stop",
"session_id": "4f8ef7233dc641aba496cb201fa59f8c",
"text": "作为AI助手,我具备多种技能,可以帮助你完成各种任务,包括但不限于:
1. **知识查询**:我可以帮助你查找各种领域的信息,比如科学、历史、文化、技术等。
2. **语言翻译**:我可以帮你翻译不同语言的文字,支持多种语言之间的互译。
3. **文本生成**:我可以生成文章、故事、诗歌、新闻稿等各种类型的文本。
4. **问题解答**:无论是学术问题、生活常识还是技术难题,我都可以尝试为你提供答案。
5. **对话交流**:我可以与你进行自然流畅的对话,提供情感支持或娱乐。
6. **代码编写与调试**:我可以帮助你编写代码、解决编程中的问题。
7. **数据分析**:我可以帮助你分析数据,提供统计结果和可视化建议。
8. **创意启发**:如果你需要创意灵感,比如设计、广告词、营销策略等,我也可以提供帮助。
如果你有任何具体的需求或问题,欢迎随时告诉我!"
},
"usage": {
"models": [
{
"output_tokens": 208,
"model_id": "qwen-plus",
"input_tokens": 125
}
]
},
"request_id": "9de2c3ed-e1f0-9963-85f4-8f289203418b"
}
<?php
# 若没有配置环境变量,可用百炼API Key将下行替换为:$api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
$api_key = getenv("DASHSCOPE_API_KEY");
$application_id = 'YOUR_APP_ID'; // 替换为实际的应用 ID
$url = "https://dashscope.aliyuncs.com/api/v1/apps/$application_id/completion";
// 构造请求数据
$data = [
"input" => [
'prompt' => '你是谁?'
]
];
// 将数据编码为 JSON
$dataString = json_encode($data);
// 检查 json_encode 是否成功
if (json_last_error() !== JSON_ERROR_NONE) {
die("JSON encoding failed with error: " . json_last_error_msg());
}
// 初始化 cURL 会话
$ch = curl_init($url);
// 设置 cURL 选项
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
]);
// 执行请求
$response = curl_exec($ch);
// 检查 cURL 执行是否成功
if ($response === false) {
die("cURL Error: " . curl_error($ch));
}
// 获取 HTTP 状态码
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// 关闭 cURL 会话
curl_close($ch);
// 解码响应数据
$response_data = json_decode($response, true);
// 处理响应
if ($status_code == 200) {
if (isset($response_data['output']['text'])) {
echo "{$response_data['output']['text']}\n";
} else {
echo "No text in response.\n";
};
if (isset($response_data['output']['session_id'])) {
echo "session_id={$response_data['output']['session_id']}\n";
}
}else {
if (isset($response_data['request_id'])) {
echo "request_id={$response_data['request_id']}\n";}
echo "code={$status_code}\n";
if (isset($response_data['message'])) {
echo "message={$response_data['message']}\n";}
else {
echo "message=Unknown error\n";}
}
?>
我是来自阿里云的超大规模语言模型,我叫通义千问。
session_id=2e658bcb514f4d30ab7500b4766a8d43
<?php
# 若没有配置环境变量,可用百炼API Key将下行替换为:$api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
$api_key = getenv("DASHSCOPE_API_KEY");
$application_id = 'YOUR_APP_ID'; // 替换为实际的应用 ID
$url = "https://dashscope.aliyuncs.com/api/v1/apps/$application_id/completion";
// 构造请求数据
$data = [
"input" => [
'prompt' => '你有什么技能?',
// 替换为实际上一轮对话返回的session_id
'session_id' => '2e658bcb514f4d30ab7500b4766a8d43'
]
];
// 将数据编码为 JSON
$dataString = json_encode($data);
// 检查 json_encode 是否成功
if (json_last_error() !== JSON_ERROR_NONE) {
die("JSON encoding failed with error: " . json_last_error_msg());
}
// 初始化 cURL 会话
$ch = curl_init($url);
// 设置 cURL 选项
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
]);
// 执行请求
$response = curl_exec($ch);
// 检查 cURL 执行是否成功
if ($response === false) {
die("cURL Error: " . curl_error($ch));
}
// 获取 HTTP 状态码
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// 关闭 cURL 会话
curl_close($ch);
// 解码响应数据
$response_data = json_decode($response, true);
// 处理响应
if ($status_code == 200) {
if (isset($response_data['output']['text'])) {
echo "{$response_data['output']['text']}\n";
} else {
echo "No text in response.\n";
};
if (isset($response_data['output']['session_id'])) {
echo "session_id={$response_data['output']['session_id']}\n";
}
}else {
if (isset($response_data['request_id'])) {
echo "request_id={$response_data['request_id']}\n";}
echo "code={$status_code}\n";
if (isset($response_data['message'])) {
echo "message={$response_data['message']}\n";}
else {
echo "message=Unknown error\n";}
}
?>
我具备多项技能,包括但不限于:
1. **多语言能力**:我可以理解和生成多种语言的文字内容。
2. **写作与创作**:帮助撰写文章、故事、诗歌等创意内容。
3. **知识问答**:回答来自各个领域的常识性和专业性问题。
4. **代码编写与理解**:能够编写简单的程序代码,并帮助解释或调试代码。
5. **逻辑推理**:解决需要逻辑思考的问题和谜题。
6. **情感支持**:提供正面的心理支持和鼓励。
7. **游戏娱乐**:参与文字游戏或其他形式的互动娱乐活动。
我的目标是成为您的得力助手,在您需要的时候提供帮助和支持。如果您有任何具体需求或想要尝试的功能,请随时告诉我!
session_id=2e658bcb514f4d30ab7500b4766a8d43
npm install axios
const axios = require('axios');
async function callDashScope() {
// 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey='sk-xxx'。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
const apiKey = process.env.DASHSCOPE_API_KEY;
const appId = 'YOUR_APP_ID';// 替换为实际的应用 ID
const url = `https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`;
const data = {
input: {
prompt: "你是谁?"
},
parameters: {},
debug: {}
};
try {
const response = await axios.post(url, data, {
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
}
});
if (response.status === 200) {
console.log(`${response.data.output.text}`);
console.log(`session_id=${response.data.output.session_id}`);
} else {
console.log(`request_id=${response.headers['request_id']}`);
console.log(`code=${response.status}`);
console.log(`message=${response.data.message}`);
}
} catch (error) {
console.error(`Error calling DashScope: ${error.message}`);
if (error.response) {
console.error(`Response status: ${error.response.status}`);
console.error(`Response data: ${JSON.stringify(error.response.data, null, 2)}`);
}
}
}
callDashScope();
我是通义千问,由阿里云开发的人工智能助手。我可以回答各种问题、提供信息和与用户进行对话。有什么我可以帮助你的吗?
session_id=fe4ce8b093bf46159ea9927a7b22f0d3
const axios = require('axios');
async function callDashScope() {
// 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey='sk-xxx'。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
const apiKey = process.env.DASHSCOPE_API_KEY;
const appId = 'YOUR_APP_ID';// 替换为实际的应用 ID
const url = `https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`;
// session_id替换为实际上一轮对话的session_id
const data = {
input: {
prompt: "你有什么技能?",
session_id: 'fe4ce8b093bf46159ea9927a7b22f0d3',
},
parameters: {},
debug: {}
};
try {
const response = await axios.post(url, data, {
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
}
});
if (response.status === 200) {
console.log(`${response.data.output.text}`);
console.log(`session_id=${response.data.output.session_id}`);
} else {
console.log(`request_id=${response.headers['request_id']}`);
console.log(`code=${response.status}`);
console.log(`message=${response.data.message}`);
}
} catch (error) {
console.error(`Error calling DashScope: ${error.message}`);
if (error.response) {
console.error(`Response status: ${error.response.status}`);
console.error(`Response data: ${JSON.stringify(error.response.data, null, 2)}`);
}
}
}
callDashScope();
我具备多种技能,可以帮助你处理不同的任务和问题。以下是一些主要的技能领域:
1. **信息查询与检索**:我可以帮助查找特定的信息、数据或新闻。
2. **写作与创作**:包括撰写文章、故事、诗歌、报告等。
3. **语言翻译**:能够提供不同语言之间的翻译服务。
4. **教育辅导**:解答学术问题,帮助理解复杂的概念。
5. **技术支持**:解决计算机使用中遇到的技术难题。
6. **生活建议**:提供建议关于健康、饮食、旅行等方面。
7. **娱乐互动**:讲笑话、玩文字游戏等轻松活动。
如果你有具体的需求或想了解更详细的某一方面,请告诉我!
session_id=fe4ce8b093bf46159ea9927a7b22f0d3
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
//若没有配置环境变量,可用百炼API Key将下行替换为:apiKey="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
string apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY") ?? throw new InvalidOperationException("DASHSCOPE_API_KEY environment variable is not set.");
string appId = "YOUR_APP_ID"; // 替换为实际的应用ID
string url = $"https://dashscope.aliyuncs.com/api/v1/apps/{appId}/completion";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
string jsonContent = @"{
""input"": {
""prompt"": ""你是谁?""
},
""parameters"": {},
""debug"": {}
}";
HttpContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
try
{
HttpResponseMessage response = await client.PostAsync(url, content);
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine("Request successful:");
Console.WriteLine(responseBody);
}
else
{
Console.WriteLine($"Request failed with status code: {response.StatusCode}");
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error calling DashScope: {ex.Message}");
}
}
}
}
{
"output": {
"finish_reason": "stop",
"session_id": "7b830e4cc8fe44faad0e648f9b71435f",
"text": "我是通义千问,由阿里云开发的AI助手。我被设计用来回答各种问题、提供信息和与用户进行对话。有什么我可以帮助你的吗?"
},
"usage": {
"models": [
{
"output_tokens": 36,
"model_id": "qwen-plus",
"input_tokens": 75
}
]
},
"request_id": "53691ae5-be17-96c6-a830-8f0f92329028"
}
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
//若没有配置环境变量,可用百炼API Key将下行替换为:apiKey="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
string apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY") ?? throw new InvalidOperationException("DASHSCOPE_API_KEY environment variable is not set.");
string appId = "YOUR_APP_ID"; // 替换为实际的应用ID
string url = $"https://dashscope.aliyuncs.com/api/v1/apps/{appId}/completion";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
string jsonContent = @"{
""input"": {
""prompt"": ""你有什么技能?"",
""session_id"": ""7b830e4cc8fe44faad0e648f9b71435f""
},
""parameters"": {},
""debug"": {}
}";
HttpContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
try
{
HttpResponseMessage response = await client.PostAsync(url, content);
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine("Request successful:");
Console.WriteLine(responseBody);
}
else
{
Console.WriteLine($"Request failed with status code: {response.StatusCode}");
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error calling DashScope: {ex.Message}");
}
}
}
}
{
"output": {
"finish_reason": "stop",
"session_id": "7b830e4cc8fe44faad0e648f9b71435f",
"text": "我具备多种技能,可以:
- 回答广泛领域的知识性问题
- 提供学习资源和建议
- 协助解决技术问题
- 进行多语言交流
- 帮助规划行程和活动
- 提供日常生活中的实用建议
如果你有任何具体需求或问题,欢迎随时告诉我!"
},
"usage": {
"models": [
{
"output_tokens": 70,
"model_id": "qwen-plus",
"input_tokens": 123
}
]
},
"request_id": "da5044ed-461e-9e91-8ca5-38a3c72a8306"
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)
func main() {
// 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey := "sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
apiKey := os.Getenv("DASHSCOPE_API_KEY")
appId := "YOUR_APP_ID" // 替换为实际的应用 ID
if apiKey == "" {
fmt.Println("请确保设置了DASHSCOPE_API_KEY。")
return
}
url := fmt.Sprintf("https://dashscope.aliyuncs.com/api/v1/apps/%s/completion", appId)
// 创建请求体
requestBody := map[string]interface{}{
"input": map[string]string{
"prompt": "你是谁?",
},
"parameters": map[string]interface{}{},
"debug": map[string]interface{}{},
}
jsonData, err := json.Marshal(requestBody)
if err != nil {
fmt.Printf("Failed to marshal JSON: %v\n", err)
return
}
// 创建 HTTP POST 请求
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
fmt.Printf("Failed to create request: %v\n", err)
return
}
// 设置请求头
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
// 发送请求
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Failed to send request: %v\n", err)
return
}
defer resp.Body.Close()
// 读取响应
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Failed to read response: %v\n", err)
return
}
// 处理响应
if resp.StatusCode == http.StatusOK {
fmt.Println("Request successful:")
fmt.Println(string(body))
} else {
fmt.Printf("Request failed with status code: %d\n", resp.StatusCode)
fmt.Println(string(body))
}
}
{
"output": {
"finish_reason": "stop",
"session_id": "f7eea37f0c734c20998a021b688d6de2",
"text": "我是通义千问,由阿里云开发的AI助手。我被设计用来回答各种问题、提供信息和与用户进行对话。有什么我可以帮助你的吗?"
},
"usage": {
"models": [
{
"output_tokens": 36,
"model_id": "qwen-plus",
"input_tokens": 75
}
]
},
"request_id": "fa65e14a-ab63-95b2-aa43-035bf5c51835"
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)
func main() {
// 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey := "sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
apiKey := os.Getenv("DASHSCOPE_API_KEY")
appId := "YOUR_APP_ID" // 替换为实际的应用 ID
if apiKey == "" {
fmt.Println("请确保设置了DASHSCOPE_API_KEY。")
return
}
url := fmt.Sprintf("https://dashscope.aliyuncs.com/api/v1/apps/%s/completion", appId)
// 创建请求体
requestBody := map[string]interface{}{
"input": map[string]string{
"prompt": "你有什么技能?",
"session_id": "f7eea37f0c734c20998a021b688d6de2", // 替换为实际上一轮对话的session_id
},
"parameters": map[string]interface{}{},
"debug": map[string]interface{}{},
}
jsonData, err := json.Marshal(requestBody)
if err != nil {
fmt.Printf("Failed to marshal JSON: %v\n", err)
return
}
// 创建 HTTP POST 请求
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
fmt.Printf("Failed to create request: %v\n", err)
return
}
// 设置请求头
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
// 发送请求
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Failed to send request: %v\n", err)
return
}
defer resp.Body.Close()
// 读取响应
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Failed to read response: %v\n", err)
return
}
// 处理响应
if resp.StatusCode == http.StatusOK {
fmt.Println("Request successful:")
fmt.Println(string(body))
} else {
fmt.Printf("Request failed with status code: %d\n", resp.StatusCode)
fmt.Println(string(body))
}
}
{
"output": {
"finish_reason": "stop",
"session_id": "f7eea37f0c734c20998a021b688d6de2",
"text": "我具备多种技能,可以:
- 回答各种知识性问题,如科学、历史、文化等领域的疑问。
- 提供实用建议,比如旅行攻略、健康小贴士、学习方法等。
- 协助处理文字工作,例如撰写文章、编辑文档、创作故事或诗歌。
- 进行多语言翻译,支持多种语言之间的互译。
- 与用户进行自然流畅的对话,陪伴聊天、解答疑惑。
如果你有任何具体需求,欢迎告诉我!"
},
"usage": {
"models": [
{
"output_tokens": 104,
"model_id": "qwen-plus",
"input_tokens": 125
}
]
},
"request_id": "badccade-9f54-986b-8d8c-75ef15e9616c"
}
Replace YOUR_APP_ID with your actual application ID. In the next request, replace the session_id field value in the input parameters with the session_id value returned from the previous request.
- Python
- Java
- HTTP
# 需要 dashscope 2.0.1 及以上版本
import os
import dashscope
from dashscope import DashScopeError
client = dashscope.Client(
# 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
api_key=os.getenv("DASHSCOPE_API_KEY"))
messages = [
{'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'user', 'content': '你是谁?'},
{"role": "assistant","content": "我是阿里云开发的大规模语言模型,我叫通义千问。"},
{"role": "user","content": "你能做什么?"}
]
try:
response = client.apps.completions.create(
app_id='YOUR_APP_ID', # 替换为实际的应用 ID
messages=messages)
print('%s\n' % (response.text))
except DashScopeError as e:
print(f'request_id={e.request_id}')
print(f'code={e.code}')
print(f'message={e.message}')
print(f'请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code')
作为通义千问,我可以帮助你完成多种任务,包括但不限于:
1. 回答问题:无论是科学知识、技术难题还是生活常识,我都可以提供准确的信息和解答。
2. 创作文字:如撰写故事、写诗、编写文章等,根据给定的条件生成创意内容。
3. 编程助手:可以辅助编程学习,解释代码逻辑,帮助调试程序错误等。
4. 语言翻译:支持多种语言之间的互译服务。
5. 提供建议:在面对决策时为你提供建议或解决方案。
6. 情感交流:与用户进行对话,倾听并给予积极正面的回应和支持。
总之,我的目标是成为你工作和生活中的得力助手。如果你有任何具体的需求,请随时告诉我!
// dashscope SDK的版本需 >= 2.17.0
import java.util.ArrayList;
import java.util.List;
import com.alibaba.dashscope.app.*;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
public class Main {
public static void appCall()
throws ApiException, NoApiKeyException, InputRequiredException {
List<Message> messages = new ArrayList<>();
messages.add(Message.builder().role("system").content("You are a helpful assistant.").build());
messages.add(Message.builder().role("user").content("你是谁?").build());
messages.add(Message.builder().role("assistant").content("我是阿里云开发的大规模语言模型,我叫通义千问。").build());
messages.add(Message.builder().role("user").content("你能做什么?").build());
ApplicationParam param = ApplicationParam.builder()
// 若没有配置环境变量,可用百炼API Key将下行替换为:.apiKey("sk-xxx")。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.appId("YOUR_APP_ID")
.messages(messages)
.build();
Application application = new Application();
ApplicationResult result = application.call(param);
System.out.printf("text: %s\n",
result.getOutput().getText());
}
public static void main(String[] args) {
try {
appCall();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.err.println("message:"+e.getMessage());
System.out.println("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code");
}
System.exit(0);
}
}
text: 我可以帮助你完成多种任务,包括但不限于:
1. 回答问题:无论是学术问题、生活常识还是专业领域的问题,我都会尽力提供准确的答案。
2. 创作文字:比如写故事、写公文、写邮件、写剧本等,只需要给我一些基本的信息和要求即可。
3. 表格处理:可以帮你整理数据,生成或修改表格。
4. 代码写作:支持多种编程语言的代码编写与解释。
5. 多语言互译:可以在不同语言之间进行翻译。
6. 模拟对话:可以扮演不同的角色与用户进行模拟对话。
如果你有任何具体的需求,请随时告诉我!
- curl
- PHP
- Node.js
- C#
- Go
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": {
"messages":[
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "你是谁?"
},
{
"role": "assistant",
"content": "我是阿里云开发的大规模语言模型,我叫通义千问。"
},
{
"role": "user",
"content": "你能做什么?"
}
]
},
"parameters": {},
"debug": {}
}'
{"output":
{"finish_reason":"stop","session_id":"990ca89d89794826976d7499ad10cddb",
"text":"我可以帮助你完成多种任务,包括但不限于:\n\n1. 回答问题:无论是学术知识、实用技巧还是常识性问题,我都会尽力提供准确的答案。\n2. 创作文字:比如写故事、写公文、写邮件、写剧本等等,只要你告诉我具体需求,我就能帮你撰写。\n3. 表达观点:对于一些主观性的问题,我也可以给出自己的看法,并与你进行讨论。\n4. 游戏娱乐:我们可以一起玩文字游戏,或者让我为你讲个笑话放松一下。\n\n总之,任何与语言相关的事情,都可以找我帮忙!"},
"usage":{"models":[{"output_tokens":126,"model_id":"qwen-max","input_tokens":86}]},"request_id":"3908c4a3-8d7a-9e51-81a5-0fc366582990"}%
<?php
# 若没有配置环境变量,可用百炼API Key将下行替换为:$api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
$api_key = getenv("DASHSCOPE_API_KEY");
$application_id = 'YOUR_APP_ID'; // 替换为实际的应用 ID
$url = "https://dashscope.aliyuncs.com/api/v1/apps/$application_id/completion";
// 构造请求数据
$data = [
"input" => [
"messages" => [
[
"role" => "system",
"content" => "You are a helpful assistant."
],
[
"role" => "user",
"content" => "你是谁?"
],
[
"role" => "assistant",
"content" => "我是阿里云开发的大规模语言模型,我叫通义千问。"
],
[
"role" => "user",
"content" => "你能做什么?"
]
]
]
];
// 将数据编码为 JSON
$dataString = json_encode($data);
// 检查 json_encode 是否成功
if (json_last_error() !== JSON_ERROR_NONE) {
die("JSON encoding failed with error: " . json_last_error_msg());
}
// 初始化 cURL 会话
$ch = curl_init($url);
// 设置 cURL 选项
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
]);
// 执行请求
$response = curl_exec($ch);
// 检查 cURL 执行是否成功
if ($response === false) {
die("cURL Error: " . curl_error($ch));
}
// 获取 HTTP 状态码
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// 关闭 cURL 会话
curl_close($ch);
// 解码响应数据
$response_data = json_decode($response, true);
// 处理响应
if ($status_code == 200) {
if (isset($response_data['output']['text'])) {
echo "{$response_data['output']['text']}\n";
} else {
echo "No text in response.\n";
}
} else {
if (isset($response_data['request_id'])) {
echo "request_id={$response_data['request_id']}\n";
}
echo "code={$status_code}\n";
if (isset($response_data['message'])) {
echo "message={$response_data['message']}\n";
} else {
echo "message=Unknown error\n";
}
}
?>
我可以帮助你完成多种任务,比如:
1. 回答问题:无论是学术问题、实用知识还是娱乐八卦,我都会尽力提供准确的答案。
2. 创作文字:包括但不限于写故事、写公文、写邮件等。
3. 提供建议:如旅行建议、学习方法、职业规划等方面的指导和建议。
4. 进行对话:我们可以聊天交流,分享心情,甚至进行一些有趣的讨论。
如果你有任何需要帮助的地方,都可以告诉我哦!
npm install axios
const axios = require('axios');
async function callDashScope() {
// 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey='sk-xxx'。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
const apiKey = process.env.DASHSCOPE_API_KEY;
const appId = 'YOUR_APP_ID';//替换为实际的应用 ID
const url = `https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`;
const data = {
"input": {
"messages":[
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "你是谁?"
},
{
"role": "assistant",
"content": "我是阿里云开发的大规模语言模型,我叫通义千问。"
},
{
"role": "user",
"content": "你能做什么?"
}
]
},
parameters: {},
debug: {}
};
try {
const response = await axios.post(url, data, {
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
}
});
if (response.status === 200) {
console.log(`${response.data.output.text}`);
} else {
console.log(`request_id=${response.headers['request_id']}`);
console.log(`code=${response.status}`);
console.log(`message=${response.data.message}`);
}
} catch (error) {
console.error(`Error calling DashScope: ${error.message}`);
if (error.response) {
console.error(`Response status: ${error.response.status}`);
console.error(`Response data: ${JSON.stringify(error.response.data, null, 2)}`);
}
}
}
callDashScope();
可以帮助你完成多种任务,包括但不限于:
1. 回答问题:无论是学术知识、实用信息还是常识性问题,我都会尽力提供准确的答案。
2. 创作文字:比如写故事、写公文、写邮件、写剧本等,只要你给出足够的背景信息和要求,我就能帮你撰写。
3. 提供建议:如果你需要在某些决策上得到建议,比如旅行目的地选择、礼物挑选、学习方法等,我也可以根据你的描述提供建议。
4. 语言翻译:支持多国语言之间的文本翻译。
5. 代码编写与解释:对于编程相关的问题,我可以帮助编写简单的程序或解释复杂的概念。
6. 进行对话:除了上述功能外,我还能够与用户进行日常交流,分享想法。
如果你有任何具体的需求,请随时告诉我!
using System.Text;
class Program
{
static async Task Main(string[] args)
{
// 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
string apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY")?? throw new InvalidOperationException("DASHSCOPE_API_KEY environment variable is not set.");;
string appId = "YOUR_APP_ID";// 替换为实际的应用ID
if (string.IsNullOrEmpty(apiKey))
{
Console.WriteLine("请确保设置了 DASHSCOPE_API_KEY。");
return;
}
string url = $"https://dashscope.aliyuncs.com/api/v1/apps/{appId}/completion";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
string jsonContent = $@"{{
""input"": {{
""messages"": [
{{
""role"": ""system"",
""content"": ""You are a helpful assistant.""
}},
{{
""role"": ""user"",
""content"": ""你是谁?""
}},
{{
""role"": ""assistant"",
""content"": ""我是阿里云开发的大规模语言模型,我叫通义千问。""
}},
{{
""role"": ""user"",
""content"": ""你能做什么?""
}}
]
}},
""parameters"": {{}},
""debug"": {{}}
}}";
HttpContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
try
{
HttpResponseMessage response = await client.PostAsync(url, content);
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
else
{
Console.WriteLine($"Request failed with status code: {response.StatusCode}");
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error calling DashScope: {ex.Message}");
}
}
}
}
{
"output": {
"finish_reason": "stop",
"session_id": "a6d041ca3d084a7ca9eff1c456afad70",
"text": "作为通义千问,我可以帮助您完成多种任务,包括但不限于:\n\n1. 回答问题:提供各类知识性问题的答案。\n2. 文本生成:撰写文章、故事、诗歌等文本内容。\n3. 语言翻译:进行不同语言之间的翻译工作。\n4. 对话交流:与用户进行自然流畅的对话。\n5. 提供建议:根据用户需求提供建议或解决方案。\n\n如果您有任何具体的需求,请告诉我,我会尽力为您提供帮助。"
},
"usage": {
"models": [
{
"output_tokens": 102,
"model_id": "qwen-max",
"input_tokens": 87
}
]
},
"request_id": "27fb8a01-70d5-974f-bb0a-e9408a9c1772"
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)
func main() {
// 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey := "sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
apiKey := os.Getenv("DASHSCOPE_API_KEY")
appId := "YOUR_APP_ID" // 替换为实际的应用 ID
if apiKey == "" {
fmt.Println("请确保设置了DASHSCOPE_API_KEY。")
return
}
url := fmt.Sprintf("https://dashscope.aliyuncs.com/api/v1/apps/%s/completion", appId)
// 创建请求体
requestBody := map[string]interface{}{
"input": map[string]interface{}{
"messages": []interface{}{
map[string]string{
"role": "system",
"content": "You are a helpful assistant.",
},
map[string]string{
"role": "user",
"content": "你是谁?",
},
map[string]string{
"role": "assistant",
"content": "我是阿里云开发的大规模语言模型,我叫通义千问。",
},
map[string]string{
"role": "user",
"content": "你能做什么?",
},
},
},
"parameters": map[string]interface{}{},
"debug": map[string]interface{}{},
}
jsonData, err := json.Marshal(requestBody)
if err != nil {
fmt.Printf("Failed to marshal JSON: %v\n", err)
return
}
// 创建 HTTP POST 请求
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
fmt.Printf("Failed to create request: %v\n", err)
return
}
// 设置请求头
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
// 发送请求
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Failed to send request: %v\n", err)
return
}
defer resp.Body.Close()
// 读取响应
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Failed to read response: %v\n", err)
return
}
// 处理响应
if resp.StatusCode == http.StatusOK {
fmt.Println("Request successful:")
fmt.Println(string(body))
} else {
fmt.Printf("Request failed with status code: %d\n", resp.StatusCode)
fmt.Println(string(body))
}
}
{
"output": {
"finish_reason": "stop",
"session_id": "2ae51a5eac3b4b269834cf0695330a05",
"text": "我可以帮助你完成多种任务,包括但不限于:\n\n1. 回答问题:提供各种领域的知识性问题解答。\n2. 文本创作:撰写故事、文章、诗歌等。\n3. 编程助手:提供编程方面的指导和代码示例。\n4. 对话聊天:进行日常对话,陪伴交流。\n5. 翻译服务:提供多语言之间的翻译支持。\n6. 信息查询:查找新闻、天气预报、历史数据等信息。\n7. 学习辅导:帮助解答学习中的疑惑,提供学习建议。\n\n如果你有任何具体的需求或问题,都可以告诉我,我会尽力帮助你!"
},
"usage": {
"models": [
{
"output_tokens": 132,
"model_id": "qwen-max",
"input_tokens": 87
}
]
},
"request_id": "1289eb09-e4ed-9f9e-98ca-805c83b333a1"
}
Streaming output
A large language model receives input and generates intermediate results step by step. It outputs these results in real time. This approach is called streaming output. It lets you view content as the model generates it, which reduces wait time. How to enable streaming output Set the appropriate parameter based on your calling method:- Python SDK: Set the
streamparameter toTrue. - Java SDK: Use the
streamCallmethod. - HTTP: In the
Header, setX-DashScope-SSEtoenable.
- Python SDK: Set the
incremental_outputparameter toTrue. - Java SDK: Use the
incrementalOutputmethod and set it totrue. - HTTP: In the
parametersobject, use theincremental_outputparameter and set it totrue.
has_thoughts parameter to True to return the thinking process in the thoughts field.
- You can enable thinking mode for the Qwen3 model in two ways. First, turn on the Thinking mode toggle in your application and publish the application. Second, set
enable_thinkingtotruein your API call. If both are set, the API parameter takes precedence. - Other thinking models enable thinking mode by default. You cannot disable it.
- Python
- Java
- HTTP
import os
import dashscope
from dashscope import DashScopeError
client = dashscope.Client(
# 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
api_key=os.getenv("DASHSCOPE_API_KEY"))
try:
stream = client.apps.completions.create(
app_id='YOUR_APP_ID',
prompt='你是谁?',
stream=True, # 流式输出
incremental_output=True) # 增量输出
for partial in stream:
print(f'{partial.text}\n') # 处理只输出文本text
except DashScopeError as e:
print(f'request_id={e.request_id}')
print(f'code={e.code}')
print(f'message={e.message}')
print(f'请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code')
我是来自
阿里
云
的大规模语言模型
,我叫通
义千问。
// 建议dashscope SDK的版本 >= 2.15.0
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 Main {
public static void streamCall() throws NoApiKeyException, InputRequiredException {
ApplicationParam param = ApplicationParam.builder()
// 若没有配置环境变量,可用百炼API Key将下行替换为:.apiKey("sk-xxx")。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
// 替换为实际的应用 ID
.appId("YOUR_APP_ID")
.prompt("你是谁?")
// 增量输出
.incrementalOutput(true)
.build();
Application application = new Application();
// .streamCall():流式输出内容
Flowable<ApplicationResult> result = application.streamCall(param);
result.blockingForEach(data -> {
System.out.printf("%s\n",
data.getOutput().getText());
});
}
public static void main(String[] args) {
try {
streamCall();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.out.printf("Exception: %s", e.getMessage());
System.out.println("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code");
}
System.exit(0);
}
}
我是阿里
云
开发的一款超大规模语言
模型,我叫
通义千问
。
- curl
- PHP
- Node.js
- C#
- Go
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": {}
}'
Replace YOUR_APP_ID with your actual application ID.Sample response
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"}
<?php
// 若没有配置环境变量,可用百炼API Key将下行替换为:$api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
$api_key = getenv("DASHSCOPE_API_KEY");
$application_id = 'YOUR_APP_ID'; // 替换为实际的应用 ID
$url = "https://dashscope.aliyuncs.com/api/v1/apps/$application_id/completion";
// 构造请求数据
$data = [
"input" => [
'prompt' => '你是谁?'],
"parameters" => [
'incremental_output' => true]];// 增量输出
// 将数据编码为 JSON
$dataString = json_encode($data);
// 检查 json_encode 是否成功
if (json_last_error() !== JSON_ERROR_NONE) {
die("JSON encoding failed with error: " . json_last_error_msg());
}
// 初始化 cURL 会话
$ch = curl_init($url);
// 设置 cURL 选项
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); // 不返回传输的数据
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $string) {
echo $string; // 处理流式数据
return strlen($string);
});
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key,
'X-DashScope-SSE: enable' // 流式输出
]);
// 执行请求
$response = curl_exec($ch);
// 检查 cURL 执行是否成功
if ($response === false) {
die("cURL Error: " . curl_error($ch));
}
// 获取 HTTP 状态码
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// 关闭 cURL 会话
curl_close($ch);
if ($status_code != 200) {
echo "HTTP Status Code: $status_code\n";
echo "Request Failed.\n";
}
?>
id:1
event:result
:HTTP_STATUS/200
data:{"output":{"session_id":"232f8a3622774c5182997c6f262c59f9","finish_reason":"null","text":"我是阿里"},"usage":{"models":[{"input_tokens":58,"output_tokens":2,"model_id":"qwen-max"}]},"request_id":"e682ec04-28a5-9957-ac48-76f87693cab5"}
id:2
event:result
:HTTP_STATUS/200
data:{"output":{"session_id":"232f8a3622774c5182997c6f262c59f9","finish_reason":"null","text":"云"},"usage":{"models":[{"input_tokens":58,"output_tokens":3,"model_id":"qwen-max"}]},"request_id":"e682ec04-28a5-9957-ac48-76f87693cab5"}
id:3
event:result
:HTTP_STATUS/200
data:{"output":{"session_id":"232f8a3622774c5182997c6f262c59f9","finish_reason":"null","text":"开发"},"usage":{"models":[{"input_tokens":58,"output_tokens":4,"model_id":"qwen-max"}]},"request_id":"e682ec04-28a5-9957-ac48-76f87693cab5"}
id:4
event:result
:HTTP_STATUS/200
data:{"output":{"session_id":"232f8a3622774c5182997c6f262c59f9","finish_reason":"null","text":"的一款超大规模语言"},"usage":{"models":[{"input_tokens":58,"output_tokens":8,"model_id":"qwen-max"}]},"request_id":"e682ec04-28a5-9957-ac48-76f87693cab5"}
id:5
event:result
:HTTP_STATUS/200
data:{"output":{"session_id":"232f8a3622774c5182997c6f262c59f9","finish_reason":"null","text":"模型,我叫"},"usage":{"models":[{"input_tokens":58,"output_tokens":12,"model_id":"qwen-max"}]},"request_id":"e682ec04-28a5-9957-ac48-76f87693cab5"}
id:6
event:result
:HTTP_STATUS/200
data:{"output":{"session_id":"232f8a3622774c5182997c6f262c59f9","finish_reason":"null","text":"通义千问"},"usage":{"models":[{"input_tokens":58,"output_tokens":16,"model_id":"qwen-max"}]},"request_id":"e682ec04-28a5-9957-ac48-76f87693cab5"}
id:7
event:result
:HTTP_STATUS/200
data:{"output":{"session_id":"232f8a3622774c5182997c6f262c59f9","finish_reason":"null","text":"。"},"usage":{"models":[{"input_tokens":58,"output_tokens":17,"model_id":"qwen-max"}]},"request_id":"e682ec04-28a5-9957-ac48-76f87693cab5"}
id:8
event:result
:HTTP_STATUS/200
data:{"output":{"session_id":"232f8a3622774c5182997c6f262c59f9","finish_reason":"stop","text":""},"usage":{"models":[{"input_tokens":58,"output_tokens":17,"model_id":"qwen-max"}]},"request_id":"e682ec04-28a5-9957-ac48-76f87693cab5"}
npm install axios
1. Output full response
1. Output full response
const axios = require('axios');
async function callDashScope() {
//若没有配置环境变量,可用百炼API Key将下行替换为:apiKey='sk-xxx'。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
const apiKey = process.env.DASHSCOPE_API_KEY;
const appId = 'YOUR_APP_ID';// 替换为实际的应用 ID
const url = `https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`;
const data = {
input: {
prompt: "你是谁?"
},
parameters: {
'incremental_output' : 'true' // 增量输出
},
debug: {}
};
try {
console.log("Sending request to DashScope API...");
const response = await axios.post(url, data, {
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
'X-DashScope-SSE': 'enable' // 流式输出
},
responseType: 'stream' // 用于处理流式响应
});
if (response.status === 200) {
// 处理流式响应
response.data.on('data', (chunk) => {
console.log(`Received chunk: ${chunk.toString()}`);
});
} else {
console.log("Request failed:");
if (response.data.request_id) {
console.log(`request_id=${response.data.request_id}`);
}
console.log(`code=${response.status}`);
if (response.data.message) {
console.log(`message=${response.data.message}`);
} else {
console.log('message=Unknown error');
}
}
} catch (error) {
console.error(`Error calling DashScope: ${error.message}`);
if (error.response) {
console.error(`Response status: ${error.response.status}`);
console.error(`Response data: ${JSON.stringify(error.response.data, null, 2)}`);
}
}
}
callDashScope();
2. Output only the text field
2. Output only the text field
const axios = require('axios');
const { Transform } = require('stream');
async function callDashScope() {
//若没有配置环境变量,可用百炼API Key将下行替换为:apiKey='sk-xxx'。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
const apiKey = process.env.DASHSCOPE_API_KEY;
const appId = 'YOUR_APP_ID'; // 替换为实际的应用 ID
const url = `https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`;
const data = {
input: { prompt: "你是谁?" },
parameters: { incremental_output: true }, // 增量输出
debug: {}
};
try {
console.log("Sending request to DashScope API...");
const response = await axios.post(url, data, {
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
'X-DashScope-SSE': 'enable' // 流式输出
},
responseType: 'stream' // 用于处理流式响应
});
if (response.status === 200) {
// // 处理流式响应 SSE协议解析转换流
const sseTransformer = new Transform({
transform(chunk, encoding, callback) {
this.buffer += chunk.toString();
// 按SSE事件分割(两个换行符)
const events = this.buffer.split(/\n\n/);
this.buffer = events.pop() || ''; // 保留未完成部分
events.forEach(eventData => {
const lines = eventData.split('\n');
let textContent = '';
// 解析事件内容
lines.forEach(line => {
if (line.startsWith('data:')) {
try {
const jsonData = JSON.parse(line.slice(5).trim());
if (jsonData.output?.text) {
textContent = jsonData.output.text;
}
} catch(e) {
console.error('JSON解析错误:', e.message);
}
}
});
if (textContent) {
// 添加换行符并推送
this.push(textContent + '\n');
}
});
callback();
},
flush(callback) {
if (this.buffer) {
this.push(this.buffer + '\n');
}
callback();
}
});
sseTransformer.buffer = '';
// 管道处理
response.data
.pipe(sseTransformer)
.on('data', (textWithNewline) => {
process.stdout.write(textWithNewline); // 自动换行输出
})
.on('end', () => console.log(""))
.on('error', err => console.error("管道错误:", err));
} else {
console.log("请求失败,状态码:", response.status);
response.data.on('data', chunk => console.log(chunk.toString()));
}
} catch (error) {
console.error(`API调用失败: ${error.message}`);
if (error.response) {
console.error(`状态码: ${error.response.status}`);
error.response.data.on('data', chunk => console.log(chunk.toString()));
}
}
}
callDashScope();
id:1
event:result
:HTTP_STATUS/200
data:{"output":{"session_id":"bb9fb75687104983ae47fc1f34ef36a1","finish_reason":"null","text":"你好!"},"usage":{"models":[{"input_tokens":56,"output_tokens":2,"model_id":"qwen-max"}]},"request_id":"d96ec7e0-5ad8-9f19-82c1-9c87f86e12b8"}
id:2
event:result
:HTTP_STATUS/200
data:{"output":{"session_id":"bb9fb75687104983ae47fc1f34ef36a1","finish_reason":"null","text":"有什么"},"usage":{"models":[{"input_tokens":56,"output_tokens":3,"model_id":"qwen-max"}]},"request_id":"d96ec7e0-5ad8-9f19-82c1-9c87f86e12b8"}
id:3
event:result
:HTTP_STATUS/200
data:{"output":{"session_id":"bb9fb75687104983ae47fc1f34ef36a1","finish_reason":"null","text":"可以帮助"},"usage":{"models":[{"input_tokens":56,"output_tokens":4,"model_id":"qwen-max"}]},"request_id":"d96ec7e0-5ad8-9f19-82c1-9c87f86e12b8"}
id:4
event:result
:HTTP_STATUS/200
data:{"output":{"session_id":"bb9fb75687104983ae47fc1f34ef36a1","finish_reason":"null","text":"你的吗?"},"usage":{"models":[{"input_tokens":56,"output_tokens":7,"model_id":"qwen-max"}]},"request_id":"d96ec7e0-5ad8-9f19-82c1-9c87f86e12b8"}
id:5
event:result
:HTTP_STATUS/200
data:{"output":{"session_id":"bb9fb75687104983ae47fc1f34ef36a1","finish_reason":"stop","text":""},"usage":{"models":[{"input_tokens":56,"output_tokens":7,"model_id":"qwen-max"}]},"request_id":"d96ec7e0-5ad8-9f19-82c1-9c87f86e12b8"}
using System.Net;
using System.Text;
class Program
{
static async Task Main(string[] args)
{
//若没有配置环境变量,可用百炼API Key将下行替换为:apiKey="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
string apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY") ?? throw new InvalidOperationException("DASHSCOPE_API_KEY environment variable is not set.");
string appId = "YOUR_APP_ID"; // 替换为实际的应用ID
string url = $"https://dashscope.aliyuncs.com/api/v1/apps/{appId}/completion";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
client.DefaultRequestHeaders.Add("X-DashScope-SSE", "enable");
string jsonContent = @"{
""input"": {
""prompt"": ""你是谁""
},
""parameters"": {""incremental_output"": true},
""debug"": {}
}";
HttpContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
try
{
var request = new HttpRequestMessage(HttpMethod.Post, url);
request.Content = content;
HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Request successful:");
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
using (var stream = await response.Content.ReadAsStreamAsync())
using (var reader = new StreamReader(stream))
{
string? line; // 声明为可空字符串
while ((line = await reader.ReadLineAsync()) != null)
{
if (line.StartsWith("data:"))
{
string data = line.Substring(5).Trim();
Console.WriteLine(data);
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
}
}
}
}
else
{
Console.WriteLine($"Request failed with status code: {response.StatusCode}");
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error calling DashScope: {ex.Message}");
}
}
}
}
2025-02-14 16:22:08:482
Request successful:
2025-02-14 16:22:09:098
{"output":{"session_id":"c2265dd99e4b40e0b5b3638824f21dd9","finish_reason":"null","text":"我是"},"usage":{"models":[{"input_tokens":51,"output_tokens":1,"model_id":"qwen-plus"}]},"request_id":"2d40821d-98bb-960e-999d-c456af8bc9e9"}
2025-02-14 16:22:09:099
{"output":{"session_id":"c2265dd99e4b40e0b5b3638824f21dd9","finish_reason":"null","text":"来自"},"usage":{"models":[{"input_tokens":51,"output_tokens":2,"model_id":"qwen-plus"}]},"request_id":"2d40821d-98bb-960e-999d-c456af8bc9e9"}
2025-02-14 16:22:09:172
{"output":{"session_id":"c2265dd99e4b40e0b5b3638824f21dd9","finish_reason":"null","text":"阿里"},"usage":{"models":[{"input_tokens":51,"output_tokens":3,"model_id":"qwen-plus"}]},"request_id":"2d40821d-98bb-960e-999d-c456af8bc9e9"}
2025-02-14 16:22:09:172
{"output":{"session_id":"c2265dd99e4b40e0b5b3638824f21dd9","finish_reason":"null","text":"云的大规模语言"},"usage":{"models":[{"input_tokens":51,"output_tokens":7,"model_id":"qwen-plus"}]},"request_id":"2d40821d-98bb-960e-999d-c456af8bc9e9"}
2025-02-14 16:22:09:463
{"output":{"session_id":"c2265dd99e4b40e0b5b3638824f21dd9","finish_reason":"null","text":"模型,我叫"},"usage":{"models":[{"input_tokens":51,"output_tokens":11,"model_id":"qwen-plus"}]},"request_id":"2d40821d-98bb-960e-999d-c456af8bc9e9"}
2025-02-14 16:22:09:618
{"output":{"session_id":"c2265dd99e4b40e0b5b3638824f21dd9","finish_reason":"null","text":"通义千问"},"usage":{"models":[{"input_tokens":51,"output_tokens":15,"model_id":"qwen-plus"}]},"request_id":"2d40821d-98bb-960e-999d-c456af8bc9e9"}
2025-02-14 16:22:09:777
{"output":{"session_id":"c2265dd99e4b40e0b5b3638824f21dd9","finish_reason":"null","text":"。我是你的人"},"usage":{"models":[{"input_tokens":51,"output_tokens":19,"model_id":"qwen-plus"}]},"request_id":"2d40821d-98bb-960e-999d-c456af8bc9e9"}
2025-02-14 16:22:09:932
{"output":{"session_id":"c2265dd99e4b40e0b5b3638824f21dd9","finish_reason":"null","text":"工智能助手,"},"usage":{"models":[{"input_tokens":51,"output_tokens":23,"model_id":"qwen-plus"}]},"request_id":"2d40821d-98bb-960e-999d-c456af8bc9e9"}
2025-02-14 16:22:10:091
{"output":{"session_id":"c2265dd99e4b40e0b5b3638824f21dd9","finish_reason":"null","text":"可以回答问题、"},"usage":{"models":[{"input_tokens":51,"output_tokens":27,"model_id":"qwen-plus"}]},"request_id":"2d40821d-98bb-960e-999d-c456af8bc9e9"}
2025-02-14 16:22:10:244
{"output":{"session_id":"c2265dd99e4b40e0b5b3638824f21dd9","finish_reason":"null","text":"创作文字,比如"},"usage":{"models":[{"input_tokens":51,"output_tokens":31,"model_id":"qwen-plus"}]},"request_id":"2d40821d-98bb-960e-999d-c456af8bc9e9"}
2025-02-14 16:22:10:389
{"output":{"session_id":"c2265dd99e4b40e0b5b3638824f21dd9","finish_reason":"null","text":"写故事、写"},"usage":{"models":[{"input_tokens":51,"output_tokens":35,"model_id":"qwen-plus"}]},"request_id":"2d40821d-98bb-960e-999d-c456af8bc9e9"}
2025-02-14 16:22:10:525
{"output":{"session_id":"c2265dd99e4b40e0b5b3638824f21dd9","finish_reason":"null","text":"公文、写"},"usage":{"models":[{"input_tokens":51,"output_tokens":39,"model_id":"qwen-plus"}]},"request_id":"2d40821d-98bb-960e-999d-c456af8bc9e9"}
2025-02-14 16:22:10:662
{"output":{"session_id":"c2265dd99e4b40e0b5b3638824f21dd9","finish_reason":"null","text":"邮件、写剧本"},"usage":{"models":[{"input_tokens":51,"output_tokens":43,"model_id":"qwen-plus"}]},"request_id":"2d40821d-98bb-960e-999d-c456af8bc9e9"}
2025-02-14 16:22:10:902
{"output":{"session_id":"c2265dd99e4b40e0b5b3638824f21dd9","finish_reason":"null","text":"等等,还能表达"},"usage":{"models":[{"input_tokens":51,"output_tokens":47,"model_id":"qwen-plus"}]},"request_id":"2d40821d-98bb-960e-999d-c456af8bc9e9"}
2025-02-14 16:22:11:062
{"output":{"session_id":"c2265dd99e4b40e0b5b3638824f21dd9","finish_reason":"null","text":"观点,玩游戏等"},"usage":{"models":[{"input_tokens":51,"output_tokens":51,"model_id":"qwen-plus"}]},"request_id":"2d40821d-98bb-960e-999d-c456af8bc9e9"}
2025-02-14 16:22:11:233
{"output":{"session_id":"c2265dd99e4b40e0b5b3638824f21dd9","finish_reason":"null","text":"。"},"usage":{"models":[{"input_tokens":51,"output_tokens":52,"model_id":"qwen-plus"}]},"request_id":"2d40821d-98bb-960e-999d-c456af8bc9e9"}
2025-02-14 16:22:11:309
{"output":{"session_id":"c2265dd99e4b40e0b5b3638824f21dd9","finish_reason":"stop","text":""},"usage":{"models":[{"input_tokens":51,"output_tokens":52,"model_id":"qwen-plus"}]},"request_id":"2d40821d-98bb-960e-999d-c456af8bc9e9"}
2025-02-14 16:22:11:388
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
)
func main() {
// 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey := "sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
apiKey := os.Getenv("DASHSCOPE_API_KEY")
appId := "YOUR_APP_ID" // 替换为实际的应用 ID
if apiKey == "" {
fmt.Println("请确保设置了DASHSCOPE_API_KEY。")
return
}
url := fmt.Sprintf("https://dashscope.aliyuncs.com/api/v1/apps/%s/completion", appId)
// 创建请求体,其中incremental_output为是否开启流式响应
requestBody := map[string]interface{}{
"input": map[string]string{
"prompt": "你是谁?",
},
"parameters": map[string]interface{}{
"incremental_output": true,
},
"debug": map[string]interface{}{},
}
jsonData, err := json.Marshal(requestBody)
if err != nil {
fmt.Printf("Failed to marshal JSON: %v\n", err)
return
}
// 创建 HTTP POST 请求
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
fmt.Printf("Failed to create request: %v\n", err)
return
}
// 设置请求头,其中X-DashScope-SSE设置为enable,表示开启流式响应
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-DashScope-SSE", "enable")
// 发送请求
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Failed to send request: %v\n", err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Printf("Request failed with status code: %d\n", resp.StatusCode)
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
return
}
// 处理流式响应
reader := io.Reader(resp.Body)
buf := make([]byte, 1024)
for {
n, err := reader.Read(buf)
if n > 0 {
data := string(buf[:n])
lines := strings.Split(data, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if len(line) >= 5 && line[:5] == "data:" {
timestamp := time.Now().Format("2006-01-02 15:04:05.000")
fmt.Printf("%s: %s\n", timestamp, line[5:])
} else if len(line) > 0 {
fmt.Println(line)
}
}
}
if err != nil {
if err == io.EOF {
break
}
fmt.Printf("Error reading response: %v\n", err)
break
}
}
}
id:1
event:result
:HTTP_STATUS/200
2025-02-13 18:21:09.050: {"output":{"session_id":"830189188149488794708ae012f4c595","finish_reason":"null","text":"我是"},"usage":{"models":[{"input_tokens":262,"output_tokens":1,"model_id":"qwen-plus"}]},"request_id":"2563953d-914c-9256-ae1a-b62beb957112"}
id:2
event:result
:HTTP_STATUS/200
2025-02-13 18:21:10.016: {"output":{"session_id":"830189188149488794708ae012f4c595","finish_reason":"null","text":"通"},"usage":{"models":[{"input_tokens":262,"output_tokens":2,"model_id":"qwen-plus"}]},"request_id":"2563953d-914c-9256-ae1a-b62beb957112"}
id:3
event:result
:HTTP_STATUS/200
2025-02-13 18:21:10.016: {"output":{"session_id":"830189188149488794708ae012f4c595","finish_reason":"null","text":"义"},"usage":{"models":[{"input_tokens":262,"output_tokens":3,"model_id":"qwen-plus"}]},"request_id":"2563953d-914c-9256-ae1a-b62beb957112"}
id:4
event:result
:HTTP_STATUS/200
2025-02-13 18:21:10.016: {"output":{"session_id":"830189188149488794708ae012f4c595","finish_reason":"null","text":"千问,由"},"usage":{"models":[{"input_tokens":262,"output_tokens":7,"model_id":"qwen-plus"}]},"request_id":"2563953d-914c-9256-ae1a-b62beb957112"}
id:5
event:result
:HTTP_STATUS/200
2025-02-13 18:21:10.017: {"output":{"session_id":"830189188149488794708ae012f4c595","finish_reason":"null","text":"阿里云开发的"},"usage":{"models":[{"input_tokens":262,"output_tokens":11,"model_id":"qwen-plus"}]},"request_id":"2563953d-914c-9256-ae1a-b62beb957112"}
id:6
event:result
:HTTP_STATUS/200
2025-02-13 18:21:10.017: {"output":{"session_id":"830189188149488794708ae012f4c595","finish_reason":"null","text":"AI助手。我"},"usage":{"models":[{"input_tokens":262,"output_tokens":15,"model_id":"qwen-plus"}]},"request_id":"2563953d-914c-9256-ae1a-b62beb957112"}
id:7
event:result
:HTTP_STATUS/200
2025-02-13 18:21:10.017: {"output":{"session_id":"830189188149488794708ae012f4c595","finish_reason":"null","text":"被设计用来回答"},"usage":{"models":[{"input_tokens":262,"output_tokens":19,"model_id":"qwen-plus"}]},"request_id":"2563953d-914c-9256-ae1a-b62beb957112"}
id:8
event:result
:HTTP_STATUS/200
2025-02-13 18:21:10.018: {"output":{"session_id":"830189188149488794708ae012f4c595","finish_reason":"null","text":"各种问题、提供"},"usage":{"models":[{"input_tokens":262,"output_tokens":23,"model_id":"qwen-plus"}]},"request_id":"2563953d-914c-9256-ae1a-b62beb957112"}
id:9
event:result
:HTTP_STATUS/200
2025-02-13 18:21:10.102: {"output":{"session_id":"830189188149488794708ae012f4c595","finish_reason":"null","text":"信息和与用户"},"usage":{"models":[{"input_tokens":262,"output_tokens":27,"model_id":"qwen-plus"}]},"request_id":"2563953d-914c-9256-ae1a-b62beb957112"}
id:10
event:result
:HTTP_STATUS/200
2025-02-13 18:21:10.257: {"output":{"session_id":"830189188149488794708ae012f4c595","finish_reason":"null","text":"进行对话。需要"},"usage":{"models":[{"input_tokens":262,"output_tokens":31,"model_id":"qwen-plus"}]},"request_id":"2563953d-914c-9256-ae1a-b62beb957112"}
id:11
event:result
:HTTP_STATUS/200
2025-02-13 18:21:10.414: {"output":{"session_id":"830189188149488794708ae012f4c595","finish_reason":"null","text":"帮助吗?"},"usage":{"models":[{"input_tokens":262,"output_tokens":34,"model_id":"qwen-plus"}]},"request_id":"2563953d-914c-9256-ae1a-b62beb957112"}
id:12
event:result
:HTTP_STATUS/200
2025-02-13 18:21:10.481: {"output":{"session_id":"830189188149488794708ae012f4c595","finish_reason":"stop","text":""},"usage":{"models":[{"input_tokens":262,"output_tokens":34,"model_id":"qwen-plus"}]},"request_id":"2563953d-914c-9256-ae1a-b62beb957112"}
Pass custom parameters
Agents can adapt to various business scenarios using custom prompt variables to guide output, plugin parameters to extend capabilities, and user-level authentication parameters for access control. When you make a call, use thebiz_params parameter to pass these custom parameters for flexible responses.
| Description | Instructions |
|---|---|
user_prompt_paramsType: objectPasses custom prompt variables.Use this to pass variables that are inserted into the prompt. You can define the variables in the console and pass their specific values during the API call.Example:Prompt: Please recommend three local dishes for {{city}}. Display only the dish names, separated by commas.Parameters:Copy | In the console, perform the following steps:
Ensure that the custom variable name added in the application matches the name passed in the API call. |
user_defined_paramsType: objectPasses custom plugin parameters.Use this to pass business data, such as a city or date, that the plugin needs to execute its task. | In the console, perform the following steps:
|
user_defined_tokensType: objectPasses custom user-level authentication parameters for a plugin.This parameter is used for user identity verification when calling a plugin, such as a DASHSCOPE_API_KEY.Copy |
- Prompt variables
- Plugin business parameters
- Plugin user-level authentication parameters
-
In the console, add a custom variable to the agent application, reference it in the prompt, and then publish the application. Example:
- Click + Custom Variable to add a variable.
- Configure the variable: set the name to
city, description to city name, and default value to Hangzhou. - In the prompt editing area, enter a prompt that references the variable, for example:
Based on the city name ${city}, recommend three local delicacies. Display only the food names, separated by commas.
-
Make an API call. The following are examples:
- Python
- Java
- HTTP
Sample requestSample responseCopyimport os import dashscope # 需要 dashscope 2.0.1 及以上版本 from dashscope import DashScopeError client = dashscope.Client( # 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 api_key=os.getenv("DASHSCOPE_API_KEY")) biz_params = { # 智能体应用的自定义变量参数,可替换为实际参数,支持传入多个变量键值对,英文逗号隔开 "user_prompt_params":{ "city": "北京"}} try: response = client.apps.completions.create( app_id='YOUR_APP_ID', # 替换为实际的应用ID,应用卡片获取 prompt='美食推荐', biz_params=biz_params) print('%s\n' % (response.text)) # 处理只输出文本text # print('%s\n' % (response.usage)) except DashScopeError as e: print(f'request_id={e.request_id}') print(f'code={e.code}') print(f'message={e.message}') print(f'请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code')Copy北京烤鸭,炸酱面,豆汁儿Sample requestSample responseCopyimport 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; public class Main { public static void appCall() throws NoApiKeyException, InputRequiredException { String bizParams = // 智能体应用的自定义变量参数传递,可替换为实际变量参数,支持传入多个变量键值对,英文逗号隔开 "{\"user_prompt_params\":{\"city\":\"北京\"}}"; ApplicationParam param = ApplicationParam.builder() // 若没有配置环境变量,可用百炼API Key将下行替换为:.apiKey("sk-xxx")。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 .apiKey(System.getenv("DASHSCOPE_API_KEY")) .appId("YOUR_APP_ID") // 替换为实际的应用ID,应用卡片获取 .prompt("美食推荐") .bizParams(JsonUtils.parse(bizParams)) .build(); Application application = new Application(); ApplicationResult result = application.call(param); System.out.printf("%s\n", result.getOutput().getText()); } public static void main(String[] args) { try { appCall(); } catch (ApiException | NoApiKeyException | InputRequiredException e) { System.out.printf("Exception: %s", e.getMessage()); System.out.println("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code"); } System.exit(0); } }Copy北京烤鸭,炸酱面,豆汁儿- curl
- PHP
- Node.js
- C#
- Go
Sample requestCopycurl -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_prompt_params":{"city": "北京"} } }, "parameters": {}, "debug":{} }'Replace YOUR_APP_ID with your actual application ID, which you can find on the application card.
The user_prompt_params parameter supports multiple custom key-value pairs separated by commas.
Sample responseCopy{ "output": { "finish_reason": "stop", "session_id": "1ad91249a37148389ac042530002ec94", "text": "北京烤鸭,炸酱面,豆汁儿" }, "usage": { "models": [ { "output_tokens": 11, "model_id": "qwen3.5-flash", "input_tokens": 78 } ] }, "request_id": "4fd714e5-dc35-9933-98f4-0ed314918c1f" }Sample requestSample responseCopy<?php # 若没有配置环境变量,可用百炼API Key将下行替换为:$api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 $api_key = getenv("DASHSCOPE_API_KEY"); $application_id = 'YOUR_APP_ID'; // 替换为实际的应用 ID $url = "https://dashscope.aliyuncs.com/api/v1/apps/$application_id/completion"; //user_prompt_params支持传递多个自定义变量键值对,英文逗号隔开 // 构造请求数据 $data = [ "input" => [ 'prompt' => '美食推荐', 'biz_params' => [ 'user_prompt_params' => [ 'city' => "北京" ] ] ], ]; // 将数据编码为 JSON $dataString = json_encode($data); // 检查 json_encode 是否成功 if (json_last_error() !== JSON_ERROR_NONE) { die("JSON encoding failed with error: " . json_last_error_msg()); } // 初始化 cURL 会话 $ch = curl_init($url); // 设置 cURL 选项 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Authorization: Bearer ' . $api_key ]); // 执行请求 $response = curl_exec($ch); // 检查 cURL 执行是否成功 if ($response === false) { die("cURL Error: " . curl_error($ch)); } // 获取 HTTP 状态码 $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 关闭 cURL 会话 curl_close($ch); // 解码响应数据 $response_data = json_decode($response, true); // 处理响应 if ($status_code == 200) { if (isset($response_data['output']['text'])) { echo "{$response_data['output']['text']}\n"; } else { echo "No text in response.\n"; } }else { if (isset($response_data['request_id'])) { echo "request_id={$response_data['request_id']}\n";} echo "code={$status_code}\n"; if (isset($response_data['message'])) { echo "message={$response_data['message']}\n";} else { echo "message=Unknown error\n";} } ?>Copy北京烤鸭,炸酱面,豆汁儿Dependency:Sample requestCopynpm install axiosSample responseCopyconst axios = require('axios'); async function callDashScope() { // 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey='sk-xxx'。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 const apiKey = process.env.DASHSCOPE_API_KEY; const appId = 'YOUR_APP_ID';// 替换为实际的应用 ID const url = `https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`; // user_prompt_params可传递多个自定义变量键值对,英文逗号隔开。 const data = { input: { prompt: "美食推荐", biz_params: { user_prompt_params: { 'city': '北京' } } }, parameters: {}, debug: {} }; try { console.log("Sending request to DashScope API..."); const response = await axios.post(url, data, { headers: { 'Authorization': `Bearer ${apiKey}`, 'Content-Type': 'application/json' } }); if (response.status === 200) { if (response.data.output && response.data.output.text) { console.log(`${response.data.output.text}`); } } else { console.log("Request failed:"); if (response.data.request_id) { console.log(`request_id=${response.data.request_id}`); } console.log(`code=${response.status}`); if (response.data.message) { console.log(`message=${response.data.message}`); } else { console.log('message=Unknown error'); } } } catch (error) { console.error(`Error calling DashScope: ${error.message}`); if (error.response) { console.error(`Response status: ${error.response.status}`); console.error(`Response data: ${JSON.stringify(error.response.data, null, 2)}`); } } } callDashScope();Copy北京烤鸭,炸酱面,豆汁儿Sample requestSample responseCopyusing System.Text; class Program { static async Task Main(string[] args) { // 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 string apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY")?? throw new InvalidOperationException("DASHSCOPE_API_KEY environment variable is not set.");; string appId = "YOUR_APP_ID";// 替换为实际的应用ID // user_prompt_params支持传递多个自定义变量键值对,英文逗号隔开 if (string.IsNullOrEmpty(apiKey)) { Console.WriteLine("请确保设置了 DASHSCOPE_API_KEY。"); return; } string url = $"https://dashscope.aliyuncs.com/api/v1/apps/{appId}/completion"; using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}"); string jsonContent = $@"{{ ""input"": {{ ""prompt"": ""美食推荐"", ""biz_params"": {{ ""user_prompt_params"": {{ ""city"": ""北京"" }} }} }}, ""parameters"": {{}}, ""debug"": {{}} }}"; HttpContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); try { HttpResponseMessage response = await client.PostAsync(url, content); if (response.IsSuccessStatusCode) { string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine("Request successful:"); Console.WriteLine(responseBody); } else { Console.WriteLine($"Request failed with status code: {response.StatusCode}"); string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseBody); } } catch (Exception ex) { Console.WriteLine($"Error calling DashScope: {ex.Message}"); } } } }Copy{ "output": { "finish_reason": "stop", "session_id": "de5b3942acce40ba8739338518c01b9e", "text": "北京烤鸭,炸酱面,豆汁儿" }, "usage": { "models": [ { "output_tokens": 11, "model_id": "qwen3.5-flash", "input_tokens": 78 } ] }, "request_id": "86f89865-b851-9d5f-b96d-181d8d402f85" }Sample requestSample responseCopypackage main import ( "bytes" "encoding/json" "fmt" "io" "net/http" "os" ) func main() { // 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey := "sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 apiKey := os.Getenv("DASHSCOPE_API_KEY") appId := "YOUR_APP_ID" // 替换为实际的应用 ID if apiKey == "" { fmt.Println("请确保设置了DASHSCOPE_API_KEY。") return } url := fmt.Sprintf("https://dashscope.aliyuncs.com/api/v1/apps/%s/completion", appId) // 创建请求体,user_prompt_params支持传递多个自定义变量键值对,英文逗号隔开 requestBody := map[string]interface{}{ "input": map[string]interface{}{ "prompt": "美食推荐", "biz_params": map[string]interface{}{ "user_prompt_params": map[string]interface{}{ "city": "北京", }, }, }, "parameters": map[string]interface{}{}, "debug": map[string]interface{}{}, } jsonData, err := json.Marshal(requestBody) if err != nil { fmt.Printf("Failed to marshal JSON: %v\n", err) return } // 创建 HTTP POST 请求 req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) if err != nil { fmt.Printf("Failed to create request: %v\n", err) return } // 设置请求头 req.Header.Set("Authorization", "Bearer "+apiKey) req.Header.Set("Content-Type", "application/json") // 发送请求 client := &http.Client{} resp, err := client.Do(req) if err != nil { fmt.Printf("Failed to send request: %v\n", err) return } defer resp.Body.Close() // 读取响应 body, err := io.ReadAll(resp.Body) if err != nil { fmt.Printf("Failed to read response: %v\n", err) return } // 处理响应 if resp.StatusCode == http.StatusOK { fmt.Println("Request successful:") fmt.Println(string(body)) } else { fmt.Printf("Request failed with status code: %d\n", resp.StatusCode) fmt.Println(string(body)) } }Copy{ "output": { "finish_reason": "stop", "session_id": "26daaa1561ba482c8248502db55df3d3", "text": "北京烤鸭,炸酱面,豆汁儿" }, "usage": { "models": [ { "output_tokens": 11, "model_id": "qwen3.5-flash", "input_tokens": 78 } ] }, "request_id": "405ab32b-9277-9408-ac1f-711845d334ce" }
<YOUR_TOOL_ID> with the ID of the associated plugin, which you can find on the plugin card. Pass the key-value pairs for the input parameters configured in the plugin. In this example, the parameter is article_index and its value is 2.
- Python
- Java
- HTTP
import os
import dashscope
# 需要 dashscope 2.0.1 及以上版本
from dashscope import DashScopeError
client = dashscope.Client(
# 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
api_key=os.getenv("DASHSCOPE_API_KEY"))
biz_params = {
# 智能体应用的自定义插件输入参数传递,自定义的插件ID替换<YOUR_TOOL_ID>
"user_defined_params": {
"<YOUR_TOOL_ID>": {
"article_index": 2}}}
try:
response = client.apps.completions.create(
app_id='YOUR_APP_ID',
prompt='寝室公约内容',
biz_params=biz_params)
print('%s\n' % (response.text)) # 处理只输出文本text
# print('%s\n' % (response.usage))
except DashScopeError as e:
print(f'request_id={e.request_id}')
print(f'code={e.code}')
print(f'message={e.message}')
print(f'请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code')
寝室公约的第二条规定如下:
"寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。"
这表明在寝室内,成员之间应该培养一种积极正面的生活和学习氛围,彼此帮助和支持,同时也要学会理解和尊重他人。如果您需要了解公约的其他条款,请告诉我!
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;
public class Main {
public static void appCall() throws NoApiKeyException, InputRequiredException {
String bizParams =
// 智能体应用的自定义插件输入参数传递,自定义的插件ID替换<YOUR_TOOL_ID>
"{\"user_defined_params\":{\"<YOUR_TOOL_ID>\":{\"article_index\":2}}}";
ApplicationParam param = ApplicationParam.builder()
// 若没有配置环境变量,可用百炼API Key将下行替换为:.apiKey("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();
ApplicationResult result = application.call(param);
System.out.printf("%s\n",
result.getOutput().getText());
}
public static void main(String[] args) {
try {
appCall();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.out.printf("Exception: %s", e.getMessage());
System.out.println("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code");
}
System.exit(0);
}
}
寝室公约的第二条规定如下:
第二条 寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。
这强调了在共同生活环境中,室友之间应该保持积极正面的关系,通过相互帮助和支持来营造一个和谐的生活和学习氛围。如果有更多具体的条款需要了解,请告知我。
- curl
- PHP
- Node.js
- C#
- Go
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_TOOL_ID>":
{
"article_index": 2
}
}
}
},
"parameters": {},
"debug":{}
}'
Replace YOUR_APP_ID with your application ID. Replace <YOUR_TOOL_ID> with your plugin ID.Sample response
{"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"}%
<?php
# 若没有配置环境变量,可用百炼API Key将下行替换为:$api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
$api_key = getenv("DASHSCOPE_API_KEY");
$application_id = 'YOUR_APP_ID'; // 替换为实际的应用 ID
$url = "https://dashscope.aliyuncs.com/api/v1/apps/$application_id/completion";
//<YOUR_TOOL_ID>替换为实际的插件ID
// 构造请求数据
$data = [
"input" => [
'prompt' => '寝室公约内容',
'biz_params' => [
'user_defined_params' => [
'<YOUR_TOOL_ID>' => [
'article_index' => 2
]
]
]
],
];
// 将数据编码为 JSON
$dataString = json_encode($data);
// 检查 json_encode 是否成功
if (json_last_error() !== JSON_ERROR_NONE) {
die("JSON encoding failed with error: " . json_last_error_msg());
}
// 初始化 cURL 会话
$ch = curl_init($url);
// 设置 cURL 选项
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
]);
// 执行请求
$response = curl_exec($ch);
// 检查 cURL 执行是否成功
if ($response === false) {
die("cURL Error: " . curl_error($ch));
}
// 获取 HTTP 状态码
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// 关闭 cURL 会话
curl_close($ch);
// 解码响应数据
$response_data = json_decode($response, true);
// 处理响应
if ($status_code == 200) {
if (isset($response_data['output']['text'])) {
echo "{$response_data['output']['text']}\n";
} else {
echo "No text in response.\n";
}
}else {
if (isset($response_data['request_id'])) {
echo "request_id={$response_data['request_id']}\n";}
echo "code={$status_code}\n";
if (isset($response_data['message'])) {
echo "message={$response_data['message']}\n";}
else {
echo "message=Unknown error\n";}
}
?>
寝室公约的第二条规定:寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。这是为了保证大家能在一个和谐友爱的环境中生活和学习。如果有更多具体的条款需要了解,或者有其他问题,随时可以问我!
npm install axios
const axios = require('axios');
async function callDashScope() {
// 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey='sk-xxx'。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
const apiKey = process.env.DASHSCOPE_API_KEY;
const appId = 'YOUR_APP_ID';// 替换为实际的应用 ID
const pluginCode = 'YOUR_TOOL_ID';// 替换为实际的插件ID
const url = `https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`;
const data = {
input: {
prompt: "寝室公约内容",
biz_params: {
user_defined_params: {
[pluginCode]: {
// article_index为自定义插件的变量,替换为实际的插件变量
'article_index': 3
}
}
}
},
parameters: {},
debug: {}
};
try {
console.log("Sending request to DashScope API...");
const response = await axios.post(url, data, {
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
}
});
if (response.status === 200) {
if (response.data.output && response.data.output.text) {
console.log(`${response.data.output.text}`);
}
} else {
console.log("Request failed:");
if (response.data.request_id) {
console.log(`request_id=${response.data.request_id}`);
}
console.log(`code=${response.status}`);
if (response.data.message) {
console.log(`message=${response.data.message}`);
} else {
console.log('message=Unknown error');
}
}
} catch (error) {
console.error(`Error calling DashScope: ${error.message}`);
if (error.response) {
console.error(`Response status: ${error.response.status}`);
console.error(`Response data: ${JSON.stringify(error.response.data, null, 2)}`);
}
}
}
callDashScope();
寝室公约的第三条规定如下:
注意安全用电,杜绝火灾隐患。寝室内严禁使用明火、违规电器、各种灶具以及其他违规物品,不得存放易爆、易燃物品,私接电源。
如果您需要了解更多的规定,请告诉我。
using System.Text;
class Program
{
static async Task Main(string[] args)
{
// 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
string apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY")?? throw new InvalidOperationException("DASHSCOPE_API_KEY environment variable is not set.");;
string appId = "YOUR_APP_ID";// 替换为实际的应用ID
if (string.IsNullOrEmpty(apiKey))
{
Console.WriteLine("请确保设置了 DASHSCOPE_API_KEY。");
return;
}
string url = $"https://dashscope.aliyuncs.com/api/v1/apps/{appId}/completion";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
string pluginCode = "YOUR_TOOL_ID"; // YOUR_TOOL_ID替换为实际的插件 ID
string jsonContent = $@"{{
""input"": {{
""prompt"": ""寝室公约内容"",
""biz_params"": {{
""user_defined_params"": {{
""{pluginCode}"": {{
""article_index"": 2
}}
}}
}}
}},
""parameters"": {{}},
""debug"": {{}}
}}";
HttpContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
try
{
HttpResponseMessage response = await client.PostAsync(url, content);
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine("Request successful:");
Console.WriteLine(responseBody);
}
else
{
Console.WriteLine($"Request failed with status code: {response.StatusCode}");
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error calling DashScope: {ex.Message}");
}
}
}
}
{
"output": {
"finish_reason": "stop",
"session_id": "237ca6187c814f3b9e7461090a5f8b74",
"text": "寝室公约的第二条规定如下:
"寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。"
这表示在寝室内,成员之间需要建立起一种积极正面的关系,通过帮助、关心和支持彼此来营造一个和谐的生活和学习环境。同时也要学会理解和接受室友之间的差异,以真诚的态度去交流沟通。如果还有其他条款或具体内容想要了解,请告诉我!"
},
"usage": {
"models": [
{
"output_tokens": 133,
"model_id": "qwen-max",
"input_tokens": 829
}
]
},
"request_id": "64e8c359-d071-9d2e-bb94-187e86cc3a79"
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)
func main() {
// 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey := "sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
apiKey := os.Getenv("DASHSCOPE_API_KEY")
appId := "YOUR_APP_ID" // 替换为实际的应用 ID
pluginCode := "YOUR_TOOL_ID" // 替换为实际的插件 ID
if apiKey == "" {
fmt.Println("请确保设置了DASHSCOPE_API_KEY。")
return
}
url := fmt.Sprintf("https://dashscope.aliyuncs.com/api/v1/apps/%s/completion", appId)
// 创建请求体
requestBody := map[string]interface{}{
"input": map[string]interface{}{
"prompt": "寝室公约内容",
"biz_params": map[string]interface{}{
"user_defined_params": map[string]interface{}{
pluginCode: map[string]interface{}{
"article_index": 2,
},
},
},
},
"parameters": map[string]interface{}{},
"debug": map[string]interface{}{},
}
jsonData, err := json.Marshal(requestBody)
if err != nil {
fmt.Printf("Failed to marshal JSON: %v\n", err)
return
}
// 创建 HTTP POST 请求
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
fmt.Printf("Failed to create request: %v\n", err)
return
}
// 设置请求头
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
// 发送请求
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Failed to send request: %v\n", err)
return
}
defer resp.Body.Close()
// 读取响应
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Failed to read response: %v\n", err)
return
}
// 处理响应
if resp.StatusCode == http.StatusOK {
fmt.Println("Request successful:")
fmt.Println(string(body))
} else {
fmt.Printf("Request failed with status code: %d\n", resp.StatusCode)
fmt.Println(string(body))
}
}
{
"output": {
"finish_reason": "stop",
"session_id": "860d2a4c1f3649ac880298537993cb51",
"text": "寝室公约的第二条规定如下:
寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。
这强调了在宿舍生活中,室友之间应该保持良好的互助关系,同时也要互相尊重对方。您想要了解其他条款的内容吗?"
},
"usage": {
"models": [
{
"output_tokens": 84,
"model_id": "qwen-max",
"input_tokens": 876
}
]
},
"request_id": "0a250055-90a4-992d-9276-e268ad35d1ab"
}
<YOUR_TOOL_ID> with the ID of the associated plugin, which you can find on the plugin card. Pass the key-value pairs for the input parameters configured in the plugin. In this example, the parameter is article_index and its value is 2.
- Python
- Java
- HTTP
import os
import dashscope
# 需要 dashscope 2.0.1 及以上版本
from dashscope import DashScopeError
client = dashscope.Client(
# 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
api_key=os.getenv("DASHSCOPE_API_KEY"))
biz_params = {
# 智能体应用的自定义插件鉴权传递,自定义的插件ID替换<YOUR_TOOL_ID>,鉴权信息替换YOUR_TOKEN,如API key
"user_defined_params": {
"<YOUR_TOOL_ID>": {
"article_index": 2}},
"user_defined_tokens": {
"<YOUR_TOOL_ID>": {
"user_token": "YOUR_TOKEN"}}}
try:
response = client.apps.completions.create(
app_id='YOUR_APP_ID',
prompt='寝室公约内容',
biz_params=biz_params)
print('%s\n' % (response.text)) # 处理只输出文本text
# print('%s\n' % (response.usage))
except DashScopeError as e:
print(f'request_id={e.request_id}')
print(f'code={e.code}')
print(f'message={e.message}')
print(f'请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code')
寝室公约的第二条规定如下:
寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。
如果您需要了解更多的规定内容,请告诉我。
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;
public class Main {
public static void appCall() throws NoApiKeyException, InputRequiredException {
String bizParams =
// <YOUR_TOOL_ID>替换为实际的插件ID,YOUR_TOKEN替换为实际的Token,如API key
"{\"user_defined_params\":{\"<YOUR_TOOL_ID>\":{\"article_index\":2}}," +
"\"user_defined_tokens\":{\"<YOUR_TOOL_ID>\":{\"user_token\":\"YOUR_TOKEN\"}}}";
ApplicationParam param = ApplicationParam.builder()
// 若没有配置环境变量,可用百炼API Key将下行替换为:.apiKey("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();
ApplicationResult result = application.call(param);
System.out.printf("%s\n",
result.getOutput().getText());
}
public static void main(String[] args) {
try {
appCall();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.out.printf("Exception: %s", e.getMessage());
System.out.println("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code");
}
System.exit(0);
}
}
寝室公约的第二条规定如下:
寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。
如果您需要查询更多的规定内容,请告诉我。
- curl
- PHP
- Node.js
- C#
- Go
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_TOOL_ID>":
{
"article_index": 2
}
},
"user_defined_tokens":
{
"<YOUR_TOOL_ID>":
{
"user_token": "YOUR_TOKEN"
}
}
}
},
"parameters": {},
"debug":{}
}'
Replace YOUR_APP_ID with your actual application ID. Replace <YOUR_TOOL_ID> with the actual plugin ID.Sample response
{"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"}%
<?php
# 若没有配置环境变量,可用百炼API Key将下行替换为:$api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
$api_key = getenv("DASHSCOPE_API_KEY");
$application_id = 'YOUR_APP_ID'; // 替换为实际的应用 ID
$url = "https://dashscope.aliyuncs.com/api/v1/apps/$application_id/completion";
// 构造请求数据
$data = [
"input" => [
'prompt' => '寝室公约内容',
'biz_params' => [
'user_defined_params' => [
'<YOUR_TOOL_ID>' => [//<YOUR_TOOL_ID>替换为实际的插件ID
'article_index' => 2
]
],
'user_defined_tokens' => [
'<YOUR_TOOL_ID>' => [//<YOUR_TOOL_ID>替换为实际的插件ID
'user_token' => 'YOUR_TOKEN'//替换为实际的Token,如API key
]
]
]
],
];
// 将数据编码为 JSON
$dataString = json_encode($data);
// 检查 json_encode 是否成功
if (json_last_error() !== JSON_ERROR_NONE) {
die("JSON encoding failed with error: " . json_last_error_msg());
}
// 初始化 cURL 会话
$ch = curl_init($url);
// 设置 cURL 选项
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
]);
// 执行请求
$response = curl_exec($ch);
// 检查 cURL 执行是否成功
if ($response === false) {
die("cURL Error: " . curl_error($ch));
}
// 获取 HTTP 状态码
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// 关闭 cURL 会话
curl_close($ch);
// 解码响应数据
$response_data = json_decode($response, true);
// 处理响应
if ($status_code == 200) {
if (isset($response_data['output']['text'])) {
echo "{$response_data['output']['text']}\n";
} else {
echo "No text in response.\n";
}
}else {
if (isset($response_data['request_id'])) {
echo "request_id={$response_data['request_id']}\n";}
echo "code={$status_code}\n";
if (isset($response_data['message'])) {
echo "message={$response_data['message']}\n";}
else {
echo "message=Unknown error\n";}
}
?>
寝室公约的第二条规定如下:
> 寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。
如果需要了解更多的公约内容或其他信息,请随时告诉我!
npm install axios
const axios = require('axios');
async function callDashScope() {
// 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey='sk-xxx'。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
const apiKey = process.env.DASHSCOPE_API_KEY;
const appId = 'YOUR_APP_ID';// 替换为实际的应用 ID
const pluginCode = 'YOUR_TOOL_ID';// 替换为实际的插件ID
const url = `https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`;
const data = {
input: {
prompt: "寝室公约内容",
biz_params: {
user_defined_params: {
[pluginCode]: {
// article_index为自定义插件的变量,替换为实际的插件变量
'article_index': 6
}
},
user_defined_tokens: {
[pluginCode]: {
// YOUR_TOKEN替换为实际的鉴权信息,如API key
user_token: 'YOUR_TOKEN'
}
}
}
},
parameters: {},
debug: {}
};
try {
console.log("Sending request to DashScope API...");
const response = await axios.post(url, data, {
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
}
});
if (response.status === 200) {
if (response.data.output && response.data.output.text) {
console.log(`${response.data.output.text}`);
}
} else {
console.log("Request failed:");
if (response.data.request_id) {
console.log(`request_id=${response.data.request_id}`);
}
console.log(`code=${response.status}`);
if (response.data.message) {
console.log(`message=${response.data.message}`);
} else {
console.log('message=Unknown error');
}
}
} catch (error) {
console.error(`Error calling DashScope: ${error.message}`);
if (error.response) {
console.error(`Response status: ${error.response.status}`);
console.error(`Response data: ${JSON.stringify(error.response.data, null, 2)}`);
}
}
}
callDashScope();
寝室公约的第六条规定:养成良好的作息习惯,每一位寝室成员都享有休息的权利和承担保证他人休息权利和义务。如果你需要了解更多的规定内容,请进一步说明。
using System.Text;
class Program
{
static async Task Main(string[] args)
{
// 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
string apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY")?? throw new InvalidOperationException("DASHSCOPE_API_KEY environment variable is not set.");;
string appId = "YOUR_APP_ID";// 替换为实际的应用ID
if (string.IsNullOrEmpty(apiKey))
{
Console.WriteLine("请确保设置了 DASHSCOPE_API_KEY。");
return;
}
string url = $"https://dashscope.aliyuncs.com/api/v1/apps/{appId}/completion";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
string pluginCode = "YOUR_TOOL_ID"; //替换为实际的插件 ID
// YOUR_TOKEN替换为实际的Token,如API key
string jsonContent = $@"{{
""input"": {{
""prompt"": ""寝室公约内容"",
""biz_params"": {{
""user_defined_params"": {{
""{pluginCode}"": {{
""article_index"": 2
}}
}},
""user_defined_tokens"": {{
""{pluginCode}"": {{
""user_token"": ""YOUR_TOKEN""
}}
}}
}}
}},
""parameters"": {{}},
""debug"": {{}}
}}";
HttpContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
try
{
HttpResponseMessage response = await client.PostAsync(url, content);
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine("Request successful:");
Console.WriteLine(responseBody);
}
else
{
Console.WriteLine($"Request failed with status code: {response.StatusCode}");
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error calling DashScope: {ex.Message}");
}
}
}
}
{
"output": {
"finish_reason": "stop",
"session_id": "1a1913a9922a401f8eba36df8ea1a062",
"text": "寝室公约的第二条规定如下:
寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。
如需了解更详细的公约内容,请进一步指明。"
},
"usage": {
"models": [
{
"output_tokens": 66,
"model_id": "qwen-max",
"input_tokens": 802
}
]
},
"request_id": "04bac806-c5e6-9fab-a846-a66641862be9"
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)
func main() {
// 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey := "sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
apiKey := os.Getenv("DASHSCOPE_API_KEY")
appId := "YOUR_APP_ID" // 替换为实际的应用 ID
pluginCode := "YOUR_TOOL_ID" // 替换为实际的插件 ID
if apiKey == "" {
fmt.Println("请确保设置了DASHSCOPE_API_KEY。")
return
}
url := fmt.Sprintf("https://dashscope.aliyuncs.com/api/v1/apps/%s/completion", appId)
// 创建请求体
requestBody := map[string]interface{}{
"input": map[string]interface{}{
"prompt": "寝室公约内容",
"biz_params": map[string]interface{}{
"user_defined_params": map[string]interface{}{
pluginCode: map[string]interface{}{
"article_index": 10,
},
},
"user_defined_tokens": map[string]interface{}{
pluginCode: map[string]interface{}{
"user_token": "YOUR_USER_TOKEN", // 替换实际的鉴权 token,如API key
},
},
},
},
"parameters": map[string]interface{}{},
"debug": map[string]interface{}{},
}
jsonData, err := json.Marshal(requestBody)
if err != nil {
fmt.Printf("Failed to marshal JSON: %v\n", err)
return
}
// 创建 HTTP POST 请求
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
fmt.Printf("Failed to create request: %v\n", err)
return
}
// 设置请求头
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
// 发送请求
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Failed to send request: %v\n", err)
return
}
defer resp.Body.Close()
// 读取响应
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Failed to read response: %v\n", err)
return
}
// 处理响应
if resp.StatusCode == http.StatusOK {
fmt.Println("Request successful:")
fmt.Println(string(body))
} else {
fmt.Printf("Request failed with status code: %d\n", resp.StatusCode)
fmt.Println(string(body))
}
}
{
"output": {
"finish_reason": "stop",
"session_id": "b8e051ba7e954ff8919208e7b84430fa",
"text": "寝室公约的第十条规定,寝室成员应共同努力,营造和维护内务整洁干净、美观、高文化品味的寝室环境。如果需要了解完整的寝室公约内容,可能还需要查看其他条款或直接咨询宿舍管理部门。对于更多具体内容,您还有想要了解的部分吗?"
},
"usage": {
"models": [
{
"output_tokens": 70,
"model_id": "qwen-max",
"input_tokens": 855
}
]
},
"request_id": "0921ee34-2754-9616-a826-cea33a0e0a14"
}
Advanced features
Retrieve from a knowledge base
The knowledge base feature powers Model Studio’s retrieval-augmented generation (RAG) capability. It helps large language models (LLMs) access private knowledge and stay up to date with the latest information. You can specify a retrieval scope when calling an agent application to improve response accuracy. For more information, see Create and use a knowledge base.Prerequisites
In the Model Studio console, enable your agent application and turn on the knowledge base switch. Then, publish the application.Specify a retrieval scope
-
You can retrieve from a specific knowledge base in three ways:
- In your application, click Configure knowledge base to associate a specific knowledge base, and then publish the application.
-
Do not associate a knowledge base in your application. Instead, pass the knowledge base ID in the
rag_optionsparameter during the API call. -
Associate a knowledge base in your application and pass the knowledge base ID in the
rag_optionsparameter during the API call.In this case, only the knowledge base passed in the API call is used for retrieval. For example, if your web-based agent application is linked to Knowledge Base A, but your API call specifies only Knowledge Base B, then only Knowledge Base B is retrieved, not Knowledge Base A.
Data.Idfield in the response from the CreateIndex API call (for unstructured knowledge bases only). The knowledge base can be one already associated with your agent application, or one that is not yet associated. For example, you can use Bailian Series Mobile Product Introduction.docx as an unstructured data knowledge base file.- Python
- Java
- HTTP
Sample requestSample responseCopyimport os import dashscope # 需要 dashscope 2.0.1 及以上版本 from dashscope import DashScopeError client = dashscope.Client( # 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 api_key=os.getenv("DASHSCOPE_API_KEY")) try: response = client.apps.completions.create( app_id='YOUR_APP_ID', # 应用ID替换YOUR_APP_ID prompt='请帮我推荐一款3000元以下的手机', rag_options={ "pipeline_ids": ["YOUR_PIPELINE_ID1","YOUR_PIPELINE_ID2"], # 替换为实际的知识库ID,逗号隔开多个 } ) print('%s\n' % (response.text)) # 处理只输出文本text # print('%s\n' % (response.usage)) except DashScopeError as e: print(f'request_id={e.request_id}') print(f'code={e.code}') print(f'message={e.message}') print(f'请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code')Copy根据您的预算,我推荐您选择**百炼 Zephyr Z9**。这款手机的参考售价在2499-2799元之间,符合您的预算需求。它拥有轻巧的6.4英寸1080 x 2340像素屏幕设计,搭配128GB存储与6GB RAM,适合日常使用。此外,它还配备了4000mAh电池以及支持30倍数字变焦的镜头,能够很好地满足拍照及续航的需求。如果您追求的是轻薄便携且功能全面的手机,那么百炼 Zephyr Z9会是一个不错的选择。Sample requestSample responseCopy// 建议dashscope SDK 的版本 >= 2.16.8; 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; public class Main { public static void streamCall() throws NoApiKeyException, InputRequiredException { ApplicationParam param = ApplicationParam.builder() // 若没有配置环境变量,可用百炼API Key将下行替换为:.apiKey("sk-xxx")。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 .apiKey(System.getenv("DASHSCOPE_API_KEY")) .appId("YOUR_APP_ID") // 替换为实际的应用ID .prompt("请帮我推荐一款3000元左右的手机") .ragOptions(RagOptions.builder() // 替换为实际指定的知识库ID,逗号隔开多个 .pipelineIds(Arrays.asList("PIPELINES_ID1", "PIPELINES_ID2")) .build()) .build(); Application application = new Application(); ApplicationResult result = application.call(param); System.out.printf("%s\n", result.getOutput().getText());// 处理只输出文本text } public static void main(String[] args) { try { streamCall(); } catch (ApiException | NoApiKeyException | InputRequiredException e) { System.out.printf("Exception: %s", e.getMessage()); System.out.println("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code"); } System.exit(0); } }Copy在3000元预算范围内,我推荐您考虑**百炼 Zephyr Z9**。这款手机定价在2499至2799之间,非常符合您的预算要求。它具有以下特点: - **轻巧设计**:6.4英寸屏幕大小适中,便于单手操作。 - **性能均衡**:搭载128GB存储与6GB RAM,对于日常使用来说足够了。 - **续航能力**:配备4000mAh电池,可以满足一天的正常使用需求。 - **拍照功能**:具备30倍数字变焦镜头,适合捕捉远处景物。 如果您更注重游戏体验或对其他方面有特别的需求,请告诉我,以便我能提供更加个性化的建议!- curl
- PHP
- Node.js
- C#
- Go
Sample requestCopycurl -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": {} }'Replace YOUR_APP_ID with your actual application ID. Replace YOUR_PIPELINE_ID1 with your actual knowledge base ID.
Sample responseCopy{"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"}%Sample requestSample responseCopy<?php # 若没有配置环境变量,可用百炼API Key将下行替换为:$api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 $api_key = getenv("DASHSCOPE_API_KEY"); $application_id = 'YOUR_APP_ID'; // 替换为实际的应用 ID $url = "https://dashscope.aliyuncs.com/api/v1/apps/$application_id/completion"; // 构造请求数据 $data = [ "input" => [ 'prompt' => '请帮我推荐一款3000元以下的手机' ], "parameters" => [ 'rag_options' => [ 'pipeline_ids' => ['YOUR_PIPELINE_ID1','YOUR_PIPELINE_ID2']//替换为指定的知识库ID,逗号隔开多个 ] ] ]; // 将数据编码为 JSON $dataString = json_encode($data); // 检查 json_encode 是否成功 if (json_last_error() !== JSON_ERROR_NONE) { die("JSON encoding failed with error: " . json_last_error_msg()); } // 初始化 cURL 会话 $ch = curl_init($url); // 设置 cURL 选项 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Authorization: Bearer ' . $api_key ]); // 执行请求 $response = curl_exec($ch); // 检查 cURL 执行是否成功 if ($response === false) { die("cURL Error: " . curl_error($ch)); } // 获取 HTTP 状态码 $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 关闭 cURL 会话 curl_close($ch); // 解码响应数据 $response_data = json_decode($response, true); // 处理响应 if ($status_code == 200) { if (isset($response_data['output']['text'])) { echo "{$response_data['output']['text']}\n"; } else { echo "No text in response.\n"; } }else { if (isset($response_data['request_id'])) { echo "request_id={$response_data['request_id']}\n";} echo "code={$status_code}\n"; if (isset($response_data['message'])) { echo "message={$response_data['message']}\n";} else { echo "message=Unknown error\n";} } ?>Copy在3000元以下的预算范围内,我推荐您考虑**百炼 Zephyr Z9**。这款手机定价在2499-2799元之间,非常适合您的预算。它具有轻巧的设计,配备6.4英寸1080 x 2340像素屏幕、128GB存储与6GB RAM,能够很好地满足日常使用需求。此外,其4000mAh电池可以保证一天的正常使用,并且配备了30倍数字变焦镜头来捕捉远处细节,既轻薄又不失强大功能。Dependency:Sample requestCopynpm install axiosSample responseCopyconst axios = require('axios'); async function callDashScope() { // 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey='sk-xxx'。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 const apiKey = process.env.DASHSCOPE_API_KEY; const appId = 'YOUR_APP_ID';//替换为实际的应用 ID const url = `https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`; const data = { input: { prompt: "请帮我推荐一款3000元以下的手机" }, parameters: { rag_options:{ pipeline_ids:['YOUR_PIPELINE_ID1','YOUR_PIPELINE_ID2'] // 替换为指定的知识库ID,多个请用逗号隔开 } }, debug: {} }; try { const response = await axios.post(url, data, { headers: { 'Authorization': `Bearer ${apiKey}`, 'Content-Type': 'application/json' } }); if (response.status === 200) { console.log(`${response.data.output.text}`); } else { console.log(`request_id=${response.headers['request_id']}`); console.log(`code=${response.status}`); console.log(`message=${response.data.message}`); } } catch (error) { console.error(`Error calling DashScope: ${error.message}`); if (error.response) { console.error(`Response status: ${error.response.status}`); console.error(`Response data: ${JSON.stringify(error.response.data, null, 2)}`); } } } callDashScope();Copy在3000元以下的预算内,我推荐您考虑**百炼 Zephyr Z9**。这款手机参考售价为3999-4299元,但如果能赶上促销活动或折扣,可能会降到您的预算范围内。 ### 百炼 Zephyr Z9 ——轻薄便携的艺术 - **屏幕**: 6.4英寸 1080 x 2340像素 - **存储与RAM**: 128GB存储 / 6GB RAM - **电池**: 4000mAh - **相机**: 30倍数字变焦镜头 这款手机的特点是轻薄便携,日常使用非常方便,而且具有不错的续航能力。如果您更关注性价比和日常使用体验,百炼 Zephyr Z9 是一个不错的选择。 如果您的预算非常严格,建议关注电商平台的促销活动,或者考虑其他品牌的同价位手机。希望这些建议对您有所帮助!Sample requestSample responseCopyusing System.Text; class Program { static async Task Main(string[] args) { // 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 string apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY")?? throw new InvalidOperationException("DASHSCOPE_API_KEY environment variable is not set.");; string appId = "YOUR_APP_ID";// 替换为实际的应用ID // YOUR_PIPELINE_ID1替换为指定的知识库ID if (string.IsNullOrEmpty(apiKey)) { Console.WriteLine("请确保设置了 DASHSCOPE_API_KEY。"); return; } string url = $"https://dashscope.aliyuncs.com/api/v1/apps/{appId}/completion"; using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}"); string jsonContent = $@"{{ ""input"": {{ ""prompt"": ""请帮我推荐一款3000元以下的手机"" }}, ""parameters"": {{ ""rag_options"" : {{ ""pipeline_ids"":[""YOUR_PIPELINE_ID1""] }} }}, ""debug"": {{}} }}"; HttpContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); try { HttpResponseMessage response = await client.PostAsync(url, content); if (response.IsSuccessStatusCode) { string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseBody); } else { Console.WriteLine($"Request failed with status code: {response.StatusCode}"); string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseBody); } } catch (Exception ex) { Console.WriteLine($"Error calling DashScope: {ex.Message}"); } } } }Copy{ "output": { "finish_reason": "stop", "session_id": "2344ddc540ec4c5fa110b92d813d3807", "text": "根据您的预算,我推荐您考虑**百炼 Zephyr Z9**。这款手机的参考售价在2499-2799元之间,符合您的预算需求。它拥有6.4英寸1080 x 2340像素屏幕、128GB存储空间和6GB RAM,对于日常使用来说已经足够了。此外,4000mAh电池可以保证一天内的正常使用,而30倍数字变焦镜头则能满足您拍摄远处景物的需求。这是一款轻薄便携且功能全面的选择。" }, "usage": { "models": [ { "output_tokens": 121, "model_id": "qwen-max", "input_tokens": 1841 } ] }, "request_id": "99fceedf-2034-9fb0-aaad-9c837136801f" }Sample requestSample responseCopypackage main import ( "bytes" "encoding/json" "fmt" "io" "net/http" "os" ) func main() { // 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey := "sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 apiKey := os.Getenv("DASHSCOPE_API_KEY") appId := "YOUR_APP_ID" // 替换为实际的应用 ID if apiKey == "" { fmt.Println("请确保设置了DASHSCOPE_API_KEY。") return } url := fmt.Sprintf("https://dashscope.aliyuncs.com/api/v1/apps/%s/completion", appId) // 创建请求体 requestBody := map[string]interface{}{ "input": map[string]string{ "prompt": "请帮我推荐一款3000元以下的手机", }, "parameters": map[string]interface{}{ "rag_options": map[string]interface{}{ "pipeline_ids": []string{"YOUR_PIPELINE_ID1"}, // 替换为指定的知识库ID }, }, "debug": map[string]interface{}{}, } jsonData, err := json.Marshal(requestBody) if err != nil { fmt.Printf("Failed to marshal JSON: %v\n", err) return } // 创建 HTTP POST 请求 req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) if err != nil { fmt.Printf("Failed to create request: %v\n", err) return } // 设置请求头 req.Header.Set("Authorization", "Bearer "+apiKey) req.Header.Set("Content-Type", "application/json") // 发送请求 client := &http.Client{} resp, err := client.Do(req) if err != nil { fmt.Printf("Failed to send request: %v\n", err) return } defer resp.Body.Close() // 读取响应 body, err := io.ReadAll(resp.Body) if err != nil { fmt.Printf("Failed to read response: %v\n", err) return } // 处理响应 if resp.StatusCode == http.StatusOK { fmt.Println("Request successful:") fmt.Println(string(body)) } else { fmt.Printf("Request failed with status code: %d\n", resp.StatusCode) fmt.Println(string(body)) } }Copy{ "output": { "finish_reason": "stop", "session_id": "fadbb4d1fe094ade88985620363506e6", "text": "根据您的预算,我为您推荐**百炼 Zephyr Z9**。这款手机的价格在2499-2799元之间,非常适合3000元以下的预算需求。它拥有轻巧的6.4英寸1080 x 2340像素屏幕设计,搭配128GB存储与6GB RAM,能够满足日常使用的需求。同时,4000mAh电池确保了一天的使用无忧,而30倍数字变焦镜头则可以捕捉远处的细节,是一款性价比很高的选择。" }, "usage": { "models": [ { "output_tokens": 119, "model_id": "qwen-max", "input_tokens": 1055 } ] }, "request_id": "3a755dd7-58a0-9a5e-8a07-b85b1db838a6" } -
Retrieve from a specific unstructured data document: Pass the knowledge base ID, document ID, document tags, or document metadata (key-value pairs) in the
rag_optionsparameter.Document IDs, tags, and metadata apply only to unstructured data documents.
-
How to obtain them:
- Document IDs (
file_ids): Use the ID returned by AddFile, or open Collections, click Details for the target file dataset, and get the ID from the document list. - Document tags (
tags): In Collections, click Details for the target file dataset to view its document tags. You can also obtain tags through DescribeFile. - Document metadata (metadata): On the Knowledge Base page, after entering a knowledge base, you can view the metadata of unstructured documents (Meta information).
- Document IDs (
- You can pass in multiple document IDs, but only documents with established knowledge indexes are supported.
- When passing in document IDs, you must also pass in the ID of the knowledge base to which the documents belong; otherwise, retrieval will not take effect.
-
Retrieval is performed only in the specified documents. For example: if the agent application on the web page references knowledge base A, but the API call specifies document IDs and the ID of knowledge base B to which they belong, then documents in knowledge base A will not be retrieved, and only documents in knowledge base B will be retrieved.
For example, you can use Bailian Series Mobile Product Introduction.docx as an unstructured data knowledge base file.
- Python
- Java
- HTTP
Sample requestSample responseCopyimport os import dashscope # 需要 dashscope 2.0.1 及以上版本 from dashscope import DashScopeError client = dashscope.Client( # 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 api_key=os.getenv("DASHSCOPE_API_KEY")) try: response = client.apps.completions.create( app_id='YOUR_APP_ID', # 应用ID替换YOUR_APP_ID prompt='请帮我推荐一款3000元以下的手机', rag_options={ "pipeline_ids": ["YOUR_PIPELINE_ID1", "YOUR_PIPELINE_ID2"], # 替换为实际的知识库ID,逗号隔开多个 "file_ids": ["YOUR_FILE_ID1", "YOUR_FILE_ID2"], # 替换为实际的非结构化文档 ID,逗号隔开多个 "metadata_filter": { # 文档元数据键值对,逗号隔开多个 "key1": "value1", "key2": "value2" }, "tags": ["tag1", "tag2"] # 文档标签,逗号隔开多个 } ) print('%s\n' % (response.text)) except DashScopeError as e: print(f'request_id={e.request_id}') print(f'code={e.code}') print(f'message={e.message}') print(f'请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code')Copy{ "text": "在3000元以下的预算范围内,我推荐您考虑**百炼 Zephyr Z9**。这款手机的特点如下: - **屏幕**:6.4英寸 1080 x 2340像素,适合日常使用和娱乐。 - **内存与存储**:6GB RAM + 128GB 存储空间,能够满足大部分用户对于流畅度以及存储的需求。 - **电池容量**:4000mAh,提供了一整天的使用保障。 - **摄像头功能**:配备了一个支持30倍数字变焦的镜头,可以捕捉到更远距离的细节。 - **其他特性**:设计轻薄便携,易于携带。 参考售价为2499至2799元之间,正好符合您的预算要求,并且提供了不错的性价比。希望这些建议对您有所帮助!", "finish_reason": "stop", "session_id": "10bdea3d1435406aad8750538b701bee", "thoughts": null, "doc_references": null }Sample requestSample responseCopy// 建议dashscope SDK 的版本 >= 2.16.8; 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.Arrays; public class Main { public static void streamCall() throws NoApiKeyException, InputRequiredException { JsonObject metadataFilter = new JsonObject(); metadataFilter.addProperty("key1", "value1"); // 元数据键值对 metadataFilter.addProperty("key2", "value2"); // 多个重复调用addProperty ApplicationParam param = ApplicationParam.builder() // 若没有配置环境变量,可用百炼API Key将下行替换为:.apiKey("sk-xxx")。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 .apiKey(System.getenv("DASHSCOPE_API_KEY")) .appId("YOUR_APP_ID") // 替换为实际的应用ID .prompt("请帮我推荐一款3000元左右的手机") .ragOptions(RagOptions.builder() .pipelineIds(Arrays.asList("PIPELINES_ID1","PIPELINES_ID2")) // 替换为实际指定的知识库ID,逗号隔开多个 .fileIds(Arrays.asList("FILE_ID1", "FILE_ID2")) // 替换为实际指定的非结构化文档 ID,逗号隔开多个 .tags(Arrays.asList("tags1", "tags2")) // 替换为指定的文档标签 ID,逗号隔开多个 .metadataFilter(metadataFilter) .build()) .build(); Application application = new Application(); ApplicationResult result = application.call(param); System.out.printf("%s\n", result.getOutput().getText());// 处理只输出文本text } public static void main(String[] args) { try { streamCall(); } catch (ApiException | NoApiKeyException | InputRequiredException e) { System.out.printf("Exception: %s", e.getMessage()); System.out.println("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code"); } System.exit(0); } }Copy根据您的预算,我为您推荐**百炼 Zephyr Z9**。这款手机的价格在2499-2799元之间,非常适合您3000元左右的预算范围。 ### 百炼 Zephyr Z9 产品亮点: - **屏幕**:6.4英寸、1080 x 2340像素,提供清晰细腻的视觉体验。 - **存储与运行内存**:128GB存储空间和6GB RAM,足以应对日常使用需求。 - **电池**:4000mAh容量电池可以确保一整天的正常使用。 - **摄像头**:支持30倍数字变焦镜头,能够捕捉远处细节。 - **设计**:轻薄便携,适合追求时尚与便捷的用户。 这款手机不仅价格适中,而且配置均衡,在外观设计上也非常出色,是这个价位段非常不错的选择。希望这些建议对您有所帮助!如果还有其他需求或疑问,请随时告诉我。- curl
- PHP
- Node.js
- C#
- Go
Sample requestCopycurl -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"], "file_ids":["YOUR_FILE_ID1"], "metadata_filter":{ "name":"张三"}, "tags":"手机" } }, "debug": {} }'Replace YOUR_APP_ID with your actual application ID. Replace YOUR_PIPELINE_ID1 with your actual knowledge base ID. Replace YOUR_FILE_ID1 with your actual unstructured document ID. Replace the metadata key-value pairs with your actual metadata.
Sample responseCopy{"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"}%Sample requestSample responseCopy<?php # 若没有配置环境变量,可用百炼API Key将下行替换为:$api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 $api_key = getenv("DASHSCOPE_API_KEY"); $application_id = 'YOUR_APP_ID'; // 替换为实际的应用 ID $url = "https://dashscope.aliyuncs.com/api/v1/apps/$application_id/completion"; // 构造请求数据 $data = [ "input" => [ 'prompt' => '请帮我推荐一款3000元以下的手机' ], "parameters" => [ 'rag_options' => [ 'pipeline_ids' => ['YOUR_PIPELINE_ID1','YOUR_PIPELINE_ID2'],// 替换为指定的知识库ID,逗号隔开多个 'file_ids' => ['YOUR_FILE_ID1','YOUR_FILE_ID2'],// 替换为实际的文档 ID,逗号隔开多个 "metadata_filter" => [ // 元数据键值对 "key1" => "value1", "key2" => "value2" ], "tags" => ["标签1", "标签2"] // 文档标签 ] ] ]; // 将数据编码为 JSON $dataString = json_encode($data); // 检查 json_encode 是否成功 if (json_last_error() !== JSON_ERROR_NONE) { die("JSON encoding failed with error: " . json_last_error_msg()); } // 初始化 cURL 会话 $ch = curl_init($url); // 设置 cURL 选项 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Authorization: Bearer ' . $api_key ]); // 执行请求 $response = curl_exec($ch); // 检查 cURL 执行是否成功 if ($response === false) { die("cURL Error: " . curl_error($ch)); } // 获取 HTTP 状态码 $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 关闭 cURL 会话 curl_close($ch); // 解码响应数据 $response_data = json_decode($response, true); // 处理响应 if ($status_code == 200) { if (isset($response_data['output']['text'])) { echo "{$response_data['output']['text']}\n"; } else { echo "No text in response.\n"; } }else { if (isset($response_data['request_id'])) { echo "request_id={$response_data['request_id']}\n";} echo "code={$status_code}\n"; if (isset($response_data['message'])) { echo "message={$response_data['message']}\n";} else { echo "message=Unknown error\n";} } ?>Copy根据您的预算,我为您推荐**百炼 Zephyr Z9**。这款手机的参考售价在2499-2799元之间,非常适合3000元以下的需求。它拥有轻巧的6.4英寸1080 x 2340像素设计,搭配128GB存储与6GB RAM,可以满足日常使用需求。此外,其配备4000mAh电池确保一天无忧,并且还有30倍数字变焦镜头捕捉远处细节,是一款既轻薄又不失强大的选择。Dependency:Sample requestCopynpm install axiosSample responseCopyconst axios = require('axios'); async function callDashScope() { // 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey='sk-xxx'。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 const apiKey = process.env.DASHSCOPE_API_KEY; const appId = 'YOUR_APP_ID';//替换为实际的应用 ID const url = `https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`; const data = { input: { prompt: "请帮我推荐一款3000元以下的手机" }, parameters: { rag_options:{ pipeline_ids:['YOUR_PIPELINE_ID1','YOUR_PIPELINE_ID2'], // 替换为指定的知识库ID,多个请用逗号隔开 file_ids:['YOUR_FILE_ID1','YOUR_FILE_ID2'], // 替换为指定的文件 ID,多个请用逗号隔开 metadata_filter:{ // 元数据键值对,多个请用逗号隔开 'key1':'value1', 'key2':'value2' }, tags: ['标签1', '标签2'] // 文档标签,多个请用逗号隔开 } }, debug: {} }; try { const response = await axios.post(url, data, { headers: { 'Authorization': `Bearer ${apiKey}`, 'Content-Type': 'application/json' } }); if (response.status === 200) { console.log(`${response.data.output.text}`); } else { console.log(`request_id=${response.headers['request_id']}`); console.log(`code=${response.status}`); console.log(`message=${response.data.message}`); } } catch (error) { console.error(`Error calling DashScope: ${error.message}`); if (error.response) { console.error(`Response status: ${error.response.status}`); console.error(`Response data: ${JSON.stringify(error.response.data, null, 2)}`); } } } callDashScope();Copy在3000元以下的预算范围内,我推荐您考虑**百炼 Zephyr Z9**。这款手机的价格区间是2499-2799元,它拥有轻薄便携的设计,配备6.4英寸1080 x 2340像素屏幕、128GB存储与6GB RAM,能够满足日常使用需求。其4000mAh电池保证了一天的使用时间,而30倍数字变焦镜头则可以帮助捕捉到更远距离的细节。总体来说,这是一款性价比较高的选择。Sample requestSample responseCopyusing System.Text; class Program { static async Task Main(string[] args) { // 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 string apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY")?? throw new InvalidOperationException("DASHSCOPE_API_KEY environment variable is not set.");; string appId = "YOUR_APP_ID";// 替换为实际的应用 ID // YOUR_PIPELINE_ID1替换为指定的知识库ID,YOUR_FILE_ID1替换为指定的非结构化文档 ID if (string.IsNullOrEmpty(apiKey)) { Console.WriteLine("请确保设置了 DASHSCOPE_API_KEY。"); return; } string url = $"https://dashscope.aliyuncs.com/api/v1/apps/{appId}/completion"; using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}"); string jsonContent = $@"{{ ""input"": {{ ""prompt"": ""请帮我推荐一款3000元以下的手机"" }}, ""parameters"": {{ ""rag_options"" : {{ ""pipeline_ids"":[""YOUR_PIPELINE_ID1""], ""file_ids"":[""YOUR_FILE_ID1""], ""metadata_filter"":{{ ""name"":""张三"" }}, ""tags"":""手机"" }} }}, ""debug"": {{}} }}"; HttpContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); try { HttpResponseMessage response = await client.PostAsync(url, content); if (response.IsSuccessStatusCode) { string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseBody); } else { Console.WriteLine($"Request failed with status code: {response.StatusCode}"); string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseBody); } } catch (Exception ex) { Console.WriteLine($"Error calling DashScope: {ex.Message}"); } } } }Copy{ "output": { "finish_reason": "stop", "session_id": "be9b5a1964fe41c9bbfd8674226bd238", "text": "根据您的预算,我为您推荐**百炼 Zephyr Z9**。这款手机售价在2499-2799元之间,非常适合3000元以下的预算要求。 ### 产品亮点 - **轻薄设计**:采用6.4英寸屏幕,分辨率1080 x 2340像素,外观精致且便于携带。 - **性能均衡**:配备6GB RAM和128GB存储空间,能够满足日常使用需求。 - **长效续航**:内置4000mAh电池,确保您一整天的正常使用不受影响。 - **出色摄影**:支持30倍数字变焦功能,轻松捕捉远方美景或细节。 如果您追求的是性价比高、能满足基本需求同时又具备一定特色的智能手机,那么百炼 Zephyr Z9将是一个不错的选择。" }, "usage": { "models": [ { "output_tokens": 180, "model_id": "qwen-max", "input_tokens": 1055 } ] }, "request_id": "d0811195-0b3f-931e-90b8-323a65053d9c" }Sample requestSample responseCopypackage main import ( "bytes" "encoding/json" "fmt" "io" "net/http" "os" ) func main() { // 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey := "sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 apiKey := os.Getenv("DASHSCOPE_API_KEY") appId := "YOUR_APP_ID" // 替换为实际的应用 ID if apiKey == "" { fmt.Println("请确保设置了DASHSCOPE_API_KEY。") return } url := fmt.Sprintf("https://dashscope.aliyuncs.com/api/v1/apps/%s/completion", appId) // 创建请求体 requestBody := map[string]interface{}{ "input": map[string]string{ "prompt": "请帮我推荐一款3000元以下的手机", }, "parameters": map[string]interface{}{ "rag_options": map[string]interface{}{ "pipeline_ids": []string{"YOUR_PIPELINE_ID1"}, // 替换为指定的非结构化知识库ID "file_ids": []string{"YOUR_FILE_ID1"}, // 替换为指定的非结构化文档 ID "metadata_filter": map[string]string{ "name": "张三", // 元数据键值对 }, "tags": "手机", // 非结构化数据文档标签 }, }, "debug": map[string]interface{}{}, } jsonData, err := json.Marshal(requestBody) if err != nil { fmt.Printf("Failed to marshal JSON: %v\n", err) return } // 创建 HTTP POST 请求 req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) if err != nil { fmt.Printf("Failed to create request: %v\n", err) return } // 设置请求头 req.Header.Set("Authorization", "Bearer "+apiKey) req.Header.Set("Content-Type", "application/json") // 发送请求 client := &http.Client{} resp, err := client.Do(req) if err != nil { fmt.Printf("Failed to send request: %v\n", err) return } defer resp.Body.Close() // 读取响应 body, err := io.ReadAll(resp.Body) if err != nil { fmt.Printf("Failed to read response: %v\n", err) return } // 处理响应 if resp.StatusCode == http.StatusOK { fmt.Println("Request successful:") fmt.Println(string(body)) } else { fmt.Printf("Request failed with status code: %d\n", resp.StatusCode) fmt.Println(string(body)) } }Copy{ "output": { "finish_reason": "stop", "session_id": "9de268b3d84748b5ac6321aba72b6ecd", "text": "根据您的预算,我推荐您考虑**百炼 Zephyr Z9**。这款手机的参考售价为2499-2799元,非常适合3000元以下的需求。它具有以下特点: - 轻巧的6.4英寸1080 x 2340像素屏幕设计。 - 搭配128GB存储与6GB RAM,能够满足日常使用需求。 - 配备了4000mAh电池,保证了一天的正常使用。 - 后置摄像头支持30倍数字变焦镜头,可以捕捉到远处的细节。 如果您对摄影或者游戏没有特别高的要求,那么百炼 Zephyr Z9应该是一个不错的选择。" }, "usage": { "models": [ { "output_tokens": 156, "model_id": "qwen-max", "input_tokens": 1055 } ] }, "request_id": "8940b597-92e1-9471-b4eb-896e563c479d" }
-
How to obtain them:
-
Retrieve specific data from a structured data document by passing the knowledge base ID and key-value pairs for structured data fields—such as table header and value—in the
rag_optionsparameter. Retrieve structured data key-value pairs (structured_filter): On the Knowledge Base page, open a knowledge base and click View index to view index information for structured documents.- Python
- Java
- HTTP
Sample requestSample responseCopyimport os import dashscope # 需要 dashscope 2.0.1 及以上版本 from dashscope import DashScopeError client = dashscope.Client( # 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 api_key=os.getenv("DASHSCOPE_API_KEY")) try: response = client.apps.completions.create( app_id='YOUR_APP_ID', # 应用ID替换YOUR_APP_ID prompt='请帮我推荐一款3000元以下的手机', rag_options={ "pipeline_ids": ["YOUR_PIPELINE_ID1", "YOUR_PIPELINE_ID2"], # 替换为实际的知识库ID,逗号隔开多个 "structured_filter": { # 结构化数据键值对,对应结构化数据,逗号隔开多个 "key1": "value1", "key2": "value2" } } ) print('%s\n' % (response.text)) except DashScopeError as e: print(f'request_id={e.request_id}') print(f'code={e.code}') print(f'message={e.message}') print(f'请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code')Copy{ "text": "我为您推荐"百炼"这款手机,它的价格是2999元,符合您的预算要求。如果您需要了解更多信息,比如性能、外观等,请告诉我。", "finish_reason": "stop", "session_id": "80a3b868b5ce42c8a12f01dccf8651e2", "thoughts": null, "doc_references": null }Sample requestSample responseCopy// 建议dashscope SDK 的版本 >= 2.16.8; 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.Arrays; public class Main { public static void streamCall() throws NoApiKeyException, InputRequiredException { JsonObject structureFilter = new JsonObject(); structureFilter.addProperty("key1", "value1"); // 结构化数据键值对 structureFilter.addProperty("key2", "value2"); // 多个重复调用addProperty ApplicationParam param = ApplicationParam.builder() // 若没有配置环境变量,可用百炼API Key将下行替换为:.apiKey("sk-xxx")。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 .apiKey(System.getenv("DASHSCOPE_API_KEY")) .appId("YOUR_APP_ID") // 替换为实际的应用ID .prompt("请帮我推荐一款3000元左右的手机") .ragOptions(RagOptions.builder() .pipelineIds(Arrays.asList("PIPELINE_ID1","PIPELINE_ID2")) // 替换为实际指定的知识库ID,逗号隔开多个 .structuredFilter(structureFilter) .build()) .build(); Application application = new Application(); ApplicationResult result = application.call(param); System.out.printf("%s\n", result.getOutput().getText());// 处理只输出文本text } public static void main(String[] args) { try { streamCall(); } catch (ApiException | NoApiKeyException | InputRequiredException e) { System.out.printf("Exception: %s", e.getMessage()); System.out.println("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code"); } System.exit(0); } }Copy我为您推荐"百炼"这款手机,它的价格是2999.0元,符合您的预算要求。如果您需要了解更多关于这款手机的信息,比如配置、性能等,请告诉我,我会为您提供更详细的资料。- curl
- PHP
- Node.js
- C#
- Go
Sample requestCopycurl -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": {} }'Replace YOUR_APP_ID with your actual application ID. Replace YOUR_PIPELINE_ID1 with your actual knowledge base ID.
Sample responseCopy{"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"}%Sample requestSample responseCopy<?php # 若没有配置环境变量,可用百炼API Key将下行替换为:$api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 $api_key = getenv("DASHSCOPE_API_KEY"); $application_id = 'YOUR_APP_ID'; // 替换为实际的应用 ID $url = "https://dashscope.aliyuncs.com/api/v1/apps/$application_id/completion"; // 构造请求数据 $data = [ "input" => [ 'prompt' => '请帮我推荐一款3000元以下的手机' ], "parameters" => [ 'rag_options' => [ 'pipeline_ids' => ['YOUR_PIPELINE_ID1','YOUR_PIPELINE_ID2'],// 替换为指定的知识库ID,逗号隔开多个 "structured_filter" => [ // 结构化数据键值对,多个逗号隔开 "key1" => "value1", "key2" => "value2" ] ] ] ]; // 将数据编码为 JSON $dataString = json_encode($data); // 检查 json_encode 是否成功 if (json_last_error() !== JSON_ERROR_NONE) { die("JSON encoding failed with error: " . json_last_error_msg()); } // 初始化 cURL 会话 $ch = curl_init($url); // 设置 cURL 选项 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Authorization: Bearer ' . $api_key ]); // 执行请求 $response = curl_exec($ch); // 检查 cURL 执行是否成功 if ($response === false) { die("cURL Error: " . curl_error($ch)); } // 获取 HTTP 状态码 $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 关闭 cURL 会话 curl_close($ch); // 解码响应数据 $response_data = json_decode($response, true); // 处理响应 if ($status_code == 200) { if (isset($response_data['output']['text'])) { echo "{$response_data['output']['text']}\n"; } else { echo "No text in response.\n"; } }else { if (isset($response_data['request_id'])) { echo "request_id={$response_data['request_id']}\n";} echo "code={$status_code}\n"; if (isset($response_data['message'])) { echo "message={$response_data['message']}\n";} else { echo "message=Unknown error\n";} } ?>Copy我为您推荐"百炼"这款手机,它的价格是2999元,符合您的预算要求。如果您需要了解更多关于这款手机的信息,请告诉我。Dependency:Sample requestCopynpm install axiosSample responseCopyconst axios = require('axios'); async function callDashScope() { // 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey='sk-xxx'。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 const apiKey = process.env.DASHSCOPE_API_KEY; const appId = 'YOUR_APP_ID'; // 替换为实际的应用 ID // YOUR_PIPELINE_ID1替换为指定的知识库ID, 多个知识库ID之间用逗号隔开 const url = `https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`; const data = { input: { prompt: "请帮我推荐一款3000元以下的手机" }, parameters: { rag_options:{ pipeline_ids:['YOUR_PIPELINE_ID1','YOUR_PIPELINE_ID2'], structured_filter:{ 'key1':'value1', 'key2':'value2' } } }, debug: {} }; try { const response = await axios.post(url, data, { headers: { 'Authorization': `Bearer ${apiKey}`, 'Content-Type': 'application/json' } }); if (response.status === 200) { console.log(`${response.data.output.text}`); } else { console.log(`request_id=${response.headers['request_id']}`); console.log(`code=${response.status}`); console.log(`message=${response.data.message}`); } } catch (error) { console.error(`Error calling DashScope: ${error.message}`); if (error.response) { console.error(`Response status: ${error.response.status}`); console.error(`Response data: ${JSON.stringify(error.response.data, null, 2)}`); } } } callDashScope();Copy我为您推荐"百炼"这款手机,它的价格是2999元,符合您的预算要求。如果您需要了解更多详情或有其他特定需求,请告诉我!Sample requestSample responseCopyusing System.Text; class Program { static async Task Main(string[] args) { // 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 string apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY")?? throw new InvalidOperationException("DASHSCOPE_API_KEY environment variable is not set.");; string appId = "YOUR_APP_ID";// 替换为实际的应用ID // YOUR_PIPELINE_ID1替换为指定的知识库ID if (string.IsNullOrEmpty(apiKey)) { Console.WriteLine("请确保设置了 DASHSCOPE_API_KEY。"); return; } string url = $"https://dashscope.aliyuncs.com/api/v1/apps/{appId}/completion"; using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}"); string jsonContent = $@"{{ ""input"": {{ ""prompt"": ""请帮我推荐一款3000元以下的手机"" }}, ""parameters"": {{ ""rag_options"" : {{ ""pipeline_ids"":[""YOUR_PIPELINE_ID1""], ""structured_filter"":{{ ""price"":""2999"" }} }} }}, ""debug"": {{}} }}"; HttpContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); try { HttpResponseMessage response = await client.PostAsync(url, content); if (response.IsSuccessStatusCode) { string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseBody); } else { Console.WriteLine($"Request failed with status code: {response.StatusCode}"); string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseBody); } } catch (Exception ex) { Console.WriteLine($"Error calling DashScope: {ex.Message}"); } } } }Copy{ "output": { "finish_reason": "stop", "session_id": "108e9104568e44f1915fb3d3d44fdc92", "text": "我为您推荐"百炼"这款手机,它的价格是2999.0元,符合您的预算要求。如果您需要更多关于这款手机的信息或者其他建议,请告诉我。" }, "usage": { "models": [ { "output_tokens": 38, "model_id": "qwen-max", "input_tokens": 104 } ] }, "request_id": "d6d103f4-5c22-9782-9682-45d51a5607f9" }Sample requestSample responseCopypackage main import ( "bytes" "encoding/json" "fmt" "io" "net/http" "os" ) func main() { // 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey := "sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 apiKey := os.Getenv("DASHSCOPE_API_KEY") appId := "YOUR_APP_ID" // 替换为实际的应用 ID if apiKey == "" { fmt.Println("请确保设置了DASHSCOPE_API_KEY。") return } url := fmt.Sprintf("https://dashscope.aliyuncs.com/api/v1/apps/%s/completion", appId) // 创建请求体 requestBody := map[string]interface{}{ "input": map[string]string{ "prompt": "请帮我推荐一款3000元以下的手机", }, "parameters": map[string]interface{}{ "rag_options": map[string]interface{}{ "pipeline_ids": []string{"YOUR_PIPELINE_ID1"}, // 替换为指定的结构化知识库ID "structured_filter": map[string]string{ "price": "2999", // 结构化数据键值对 }, }, }, "debug": map[string]interface{}{}, } jsonData, err := json.Marshal(requestBody) if err != nil { fmt.Printf("Failed to marshal JSON: %v\n", err) return } // 创建 HTTP POST 请求 req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) if err != nil { fmt.Printf("Failed to create request: %v\n", err) return } // 设置请求头 req.Header.Set("Authorization", "Bearer "+apiKey) req.Header.Set("Content-Type", "application/json") // 发送请求 client := &http.Client{} resp, err := client.Do(req) if err != nil { fmt.Printf("Failed to send request: %v\n", err) return } defer resp.Body.Close() // 读取响应 body, err := io.ReadAll(resp.Body) if err != nil { fmt.Printf("Failed to read response: %v\n", err) return } // 处理响应 if resp.StatusCode == http.StatusOK { fmt.Println("Request successful:") fmt.Println(string(body)) } else { fmt.Printf("Request failed with status code: %d\n", resp.StatusCode) fmt.Println(string(body)) } }Copy{ "output": { "finish_reason": "stop", "session_id": "9e0a031b51d1492e8b613ca391b445b0", "text": "我推荐您考虑"百炼"这款手机,它的价格是2999.0元,符合您的预算要求。如果您需要更多关于这款手机的信息或者其他推荐,请告诉我。" }, "usage": { "models": [ { "output_tokens": 39, "model_id": "qwen-max", "input_tokens": 104 } ] }, "request_id": "036abd4f-10c8-9709-881d-8cc9f8095d54" }
View information
- View retrieval process information: Add
has_thoughtsin the code and set it to True when calling. The retrieval process information will then be returned in thethoughtsfield of the response. - View answer source information: Click Configure next to the Knowledge Base switch, turn on the Show Source switch on the page, and then publish the application. You can then view the answer sources in the returned results of the call.
Deep thinking
If you select the deep thinking model in your agent application and successfully publish it, then: Enable thinking mode: To enable thinking mode and obtain its output, you can configure it in one of two ways.- Console settings For Qwen3 models: In the console, make sure the thinking mode switch is turned on, then republish the application. For other models: Thinking mode is enabled by default. No action is required.
- API parameter settings For Qwen3 models: Set the enable_thinking parameter to true. For other models: The enable_thinking parameter has no effect.
- Set the has_thoughts parameter to true.
- Thinking process: Is returned in the
thoughtfield of the response. - Final reply: Is returned in the
textfield of the response.
- Python
- Java
- curl
import os
import dashscope
from dashscope import DashScopeError
client = dashscope.Client(
# 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
api_key=os.getenv('DASHSCOPE_API_KEY'))
try:
stream = client.apps.completions.create(
app_id='YOUR_APP_ID',# 替换为实际的应用 ID
prompt='你是谁?',# 替换为实际的应用输入
stream=True, # 是否流式输出,True:流式输出;False(默认值): 非流式输出
incremental_output=True, # 是否增量输出,True:增量输出;False(默认值): 非增量输出
has_thoughts=True # 是否返回思考过程,True:返回;False(默认值): 不返回
)
except Exception as e:
print(f"API请求异常: {str(e)}")
exit(1)
# 定义完整思考过程
reasoning_content = []
# 定义完整回复
answer_content = ""
# 判断是否结束思考过程并开始回复
is_answering = False
def print_section(title):
"""打印带装饰的分段标题"""
print(f"\n{'=' * 20} {title} {'=' * 20}\n", flush=True)
print_section("思考过程")
try:
for chunk in stream:
# 跳过空数据块
if not chunk.thoughts and not chunk.text:
continue
# 处理思考过程
if chunk.thoughts:
for it in chunk.thoughts:
if it.action_type == 'reasoning':# 推理类模型的思考过程action_type为reasoning,输出在thought
content = str(it.thought) if not isinstance(it.thought, str) else it.thought
reasoning_content.append(content)
print(content, end="", flush=True)
# 处理回答内容
if chunk.text:
if not is_answering:
print_section("完整回复")
is_answering = True
answer_content += str(chunk.text)
print(chunk.text, end="", flush=True)
except DashScopeError as e:
# 流式输出中的错误会直接抛出异常
print(f'request_id={e.request_id}')
print(f'code={e.code}')
print(f'message={e.message}')
print(f'请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code')
# 最终结果整合
final_reasoning = "".join(reasoning_content)
final_answer = "".join(answer_content)
# 如果您需要打印完整思考过程与完整回复,请将以下代码解除注释后运行
#print_section("完整思考过程")
#print(final_reasoning)
#print_section("完整回复")
#print(final_answer)
==================== 思考过程 ====================
嗯,用户问我“你是谁?”,我需要用中文回答。首先,我得介绍自己是一个AI助手,由DeepSeek公司开发。然后要说明我的功能,比如回答问题、提供信息、帮助学习等。要记得保持口语化,不用太正式。还要注意用户可能想了解我的背景,所以可以提到基于大语言模型,但不用太技术化。另外,用户可能在测试我的回答能力,所以需要简洁明了,同时友好自然。可能需要检查有没有遗漏的关键点,比如公司的正确名称,以及是否强调帮助性质。最后确保回答符合格式要求,不使用markdown,保持段落结构清晰。
==================== 完整回复 ====================
您好!我是DeepSeek-R1,一个由深度求索(DeepSeek)公司开发的智能助手,我会尽我所能为您提供帮助,包括回答问题、信息查询以及学习辅助等。
// 建议dashscope SDK的版本 >= 2.15.0
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 Main {
public static void streamCall() throws NoApiKeyException, InputRequiredException {
ApplicationParam param = ApplicationParam.builder()
// 若没有配置环境变量,可用百炼API Key将下行替换为:.apiKey("sk-xxx")。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
// 替换为实际的应用 ID
.appId("YOUR_APP_ID")
.prompt("你好")
.hasThoughts(true)
// 增量输出
.incrementalOutput(true)
.build();
Application application = new Application();
// .streamCall():流式输出内容
Flowable<ApplicationResult> result = application.streamCall(param);
result.blockingForEach(data -> {
System.out.printf("%s\n",
data.getOutput());
});
}
public static void main(String[] args) {
try {
streamCall();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.out.printf("Exception: %s", e.getMessage());
System.out.println("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code");
}
System.exit(0);
}
}
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=好的,用户, actionType=reasoning, response=好的,用户, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=打招呼, actionType=reasoning, response=打招呼, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=说, actionType=reasoning, response=说, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=“你好”,我需要友好, actionType=reasoning, response=“你好”,我需要友好, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=回应。首先,, actionType=reasoning, response=回应。首先,, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=要确认用户的需求,, actionType=reasoning, response=要确认用户的需求,, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=可能只是测试或者想, actionType=reasoning, response=可能只是测试或者想, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=开始对话。要, actionType=reasoning, response=开始对话。要, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=保持热情,用, actionType=reasoning, response=保持热情,用, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=中文回复。可以, actionType=reasoning, response=中文回复。可以, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=问他们需要什么, actionType=reasoning, response=问他们需要什么, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=帮助,或者有没有具体, actionType=reasoning, response=帮助,或者有没有具体, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=的问题。同时,注意, actionType=reasoning, response=的问题。同时,注意, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=使用合适的表情符号增加, actionType=reasoning, response=使用合适的表情符号增加, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=亲切感。检查, actionType=reasoning, response=亲切感。检查, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=有没有需要特别注意的地方,, actionType=reasoning, response=有没有需要特别注意的地方,, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=比如用户是否有特殊身份, actionType=reasoning, response=比如用户是否有特殊身份, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=或需求,但目前, actionType=reasoning, response=或需求,但目前, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=看起来是普通问候。简单, actionType=reasoning, response=看起来是普通问候。简单, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=回应即可,避免, actionType=reasoning, response=回应即可,避免, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=复杂句子。保持, actionType=reasoning, response=复杂句子。保持, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=自然,让用户觉得, actionType=reasoning, response=自然,让用户觉得, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=被重视。
现在, actionType=reasoning, response=被重视。
现在, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=组织语言:欢迎, actionType=reasoning, response=组织语言:欢迎, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=用户,询问需要, actionType=reasoning, response=用户,询问需要, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=什么帮助。使用, actionType=reasoning, response=什么帮助。使用, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=友好表情符号,比如笑脸, actionType=reasoning, response=友好表情符号,比如笑脸, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=。确保用词, actionType=reasoning, response=。确保用词, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=简洁,让对话, actionType=reasoning, response=简洁,让对话, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=容易继续下去。比如, actionType=reasoning, response=容易继续下去。比如, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=:“你好!有什么我可以, actionType=reasoning, response=:“你好!有什么我可以, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=帮助你的吗?”, actionType=reasoning, response=帮助你的吗?”, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought= 这样既, actionType=reasoning, response= 这样既, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=直接又开放,, actionType=reasoning, response=直接又开放,, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=鼓励用户进一步交流, actionType=reasoning, response=鼓励用户进一步交流, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=。, actionType=reasoning, response=。, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=你好!有什么, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=, actionType=reasoning, response=, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=我可以帮助你的吗?, finishReason=null, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=, actionType=reasoning, response=, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
ApplicationOutput(text=, finishReason=stop, sessionId=380427d07790470c8e63e0d28dfa98bb, thoughts=[ApplicationOutput.Thought(thought=, actionType=reasoning, response=, actionName=思考过程, action=reasoning, actionInputStream=null, actionInput=null, observation=null)], docReferences=null, workflowMessage=null)
- Replace YOUR_APP_ID with your actual application ID.
- To pass your API key directly, replace $DASHSCOPE_API_KEY with your actual key.
- In the request header, set X-DashScope-SSE to enable to enable streaming output.
- Add the
has_thoughtsparameter to theparametersobject to specify whether to return the thinking process. Set the value to true to return the thinking process, or set it to false (the default) to omit it. - You can add the
incremental_outputparameter to theparametersobject to specify whether to enable incremental output. Set the value to true to enable incremental output. The default value is false.
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": {
"has_thoughts":true,
"incremental_output":true
},
"debug": {}
}'
id:1
event:result
:HTTP_STATUS/200
data:{"output":{"thoughts":[{"action":"reasoning","thought":"嗯","action_type":"reasoning","response":"嗯","action_name":"思考过程"}],"session_id":"ea188f5f795a485f8956e3af0212ba29","finish_reason":"null","text":""},"usage":{"models":[{"input_tokens":31,"output_tokens":3,"model_id":"qwen3.7-plus"}]},"request_id":"b4b0b65f-4378-93b6-8ca2-7936b2725bc8"}
id:2
event:result
:HTTP_STATUS/200
data:{"output":{"thoughts":[{"action":"reasoning","thought":",","action_type":"reasoning","response":",","action_name":"思考过程"}],"session_id":"ea188f5f795a485f8956e3af0212ba29","finish_reason":"null","text":""},"usage":{"models":[{"input_tokens":31,"output_tokens":4,"model_id":"qwen3.7-plus"}]},"request_id":"b4b0b65f-4378-93b6-8ca2-7936b2725bc8"}
id:3
event:result
:HTTP_STATUS/200
data:{"output":{"thoughts":[{"action":"reasoning","thought":"用户","action_type":"reasoning","response":"用户","action_name":"思考过程"}],"session_id":"ea188f5f795a485f8956e3af0212ba29","finish_reason":"null","text":""},"usage":{"models":[{"input_tokens":31,"output_tokens":5,"model_id":"qwen3.7-plus"}]},"request_id":"b4b0b65f-4378-93b6-8ca2-7936b2725bc8"}
......
id:320
event:result
:HTTP_STATUS/200
data:{"output":{"thoughts":[{"action":"reasoning","thought":"","action_type":"reasoning","response":"","action_name":"思考过程"}],"session_id":"ea188f5f795a485f8956e3af0212ba29","finish_reason":"null","text":"问题"},"usage":{"models":[{"input_tokens":31,"output_tokens":322,"model_id":"qwen3.7-plus"}]},"request_id":"b4b0b65f-4378-93b6-8ca2-7936b2725bc8"}
id:321
event:result
:HTTP_STATUS/200
data:{"output":{"thoughts":[{"action":"reasoning","thought":"","action_type":"reasoning","response":"","action_name":"思考过程"}],"session_id":"ea188f5f795a485f8956e3af0212ba29","finish_reason":"null","text":"吗"},"usage":{"models":[{"input_tokens":31,"output_tokens":323,"model_id":"qwen3.7-plus"}]},"request_id":"b4b0b65f-4378-93b6-8ca2-7936b2725bc8"}
id:322
event:result
:HTTP_STATUS/200
data:{"output":{"thoughts":[{"action":"reasoning","thought":"","action_type":"reasoning","response":"","action_name":"思考过程"}],"session_id":"ea188f5f795a485f8956e3af0212ba29","finish_reason":"null","text":"?"},"usage":{"models":[{"input_tokens":31,"output_tokens":324,"model_id":"qwen3.7-plus"}]},"request_id":"b4b0b65f-4378-93b6-8ca2-7936b2725bc8"}
id:323
event:result
:HTTP_STATUS/200
data:{"output":{"thoughts":[{"action":"reasoning","thought":"","action_type":"reasoning","response":"","action_name":"思考过程"}],"session_id":"ea188f5f795a485f8956e3af0212ba29","finish_reason":"stop","text":""},"usage":{"models":[{"input_tokens":31,"output_tokens":324,"model_id":"qwen3.7-plus"}]},"request_id":"b4b0b65f-4378-93b6-8ca2-7936b2725bc8"}
Retrieving from a knowledge base
Retrieving from a knowledge base
- The model's thinking process is returned in the
thoughtsobject'sthoughtfield. Itsaction_typeisreasoning. - The retrieval process is returned in the
thoughtsobject'sobservationfield. Itsaction_typeisagentRag.
action_type to distinguish between these processes and handle the output accordingly.Long-term memory
Alibaba Cloud Model Studio’s agent applications retain some of your conversation history. However, due to large language model (LLM) attention limits, they may forget details. To address this, store important information in long-term memory. Your application will then reference that memory in future conversations. Steps Step 1: Enable thelong-term memoryfeature Go to the Application Management page. Find your agent application, turn on the long-term memory switch, and then publish the application. Step 2: Create a memory Call the CreateMemory API to create a memory. From the response, retrieve a uniquememoryId.
Step 3: Save conversation information
Pass the memoryId you retrieved earlier. The system automatically analyzes your conversation and saves key information as memory linked to that memoryId.
Step 4: Use long-term memory in conversations
Each time you interact with the agent, provide the same memoryId. The system retrieves the corresponding memory content based on the provided memoryId and passes it along with the current question to the model to generate a response.
Example call
- Python
- Java
- HTTP
# 需要 dashscope 2.0.1 及以上版本
import os
import dashscope
from dashscope import DashScopeError
client = dashscope.Client(
# 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
api_key=os.getenv("DASHSCOPE_API_KEY"))
try:
response = client.apps.completions.create(
app_id='YOUR_APP_ID', # 请输入实际的应用 ID
prompt='用户饮食偏好:面食',
memory_id='YOUR_MEMORY_ID') # 请输入实际的记忆体 ID
print('%s\n' % (response.text)) # 处理只输出text
# print('%s\n' % (response.usage))
except DashScopeError as e:
print(f'request_id={e.request_id}')
print(f'code={e.code}')
print(f'message={e.message}')
print(f'请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code')
了解了,您对面食情有独钟。如果想要推荐或者寻找面食相关的食谱、餐厅等信息,请告诉我更多细节,比如是想吃哪种类型的面条(如拉面、意大利面等),还是有什么特别的口味偏好?这样我能更好地为您提供帮助。
# 需要 dashscope 2.0.1 及以上版本
import os
import dashscope
from dashscope import DashScopeError
client = dashscope.Client(
# 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
api_key=os.getenv("DASHSCOPE_API_KEY"))
try:
response = client.apps.completions.create(
app_id='YOUR_APP_ID', # 请输入实际的应用 ID
prompt='美食推荐',
memory_id='YOUR_MEMORY_ID') # 请输入实际的记忆体 ID
print('%s\n' % (response.text)) # 处理只输出text
# print('%s\n' % (response.usage))
except DashScopeError as e:
print(f'request_id={e.request_id}')
print(f'code={e.code}')
print(f'message={e.message}')
print(f'请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code')
既然您偏好面食,我推荐您可以尝试一下几种美食:
1. **炸酱面**:经典的北京风味,面条搭配特制的黄豆酱和肉末,口感丰富。
2. **担担面**:四川特色,辣中带麻,非常开胃。如果您喜欢尝试一些稍微刺激一点的味道,这会是个不错的选择。
3. **刀削面**:山西的传统名吃之一,以其独特的制作方法——用刀将面团直接削入锅中煮熟而得名,口感劲道。
4. **意大利面**:如果想要换换口味的话,不妨试试西式的意面,比如番茄肉酱意面或是奶油培根意面等,都是不错的选择。
希望这些建议对您有所帮助!如果有更具体的口味偏好或其他需求,请随时告诉我哦~
// DashScope SDK版本不低于2.17.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 Main {
public static void callWithMemory() throws NoApiKeyException, InputRequiredException {
ApplicationParam param = ApplicationParam.builder()
// 若没有配置环境变量,可用百炼API Key将下行替换为:.apiKey("sk-xxx")。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.appId("YOUR_APP_ID") // 替换为实际的应用 ID
.prompt("用户饮食偏好:面食")
.memoryId("YOUR_MEMORY_ID") // 替换为实际的记忆体 ID
.build();
Application application = new Application();
ApplicationResult result = application.call(param);
System.out.printf("%s\n",
result.getOutput().getText()); // 处理只输出文本text
}
public static void main(String[] args) {
try {
callWithMemory();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.out.printf("Exception: %s", e.getMessage());
System.out.println("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code");
}
System.exit(0);
}
}
了解了,您对面食情有独钟。如果您需要面食的推荐、做法或者是哪里可以吃到美味的面食,请告诉我,我很乐意为您提供帮助!
// DashScope SDK版本不低于2.17.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 Main {
public static void callWithMemory() throws NoApiKeyException, InputRequiredException {
ApplicationParam param = ApplicationParam.builder()
// 若没有配置环境变量,可用百炼API Key将下行替换为:.apiKey("sk-xxx")。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.appId("YOUR_APP_ID") // 替换为实际的应用 ID
.prompt("美食推荐")
.memoryId("YOUR_MEMORY_ID") // 替换为实际的记忆体 ID
.build();
Application application = new Application();
ApplicationResult result = application.call(param);
System.out.printf("text: %s\n",
result.getOutput().getText()); // 处理只输出文本text
}
public static void main(String[] args) {
try {
callWithMemory();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.out.printf("Exception: %s", e.getMessage());
System.out.println("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code");
}
System.exit(0);
}
}
既然您偏好面食,这里有一些美味的面食推荐给您:
1. **炸酱面**:北京的传统美食,用黄豆酱和肉末制成的炸酱,搭配黄瓜丝、豆芽等蔬菜,味道鲜美。
2. **担担面**:四川特色小吃,以麻辣著称,面条细滑,汤汁浓郁,上面撒上花生碎和葱花,非常开胃。
3. **刀削面**:山西的传统面食,面条宽厚有嚼劲,通常搭配各种肉类和蔬菜炖煮的汤底,口感丰富。
4. **油泼面**:陕西地区的经典面食,将手工拉制的面条煮熟后,淋上热油和辣椒粉,再加上酱油、醋等调料,香气扑鼻。
5. **意面**:如果想尝试一些西式面食,可以试试意大利面,如经典的番茄肉酱面或是奶油培根意面,都非常受欢迎。
希望这些建议能让您满意!如果您有任何特殊需求或想要尝试特定类型的面食,请告诉我。
- curl
- PHP
- Node.js
- C#
- Go
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": {}
}'
Replace YOUR_APP_ID with your actual application ID.Sample response
{
"output": {
"finish_reason": "stop",
"session_id": "36ca92e5689f4dfbab889525da8a784b",
"text": "了解了,您偏好面食。那么在推荐美食或者提供饮食建议时,我会更多地考虑到各种面食的选择。请问您有特别喜欢的面食种类吗?比如拉面、意大利面或者是某种地方特色的面条?或者,如果您需要一些建议来尝试新的面食,我也很乐意提供帮助!"
},
"usage": {
"models": [
{
"output_tokens": 70,
"model_id": "qwen-max",
"input_tokens": 119
}
]
},
"request_id": "050acdc4-d427-969e-8ba8-aa95075c2d9a"
}
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": {}
}'
Replace YOUR_APP_ID with your actual application ID.Sample response
{"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"}
<?php
# 若没有配置环境变量,可用百炼API Key将下行替换为:$api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
$api_key = getenv("DASHSCOPE_API_KEY");
$application_id = 'YOUR_APP_ID'; // 替换为实际的应用 ID
$memory_id = 'YOUR_MEMORY_ID'; // 替换为实际的记忆体 ID
$url = "https://dashscope.aliyuncs.com/api/v1/apps/{$application_id}/completion";
// 构造请求数据
$data = [
"input" => [
'prompt' => '用户饮食偏好:面食',
'memory_id' => $memory_id
]
];
// 将数据编码为 JSON
$dataString = json_encode($data);
// 检查 json_encode 是否成功
if (json_last_error() !== JSON_ERROR_NONE) {
die("JSON encoding failed with error: " . json_last_error_msg());
}
// 初始化 cURL 会话
$ch = curl_init($url);
// 设置 cURL 选项
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
]);
// 执行请求
$response = curl_exec($ch);
// 检查 cURL 执行是否成功
if ($response === false) {
die("cURL Error: " . curl_error($ch));
}
// 获取 HTTP 状态码
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// 关闭 cURL 会话
curl_close($ch);
// 解码响应数据
$response_data = json_decode($response, true);
// 处理响应
if ($status_code == 200) {
if (isset($response_data['output']['text'])) {
echo "{$response_data['output']['text']}\n";
} else {
echo "No text in response.\n";
}
}else {
if (isset($response_data['request_id'])) {
echo "request_id={$response_data['request_id']}\n";}
echo "code={$status_code}\n";
if (isset($response_data['message'])) {
echo "message={$response_data['message']}\n";}
else {
echo "message=Unknown error\n";}
}
?>
了解了,您对面食情有独钟。如果您正在寻找新的面食食谱或是想要知道哪里可以吃到美味的面食,请告诉我更多细节,我会尽力提供帮助!比如,您是想在家自己动手做还是打算外出就餐呢?
<?php
# 若没有配置环境变量,可用百炼API Key将下行替换为:$api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
$api_key = getenv("DASHSCOPE_API_KEY");
$application_id = 'YOUR_APP_ID'; // 替换为实际的应用 ID
$memory_id = 'YOUR_MEMORY_ID'; // 替换为实际的记忆体 ID
$url = "https://dashscope.aliyuncs.com/api/v1/apps/{$application_id}/completion";
// 构造请求数据
$data = [
"input" => [
'prompt' => '美食推荐',
'memory_id' => $memory_id
]
];
// 将数据编码为 JSON
$dataString = json_encode($data);
// 检查 json_encode 是否成功
if (json_last_error() !== JSON_ERROR_NONE) {
die("JSON encoding failed with error: " . json_last_error_msg());
}
// 初始化 cURL 会话
$ch = curl_init($url);
// 设置 cURL 选项
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
]);
// 执行请求
$response = curl_exec($ch);
// 检查 cURL 执行是否成功
if ($response === false) {
die("cURL Error: " . curl_error($ch));
}
// 获取 HTTP 状态码
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// 关闭 cURL 会话
curl_close($ch);
// 解码响应数据
$response_data = json_decode($response, true);
// 处理响应
if ($status_code == 200) {
if (isset($response_data['output']['text'])) {
echo "{$response_data['output']['text']}\n";
} else {
echo "No text in response.\n";
}
}else {
if (isset($response_data['request_id'])) {
echo "request_id={$response_data['request_id']}\n";}
echo "code={$status_code}\n";
if (isset($response_data['message'])) {
echo "message={$response_data['message']}\n";}
else {
echo "message=Unknown error\n";}
}
?>
既然您偏好面食,这里有一些建议您可以尝试的美味面食:
1. **炸酱面** - 一道经典的北京风味面食,主要以黄豆酱和肉末制成的炸酱拌面条,非常香浓可口。
2. **刀削面** - 来自山西的一种特色面条,以其独特的制作工艺——师傅手持面团直接削入滚水中而得名。搭配各种汤底或调料都非常好吃。
3. **担担面** - 四川省的传统小吃之一,特点是麻辣鲜香,通常会加入特制辣椒油、花生碎等材料提味。
4. **兰州拉面** - 起源于甘肃省兰州市,以其手工拉制而成细长且富有弹性的面条著称,常配以牛肉高汤、几片薄牛肉以及葱花等。
希望这些建议能给您带来灵感!如果您有特别想了解的地方或是其他需求,请告诉我。
npm install axios
const axios = require('axios');
async function callDashScope() {
//若没有配置环境变量,可用百炼API Key将下行替换为:apiKey='sk-xxx'。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
const apiKey = process.env.DASHSCOPE_API_KEY;
const appId = 'YOUR_APP_ID';// 替换为实际的应用 ID
const memoryId = 'YOUR_MEMORY_ID';// 替换为实际的memory_id
const url = `https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`;
const data = {
input: {
prompt: "用户饮食偏好:面食",
memory_id: memoryId
},
parameters: {},
debug: {}
};
try {
console.log("Sending request to DashScope API...");
const response = await axios.post(url, data, {
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
}
});
if (response.status === 200) {
if (response.data.output && response.data.output.text) {
console.log(`${response.data.output.text}`);
}
} else {
console.log("Request failed:");
if (response.data.request_id) {
console.log(`request_id=${response.data.request_id}`);
}
console.log(`code=${response.status}`);
if (response.data.message) {
console.log(`message=${response.data.message}`);
} else {
console.log('message=Unknown error');
}
}
} catch (error) {
console.error(`Error calling DashScope: ${error.message}`);
if (error.response) {
console.error(`Response status: ${error.response.status}`);
console.error(`Response data: ${JSON.stringify(error.response.data, null, 2)}`);
}
}
}
callDashScope();
解了,您对面食情有独钟。如果您想要尝试一些新的面食菜品或是寻找某个特定地方的特色面食,请告诉我,我很乐意为您提供一些建议!
const axios = require('axios');
async function callDashScope() {
//若没有配置环境变量,可用百炼API Key将下行替换为:apiKey='sk-xxx'。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
const apiKey = process.env.DASHSCOPE_API_KEY;
const appId = 'YOUR_APP_ID';// 替换为实际的应用 ID
const memoryId = 'YOUR_MEMORY_ID';// 替换为实际的memory_id
const url = `https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`;
const data = {
input: {
prompt: "美食推荐",
memory_id: memoryId
},
parameters: {},
debug: {}
};
try {
console.log("Sending request to DashScope API...");
const response = await axios.post(url, data, {
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
}
});
if (response.status === 200) {
if (response.data.output && response.data.output.text) {
console.log(`${response.data.output.text}`);
}
} else {
console.log("Request failed:");
if (response.data.request_id) {
console.log(`request_id=${response.data.request_id}`);
}
console.log(`code=${response.status}`);
if (response.data.message) {
console.log(`message=${response.data.message}`);
} else {
console.log('message=Unknown error');
}
}
} catch (error) {
console.error(`Error calling DashScope: ${error.message}`);
if (error.response) {
console.error(`Response status: ${error.response.status}`);
console.error(`Response data: ${JSON.stringify(error.response.data, null, 2)}`);
}
}
}
callDashScope();
既然您偏好面食,这里有一些建议供您参考:
1. **刀削面**:这种面条以其独特的口感和形状闻名,可以搭配各种美味的汤底和配料。
2. **担担面**:四川风味的辣味面条,用花生酱、辣椒油等调料制成,味道鲜美而富有层次感。
3. **炸酱面**:北京的传统美食之一,主要由黄豆酱炒肉末制成的酱料拌入煮好的面条中食用。
4. **意大利面**:如果您想尝试一些不同的口味,可以选择意式面食。比如番茄肉酱意面或是奶油蘑菇意面等。
希望这些建议对您有所帮助!如果还有其他需求或想要更具体的推荐,请告诉我。
using System.Text;
class Program
{
static async Task Main(string[] args)
{
// 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
string apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY")?? throw new InvalidOperationException("DASHSCOPE_API_KEY environment variable is not set.");;
string appId = "YOUR_APP_ID";// 替换为实际的应用ID
string memoryId = "YOUR_MEMORY_ID";//替换为实际的memory_id
if (string.IsNullOrEmpty(apiKey))
{
Console.WriteLine("请确保设置了 DASHSCOPE_API_KEY。");
return;
}
string url = $"https://dashscope.aliyuncs.com/api/v1/apps/{appId}/completion";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
string jsonContent = $@"{{
""input"": {{
""prompt"": ""用户饮食偏好:面食"",
""memory_id"":""{memoryId}""
}},
""parameters"": {{}},
""debug"": {{}}
}}";
HttpContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
try
{
HttpResponseMessage response = await client.PostAsync(url, content);
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
else
{
Console.WriteLine($"Request failed with status code: {response.StatusCode}");
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error calling DashScope: {ex.Message}");
}
}
}
}
{
"output": {
"finish_reason": "stop",
"session_id": "c24255e945b94f22bc3fe5fb515177c3",
"text": "了解了,您偏好面食。如果想要推荐或寻找一些好吃的面食菜品或餐厅,请告诉我更多的细节,比如您现在的位置、想尝试哪种风味(如川味、北方菜等),或者是否有特别的需求(例如素食选项)。这样我能更好地为您提供帮助。"
},
"usage": {
"models": [
{
"output_tokens": 59,
"model_id": "qwen-max",
"input_tokens": 146
}
]
},
"request_id": "f732635f-3082-9dfe-9c09-df679d6d5b2e"
}
using System.Text;
class Program
{
static async Task Main(string[] args)
{
// 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
string apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY")?? throw new InvalidOperationException("DASHSCOPE_API_KEY environment variable is not set.");;
string appId = "YOUR_APP_ID";// 替换为实际的应用ID
string memoryId = "YOUR_MEMORY_ID";//替换为实际的memory_id
if (string.IsNullOrEmpty(apiKey))
{
Console.WriteLine("请确保设置了 DASHSCOPE_API_KEY。");
return;
}
string url = $"https://dashscope.aliyuncs.com/api/v1/apps/{appId}/completion";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
string jsonContent = $@"{{
""input"": {{
""prompt"": ""美食推荐"",
""memory_id"":""{memoryId}""
}},
""parameters"": {{}},
""debug"": {{}}
}}";
HttpContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
try
{
HttpResponseMessage response = await client.PostAsync(url, content);
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
else
{
Console.WriteLine($"Request failed with status code: {response.StatusCode}");
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error calling DashScope: {ex.Message}");
}
}
}
}
{
"output": {
"finish_reason": "stop",
"session_id": "665c16a3d10b48a0bb0ba92d42b6707b",
"text": "既然您对面食情有独钟,这里有一些建议供您参考:
1. **担担面** - 来自四川的传统美食,以其麻辣鲜香著称。
2. **炸酱面** - 北京特色小吃之一,以黄豆酱或甜面酱炒肉末作为主要调料。
3. **刀削面** - 山西的代表性面食,面条宽厚、口感劲道。
4. **热干面** - 湖北武汉地区的传统早点,以芝麻酱为主要调料。
5. **牛肉拉面** - 甘肃兰州最著名的风味小吃之一,汤清味美。
如果您想要尝试其他类型的美食但仍然希望包含一些面食元素的话,也可以考虑意大利面或者日式乌冬面等国际化的选择。希望这些建议能够帮到您!如果有特定口味偏好的话,请告诉我,我可以提供更加个性化的建议。"
},
"usage": {
"models": [
{
"output_tokens": 196,
"model_id": "qwen-max",
"input_tokens": 142
}
]
},
"request_id": "305c5058-708e-94ce-b8ee-3bf539e5f35c"
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)
func main() {
// 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey := "sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
apiKey := os.Getenv("DASHSCOPE_API_KEY")
appId := "YOUR_APP_ID" // 替换为实际的应用 ID
if apiKey == "" {
fmt.Println("请确保设置了DASHSCOPE_API_KEY。")
return
}
url := fmt.Sprintf("https://dashscope.aliyuncs.com/api/v1/apps/%s/completion", appId)
// 创建请求体
requestBody := map[string]interface{}{
"input": map[string]string{
"prompt": "用户饮食偏好:面食",
"memory_id": "YOUR_MEMORY_ID", // 替换为实际的记忆体 ID
},
"parameters": map[string]interface{}{},
"debug": map[string]interface{}{},
}
jsonData, err := json.Marshal(requestBody)
if err != nil {
fmt.Printf("Failed to marshal JSON: %v\n", err)
return
}
// 创建 HTTP POST 请求
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
fmt.Printf("Failed to create request: %v\n", err)
return
}
// 设置请求头
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
// 发送请求
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Failed to send request: %v\n", err)
return
}
defer resp.Body.Close()
// 读取响应
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Failed to read response: %v\n", err)
return
}
// 处理响应
if resp.StatusCode == http.StatusOK {
fmt.Println("Request successful:")
fmt.Println(string(body))
} else {
fmt.Printf("Request failed with status code: %d\n", resp.StatusCode)
fmt.Println(string(body))
}
}
{
"output": {
"finish_reason": "stop",
"session_id": "2e2ac5be49d2469794f310a92f34c9c7",
"text": "了解了,您对面食情有独钟。如果您想要一些面食的推荐或是寻找特定类型的面食食谱,请告诉我,我很乐意帮助您!比如,您是想了解中式面条、意大利面还是其他种类的面食呢?"
},
"usage": {
"models": [
{
"output_tokens": 53,
"model_id": "qwen-max",
"input_tokens": 119
}
]
},
"request_id": "3f1c66ba-1d19-98f2-89a5-4c3b53c80258"
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)
func main() {
// 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey := "sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
apiKey := os.Getenv("DASHSCOPE_API_KEY")
appId := "YOUR_APP_ID" // 替换为实际的应用 ID
if apiKey == "" {
fmt.Println("请确保设置了DASHSCOPE_API_KEY。")
return
}
url := fmt.Sprintf("https://dashscope.aliyuncs.com/api/v1/apps/%s/completion", appId)
// 创建请求体
requestBody := map[string]interface{}{
"input": map[string]string{
"prompt": "美食推荐",
"memory_id": "YOUR_MEMORY_ID", // 替换为实际的记忆体 ID
},
"parameters": map[string]interface{}{},
"debug": map[string]interface{}{},
}
jsonData, err := json.Marshal(requestBody)
if err != nil {
fmt.Printf("Failed to marshal JSON: %v\n", err)
return
}
// 创建 HTTP POST 请求
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
fmt.Printf("Failed to create request: %v\n", err)
return
}
// 设置请求头
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
// 发送请求
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Failed to send request: %v\n", err)
return
}
defer resp.Body.Close()
// 读取响应
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Failed to read response: %v\n", err)
return
}
// 处理响应
if resp.StatusCode == http.StatusOK {
fmt.Println("Request successful:")
fmt.Println(string(body))
} else {
fmt.Printf("Request failed with status code: %d\n", resp.StatusCode)
fmt.Println(string(body))
}
}
{
"output": {
"finish_reason": "stop",
"session_id": "dcb3e991be904cfcb11b9b06109d4c73",
"text": "既然您偏好面食,那我推荐几种不同风格的面食供您选择:
1. **炸酱面**:这是北京的传统名吃之一,面条劲道,搭配特制的肉酱,味道鲜美。
2. **担担面**:源自四川,以其麻辣味闻名,面条细软,加上花生碎和特制辣酱,十分开胃。
3. **刀削面**:山西特色美食,面条宽厚且有嚼劲,通常与各种肉类和蔬菜一起炖煮。
4. **意大利面**:如果想尝试国际风味的话,可以选择意面,比如经典的番茄肉酱意面或奶油蘑菇鸡肉意面等。
希望这些建议对您有所帮助!如果您更倾向于某种特定口味或是想要了解更多细节,请告诉我。"
},
"usage": {
"models": [
{
"output_tokens": 167,
"model_id": "qwen-max",
"input_tokens": 142
}
]
},
"request_id": "08b629ce-b3dd-96b6-8177-6443893e0b66"
}
Upload files (documents, images, videos, or audio)
In your agent application, you can upload files such as documents, images, videos, or audio, and ask questions based on their content.Scenarios
- Text parsing: You can extract text content from documents, images, videos, or audio, and combine the extracted content with large language models to answer questions.
- Visual understanding: You can use the Qwen-VL series models to analyze image content (objects, scenes, and actions) without requiring text input.
- Text parsing
- Visual understanding
- Document (Up to 100 MB): .doc, .docx, .ppt, .pptx, .xls, .xlsx, .md, .txt, .pdf;
- Image (Up to 20 MB): .png, .jpg, .jpeg, .bmp, .gif; Currently, only local images that contain text are supported.
- Videos (Up to 512 MB): .mp4, .mkv, .avi, .mov, .wmv;
- Audio (Up to 512 MB): .aac, .amr, .flac, .flv, .m4a, .mp3, .mpeg, .ogg, .opus, .wav, .webm, .wma.
Step 2: Get a session file ID
- You can obtain a session file ID starting with “file_session” using the API to obtain a session file ID.
- Ensure the file status is FILE_IS_READY.
-
In the console agent application, you can select any model, turn on the dynamic file parsing switch, and then publish the application.
Your agent application and the uploaded files must be in the same workspace.
-
When calling the API, you must pass the session file ID obtained in Step 2 in the
session_file_idsparameter. For example:In the Java SDK, it is sessionFileIds. When calling via HTTP, put
session_file_idsinto the parameters object.The session file ID must start with “file_session_”, and its file status must be FILE_IS_READY. Otherwise, the call will fail.- Python
- Java
- HTTP
Sample requestSample responseCopyimport os import dashscope # 需要 dashscope 2.0.1 及以上版本 from dashscope import DashScopeError client = dashscope.Client( # 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 api_key=os.getenv("DASHSCOPE_API_KEY")) try: response = client.apps.completions.create( app_id='YOUR_APP_ID', # 应用ID替换YOUR_APP_ID prompt='请根据以下文件帮我推荐一款3000元以下的手机', rag_options={ "session_file_ids": ["FILE_ID1"], # FILE_ID1 替换为实际的临时文件ID,逗号隔开多个 } ) print('%s\n' % (response.text)) # 处理只输出文本text # print('%s\n' % (response.usage)) except DashScopeError as e: print(f'request_id={e.request_id}') print(f'code={e.code}') print(f'message={e.message}') print(f'请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code')Copy根据您的预算,我推荐您选择**通义 Vivid 7**这款手机。以下是它的主要特点: - **屏幕**:6.5英寸,1080 x 2400像素 - **存储与内存**:128GB存储,8GB RAM - **电池**:4500mAh - **特色功能**:AI智能摄影,侧面指纹解锁 - **参考售价**:2999-3299元 通义Vivid 7不仅价格适中,而且具有良好的性能和实用的功能,特别适合喜欢拍照的用户。希望这个建议对您有所帮助!Sample requestSample responseCopy// 建议dashscope SDK 的版本 >= 2.17.0; 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; public class Main { public static void appCall() throws NoApiKeyException, InputRequiredException { ApplicationParam param = ApplicationParam.builder() // 若没有配置环境变量,可用百炼API Key将下行替换为:.apiKey("sk-xxx")。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 .apiKey(System.getenv("DASHSCOPE_API_KEY")) .appId("YOUR_APP_ID") // 替换为实际的应用ID .prompt("请根据以下文件帮我推荐一款3000元左右的手机") .ragOptions(RagOptions.builder() .sessionFileIds(Arrays.asList("FILE_ID1", "FILE_ID2")) // 替换为实际指定的临时文件 ID,逗号隔开多个 .build()) .build(); Application application = new Application(); ApplicationResult result = application.call(param); System.out.printf("%s\n", result.getOutput().getText());// 处理只输出文本text } public static void main(String[] args) { try { appCall(); } catch (ApiException | NoApiKeyException | InputRequiredException e) { System.out.printf("Exception: %s", e.getMessage()); System.out.println("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code"); } System.exit(0); } }Copy根据您的预算(3000元左右),我推荐您选择**通义 Vivid 7**这款手机。 ### 通义 Vivid 7 —— 智能摄影新体验 - **屏幕**:6.5英寸,1080 x 2400像素 - **存储与内存**:128GB存储,8GB RAM - **电池**:4500mAh - **特色功能**:AI智能摄影,侧面指纹解锁 - **参考售价**:2999-3299元 这款手机拥有不错的屏幕显示效果、充足的存储空间和RAM,以及出色的续航能力。特别是其AI智能摄影功能,可以带来更高质量的照片拍摄体验。整体来看,它非常适合日常使用,并且价格也符合您的预算范围。- curl
- PHP
- Node.js
- C#
- Go
Sample requestSample responseCopycurl -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" : { "session_file_ids":["FILE_ID1"]} }, "debug": {} }'Copy{"output":{"finish_reason":"stop","session_id":"fb0081f56ace400bb4f1c12f6b5d1247", "text":"根据您的预算(3000元以下),我为您推荐**通义 Vivid 7**这款手机。\n\n### 推荐理由\n- **价格适中**:参考售价为2999-3299元,正好符合您的预算范围。\n- **智能摄影功能**:具备AI智能摄影技术,能够帮助您拍摄出专业级别的照片,非常适合喜欢拍照的用户。\n- **性能均衡**:拥有6.5英寸的1080 x 2400像素屏幕,搭配8GB RAM与128GB存储空间,在保证日常使用流畅的同时也提供了足够的存储容量。\n- **长续航能力**:内置4500mAh电池,可以满足一天的正常使用需求。\n- **安全便捷**:采用侧面指纹解锁设计,既方便又安全。\n\n综上所述,通义 Vivid 7 在性价比方面表现优秀,尤其适合追求良好拍照体验和合理价位的消费者。"},"usage":{"models":[{"output_tokens":201,"model_id":"qwen-max","input_tokens":1594}]},"request_id":"596f5055-2736-985d-8024-5849df5b799b"}%Sample requestSample responseCopy<?php # 若没有配置环境变量,可用百炼API Key将下行替换为:$api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 $api_key = getenv("DASHSCOPE_API_KEY"); $application_id = 'YOUR_APP_ID'; // 替换为实际的应用 ID $url = "https://dashscope.aliyuncs.com/api/v1/apps/$application_id/completion"; // 构造请求数据 $data = [ "input" => [ 'prompt' => '请根据以下文件帮我推荐一款3000元以下的手机' ], "parameters" => [ 'rag_options' => [ 'session_file_ids' => ['FILE_ID1']//替换为指定的临时文件ID,逗号隔开多个 ] ] ]; // 将数据编码为 JSON $dataString = json_encode($data); // 检查 json_encode 是否成功 if (json_last_error() !== JSON_ERROR_NONE) { die("JSON encoding failed with error: " . json_last_error_msg()); } // 初始化 cURL 会话 $ch = curl_init($url); // 设置 cURL 选项 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Authorization: Bearer ' . $api_key ]); // 执行请求 $response = curl_exec($ch); // 检查 cURL 执行是否成功 if ($response === false) { die("cURL Error: " . curl_error($ch)); } // 获取 HTTP 状态码 $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 关闭 cURL 会话 curl_close($ch); // 解码响应数据 $response_data = json_decode($response, true); // 处理响应 if ($status_code == 200) { if (isset($response_data['output']['text'])) { echo "{$response_data['output']['text']}\n"; } else { echo "No text in response.\n"; } }else { if (isset($response_data['request_id'])) { echo "request_id={$response_data['request_id']}\n";} echo "code={$status_code}\n"; if (isset($response_data['message'])) { echo "message={$response_data['message']}\n";} else { echo "message=Unknown error\n";} } ?>Copy根据您的预算(3000元以下),我为您推荐**通义 Vivid 7**。这款手机的主要特点包括: - **屏幕**:6.5英寸,1080 x 2400像素 - **存储与内存**:128GB存储,8GB RAM - **电池**:4500mAh - **特色功能**:AI智能摄影,侧面指纹解锁 - **参考售价**:2999-3299元 通义Vivid 7以其实惠的价格和出色的性能,特别是其在拍照方面的优秀表现,非常适合追求性价比且对摄影有一定要求的用户。希望这个建议能够帮到您!如果还有其他需求或疑问,请随时告诉我。Dependency:Sample requestCopynpm install axiosSample responseCopyconst axios = require('axios'); async function callDashScope() { // 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey='sk-xxx'。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 const apiKey = process.env.DASHSCOPE_API_KEY; const appId = 'YOUR_APP_ID';//替换为实际的应用 ID const url = `https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`; const data = { input: { prompt: "请根据以下文件帮我推荐一款3000元以下的手机" }, parameters: { rag_options:{ session_file_ids:['YOUR_FILE_ID1'] // 替换为指定的临时文件 ID,多个请用逗号隔开 } }, debug: {} }; try { const response = await axios.post(url, data, { headers: { 'Authorization': `Bearer ${apiKey}`, 'Content-Type': 'application/json' } }); if (response.status === 200) { console.log(`${response.data.output.text}`); } else { console.log(`request_id=${response.headers['request_id']}`); console.log(`code=${response.status}`); console.log(`message=${response.data.message}`); } } catch (error) { console.error(`Error calling DashScope: ${error.message}`); if (error.response) { console.error(`Response status: ${error.response.status}`); console.error(`Response data: ${JSON.stringify(error.response.data, null, 2)}`); } } } callDashScope();Copy根据您的预算(3000元以下),我为您推荐**通义 Vivid 7**这款手机。 ### 推荐理由: - **屏幕表现良好**:6.5英寸1080 x 2400像素全面屏,视觉效果细腻。 - **AI智能摄影**:支持AI智能摄影功能,能够自动优化照片色彩与细节,让您轻松拍出专业级照片。 - **充足的内存和存储空间**:配备8GB RAM + 128GB存储组合,满足日常使用及娱乐需求。 - **持久续航**:内置4500mAh大容量电池,保障一天的正常使用。 - **便捷安全解锁方式**:采用侧面指纹解锁设计,既快速又方便。 - **价格合理**:参考售价为2999-3299元,在您的预算范围内。 综上所述,如果您对摄影有一定兴趣且追求性价比的话,通义 Vivid 7将是不错的选择。当然,具体购买时请以官方渠道或授权零售商提供的最新信息为准。Sample requestSample responseCopyusing System.Text; class Program { static async Task Main(string[] args) { // 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 string apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY")?? throw new InvalidOperationException("DASHSCOPE_API_KEY environment variable is not set.");; string appId = "YOUR_APP_ID";// 替换为实际的应用ID // FILE_ID1替换为指定的临时文件ID if (string.IsNullOrEmpty(apiKey)) { Console.WriteLine("请确保设置了 DASHSCOPE_API_KEY。"); return; } string url = $"https://dashscope.aliyuncs.com/api/v1/apps/{appId}/completion"; using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}"); string jsonContent = $@"{{ ""input"": {{ ""prompt"": ""请根据以下文件帮我推荐一款3000元以下的手机"" }}, ""parameters"": {{ ""rag_options"" : {{ ""session_file_ids"":[""FILE_ID1""] }} }}, ""debug"": {{}} }}"; HttpContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); try { HttpResponseMessage response = await client.PostAsync(url, content); if (response.IsSuccessStatusCode) { string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseBody); } else { Console.WriteLine($"Request failed with status code: {response.StatusCode}"); string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseBody); } } catch (Exception ex) { Console.WriteLine($"Error calling DashScope: {ex.Message}"); } } } }Copy{ "output": { "finish_reason": "stop", "session_id": "11c4c233714d45129fb9b63a7e708fb8", "text": "根据您的预算(3000元以下),我为您推荐**通义 Vivid 7**这款手机。它具有以下特点:\n\n- **屏幕**:6.5英寸,1080 x 2400像素\n- **存储与内存**:128GB存储,8GB RAM\n- **电池**:4500mAh\n- **特色功能**:AI智能摄影,侧面指纹解锁\n- **参考售价**:2999-3299元\n\n虽然其最高配置版本的价格略高于3000元,但考虑到其出色的性价比以及在摄影方面的优秀表现,如果能够接受这个价格范围的话,这将是一个非常不错的选择。此外,您还可以留意一下电商平台或官方渠道是否会有优惠活动,从而以更低的价格购得此款手机。" }, "usage": { "models": [ { "output_tokens": 177, "model_id": "qwen-max", "input_tokens": 1594 } ] }, "request_id": "663c2641-bfe3-908e-a10f-5ccb819cb136" }Sample requestSample responseCopypackage main import ( "bytes" "encoding/json" "fmt" "io" "net/http" "os" ) func main() { // 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey := "sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 apiKey := os.Getenv("DASHSCOPE_API_KEY") appId := "YOUR_APP_ID" // 替换为实际的应用 ID if apiKey == "" { fmt.Println("请确保设置了DASHSCOPE_API_KEY。") return } url := fmt.Sprintf("https://dashscope.aliyuncs.com/api/v1/apps/%s/completion", appId) // 创建请求体 requestBody := map[string]interface{}{ "input": map[string]string{ "prompt": "请根据以下文件帮我推荐一款3000元以下的手机", }, "parameters": map[string]interface{}{ "rag_options": map[string]interface{}{ "session_file_ids": []string{"FILE_ID1"}, // 替换为实际的临时文件 }, }, "debug": map[string]interface{}{}, } jsonData, err := json.Marshal(requestBody) if err != nil { fmt.Printf("Failed to marshal JSON: %v\n", err) return } // 创建 HTTP POST 请求 req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) if err != nil { fmt.Printf("Failed to create request: %v\n", err) return } // 设置请求头 req.Header.Set("Authorization", "Bearer "+apiKey) req.Header.Set("Content-Type", "application/json") // 发送请求 client := &http.Client{} resp, err := client.Do(req) if err != nil { fmt.Printf("Failed to send request: %v\n", err) return } defer resp.Body.Close() // 读取响应 body, err := io.ReadAll(resp.Body) if err != nil { fmt.Printf("Failed to read response: %v\n", err) return } // 处理响应 if resp.StatusCode == http.StatusOK { fmt.Println("Request successful:") fmt.Println(string(body)) } else { fmt.Printf("Request failed with status code: %d\n", resp.StatusCode) fmt.Println(string(body)) } }Copy{ "output": { "finish_reason": "stop", "session_id": "846db8ba50514d69bfa0faebd639cb33", "text": "根据您的预算,我推荐您选择**通义 Vivid 7 —— 智能摄影新体验**这款手机。它拥有6.5英寸的全面屏、8GB RAM和128GB存储空间,支持AI智能摄影功能,还有4500mAh电池容量,能够满足日常使用需求。侧面指纹解锁设计既方便又安全。参考售价为2999-3299元,正好符合您的预算范围。希望这个建议对您有所帮助!" }, "usage": { "models": [ { "output_tokens": 106, "model_id": "qwen-max", "input_tokens": 1594 } ] }, "request_id": "af305d07-a24a-9163-a015-4ec52909ec55" }
Get a session file ID via API
Steps
Steps
-
Request a file upload lease
Try it online You can use the ApplyFileUploadLease API.
Call diagram Key parameters Set CategoryType to SESSION_FILE, complete the parameter configuration, and then click Initiate Call.CategoryId: Must be default. FileName: File name with extension. Md5: You can run the sample code below to obtain the MD5 hash of your file. MD5 generation sample code
PythonCopy# 示例代码仅供参考,请勿在生产环境中直接使用 import hashlib def calculate_md5(file_path): """计算文档的 MD5 值。 Args: file_path (str): 文档的路径。 Returns: str: 文档的 MD5 值。 """ md5_hash = hashlib.md5() # 以二进制形式读取文件 with open(file_path, "rb") as f: # 按块读取文件,避免大文件占用过多内存 for chunk in iter(lambda: f.read(4096), b""): md5_hash.update(chunk) return md5_hash.hexdigest() # 使用示例 file_path = "请替换为您需要上传文档的实际本地路径,例如/Users/<username>/Desktop/阿里云百炼系列手机产品介绍.docx" md5_value = calculate_md5(file_path) print(f"文档的MD5值为: {md5_value}")SizeInBytes: File size in bytes. For example: 6 KB equals 6 × 1024 bytes, which is 6144 bytes. CategoryType: Must be SESSION_FILE. Success responseA successful call returns the following JSON response (sensitive fields are masked): Copy{ "Status": "200", "Message": "", "RequestId": "519DD942-4701-xxx", "Data": { "FileUploadLeaseId": "ed76eb4xxx", "Type": "OSS.PreSignedUrl", "Param": { "Headers": { "X-bailian-extra": "MTY3MzAyNxxx==", "Content-Type": "application/msword" }, "Method": "PUT", "Url": "https://dashscope-file-datacenter-xxx" } } }Instructions - Make sure to save the values of the
Data.FileUploadLeaseId,Data.Param.Method,Data.Param.Url, Data.Param.Headers.X-bailian-extra, andData.Param.Headers.Content-Typefields from this API response. They are required for the subsequent upload steps. - The
Data.Param.Urlvalue (the lease) expires in minutes—upload your file promptly to avoid expiration.
- Make sure to save the values of the
-
Upload file to Alibaba Cloud Model Studio temporary storage
You can run the sample code below. Replace placeholder values with the information obtained in Step 1. If the response indicates “File uploaded successfully.”, the upload was successful.
Sample code
You can write multilingual examples yourself.PythonCopy# 示例代码仅供参考,请勿在生产环境中直接使用 import requests from urllib.parse import urlparse def upload_file(pre_signed_url, file_path): try: # 设置请求头 headers = { "X-bailian-extra": "请替换为您在上一步中调用ApplyFileUploadLease接口实际返回的Data.Param.Headers中X-bailian-extra字段的值", "Content-Type": "请替换为您在上一步中调用ApplyFileUploadLease接口实际返回的Data.Param.Headers中Content-Type字段的值" } # 读取文档并上传 with open(file_path, 'rb') as file: # 下方设置请求方法用于文档上传,需与您在上一步中调用ApplyFileUploadLease接口实际返回的Data.Param中Method字段的值一致 response = requests.put(pre_signed_url, data=file, headers=headers) # 检查响应状态码 if response.status_code == 200: print("File uploaded successfully.") else: print(f"Failed to upload the file. ResponseCode: {response.status_code}") except Exception as e: print(f"An error occurred: {str(e)}") def upload_file_link(pre_signed_url, source_url_string): try: # 设置请求头 headers = { "X-bailian-extra": "请替换为您在上一步中调用ApplyFileUploadLease接口实际返回的Data.Param.Headers中X-bailian-extra字段的值", "Content-Type": "请替换为您在上一步中调用ApplyFileUploadLease接口实际返回的Data.Param.Headers中Content-Type字段的值" } # 设置访问OSS的请求方法为GET source_response = requests.get(source_url_string) if source_response.status_code != 200: raise RuntimeError("Failed to get source file.") # 下方设置请求方法用于文档上传,需与您在上一步中调用ApplyFileUploadLease接口实际返回的Data.Param中Method字段的值一致 response = requests.put(pre_signed_url, data=source_response.content, headers=headers) # 检查响应状态码 if response.status_code == 200: print("File uploaded successfully.") else: print(f"Failed to upload the file. ResponseCode: {response.status_code}") except Exception as e: print(f"An error occurred: {str(e)}") if __name__ == "__main__": pre_signed_url_or_http_url = "请替换为您在上一步中调用ApplyFileUploadLease接口实际返回的Data.Param中Url字段的值" # 文档来源可以是本地,上传本地文档至百炼临时存储 file_path = "请替换为您需要上传文档的实际本地路径" upload_file(pre_signed_url_or_http_url, file_path) # 文档来源还可以是阿里云对象存储OSS # file_path = "请替换为您需要上传文档的实际阿里云对象存储OSS可公网访问地址" # upload_file_link(pre_signed_url_or_http_url, file_path) -
Add file to Alibaba Cloud Model Studio Data Management
After Step 2 is successful, your file remains in Model Studio’s temporary storage for 12 hours. Then, you can call the Try it online AddFile API to obtain the session file ID.
- LeaseId: Value of Data.FileUploadLeaseId from Step 1’s response.
- CategoryId: Must be default.
- CategoryType: Must be SESSION_FILE.
Success responseA successful call returns the following JSON response, where FileIdis the session file ID that will be used in all subsequent API operations related to this document.Copy{ "Status": 200, "Message": "", "RequestId": "AD2FB14B-8A0E-5DCB-xxx", "Data": { "FileId": "file_session_6c6bb333395xxx", "Parser": "DASHSCOPE_DOCMIND" }, "Code": "Success", "Success": true }Instructions -
Get the session file ID. Example: "file_session_6c6bb33339524b7xxx".
Only IDs starting with “file_session_” can be used in the next step. If not, verify that CategoryId and CategoryType were entered correctly in Step 1.
-
After AddFile succeeds, the
LeaseId(lease ID) becomes invalid—do not reuse it.
-
Check file parsing status
You can call the Try it online DescribeFile API to check the parsing status.
Status code
Instructions
INITFile uploaded—waiting for parsing.
PARSINGParsing file content.
PARSE_SUCCESSFile parsed successfully.
PARSE_FAILEDFile parsing failed—re-upload required.
SAFE_CHECKINGRunning security scan.
SAFE_CHECK_FAILEDSecurity scan failed—re-upload or choose another file.
INDEX_BUILDINGBuilding index for file.
INDEX_BUILD_SUCCESSIndex built successfully.
INDEX_BUILDING_FAILEDIndex build failed—re-upload required.
INDEX_DELETEDIndex deleted.
FILE_IS_READYFile ready: Parsing, security scan, and indexing all complete.
FILE_EXPIREDFile expired. Files last only for the current session (max 7 days). They expire automatically after closing the session—re-upload required.
On the DescribeFile API debugging page, fill in the WorkspaceId and FileId parameters, and then click Initiate Call. Verify that theYou must wait until theStatusfield showsFILE_IS_READYbefore proceeding.Statusfield in the response isFILE_IS_READYandParserisDASHSCOPE_DOCMIND. - After obtaining a valid session file ID, proceed to Step 3 above: Call your application via API.
- In the console agent application, select the Qwen-VL series models, and then publish the application.
-
When calling the API, you can pass image URLs in the
image_listparameter. You can pass multiple URLs. For example:In the Java SDK, use images. In HTTP calls, place image_list inside the input object.
- Python
- Java
- HTTP
Sample requestSample responseCopyimport os import dashscope # 需要 dashscope 2.0.1 及以上版本 from dashscope import DashScopeError client = dashscope.Client( # 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 api_key=os.getenv("DASHSCOPE_API_KEY")) try: response = client.apps.completions.create( app_id='YOUR_APP_ID', # 应用ID替换YOUR_APP_ID prompt='图中描绘的是什么景象?', image_list=["https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241022/emyrja/dog_and_girl.jpeg"], # 替换为实际的图片链接,逗号隔开多个 ) print('%s\n' % (response.text)) # 处理只输出文本text # print('%s\n' % (response.usage)) except DashScopeError as e: print(f'request_id={e.request_id}') print(f'code={e.code}') print(f'message={e.message}') print(f'请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code')Copy图中描绘的是一个女人和一只狗在海滩上互动的景象。女人坐在沙滩上,面带微笑地与狗握手。背景是大海和天空,阳光洒在她们身上,营造出温暖和谐的氛围。Sample requestSample responseCopy// 建议dashscope SDK 的版本 >= 2.17.0; 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 Main { public static void appCall() throws NoApiKeyException, InputRequiredException { ApplicationParam param = ApplicationParam.builder() // 若没有配置环境变量,可用百炼API Key将下行替换为:.apiKey("sk-xxx")。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 .apiKey(System.getenv("DASHSCOPE_API_KEY")) .appId("YOUR_APP_ID") // 替换为实际的应用ID .prompt("图中描绘的是什么景象?") .images(Arrays.asList("https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241022/emyrja/dog_and_girl.jpeg")) // 替换为实际的图片链接,逗号隔开多个 .build(); Application application = new Application(); ApplicationResult result = application.call(param); System.out.printf("%s\n", result.getOutput().getText());// 处理只输出文本text } public static void main(String[] args) { try { appCall(); } catch (ApiException | NoApiKeyException | InputRequiredException e) { System.out.printf("Exception: %s", e.getMessage()); System.out.println("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code"); } System.exit(0); } }Copy图中描绘的是一个女人和一只狗在海滩上互动的景象。女人坐在沙滩上,面带微笑地与狗握手。背景是大海和天空,阳光洒在她们身上,营造出温暖和谐的氛围。- curl
- PHP
- Node.js
- C#
- Go
Sample requestSample responseCopycurl -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": "图中描绘的是什么景象?", "image_list":["https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241022/emyrja/dog_and_girl.jpeg"] }, "debug": {} }'Copy{ "output": { "finish_reason": "stop", "session_id": "6c67678038e14f138f384e477e7126f6", "text": "图中描绘的是一个女人和一只狗在海滩上互动的景象。女人坐在沙滩上,面带微笑地与狗握手。背景是大海和天空,阳光洒在她们身上,营造出温暖和谐的氛围。" }, "usage": { "models": [ { "output_tokens": 49, "model_id": "qwen3-vl-plus", "input_tokens": 1305 } ] }, "request_id": "4a8a6a76-eecd-9298-a0fb-16a4c8f9a205" }Sample requestSample responseCopy<?php # 若没有配置环境变量,可用百炼API Key将下行替换为:$api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 $api_key = getenv("DASHSCOPE_API_KEY"); $application_id = 'YOUR_APP_ID'; // 替换为实际的应用 ID $url = "https://dashscope.aliyuncs.com/api/v1/apps/$application_id/completion"; // 构造请求数据 $data = [ "input" => [ 'prompt' => '图中描绘的是什么景象?', 'image_list' => ['https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241022/emyrja/dog_and_girl.jpeg']//替换为实际的图片链接,逗号隔开多个 ] ]; // 将数据编码为 JSON $dataString = json_encode($data); // 检查 json_encode 是否成功 if (json_last_error() !== JSON_ERROR_NONE) { die("JSON encoding failed with error: " . json_last_error_msg()); } // 初始化 cURL 会话 $ch = curl_init($url); // 设置 cURL 选项 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Authorization: Bearer ' . $api_key ]); // 执行请求 $response = curl_exec($ch); // 检查 cURL 执行是否成功 if ($response === false) { die("cURL Error: " . curl_error($ch)); } // 获取 HTTP 状态码 $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 关闭 cURL 会话 curl_close($ch); // 解码响应数据 $response_data = json_decode($response, true); // 处理响应 if ($status_code == 200) { if (isset($response_data['output']['text'])) { echo "{$response_data['output']['text']}\n"; } else { echo "No text in response.\n"; } } else { if (isset($response_data['request_id'])) { echo "request_id={$response_data['request_id']}\n";} echo "code={$status_code}\n"; if (isset($response_data['message'])) { echo "message={$response_data['message']}\n";} else { echo "message=Unknown error\n";} } ?>Copy图中描绘的是一个女人和一只狗在海滩上互动的景象。女人坐在沙滩上,面带微笑地与狗握手。背景是大海和天空,阳光洒在她们身上,营造出温暖和谐的氛围。Dependency:Sample requestCopynpm install axiosSample responseCopyconst axios = require('axios'); async function callDashScope() { // 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey='sk-xxx'。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 const apiKey = process.env.DASHSCOPE_API_KEY; const appId = 'YOUR_APP_ID';//替换为实际的应用 ID const url = `https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`; const data = { input: { prompt: "图中描绘的是什么景象?", image_list:['https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241022/emyrja/dog_and_girl.jpeg'] // 替换为实际的图片链接,多个请用逗号隔开 }, debug: {} }; try { const response = await axios.post(url, data, { headers: { 'Authorization': `Bearer ${apiKey}`, 'Content-Type': 'application/json' } }); if (response.status === 200) { console.log(`${response.data.output.text}`); } else { console.log(`request_id=${response.headers['request_id']}`); console.log(`code=${response.status}`); console.log(`message=${response.data.message}`); } } catch (error) { console.error(`Error calling DashScope: ${error.message}`); if (error.response) { console.error(`Response status: ${error.response.status}`); console.error(`Response data: ${JSON.stringify(error.response.data, null, 2)}`); } } } callDashScope();Copy图中描绘的是一个女人和一只狗在海滩上互动的景象。女人坐在沙滩上,面带微笑地与狗握手。背景是大海和天空,阳光洒在她们身上,营造出温暖和谐的氛围。Sample requestSample responseCopyusing System.Text; class Program { static async Task Main(string[] args) { // 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 string apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY")?? throw new InvalidOperationException("DASHSCOPE_API_KEY environment variable is not set.");; string appId = "YOUR_APP_ID";// 替换为实际的应用ID if (string.IsNullOrEmpty(apiKey)) { Console.WriteLine("请确保设置了 DASHSCOPE_API_KEY。"); return; } string url = $"https://dashscope.aliyuncs.com/api/v1/apps/{appId}/completion"; // image_list中替换为实际的图片链接,如果多个图片链接请用逗号隔开 using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}"); string jsonContent = $@"{{ ""input"": {{ ""prompt"": ""图中描绘的是什么景象?"", ""image_list"":[""https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241022/emyrja/dog_and_girl.jpeg"" ] }}, ""debug"": {{}} }}"; HttpContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); try { HttpResponseMessage response = await client.PostAsync(url, content); if (response.IsSuccessStatusCode) { string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseBody); } else { Console.WriteLine($"Request failed with status code: {response.StatusCode}"); string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseBody); } } catch (Exception ex) { Console.WriteLine($"Error calling DashScope: {ex.Message}"); } } } }Copy{ "output": { "finish_reason": "stop", "session_id": "5149b58d713e49ed80c30372aee377ae", "text": "图中描绘的是一个女人和一只狗在海滩上互动的景象。女人坐在沙滩上,面带微笑地与狗握手。背景是大海和天空,阳光洒在她们身上,营造出温暖和谐的氛围。" }, "usage": { "models": [ { "output_tokens": 49, "model_id": "qwen3-vl-plus", "input_tokens": 1305 } ] }, "request_id": "702810f5-d21d-9b74-8b4f-e58d0a8da413" }Sample requestSample responseCopypackage main import ( "bytes" "encoding/json" "fmt" "io" "net/http" "os" ) func main() { // 若没有配置环境变量,可用百炼API Key将下行替换为:apiKey := "sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 apiKey := os.Getenv("DASHSCOPE_API_KEY") appId := "YOUR_APP_ID" // 替换为实际的应用 ID if apiKey == "" { fmt.Println("请确保设置了DASHSCOPE_API_KEY。") return } url := fmt.Sprintf("https://dashscope.aliyuncs.com/api/v1/apps/%s/completion", appId) // 创建请求体 requestBody := map[string]interface{}{ "input": map[string]interface{}{ "prompt": "图中描绘的是什么景象?", "image_list": []string{"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241022/emyrja/dog_and_girl.jpeg", "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241022/emyrja/dog_and_girl.jpeg"}, // 替换为实际的图片链接,多个请用逗号隔开 }, "debug": map[string]interface{}{}, } jsonData, err := json.Marshal(requestBody) if err != nil { fmt.Printf("Failed to marshal JSON: %v\n", err) return } // 创建 HTTP POST 请求 req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) if err != nil { fmt.Printf("Failed to create request: %v\n", err) return } // 设置请求头 req.Header.Set("Authorization", "Bearer "+apiKey) req.Header.Set("Content-Type", "application/json") // 发送请求 client := &http.Client{} resp, err := client.Do(req) if err != nil { fmt.Printf("Failed to send request: %v\n", err) return } defer resp.Body.Close() // 读取响应 body, err := io.ReadAll(resp.Body) if err != nil { fmt.Printf("Failed to read response: %v\n", err) return } // 处理响应 if resp.StatusCode == http.StatusOK { fmt.Println("Request successful:") fmt.Println(string(body)) } else { fmt.Printf("Request failed with status code: %d\n", resp.StatusCode) fmt.Println(string(body)) } }Copy{ "output": { "finish_reason": "stop", "session_id": "8a3d495588d34c2e99ea42b12d265c31", "text": "图中描绘的是一个女人和一只狗在海滩上互动的景象。女人穿着格子衬衫,坐在沙滩上,与她的宠物狗进行亲密的互动。狗狗戴着项圈,伸出前爪与女人握手,显得非常友好和温顺。背景是广阔的海洋和天空,阳光洒在她们身上,营造出一种温暖和谐的氛围。这张照片捕捉到了人与动物之间美好的友谊时刻。" }, "usage": { "models": [ { "output_tokens": 88, "model_id": "qwen3-vl-plus", "input_tokens": 2554 } ] }, "request_id": "22db4110-6c7b-9b06-8769-c8a1a3edfd39" }
Private network calls
Private network calls
- Add a PrivateLink connection and associate your workspace: In the Model Studio console, add a PrivateLink connection for your VPC, and then associate the target workspace with the connection.
-
Replace the domain name: Replace the public domain name in the API request URL,
dashscope.aliyuncs.com, with the private domain name that is generated after the workspace is associated. For example:https://{WorkspaceId}-{VpcId}.cn-beijing.maas.aliyuncs.com/api/v1/ -
Make the request.
- Python
- Java
- HTTP
Copyimport os import dashscope from dashscope import DashScopeError # 配置私网域名:显式指定base_url client = dashscope.Client( # 若没有配置环境变量,可用阿里云百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 api_key=os.getenv("DASHSCOPE_API_KEY"), base_url='https://{WorkspaceId}-{VpcId}.cn-beijing.maas.aliyuncs.com/api/v1/') try: response = client.apps.completions.create( app_id='APP_ID',# 替换为实际的应用 ID prompt='你是谁?') print(response.text) except DashScopeError as e: print(f'request_id={e.request_id}') print(f'code={e.code}') print(f'message={e.message}') print(f'请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code')Copy// 建议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 Main { public static void appCall() throws ApiException, NoApiKeyException, InputRequiredException { ApplicationParam param = ApplicationParam.builder() // 若没有配置环境变量,可用阿里云百炼API Key将下行替换为:.apiKey("sk-xxx")。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。 .apiKey(System.getenv("DASHSCOPE_API_KEY")) .appId("APP_ID") .prompt("你是谁?") .build(); // 配置私网域名 Application application = new Application("https://{WorkspaceId}-{VpcId}.cn-beijing.maas.aliyuncs.com/api/v1/"); ApplicationResult result = application.call(param); System.out.printf("text: %s\n", result.getOutput().getText()); } public static void main(String[] args) { try { appCall(); } catch (ApiException | NoApiKeyException | InputRequiredException e) { System.err.println("message:"+e.getMessage()); System.out.println("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code"); } System.exit(0); } }curl example:Copycurl -X POST https://{WorkspaceId}-{VpcId}.cn-beijing.maas.aliyuncs.com/api/v1/apps/APP_ID/completion \ --header "Authorization: Bearer $DASHSCOPE_API_KEY" \ --header 'Content-Type: application/json' \ --data '{ "input": { "prompt": "你是谁?" }, "parameters": {}, "debug": {} }'APP_ID: Your application ID.
API Reference
For a complete list of parameters, refer to the Workflow and Legacy Agent Application API.Error codes
If the call fails (the Python SDK throws aDashScopeError exception and HTTP calls return an error message), see error messages for troubleshooting steps.
References
- When calling an application, if you need to convert the text information of model replies into speech, see Real-time Speech Synthesis-CosyVoice /Sambert.
- For building and using applications, see Agent Application (Agent 1.0).
- For using the Prompt assistance tools within an application, see Prompt engineering.
- For use in a frontend production environment, see Add an AI assistant to your website in 10 minutes.
FAQ
How do I fix the "java: package com.alibaba.dashscope.app does not exist" error when running a Java code example?
How do I fix the "java: package com.alibaba.dashscope.app does not exist" error when running a Java code example?
- Verify that the class name and package name in your import statement are correct.
- Add the dependency library: If you use Maven or Gradle to manage your project, make sure the DashScope Java SDK dependency is added to your
pom.xmlorbuild.gradlefile and that it is the latest version. You can obtain the latest version number for the DashScope Java SDK from Maven.
<!-- 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'
- Upgrade the SDK: Older versions of the DashScope Java SDK may not include the features or classes that you are trying to use. If your current version is outdated, upgrade it by updating the DashScope Java SDK version in your
pom.xmlorbuild.gradlefile.
<!-- 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: '请将此处的版本号修改为最新版本'
- Reload your project to apply the changes.
- Run the code example again.
What is the difference between multi-turn conversation (session_id) and long-term memory (memory_id) ?
What is the difference between multi-turn conversation (session_id) and long-term memory (memory_id) ?
session_id: Used for cloud-hosted multi-turn conversations, automatically maintaining the conversation context. Valid for 1 hour, with a maximum of 50 history rounds. Callers do not need to maintain the context themselves, but must pass in thesession_idof the previous round of conversation in the next round.memory_id: Used to create a long-term memory that stores specific information. You need to call the CreateMemory API to create it and obtainmemoryId. To reference specific information in subsequent conversations, pass inmemoryId.