Skip to main content
Application Calling

Call applications

You can integrate Model Studio applications—such as agents, workflows, or agent orchestrations—into your business systems using the DashScope SDK or HTTP.

Prerequisites

You can use the DashScope SDK or an HTTP interface to call a Model Studio application.
Regardless of the calling method, you should configure the API key as an environment variable. If you use the DashScope SDK, you must also install the DashScope SDK.

Usage

Single-round conversation

Sample code for the DashScope SDK or HTTP method to implement single-round conversation.
  • Python
  • Java
  • HTTP
Sample request
import os
from http import HTTPStatus
from dashscope import Application
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
response = Application.call(
    # If environment variables are not configured, you can replace the following line with api_key="sk-xxx". However, it is not recommended to hard code the API Key directly into the code in a production environment to reduce the risk of API Key leakage.
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    app_id='YOUR_APP_ID',# Replace with the 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: https://www.alibabacloud.com/help/en/model-studio/developer-reference/error-code')
else:
    print(response.output.text)
Sample response
I am a large language model developed by Alibaba Cloud, named Qwen. I am designed to help users generate various types of text, such as articles, stories, poems, stories, etc., and can be adjusted and optimized according to different scenarios and needs. In addition, I can also answer various questions, provide information and explanations, and assist in learning and research. If you have any needs, please feel free to ask me questions at any time!

Multi-round conversation

In multi-round conversations, the LLM can reference the conversation history, making it more similar to everyday communication scenarios.
Currently, only agent applications and dialog workflow applications support multi-round conversation.
  • When you pass in session_id, the request automatically carries the conversation history stored in the cloud.
    When passing in session_id, prompt is required.
  • You can also choose to maintain a messages array. Add each round of conversation history and new instructions to the messages array. Then, pass the history through messages.
    When passing in messages, prompt is optional. If both are passed in, prompt will be appended to the end of messages as supplementary information.
If both session_id and messages are passed in, messages will be used preferentially.
  • Cloud storage (session_id)
  • Self-management (messages)
  • Python
  • Java
  • HTTP
Sample request
import os
from http import HTTPStatus
from dashscope import Application
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'

def call_with_session():
    response = Application.call(
        # If environment variables are not configured, you can replace the following line with api_key="sk-xxx". However, it is not recommended to hard-code the API Key directly into the code in a production environment to reduce the risk of API Key leakage.
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        app_id='YOUR_APP_ID',  # Replace with the 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: https://www.alibabacloud.com/help/en/model-studio/developer-reference/error-code')
        return response

    responseNext = Application.call(
                # If environment variables are not configured, you can replace the following line with api_key="sk-xxx". However, it is not recommended to hard-code the API Key directly into the code in a production environment to reduce the risk of API Key leakage.
                api_key=os.getenv("DASHSCOPE_API_KEY"),
                app_id='YOUR_APP_ID',  # Replace with the actual application ID
                prompt='What skills do you have?',
                session_id=response.output.session_id)  # session_id from the 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}')
        print(f'Refer to: https://www.alibabacloud.com/help/en/model-studio/developer-reference/error-code')
    else:
        print('%s\n session_id=%s\n' % (responseNext.output.text, responseNext.output.session_id))
        # print('%s\n' % (response.usage))

if __name__ == '__main__':
    call_with_session()
Sample response
I have multiple skills and can assist you with various tasks. Here are some of my main skills:

1. **Information retrieval**: Providing weather, news, historical facts, scientific knowledge, and various other information.
2. **Language processing**: Translating text, correcting grammar errors, generating articles and stories.
3. **Technical problem solving**: Answering programming questions, software usage, technical troubleshooting, etc.
4. **Educational assistance**: Helping with questions in subjects like mathematics, physics, chemistry, etc.
5. **Life advice**: Providing advice on health, diet, travel, shopping, etc.
6. **Entertainment interaction**: Telling jokes, playing word games, engaging in simple chat interactions.
7. **Schedule management**: Reminding important dates, arranging schedules, setting reminders.
8. **Data analysis**: Explaining data charts, providing data analysis suggestions.
9. **Emotional support**: Listening to your feelings, providing comfort and support.

If you have specific needs or questions, you can tell me directly, and I'll do my best to help you!
 session_id=98ceb3ca0c4e4b05a20a00f913050b42

Pass custom parameters

To adapt the same agent or workflow to different business scenarios, you can configure custom parameters for plug-ins or nodes, and pass parameters through biz_params when calling the application. For how to configure the parameters, see Application parameter pass-through. Sample code:
  1. Custom plugins parameters: Pass through the associated Agent Application or through the Plug-in Node of the associated Workflow Application. You can pass parameter and user-level authentication information for custom plug-ins:
    • Parameters: user_defined_params.
    • User-level authentication: user_defined_tokens. user_token is the authentication information required by the plug-in, such as the DASHSCOPE_API_KEY.
    The following sample is an Agent Application that requires the index parameter and user-level authentication information of the associated plug-in.
    Plug-in tools can only be associated with Agent Applications in the same workspace.
    Replace your_plugin_code with the associated plug-in tool ID displayed on the plug-in card, and pass the key-value pairs of input parameters. In this example, article_index with a value of 2.
    • Parameter passing
    • User-level authentication
    • Python
    • Java
    • HTTP
    Sample request
    import os
    from http import HTTPStatus
    # Recommended dashscope SDK version >= 1.14.0
    from dashscope import Application
    import dashscope
    dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
    biz_params = {
        # Custom plug-in input parameter passing for agent applications, replace your_plugin_code with your custom plug-in ID
        "user_defined_params": {
            "your_plugin_code": {
                "article_index": 2}}}
    response = Application.call(
            # If environment variables are not configured, you can replace the following line with api_key="sk-xxx". However, it is not recommended to hard code the API Key directly into the code in a production environment to reduce the risk of API Key leakage.
            api_key=os.getenv("DASHSCOPE_API_KEY"),
            app_id='YOUR_APP_ID',
            prompt='Dormitory convention content',
            biz_params=biz_params)
    
    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: https://www.alibabacloud.com/help/en/model-studio/developer-reference/error-code')
    else:
        print('%s\n' % (response.output.text))  # Process text output only
        # print('%s\n' % (response.usage))
    
    Sample response
    The second rule of the Dormitory Convention states:
    
    "Dormitory members should help each other, care for each other, learn from each other, and improve together; be tolerant, humble, respect each other, and treat each other with sincerity."
    
    This indicates that within the dormitory, members should cultivate a positive atmosphere for living and studying, support and assist each other, and also learn to understand and respect one another. If you need to know about other clauses of the convention, please let me know!
    
  2. Custom node parameters: Pass through the Workflow Application's Start Node, or through the Agent Orchestration Application's Application Node. In the following sample, the Workflow Application defines a parameter city in the Start Node. Insert the variables city and query in the Prompt, and then Publish the application.
    image
    When calling, pass city through biz_params and pass query through prompt.
    • Python
    • Java
    • HTTP
    Sample request
    import os
    from http import HTTPStatus
    from dashscope import Application
    import dashscope
    dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
    # Custom parameter passing for workflow and agent orchestration applications
    biz_params = {"city": "Hangzhou"}
    response = Application.call(
        # If environment variables are not configured, you can replace the following line with api_key="sk-xxx". However, it is not recommended to hard code the API Key directly into the code in a production environment to reduce the risk of API Key leakage.
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        app_id='YOUR_APP_ID',  # Replace with the actual application ID
        prompt='Query the administrative divisions of this city',
        biz_params=biz_params  # Pass business parameters
    )
    
    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: https://www.alibabacloud.com/help/en/model-studio/developer-reference/error-code')
    else:
        print(f'{response.output.text}')  # Process text output only
    
    Sample response
    The city of Hangzhou, as the capital of Zhejiang Province, has an administrative division consisting of 10 districts: Shangcheng District, Gongshu District, Xihu District, Binjiang District, Xiaoshan District, Yuhang District, Linping District, Qiantang District, Fuyang District, and Lin'an District. Each district has its own unique characteristics and development focus.
    
    - Shangcheng District: Located in the central area of Hangzhou, it is one of the political, economic, and cultural centers of the city.
    - Gongshu District: Known for its canal culture, it features numerous historical and cultural heritage sites.
    - Xihu District: Famous for the West Lake scenic area, it is an important destination for tourism.
    - Binjiang District: A hub for high-tech industries, with renowned companies like Alibaba situated here.
    - Xiaoshan District: An administrative district in the southeast, experiencing rapid economic growth, particularly in the manufacturing sector.
    - Yuhang District: Has developed rapidly in recent years, especially in the field of internet economy; Alibaba's headquarters is also located here (Note: Alibaba headquarters is actually in Binjiang District).
    - Linping District: A newly established district aimed at promoting comprehensive economic and social development in the area.
    - Qiantang District: Also a result of recent administrative adjustments, focusing on the integration of innovation and ecological protection.
    - Fuyang District: Located southwest of Hangzhou, known for its rich natural landscapes and long history and culture.
    - Lin'an District: Situated west of Hangzhou, famous for its beautiful ecological environment and profound cultural heritage.
    
    Please note that city planning may change over time, and it is recommended to refer to the latest official information.
    

Streaming output

In streaming output mode, the model generates intermediate results, and the final result is formed by concatenating these intermediate results. You can read as the model outputs, thereby shortening the wait for the model's response. Depending on the calling method, you can set parameters to implement streaming output:
  • Python SDK: Set stream to True.
  • Java SDK: Call through the streamCall interface.
  • HTTP: Specify X-DashScope-SSE as enable in the Header.
By default, streaming output is non-incremental, meaning each return includes all previously generated content. To use incremental streaming output, set the incremental_output (incrementalOutput for Java) parameter to true. For HTTP, set incremental_output to true and place it in the parameters object.
Examples:
  • For Agent Application:
    • Python
    • Java
    • HTTP
    Sample request
    import os
    from http import HTTPStatus
    from dashscope import Application
    import dashscope
    dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
    responses = Application.call(
                # If environment variables are not configured, replace the following line with: api_key="sk-xxx". However, it is not recommended to hardcode the API Key directly into the code in a production environment to reduce the risk of API Key leakage.
                api_key=os.getenv("DASHSCOPE_API_KEY"),
                app_id='YOUR_APP_ID',
                prompt='Who are you?',
                stream=True,  # Streaming output
                incremental_output=True)  # Incremental output
    
    for response in responses:
        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'Please refer to the documentation: https://www.alibabacloud.com/help/en/model-studio/developer-reference/error-code')
        else:
            print(f'{response.output.text}\n')  # Process to output only the text
    
    Sample response
    I am
    
    Alibaba
    
    Cloud
    
    's large-scale language model
    
    , my name is
    
    Qwen.
    
  • Workflow Application provides two streaming output modes, determined by the value of flow_stream_mode. Parameter values and usage methods:
    • full_thoughts (default value):
      • Description: Streaming results of all nodes will be output in the thoughts field.
      • Requirement: You must also set has_thoughts to True.
    • agent_format:
      • Description: The same output mode as agent applications.
      • Effect: In the console, you can turn on the Response switch for a specific node, and the streaming results of that node will be included in the text field of output.
      • Scenario: Suitable for scenarios where you only care about the output of specific intermediate nodes.
      The Response switch is available only in Text Conversion nodes, LLM nodes, and End nodes (The switch is on for End nodes by default). Nodes that do not support streaming output will output their content all at once.
    Example:
    • full_thoughts
    • agent_format
    This is a published Workflow Application with streaming output.
    image
    • Python
    • Java
    • HTTP
    Sample request
    import os
    from http import HTTPStatus
    from dashscope import Application
    import dashscope
    dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
    biz_params = {
        "city": "Hangzhou"}
    responses = Application.call(
        # If environment variables are not configured, replace the following line with: api_key="sk-xxx". However, it is not recommended to hardcode the API Key directly into the code in a production environment to reduce the risk of API Key leakage.
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        # Replace with the actual application ID
        app_id='YOUR_APP_ID',
        prompt='Hello',
        biz_params=biz_params,
        # Enable streaming output
        stream=True,
        # incremental_output=true enables incremental output, false disables it, default is false if not specified
        incremental_output=True,
        # Need to set has_thoughts to True
        has_thoughts=True)
    
    for response in responses:
        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'Please refer to the documentation: https://www.alibabacloud.com/help/en/model-studio/developer-reference/error-code')
        else:
            print(f'{response.output.thoughts}\n')  # Process output to return only thoughts; process information is returned in the thoughts field of output
    
    Sample response
    [ApplicationThought(thought=None, action_type=None, response='{"nodeName":"Start","nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"0ms"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None), ApplicationThought(thought=None, action_type=None, response='{"nodeName":"LLM_99FA","nodeResult":"{\\"result\\":\\"\\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_99FA"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None)]
    
    [ApplicationThought(thought=None, action_type=None, response='{"nodeName":"Start","nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"0ms"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None), ApplicationThought(thought=None, action_type=None, response='{"nodeName":"LLM_1","nodeResult":"{\\"result\\":\\"Dongpo\\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_99FA"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None)]
    
    [ApplicationThought(thought=None, action_type=None, response='{"nodeName":"Start","nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"0ms"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None), ApplicationThought(thought=None, action_type=None, response='{"nodeName":"LLM_1","nodeResult":"{\\"result\\":\\" Pork, West Lake Vinegar Fish,\\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_99FA"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None)]
    
    [ApplicationThought(thought=None, action_type=None, response='{"nodeName":"Start","nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"0ms"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None), ApplicationThought(thought=None, action_type=None, response='{"nodeName":"LLM_1","nodeResult":"{\\"result\\":\\" Longjing Shrimp, Hangzhou Pa\\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_99FA"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None)]
    
    [ApplicationThought(thought=None, action_type=None, response='{"nodeName":"Start","nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"0ms"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None), ApplicationThought(thought=None, action_type=None, response='{"nodeName":"LLM_1","nodeResult":"{\\"result\\":\\"stry, Beggar\'s Chicken\\"}","nodeType":"LLM","nodeStatus":"success","nodeId":"LLM_99FA","nodeExecTime":"1027ms"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None), ApplicationThought(thought=None, action_type=None, response='{"nodeName":"LLM_qkYJ","nodeResult":"{\\"result\\":\\"\\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_qkYJ"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None)]
    
    [ApplicationThought(thought=None, action_type=None, response='{"nodeName":"Start","nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"0ms"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None), ApplicationThought(thought=None, action_type=None, response='{"nodeName":"LLM_1","nodeResult":"{\\"result\\":\\"\\"}","nodeType":"LLM","nodeStatus":"success","nodeId":"LLM_99FA","nodeExecTime":"1027ms"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None), ApplicationThought(thought=None, action_type=None, response='{"nodeName":"LLM_2","nodeResult":"{\\"result\\":\\"West Lake,\\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_qkYJ"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None)]
    
    [ApplicationThought(thought=None, action_type=None, response='{"nodeName":"Start","nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"0ms"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None), ApplicationThought(thought=None, action_type=None, response='{"nodeName":"LLM_1","nodeResult":"{\\"result\\":\\"\\"}","nodeType":"LLM","nodeStatus":"success","nodeId":"LLM_99FA","nodeExecTime":"1027ms"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None), ApplicationThought(thought=None, action_type=None, response='{"nodeName":"LLM_2","nodeResult":"{\\"result\\":\\" Lingyin Temple, Xixi\\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_qkYJ"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None)]
    
    [ApplicationThought(thought=None, action_type=None, response='{"nodeName":"Start","nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"0ms"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None), ApplicationThought(thought=None, action_type=None, response='{"nodeName":"LLM_1","nodeResult":"{\\"result\\":\\"\\"}","nodeType":"LLM","nodeStatus":"success","nodeId":"LLM_99FA","nodeExecTime":"1027ms"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None), ApplicationThought(thought=None, action_type=None, response='{"nodeName":"LLM_2","nodeResult":"{\\"result\\":\\" National Wetland Park, Hefang\\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_qkYJ"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None)]
    
    [ApplicationThought(thought=None, action_type=None, response='{"nodeName":"Start","nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"0ms"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None), ApplicationThought(thought=None, action_type=None, response='{"nodeName":"LLM_1","nodeResult":"{\\"result\\":\\"\\"}","nodeType":"LLM","nodeStatus":"success","nodeId":"LLM_99FA","nodeExecTime":"1027ms"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None), ApplicationThought(thought=None, action_type=None, response='{"nodeName":"LLM_2","nodeResult":"{\\"result\\":\\" Street, Hangzhou Botanical Garden\\"}","nodeType":"LLM","nodeStatus":"executing","nodeId":"LLM_qkYJ"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None)]
    
    [ApplicationThought(thought=None, action_type=None, response='{"nodeName":"Start","nodeType":"Start","nodeStatus":"success","nodeId":"Start_bYxoRU","nodeExecTime":"0ms"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None), ApplicationThought(thought=None, action_type=None, response='{"nodeName":"LLM_1","nodeResult":"{\\"result\\":\\"\\"}","nodeType":"LLM","nodeStatus":"success","nodeId":"LLM_99FA","nodeExecTime":"1027ms"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None), ApplicationThought(thought=None, action_type=None, response='{"nodeName":"LLM_2","nodeResult":"{\\"result\\":\\"\\"}","nodeType":"LLM","nodeStatus":"success","nodeId":"LLM_qkYJ","nodeExecTime":"1048ms"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None), ApplicationThought(thought=None, action_type=None, response='{"nodeName":"End","nodeResult":"{\\"result\\":\\"What do you think about our recommendation?\\"}","nodeType":"End","nodeStatus":"success","nodeId":"End_DrQn7F","nodeExecTime":"0ms"}', action_name=None, action=None, action_input_stream=None, action_input=None, observation=None)]
    
    • Each item in thoughts is the execution details of a node. Below is an example of an LLM node result.
    id:7
    event:result
    :HTTP_STATUS/200
    data:
    {
        "output": {
            "thoughts": [
                {
                    "response": "{\"nodeName\":\"Start\",\"nodeType\":\"Start\",\"nodeStatus\":\"success\",\"nodeId\":\"Start_bYxoRU\",\"nodeExecTime\":\"0ms\"}"
                },
                {
                    "response": "{\"nodeName\":\"LLM_1\",\"nodeResult\":\"{\\\"result\\\":\\\"Songcheng Fish Soup\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"success\",\"nodeId\":\"LLM_j45e\",\"nodeExecTime\":\"1167ms\"}"
                },
                {
                    "response": "{\"nodeName\":\"LLM_2Km9\",\"nodeResult\":\"{\\\"result\\\":\\\"\\\"}\",\"nodeType\":\"LLM\",\"nodeStatus\":\"executing\",\"nodeId\":\"LLM_2Km9\"}"
                }
            ],
            "session_id": "6035ee0814b64a9fb88346ecaf8b44bf",
            "finish_reason": "null"
        },
        "usage": {
            "models": [
                {
                    "input_tokens": 25,
                    "output_tokens": 21,
                    "model_id": "qwen-max"
                }
            ]
        },
        "request_id": "64825069-b3aa-93a7-bcf1-c66fe57111fd"
    }
    
    If you are interested in the streaming results of an LLM node (using LLM_j45e in the example above), you can focus on the output of the node with nodeId LLM_j45e in each push of thoughts.
    • If a node fails, the entire task will also fail.

Retrieve knowledge base

Knowledge base is the RAG capability of Model Studio. It can effectively supplement private and latest knowledge for models. You can specify the retrieval scope when calling Agent Applications to improve the accuracy of answers.

Before you go

In the Model Studio console, turn on the Knowledge Base Retrieval Augmentation for your Agent Application. Then, Publish the application.
Skip this prerequisite for RAG Applications.

Specify retrieval scope

  1. To retrieve a specified knowledge base, choose one of the following methods:
    • In the console, click Configure Knowledge Base in the application and select the specified knowledge base. Then, Publish the application.
    • Do not associate the specified knowledge base in the console. Pass the knowledge base ID through rag_options when making an API call;
    • Associate the specified knowledge base in the console, and pass the knowledge base ID through rag_options when making an API call.
      In this case, only the knowledge base passed during the call will be retrieved. For example, an Agent Application is associated with knowledge base A. However, you specify knowledge base B when making the API call. Then, knowledge base A will not be retrieved, and only knowledge base B will be retrieved.
    To obtain the knowledge base ID (pipeline_ids): You can get it on the Knowledge Base page, or use the Data.Id returned by the CreateIndex API. CreateIndex only supports unstructured knowledge bases. Bailian Phones Specifications.docx is used in the following samples as an unstructured knowledge base.
    • Python
    • Java
    • HTTP
    Sample request
    import os
    from http import HTTPStatus
    # Recommended dashscope SDK version >= 1.20.11
    from dashscope import Application
    import dashscope
    dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
    
    response = Application.call(
        # If environment variables are not configured, you can replace the following line with api_key="sk-xxx". However, it is not recommended to hard-code the API Key directly into the code in a production environment to reduce the risk of API Key leakage.
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        app_id='YOUR_APP_ID',  # Replace YOUR_APP_ID with the application ID
        prompt='Please recommend a mobile phone under 3000 yuan',
        rag_options={
            "pipeline_ids": ["YOUR_PIPELINE_ID1,YOUR_PIPELINE_ID2"],  # Replace with actual knowledge base IDs, separate multiple IDs with commas
        }
    )
    
    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: https://www.alibabacloud.com/help/en/model-studio/developer-reference/error-code')
    else:
        print('%s\n' % (response.output.text))  # Process text output only
        # print('%s\n' % (response.usage))
    
    Sample response
    Based on your budget, I recommend the **Bailian Zephyr Z9**. This phone is priced between 2499-2799 yuan, which fits your budget. It features a lightweight 6.4-inch 1080 x 2340 pixel screen design, paired with 128GB storage and 6GB RAM, suitable for daily use. Additionally, it has a 4000mAh battery and a lens that supports 30× digital zoom, which can meet your photography and battery life needs. If you're looking for a slim, portable phone with comprehensive features, the Bailian Zephyr Z9 would be a good choice.
    
  2. Retrieve specified unstructured documents: Pass the knowledge base ID, document ID, tags, or metadata (key-value pairs) in rag_options.
    Document ID, tags, and metadata are only effective for unstructureddocuments.
    • How to get them:
      • Document ID (file_ids): You can find it on the Application Data page, or use the ID returned by the AddFile API when importing documents.
      • Document tags: You can find the tags on the Application Data page, or get them from the DescribeFile API.
      • Document metadata: On the Knowledge Base page, click View to enter a knowledge base. Then, click Metadata Information.
    • You can specify multiple document IDs, but the documents must have been included in knowledge indexes.
    • When specifying document IDs, you must also specify the knowledge base ID to which the documents belong.
    • Only the specified documents will be retrieved. For example, an Agent Application associates knowledge base A, but the API call specifies documents of knowledge base B. Then, documents from A will not be retrieved, only documents from B will be retrieved. Bailian Phones Specifications.docx is used in the following samples as an unstructured knowledge base.
      • Python
      • Java
      • HTTP
      Sample request
      import os
      from http import HTTPStatus
      # Recommended dashscope SDK version >= 1.20.11
      from dashscope import Application
      import dashscope
      dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
      
      response = Application.call(
          # If environment variables are not configured, you can replace the following line with api_key="sk-xxx". However, it is not recommended to hard-code the API Key directly into the code in a production environment to reduce the risk of API Key leakage.
          api_key=os.getenv("DASHSCOPE_API_KEY"),
          app_id='YOUR_APP_ID',  # Replace YOUR_APP_ID with application ID
          prompt='Please recommend a mobile phone under 3000 yuan',
          rag_options={
              "pipeline_ids": ["YOUR_PIPELINE_ID1", "YOUR_PIPELINE_ID2"],  # Replace with actual knowledge base IDs, separate multiple with commas
              "file_ids": ["YOUR_FILE_ID1", "YOUR_FILE_ID2"],  # Replace with actual unstructured document IDs, separate multiple with commas
              "metadata_filter": {  # Document metadata key-value pairs, separate multiple with commas
                  "key1": "value1",
                  "key2": "value2"
              },
              "tags": ["tag1", "tag2"]  # Document tags, separate multiple with commas
          }
      )
      
      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: https://www.alibabacloud.com/help/en/model-studio/developer-reference/error-code')
      else:
          print('%s\n' % (response.output))
      
      Sample response
      {
          "text": "Within a budget of under 3000 yuan, I recommend you consider the **Bailian Zephyr Z9**. This phone has the following features:
      
      - **Screen**: 6.4-inch 1080 x 2340 pixels, suitable for daily use and entertainment.
      - **Memory and storage**: 6GB RAM + 128GB storage space, which can meet most users' needs for smoothness and storage.
      - **Battery capacity**: 4000mAh, providing all-day usage guarantee.
      - **Camera function**: Equipped with a lens supporting 30× digital zoom, capable of capturing details from greater distances.
      - **Other features**: Lightweight and portable design, easy to carry.
      
      The reference price is between 2499 and 2799 yuan, which perfectly fits your budget requirements and offers good value for money. Hope these suggestions are helpful!",
          "finish_reason": "stop",
          "session_id": "10bdea3d1435406aad8750538b701bee",
          "thoughts": null,
          "doc_references": null
      }
      
  3. Retrieve specified data from structured documents: Pass the knowledge base ID and "structured data header + value" key-value pairs in rag_options. Get structured data key-value pairs (structured_filter): On the Knowledge Base page, click View to enter a knowledge base. Then, click View Index.
    • Python
    • Java
    • HTTP
    Sample request
    import os
    from http import HTTPStatus
    # Recommended dashscope SDK version >= 1.20.11
    from dashscope import Application
    import dashscope
    dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
    
    response = Application.call(
        # If environment variables are not configured, you can replace the following line with api_key="sk-xxx". However, it is not recommended to hard-code the API Key directly into the code in a production environment to reduce the risk of API Key leakage.
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        app_id='YOUR_APP_ID',  # Replace YOUR_APP_ID with application ID
        prompt='Please recommend a mobile phone under 3000 yuan',
        rag_options={
            "pipeline_ids": ["YOUR_PIPELINE_ID1", "YOUR_PIPELINE_ID2"],  # Replace with actual knowledge base IDs, separate multiple with commas
             "structured_filter": {  # Structured data key-value pairs, corresponding to structured data, separate multiple with commas
                "key1": "value1",
                "key2": "value2"
             }
        }
    )
    
    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: https://www.alibabacloud.com/help/en/model-studio/developer-reference/error-code')
    else:
        print('%s\n' % (response.output))
    
    Sample response
    {
        "text": "I recommend the \"Bailian\" phone, which is priced at 2999 yuan, fitting your budget requirement. If you need to know more information, such as performance, appearance, etc., please let me know.",
        "finish_reason": "stop",
        "session_id": "80a3b868b5ce42c8a12f01dccf8651e2",
        "thoughts": null,
        "doc_references": null
    }
    
View information

View retrieval process: When making a call, add has_thoughts to the code and set it to True. The retrieval process will be returned in the thoughts field of output.

View answer source: Turn on Show Source in the Retrieval Configuration of the Agent Application and publish the application.
image

API reference

For a full list of parameters, see Workflow and legacy agent application API reference for DashScope.

Error codes

If a call fails, see Error codes for troubleshooting.

References

FAQ

  1. Ensure the class and package names in your import statements are correct.
  2. Add the dependency: If you use Maven or Gradle, ensure the DashScope Java SDK dependency is in your pom.xml or build.gradle file and is the latest version. You can find the latest version number of the DashScope Java SDK on Maven.
<!-- https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dashscope-sdk-java</artifactId>
    <version>Enter the latest version, e.g., 2.16.4</version>
</dependency>
// https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java
implementation group: 'com.alibaba', name: 'dashscope-sdk-java', version: 'Enter the latest version, e.g., 2.16.4'
  1. Upgrade the SDK: An older version of the DashScope Java SDK might lack the features or classes you need. If your version is outdated, upgrade it by modifying the version number in your pom.xml or build.gradle file.
<!-- https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dashscope-sdk-java</artifactId>
    <version>Replace this with the latest version number</version>
</dependency>
// https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java
implementation group: 'com.alibaba', name: 'dashscope-sdk-java', version: 'Replace this with the latest version number'
  1. Reload the project to apply the changes.
  2. Rerun the code sample. If the issue persists, check the developer forum for similar issues and their solutions, or submit a ticket.