Skip to main content
Invocation

Calling Agent Applications

You can integrate Alibaba Cloud Bailian agent applications into your business systems using either the DashScope SDK or HTTP requests.

Prerequisites

Before you begin, complete the following three steps to configure your development environment.
  1. Obtain Credentials
    • API Key: Go to the API Key Management page to create and retrieve your API Key.
    • Bailian Application ID (APP_ID): Go to the Application Management page to create an agent application, then copy its APP_ID from the application card.
  2. Install the DashScope SDK
    Skip this step if you're calling the HTTP API directly.
    Select and run the appropriate installation command for your programming language.
    # Install the SDK into your Python 3 environment
    python3 -m pip install -U dashscope
    
  3. Configure Environment Variables (Recommended) To ensure credential security and avoid hardcoding secrets in your source code, we recommend storing your API Key in an environment variable. The SDK will automatically read it from there.

Quick Start

import os
from http import HTTPStatus
from dashscope import Application

response = Application.call(
    # If environment variables are not configured, replace the line below with: api_key="sk-xxx". However, hardcoding your API Key in production code is strongly discouraged to minimize exposure risk.
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    app_id='APP_ID',  # Replace with your actual application ID
    prompt='Who are you?')

if response.status_code != HTTPStatus.OK:
    print(f'request_id={response.request_id}')
    print(f'code={response.status_code}')
    print(f'message={response.message}')
    print(f'Refer to documentation: https://help.aliyun.com/zh/model-studio/developer-reference/error-code')
else:
    print(response.output.text)
Sample Response
{
    "output": {
        "finish_reason": "stop",
        "session_id": "232ea2e9e6ef448db6b14465c06a9a56",
        "text": "I am a large-scale language model developed by Alibaba Cloud, named Qwen. I am an AI assistant capable of answering questions, generating text, expressing opinions, and writing code. If you have any questions or need assistance, feel free to let me know—I’ll do my best to help!"
    },
    "usage": {
        "models": [{"output_tokens": 51, "model_id": "qwen-max", "input_tokens": 121}]
    },
    "request_id": "661c9cad-e59c-9f78-a262-78eff243f151"
}

Multi-turn Conversations

Compared to single-turn interactions, multi-turn conversations allow the large language model to reference prior conversation history—making them more natural and aligned with real-world dialogue scenarios.
import os
from http import HTTPStatus
from dashscope import Application

def call_with_session():
    response = Application.call(
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        app_id='APP_ID',  # Replace with your actual application ID
        prompt='Who are you?')

    if response.status_code != HTTPStatus.OK:
        print(f'request_id={response.request_id}')
        print(f'code={response.status_code}')
        print(f'message={response.message}')
        return response

    responseNext = Application.call(
                api_key=os.getenv("DASHSCOPE_API_KEY"),
                app_id='APP_ID',  # Replace with your actual application ID
                prompt='What skills do you have?',
                session_id=response.output.session_id)  # Use session_id from previous response

    if responseNext.status_code != HTTPStatus.OK:
        print(f'request_id={responseNext.request_id}')
        print(f'code={responseNext.status_code}')
        print(f'message={responseNext.message}')
    else:
        print('%s\n session_id=%s\n' % (responseNext.output.text, responseNext.output.session_id))

if __name__ == '__main__':
    call_with_session()
Sample Response
I possess a wide range of capabilities to assist you with various tasks. Here are some key areas:

1. **Information Retrieval**: Providing weather forecasts, news updates, historical facts, scientific knowledge, etc.
2. **Language Processing**: Translating text, correcting grammar, generating articles and stories.
3. **Technical Support**: Answering programming questions, guiding software usage, troubleshooting technical issues.
4. **Academic Tutoring**: Assisting with math, physics, chemistry, and other subject-related queries.
5. **Life Advice**: Offering guidance on health, nutrition, travel planning, shopping recommendations, and more.

 session_id=98ceb3ca0c4e4b05a20a00f913050b42