All Products
Search
Document Center

Alibaba Cloud Model Studio:Make your first API call to Qwen

Last Updated:Nov 22, 2024

You can use API operations to call models in Alibaba Cloud Model Studio. Model Studio supports multiple methods, including OpenAI-compatible interfaces and DashScope SDK.

Note

If you already know how to call a model, you can go to Qwen directly.

This topic uses Qwen as an example to guide you through your first API call. You will learn how to:

  • Obtain an API Key.

  • Set up the development environment.

  • Make an API call to Qwen.

Manage account

  1. Register an account: If you do not have an Alibaba Cloud account, you must first register for one.

  2. Activate Model Studio: Go to the Alibaba Cloud Model Studio Console. Follow the on-screen instructions to activate model services.

  3. Obtain an API key: In the upper right corner of the console, select API-KEY. On the page that appears, create an API key.

    image

Set API key as environment variable

We recommend that you set the API key as an environment variable to avoid explicitly specifying it in the code, thus reducing the risk of leaks.

Steps

Linux

Permanent environment variable

To configure your API key as a permanent environment variable for the current user to use in new sessions, set it as a permanent environment variable.

  1. Run the following command to add the environment variable to the ~/.bashrc file:

    # Replace YOUR_DASHSCOPE_API_KEY with your API key.
    echo "export DASHSCOPE_API_KEY='YOUR_DASHSCOPE_API_KEY'" >> ~/.bashrc

    You can also edit the ~/.bashrc file manually.

    Edit manually

    Run the following command to open the ~/.bashrc file.

    nano ~/.bashrc

    Add the following content to the file.

    # Replace YOUR_DASHSCOPE_API_KEY with your API key.
    export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"

    In the nano editor, press Ctrl+X and then Y. Press Enter to save and close the file.

  2. Run the following command to make the change take effect:

    source ~/.bashrc
  3. Create a session and run the following command to check whether the environment variable takes effect:

    echo $DASHSCOPE_API_KEY

Temporary environment variable

To use your API key as a temporary environment variable only for the current session, set it as a temporary environment variable.

  1. Run the following command:

    # Replace YOUR_DASHSCOPE_API_KEY with your API key.
    export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"
  2. Run the following command to check whether the environment variable takes effect:

    echo $DASHSCOPE_API_KEY

macOS

Permanent environment variable

To configure your API key as a permanent environment variable for the current user to use in new sessions, set it as a permanent environment variable.

  1. Run the following command to check the default shell type.

    echo $SHELL
  2. Perform the following operation based on your shell type.

    Zsh
    1. Run the following command to add the environment variable to the ~/.zshrc file.

      # Replace YOUR_DASHSCOPE_API_KEY with your API key.
      echo "export DASHSCOPE_API_KEY='YOUR_DASHSCOPE_API_KEY'" >> ~/.zshrc

      You can also edit the ~/.zshrc file manually.

      Edit manually

      Run the following command to open the configuration file.

      nano ~/.zshrc

      Add the following content to the file.

      # Replace YOUR_DASHSCOPE_API_KEY with your API key.
      export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"

      In the nano editor, press Ctrl+X and then Y. Press Enter to save and close the file.

    2. Run the following command to make the change take effect:

      source ~/.zshrc
    3. Create a session and run the following command to check whether the environment variable takes effect:

      echo $DASHSCOPE_API_KEY
    Bash
    1. Run the following command to add the environment variable to the ~/.bash_profile file.

      # Replace YOUR_DASHSCOPE_API_KEY with your API key.
      echo "export DASHSCOPE_API_KEY='YOUR_DASHSCOPE_API_KEY'" >> ~/.bash_profile

      You can also edit the ~/.bash_profile file manually.

      手动修改

      Run the following command to open the file.

      nano ~/.bash_profile

      Add the following content to the file.

      # Replace YOUR_DASHSCOPE_API_KEY with your API key.
      export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"

      In the nano editor, press Ctrl+X and then Y. Press Enter to save and close the file.

    2. Run the following command to make the change take effect:

      source ~/.bash_profile
    3. Create a session and run the following command to check whether the environment variable takes effect:

      echo $DASHSCOPE_API_KEY

Temporary environment variable

To use your API key as a temporary environment variable only for the current session, set it as a temporary environment variable.

  1. Run the following command:

    # Replace YOUR_DASHSCOPE_API_KEY with your API key.
    export DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"
  2. Run the following command to check whether the environment variable takes effect:

    echo $DASHSCOPE_API_KEY

Windows

In Windows, you can run commands by using Command Line or PowerShell.

Command Line

Permanent environment variable

To configure your API key as a permanent environment variable for the current user to use in new sessions, set it as a permanent environment variable.

  1. Run the following command:

    # Replace YOUR_DASHSCOPE_API_KEY with your API key.
    setx DASHSCOPE_API_KEY "YOUR_DASHSCOPE_API_KEY"
  2. Create a new session.

  3. Run the following command to check whether the environment variable takes effect:

    echo %DASHSCOPE_API_KEY%
Temporary environment variable

To use your API key as a temporary environment variable only for the current session, set it as a temporary environment variable.

  1. Run the following command:

    # Replace YOUR_DASHSCOPE_API_KEY with your API key.
    set DASHSCOPE_API_KEY="YOUR_DASHSCOPE_API_KEY"
  2. Run the following command in the current session to check whether the environment variable takes effect:

    echo %DASHSCOPE_API_KEY%

PowerShell

Permanent environment variable

To configure your API key as a permanent environment variable for the current user to use in new sessions, set it as a permanent environment variable.

  1. Run the following command:

    # Replace YOUR_DASHSCOPE_API_KEY with your API key.
    [Environment]::SetEnvironmentVariable("DASHSCOPE_API_KEY", "YOUR_DASHSCOPE_API_KEY", [EnvironmentVariableTarget]::User)
  2. Create a session.

  3. Run the following command to check whether the environment variable takes effect:

    echo $env:DASHSCOPE_API_KEY
Temporary environment variable

To use your API key as a temporary environment variable only for the current session, set it as a temporary environment variable.

  1. Run the following command:

    # Replace YOUR_DASHSCOPE_API_KEY with your API key.
    $env:DASHSCOPE_API_KEY = "YOUR_DASHSCOPE_API_KEY"
  2. Run the following command in the current session to check whether the environment variable takes effect:

    echo $env:DASHSCOPE_API_KEY

Select development language

Choose the programming language or tool with which you are most comfortable.

Python

Step 1: Configure Python environment

Check Python version

Run the following command in the terminal to check whether Python is installed:

# If the run fails, replace 'python' with 'python3' and try again
python -V

Make sure that you have Python version 3.8 or higher. If Python is not installed or the version is below 3.8, see Install Python.

Configure virtual environment (optional)

After Python is installed, you can set up a virtual environment to manage dependencies for OpenAI Python SDK or DashScope Python SDK. The virtual environment can avoid conflicts with your other projects.

  1. Create a virtual environment

    Run the following command to create a virtual environment named .venv:

    # If the run fails, replace 'python' with 'python3' and try again
    python -m venv .venv
  2. Activate the virtual environment

    For Windows systems, use the following command:

    .venv\Scripts\activate

    For macOS or Linux systems, use the following command:

    source .venv/bin/activate

Install SDK

Install OpenAI Python SDK or DashScope Python SDK to call models in Model Studio.

Install OpenAI Python SDK

Run the following command to install OpenAI Python SDK:

pip3 install -U openai

Install DashScope Python SDK

Run the following command to install DashScope Python SDK:

pip3 install -U dashscope

Step 2: Call the API

OpenAI Python SDK

After installing Python and OpenAI Python SDK, you can use the following sample code to make your API request. Create a Python file hello_qwen.py, copy the following sample into hello_qwen.py, and save the file.

import os
from openai import OpenAI

try:
    client = OpenAI(
        # Replace the next line with your API Key if you did not configure the environment variable: api_key="sk-xxx",
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
    )

    completion = client.chat.completions.create(
        model="qwen-plus",  # For a list of models, see https://www.alibabacloud.com/help/en/model-studio/getting-started/models
        messages=[
            {'role': 'system', 'content': 'You are a helpful assistant.'},
            {'role': 'user', 'content': 'Who are you?'}
            ]
    )
    print(completion.choices[0].message.content)
except Exception as e:
    print(f"Error message: {e}")
    print("For more information, see https://www.alibabacloud.com/help/en/model-studio/developer-reference/error-code.")

After the file is saved, run python hello_qwen.py or python3 hello_qwen.py. Sample response:

I am a large language model created by Alibaba Cloud. I'm called Qwen.

DashScope Python SDK

After installing Python and DashScope Python SDK, you can use the following sample code to make your API request. Create a Python file hello_qwen.py, copy the following sample into hello_qwen.py, and save the file.

import os
import dashscope
from dashscope import Generation

dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'

messages = [
    {'role': 'system', 'content': 'You are a helpful assistant.'},
    {'role': 'user', 'content': 'Who are you?'}
    ]
response = Generation.call(
    # Replace the next line with your Bailian API Key if the environment variable is not set: api_key = "sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"), 
    model="qwen-plus",   # For a list of models, see https://www.alibabacloud.com/help/en/model-studio/getting-started/models
    messages=messages,
    result_format="message"
)

if response.status_code == 200:
    print(response.output.choices[0].message.content)
else:
    print(f"HTTP return code: {response.status_code}")
    print(f"Error code: {response.code}")
    print(f"Error message: {response.message}")
    print("For more information, see .")

After the file is saved, run python hello_qwen.py or python3 hello_qwen.py. Sample response:

I am a large language model created by Alibaba Cloud. I'm called Qwen.

Node.js

Step 1: Configure Node.js environment

Check Node.js

Run the following command in the terminal to check whether Node.js is installed:

node -v

This command displays the installed version of Node.js. If Node.js is not installed, you can download it from the Node.js official website.

Install SDK

Run the following command in the terminal to install the SDK:

npm install --save openai
# Or
yarn add openai

If the installation fails, you can run the following command to configure a repository mirror:

npm config set registry https://registry.npmmirror.com/

Then, you can try install the SDK again.

Step 2: Call the model

Create a new file hello_qwen.mjs and copy the following sample code into it.

import OpenAI from "openai";

try {
    const openai = new OpenAI({
        // If you did not set the environment variable, replace the following line with your API Key: apiKey: "sk-xxx",
        apiKey: process.env.DASHSCOPE_API_KEY,
        baseURL: "https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
    });
    const completion = await openai.chat.completions.create({
        model: "qwen-plus",  // For a list of models, see: https://www.alibabacloud.com/help/en/model-studio/getting-started/models
        messages: [
            { role: "system", content: "You are a helpful assistant." },
            { role: "user", content: "Who are you?" }
        ],
    });
    console.log(completion.choices[0].message.content);
} catch (error) {
    console.error(`Error message: ${error}`);
}

Run the following command in the command line to send the API request:

node hello_qwen.mjs

Sample response:

I am Qwen, a large language model developed by Alibaba Cloud.

Java

Step 1: Configure Java environment

Check Java version

Run the following command in the terminal:

java -version

The DashScope Java SDK requires Java 8 or higher. The first line of the output shows your Java version. For example, openjdk version "16.0.1" 2021-04-20 shows that the version is Java 16. If Java is not installed, or if the installed version is earlier than Java 8, go to Java Downloads to download and install the appropriate version.

Install SDK

After you install Java, you can then install DashScope Java SDK. For more information about the SDK versions, see DashScope Java SDK. Run the following command to include the Java SDK dependency in your project. Replace the-latest-version with the latest version number.

<!-- https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dashscope-sdk-java</artifactId>
    <version>the-latest-version</version>
</dependency>
// https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java
implementation group: 'com.alibaba', name: 'dashscope-sdk-java', version: 'the-latest-version'

Step 2: Call the model

To call the model, execute the following code.

import java.util.Arrays;
import java.lang.System;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.protocol.Protocol;
public class Main {
    public static GenerationResult callWithMessage() throws ApiException, NoApiKeyException, InputRequiredException {
        Generation gen = new Generation(Protocol.HTTP.getValue(), "https://dashscope-intl.aliyuncs.com/api/v1");
        Message systemMsg = Message.builder()
                .role(Role.SYSTEM.getValue())
                .content("You are a helpful assistant.")
                .build();
        Message userMsg = Message.builder()
                .role(Role.USER.getValue())
                .content("Who are you?")
                .build();
        GenerationParam param = GenerationParam.builder()
                // If you did not set the environment variable, replace the following line with your API Key: .apiKey("sk-xxx")
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                // For a list of models, see: https://www.alibabacloud.com/help/en/model-studio/getting-started/models
                .model("qwen-plus")
                .messages(Arrays.asList(systemMsg, userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .build();
        return gen.call(param);
    }
    public static void main(String[] args) {
        try {
            GenerationResult result = callWithMessage();
            System.out.println(result.getOutput().getChoices().get(0).getMessage().getContent());
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            System.err.println("Error message: "+e.getMessage());
            System.out.println("For more information, see https://www.alibabacloud.com/help/en/model-studio/developer-reference/error-code");
        }
        System.exit(0);
    }
}

Sample response:

I am Qwen, a large language model developed by Alibaba Cloud.

cURL

You call models in Model Studio by using OpenAI-compatible HTTP or DashScope HTTP protocols. For a list of supported models, see List of models.

If you did not set the environment variable, replace: -H "Authorization: Bearer $DASHSCOPE_API_KEY" \ with: -H "Authorization: Bearer sk-xxx" \ (sk-xxx is your API key).

OpenAI-compatible HTTP

Run the following command to send an API request:

curl -X POST https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen-plus",
    "messages": [
        {
            "role": "system",
            "content": "You are a helpful assistant."
        },
        {
            "role": "user", 
            "content": "Who are you?"
        }
    ]
}'

Sample response:

{
    "choices": [
        {
            "message": {
                "role": "assistant",
                "content": "I am a large language model developed by Alibaba Cloud, known as Qwen."
            },
            "finish_reason": "stop",
            "index": 0,
            "logprobs": null
        }
    ],
    "object": "chat.completion",
    "usage": {
        "prompt_tokens": 22,
        "completion_tokens": 16,
        "total_tokens": 38
    },
    "created": 1728353155,
    "system_fingerprint": null,
    "model": "qwen-plus",
    "id": "chatcmpl-39799876-eda8-9527-9e14-2214d641cf9a"
}

DashScope HTTP

Run the following command to send an API request:

curl -X POST https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen-plus",
    "input":{
        "messages":[      
            {
                "role": "system",
                "content": "You are a helpful assistant."
            },
            {
                "role": "user",
                "content": "Who are you?"
            }
        ]
    },
    "parameters": {
        "result_format":"message"
    }
}'

Sample response:

{
    "output": {
        "choices": [
            {
                "finish_reason": "stop",
                "message": {
                    "role": "assistant",
                    "content": "I am a large language model developed by Alibaba Cloud, known as Qwen."
                }
            }
        ]
    },
    "usage": {
        "total_tokens": 38,
        "output_tokens": 16,
        "input_tokens": 22
    },
    "request_id": "87f776d7-3c82-9d39-b238-d1ad38c9b6a9"
}

Other languages

Call the model

            package main

            import (
                "bytes"
                "encoding/json"
                "fmt"
                "io"
                "log"
                "net/http"
                "os"
            )

            type Message struct {
                Role    string `json:"role"`
                Content string `json:"content"`
            }
            type RequestBody struct {
                Model    string    `json:"model"`
                Messages []Message `json:"messages"`
            }

            func main() {
                // Initialize HTTP client
                client := &http.Client{}

                // Construct request body
                requestBody := RequestBody{
                    // For a list of models, see: https://www.alibabacloud.com/help/en/model-studio/getting-started/models
                    Model: "qwen-plus",
                    Messages: []Message{
                        {
                            Role:    "system",
                            Content: "You are a helpful assistant.",
                        },
                        {
                            Role:    "user",
                            Content: "Who are you?",
                        },
                    },
                }

                jsonData, err := json.Marshal(requestBody)
                if err != nil {
                    log.Fatal(err)
                }

                // Create a POST request
                req, err := http.NewRequest("POST", "https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions", bytes.NewBuffer(jsonData))
                if err != nil {
                    log.Fatal(err)
                }

                // Configure request headers
                // If you did not set the environment variable, use your API key instead: apiKey := "sk-xxx"
                apiKey := os.Getenv("DASHSCOPE_API_KEY")
                req.Header.Set("Authorization", "Bearer "+apiKey)
                req.Header.Set("Content-Type", "application/json")

                // Execute the request
                resp, err := client.Do(req)
                if err != nil {
                    log.Fatal(err)
                }
                defer resp.Body.Close()

                // Read and output the response body
                bodyText, err := io.ReadAll(resp.Body)
                if err != nil {
                    log.Fatal(err)
                }
                fmt.Printf("%s\n", bodyText)
            }
        
            <?php
            $url = 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions';

            // If you did not set the environment variable is not set, use your API Key instead: $apiKey = "sk-xxx";
            $apiKey = getenv('DASHSCOPE_API_KEY');

            // Define request headers
            $headers = [
                'Authorization: Bearer '.$apiKey,
                'Content-Type: application/json'
            ];

            // Construct request body
            $data = [
                // For a list of models, see: https://www.alibabacloud.com/help/en/model-studio/getting-started/models
                "model" => "qwen-plus",
                "messages" => [
                    [
                        "role" => "system",
                        "content" => "You are a helpful assistant."
                    ],
                    [
                        "role" => "user",
                        "content" => "Who are you?"
                    ]
                ]
            ];

            // Initialize and configure cURL session
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

            // Execute the cURL session and capture the response
            $response = curl_exec($ch);

            // Handle errors, if any
            if (curl_errno($ch)) {
                echo 'Curl error: ' . curl_error($ch);
            }

            // Close the cURL session
            curl_close($ch);

            // Display the response
            echo $response;
            ?>
        
            using System.Net.Http.Headers;
            using System.Text;

            class Program
            {
                private static readonly HttpClient httpClient = new HttpClient();

                static async Task Main(string[] args)
                {
                    // If you did not set the environment variable, use your API Key instead: string? apiKey = "sk-xxx";
                    string? apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY");

                    if (string.IsNullOrEmpty(apiKey))
                    {
                        Console.WriteLine("API Key not set. Please ensure the 'DASHSCOPE_API_KEY' environment variable is configured.");
                        return;
                    }

                    string url = "https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions";
                    // For a list of models, see: https://www.alibabacloud.com/help/en/model-studio/getting-started/models
                    string jsonContent = @"{
                        ""model"": ""qwen-plus"",
                        ""messages"": [
                            {
                                ""role"": ""system"",
                                ""content"": ""You are a helpful assistant.""
                            },
                            {
                                ""role"": ""user"", 
                                ""content"": ""Who are you?""
                            }
                        ]
                    }";

                    // Send the request and display the result
                    string result = await SendPostRequestAsync(url, jsonContent, apiKey);
                    Console.WriteLine(result);
                }

                private static async Task<string> SendPostRequestAsync(string url, string jsonContent, string apiKey)
                {
                    using (var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"))
                    {
                        // Configure request headers
                        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
                        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                        // Execute the request and process the response
                        HttpResponseMessage response = await httpClient.PostAsync(url, content);

                        if (response.IsSuccessStatusCode)
                        {
                            return await response.Content.ReadAsStringAsync();
                        }
                        else
                        {
                            return $"Request failed: {response.StatusCode}";
                        }
                    }
                }
            }
        

What to do next