Skip to main content

Custom parameter passing for applications

This topic describes how to use the custom parameter passing feature in agent applications and workflow applications (agent orchestration applications have been replaced by workflow applications) in Alibaba Cloud Model Studio. It is mainly applicable to parameter passing for custom plug-ins and custom nodes.

You must have obtained and configured an API Key and configured the API Key in environment variables. If you call through an SDK, you also need to install the DashScope SDK. Replace DASHSCOPE_API_HOST in the sample code with the obtained API Host.

Custom plug-in parameter passing

This topic uses the dormitory convention content query tool as an example to show you how to call the custom plug-in parameter passing feature of an application via API.
Parameters of a custom plug-in are passed through the associated agent application, or through a plug-in node in a workflow application.
The following example shows how to pass custom plug-in parameters in an agent application.

How to use

Step 1: Create a custom plug-in tool

If you have already created a plug-in tool or imported a plug-in, skip this step.
  1. Create a custom plug-in: Visit the Plug-in page in the Model Studio console, click New custom plug-in, and fill in the plug-in information. If authentication is required, turn on the Enable Authentication switch and fill in the authentication configuration information.
    Example plug-in description: Dormitory convention query tool, which can query a specific entry by the input numeric index.
    Example plug-in URL: https://domitorgreement-plugin-example-icohrkdjxy.cn-beijing.fcapp.run
    Example authentication configuration: Select User-level Authentication as the authentication type, Header as the location, and basic as the Type. Note: The plug-in description is a brief description of the plug-in's purpose, which helps the large model determine whether to call the current plug-in for the current task. Describe it in natural language.
  2. Create a tool: Fill in the tool information and configure the input and output parameters. Note:
    1. The tool description helps the large model better understand the tool's features and usage scenarios. Describe it in natural language and provide usage examples where possible.
    2. Use meaningful parameter names to help the large model understand what parameter information needs to be identified.
    3. The parameter description describes the function of the input parameter. Keep it concise and accurate to help the large model further understand how to obtain parameters.
    4. For input parameters, be sure to select Business Pass-through as the passing method.
    In this example, the dormitory convention content index article_index is set as a business pass-through parameter. Fill in the tool name as Dormitory convention query tool, select POST as the request method, and application/json as the submission method. Set the type of the input parameter article_index to Number, the passing method to Body, and mark it as required. For the output parameter, fill in article as the name and "Dormitory convention content" as the description, and set the type to String.
  3. Click Test Tool, and after the run passes, publish the plug-in.

Step 2: Associate the specified plug-in with an agent application

Plug-in tools can only be associated with agent applications in the same business space.
  1. On the published plug-in card, click Add to Agent and select the agent application to associate;
  2. You can also click + Plug-in within the application to associate the specified custom plug-in;
  3. Then publish the application directly.

Step 3: API calling

  • When authentication is not required: Call the custom plug-in via API, use user_defined_params of biz_params to pass the custom plug-in information, replace your_plugin_code with the actual plug-in ID, and pass the key-value pairs of the input parameters configured in the plug-in.
    The plug-in ID can be obtained from the plug-in card.
    In this example, the value 2 is passed as the article_index parameter (dormitory convention index) to query the second dormitory convention, and the correct result is returned.
    • Python
    • Java
    • HTTP
    Request example
    import os
    import dashscope
    # dashscope 2.0.1 or later is required.
    from dashscope import DashScopeError
    
    # The following is the configuration for the Singapore (ap-southeast-1) region. The region value varies by region.
    client = dashscope.Client(
            # If the environment variable is not set, you can use your Model Studio API key by replacing the following line with api_key="sk-xxx". We recommend using an environment variable in production to avoid leaking your API key.
            api_key=os.getenv("DASHSCOPE_API_KEY"),
            region="ap-southeast-1")
    biz_params = {
        # Pass the input parameters for the agent application's custom plugin. Replace `your_plugin_code` with your plugin ID.
        "user_defined_params": {
            "your_plugin_code": {
                "article_index": 2}}}
    try:
        response = client.apps.completions.create(
                app_id='YOUR_APP_ID',
                prompt='寝室公约内容',
                biz_params=biz_params)
        print('%s\n' % (response.text))  # Process and output only the 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'See the documentation: https://www.alibabacloud.com/help/model-studio/developer-reference/error-code')
    
    Response example
    寝室公约的第二条规定如下:
    
    "寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。"
    
    这表明在寝室内,成员之间应该培养一种积极正面的生活和学习氛围,彼此帮助和支持,同时也要学会理解和尊重他人。如果您需要了解公约的其他条款,请告诉我!
    
  • When authentication is required: Call the custom plug-in through the API. The authentication switch is enabled in the plug-in, and user-level authentication is configured.
    Obtain the plug-in ID from the plug-in card.
    • Use user_defined_params of biz_params to pass custom plug-in information, replace your_plugin_code with the actual plug-in ID, and pass in the key-value pairs of the input parameters configured in the plug-in.
    • Use user_defined_tokens of biz_params to pass related information, replace your_plugin_code with the actual plug-in ID, and fill in the authentication information as the value of the user_token parameter, such as the actual value of DASHSCOPE_API_KEY.
    • After authentication passes, query the specific entry based on the passed index parameters and return the correct result.
    In this example, the value 2 is passed in as the article_index parameter for the dormitory convention index. Replace the value YOUR_TOKEN of user_token with the actual value of DASHSCOPE_API_KEY. After authentication passes, the second dormitory convention is queried and the correct result is returned.
    • Python
    • Java
    • HTTP
    Request example
    import os
    import dashscope
    # dashscope 2.0.1 or later is required.
    from dashscope import DashScopeError
    
    # The following is the configuration for the Singapore (ap-southeast-1) region. The region value varies by region.
    client = dashscope.Client(
            # If the environment variable is not set, you can use your Model Studio API key by replacing the following line with api_key="sk-xxx". We recommend using an environment variable in production to avoid leaking your API key.
            api_key=os.getenv("DASHSCOPE_API_KEY"),
            region="ap-southeast-1")
    biz_params = {
        # Pass the authentication information for the agent application's custom plugin. Replace `your_plugin_code` with your plugin ID and replace `YOUR_TOKEN` with the authentication information, such as an API key.
        "user_defined_params": {
            "your_plugin_code": {
                "article_index": 2}},
        "user_defined_tokens": {
            "your_plugin_code": {
                "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))  # Process and output only the 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'See the documentation: https://www.alibabacloud.com/help/model-studio/developer-reference/error-code')
    
    Response example
    寝室公约的第二条规定如下:
    
    寝室成员应当互帮互助、互相关心、互相学习、共同提高;宽容谦让、相互尊重、以诚相待。
    
    如果您需要了解更多的规定内容,请告诉我。
    

Custom node parameter passing

This article uses querying the administrative divisions of a city by city name as an example to show you how to call the custom node parameter passing feature of an application through the API.
Custom node parameters of an application are passed through the start node of a workflow application.
The following example shows how to pass the custom parameters of the start node in a workflow application.

How to use

Step 1: Custom node parameters

Open the target application in Workflows. In the Start node, add a String variable named city. Insert city and query into the prompt, then publish the application.

Step 2: API calls

When calling, pass city through the biz_params field and query through the prompt field.
  • Python
  • Java
  • HTTP
Request example
import os
import dashscope
from dashscope import DashScopeError

# The following is the configuration for the Singapore (ap-southeast-1) region. The region value varies by region.
client = dashscope.Client(
    # If you have not configured an environment variable, you can use your Model Studio API key by replacing the following line with: api_key="sk-xxx".
    # We recommend that you do not hardcode the API key in your code in a production environment to reduce the risk of API key leaks.
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    region="ap-southeast-1")
# Pass custom parameters for a workflow application.
biz_params = {"city": "杭州"}
try:
    response = client.apps.completions.create(
        app_id='YOUR_APP_ID',  # Replace with your actual application ID.
        prompt='查询这个城市的行政区域划分',
        biz_params=biz_params  # Pass business parameters.
    )
    print(f'{response.text}')  # Process and output only the text.
except DashScopeError as e:
    print(f'request_id={e.request_id}')
    print(f'code={e.code}')
    print(f'message={e.message}')
    print(f'See the documentation: https://www.alibabacloud.com/help/model-studio/developer-reference/error-code')
Response example
杭州市,作为浙江省的省会城市,其行政区域划分包括10个市辖区:上城区、拱墅区、西湖区、滨江区、萧山区、余杭区、临平区、钱塘区、富阳区、临安区。每个区都有其独特的特色和发展重点。

- 上城区:位于杭州市中心地带,是杭州的政治、经济、文化中心之一。
- 拱墅区:以运河文化为特色,拥有众多历史文化遗产。
- 西湖区:著名的西湖风景区就位于此区,是旅游观光的重要目的地。
- 滨江区:高新技术产业聚集地,阿里巴巴等知名企业坐落于此。
- 萧山区:东南部的一个行政区,经济发展迅速,特别是制造业方面。
- 余杭区:近年来发展快速,尤其是互联网经济领域,阿里巴巴总部也设在这里(注:阿里巴巴总部实际位于滨江区)。
- 临平区:新成立的行政区,旨在促进该地区经济社会全面发展。
- 钱塘区:同样是一个较新的行政区划调整结果,强调创新发展和生态保护相结合。
- 富阳区:位于杭州西南方向,以其丰富的自然景观和悠久的历史文化著称。
- 临安区:地处杭州西部,以生态优美闻名,并且有着深厚的文化底蕴。

请注意,随着时间推移,具体的城市规划可能会有所变化,请参考最新的官方信息。
Custom plug-ins: Steps for creating a custom plug-in. Application calling: How to call applications and more usage. Application calling API: Complete parameter list and calling examples.