API详情

重要

Llama系列模型API调用需"申请体验"(Llama2/Llama3)并通过后才可使用;如果您需要通过API使用Llama 3.1模型,请开通百炼服务,前往百炼-模型广场查找Llama 3.1模型并申请体验,等待申请通过即可使用该模型。申请未通过时进行API的调用会返回错误状态码。

Llama系列模型

说明

支持的领域 / 任务:aigc 。

Llama系列大语言模型是Meta开发并公开发布的系列大型语言模型(LLMs)。

模型概览

模型名

模型简介

llama3.1-8b-instruct

llama3.1-70b-instruct

llama3.1-405b-instruct

Llama3.1系列是Meta推出的最新一代开源大语言模型,其405B版本是首个媲美商业模型的开源大语言模型。最大支持128K tokens输入。

llama3-8b-instruct

llama3-70b-instruct

Llama3系列模型,模型最大输入为6500,最大输出为1500,仅支持message格式。

llama2-7b-chat-v2

llama2-13b-chat-v2

Llama2系列大语言模型由Meta开发并公开发布,其规模从70亿到700亿参数不等。在DashScope上提供的llama2-7b-chat-v2和llama2-13b-chat-v2,分别为7B和13B规模的Llama2模型,针对对话场景微调优化后的版本。

SDK使用

前提条件

文本生成

以下示例展示了调用Llama系列模型对一个用户指令进行响应的代码。

说明

需要使用您的API-KEY替换示例中的YOUR_DASHSCOPE_API_KEY,代码才能正常运行。

设置API-KEY

export DASHSCOPE_API_KEY=YOUR_DASHSCOPE_API_KEY
# For prerequisites running the following sample, visit https://help.aliyun.com/document_detail/611472.html
from http import HTTPStatus
import dashscope


def call_with_messages():
    messages = [{'role': 'system', 'content': 'You are a helpful assistant.'},
                {'role': 'user', 'content': '介绍下故宫?'}]
    response = dashscope.Generation.call(
        model='llama3-8b-instruct',
        messages=messages,
        result_format='message',  # set the result to be "message" format.
    )
    if response.status_code == HTTPStatus.OK:
        print(response)
    else:
        print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
            response.request_id, response.status_code,
            response.code, response.message
        ))


if __name__ == '__main__':
    call_with_messages()
// Copyright (c) Alibaba, Inc. and its affiliates.

import java.util.ArrayList;
import java.util.List;
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.utils.JsonUtils;

public class Main {
    public static void usage()
            throws NoApiKeyException, ApiException, InputRequiredException {
        List<Message> messages = new ArrayList<>();
        Message systemMsg = Message.builder().role(Role.SYSTEM.getValue()).content("You are a helpful assistant.").build();
        Message userMsg = Message.builder().role(Role.USER.getValue()).content("介绍下故宫?").build();
        messages.add(systemMsg);
        messages.add(userMsg);

        GenerationParam param = GenerationParam.builder()
                .model("llama3-8b-instruct")
                .messages(messages)
                .build();
        Generation gen = new Generation();
        GenerationResult result = gen.call(param);
        System.out.println(JsonUtils.toJson(result));
    }

    public static void main(String[] args) {
        try {
            usage();
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            System.out.println(e.getMessage());
        }
        System.exit(0);
    }
}

参数配置

参数

类型

默认值

说明

model

string

-

llama2-7b-chat-v2/llama2-13b-chat-v2

llama3-8b-instruct/llama3-70b-instruct

messages

list dict

-

用户输入的内容,dict内主要包含2个key:role和content,其中role支持user、assistant、system,content为对应role的text输入。

result_format

string

-

用户返回的内容类型,默认为text,当输入格式为messages时可配置为message。

返回结果

  • 返回结果示例

    {
      "status_code": 200,
      "request_id": "1b96a7d7-ca10-910d-9a8c-6af889021fdd",
      "code": "",
      "message": "",
      "output": {
        "text": null,
        "finish_reason": null,
        "choices": [
          {
            "finish_reason": "stop",
            "message": {
              "role": "assistant",
              "content": "The Forbidden City!  It's an iconic and historic palace complex located in the heart of Beijing, China. Here's an introduction:\n\n**History**\n\nThe Forbidden City was built in 1406 during the Ming Dynasty (1368-1644) and served as the imperial palace of the Ming and Qing dynasties (1644-1912) for over 500 years. It was the seat of power for 24 emperors and the center of Chinese politics, culture, and society.\n\n**Architecture**\n\nThe Forbidden City is a massive complex, covering an area of about 7 million square feet (0.7 million square meters). It's surrounded by a 7.3-meter-high (24-foot-high) wall and has a total of 980 buildings and over 8,700 rooms. The palace is designed in a traditional Chinese architectural style, with a blend of Han, Tibetan, and Mongolian influences.\n\n**Layout**\n\nThe Forbidden City is divided into three main parts: the Outer Court, the Middle Court, and the Inner Court. The Outer Court is where the emperor would receive foreign ambassadors and hold important ceremonies. The Middle Court is where the emperor would hold court and conduct official business. The Inner Court is where the emperor and his family lived.\n\n**Highlights**\n\nSome of the most famous attractions within the Forbidden City include:\n\n1. The Gate of Supreme Harmony: The main entrance to the palace complex.\n2. The Hall of Supreme Harmony: The largest and most impressive hall in the palace, where the emperor would hold important ceremonies.\n3. The Hall of Preserving Harmony: A beautiful hall with intricate carvings and paintings.\n4. The Palace of Heavenly Purity: The residence of the emperor and his family.\n5. The Imperial Garden: A tranquil oasis within the palace complex.\n\n**Interesting Facts**\n\n1. The Forbidden City is often referred to as the \"Palace Museum\" because it's a museum today.\n2. The complex has over 1 million precious artifacts, including ceramics, paintings, and jade carvings.\n3. The Forbidden City is a UNESCO World Heritage Site since 1987.\n4. The palace complex has undergone several restorations and renovations over the centuries, including a major restoration project in the 1980s.\n\n**Visiting the Forbidden City**\n\nThe Forbidden City is open to the public and attracts millions of visitors each year. Visitors can explore the complex on their own or take a guided tour. The best time to visit is during spring and autumn, when the weather is mild and comfortable.\n\nI hope this introduction has sparked your interest in the Forbidden City! "
            }
          }
        ]
      },
      "usage": {
        "input_tokens": 26,
        "output_tokens": 533,
        "total_tokens": 559
      }
    }
  • 返回参数说明

    返回参数

    类型

    说明

    status_code

    int

    200(HTTPStatus.OK)表示请求成功,否则表示请求失败,可以通过code获取错误码,通过message字段获取错误详细信息。

    request_Id

    string

    系统生成的标志本次调用的id。

    code

    string

    表示请求失败,表示错误码,成功忽略。

    message

    string

    失败,表示失败详细信息,成功忽略。

    output

    dict

    调用结果信息,对于Llama系列模型,包含输出text。

    output.text

    string

    模型生成回复。

    usage.input_tokens

    int

    用户输入文本转换成Token后的长度。

    usage.output_tokens

    int

    模型生成回复转换为Token后的长度。

HTTP调用接口

功能描述

Llama系列模型也同时提供了HTTP接口,客户可以根据自己的需求选择。

前提条件

已开通服务并获得API-KEY:API-KEY的获取与配置

提交接口调用

POST https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation

入参描述

传参方式

字段

类型

必选

描述

示例值

Header

Content-Type

String

请求类型:application/json

application/json

Authorization

String

API-Key,例如:Bearer d1**2a

Bearer d1**2a

Body

model

String

指明需要调用的模型,目前可选

llama2-7b-chat-v2

llama2-13b-chat-v2

llama3-8b-instruct

llama3-70b-instruct

llama3-8b-instruct

input.prompt

String

用户当前输入的期望模型执行指令,7b模型最长不超过2048 tokens,13b模型最长不超过4096 tokens

hello, who are you?

出参描述

字段

类型

描述

示例值

output.text

String

本次请求的算法输出内容。

hello, who are you?\n\nI am an artificial intelligence designed to assist and communicate ... ...

usage.output_tokens

Integer

本次请求算法输出内容的 token 数目。

104

usage.input_tokens

Integer

本次请求用户输入内容的 token 数目。

41

request_id

String

本次请求的系统唯一码

fbd7e41a-363c-938a-81be-8ae0f9fbdb3d

请求示例

以下示例展示通过CURL命令来调用 Llama系列模型的脚本。

说明

需要使用您的API-KEY替换示例中的YOUR-DASHSCOPE-API-KEY,代码才能正常运行。

curl --location 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation' \
--header 'Authorization: Bearer <YOUR-DASHSCOPE-API-KEY>' \
--header 'Content-Type: application/json' \
--data '{
    "model": "llama3-8b-instruct",
    "input":{
        "messages":[
            {"content":"Where is the capital of Zhejiang?","role":"user"},
            {"content":"Thank you for asking! The capital of Zhejiang is Hangzhou.","role":"assistant"},
            {"content":"What are the interesting places there?","role":"user"}
        ]
    }
}'

响应示例

{
    "output":{
        "text":"Hangzhou, the capital of Zhejiang Province, is a city with a rich history and culture, known for its beautiful scenery and interesting places to visit. Here are some of the top attractions in Hangzhou:\n\n1. West Lake (Xi Hu): A must-visit attraction in Hangzhou, West Lake is a beautiful and peaceful natural lake with numerous temples, gardens, and scenic spots. Visitors can take a leisurely boat ride or stroll along the lake's banks to enjoy the picturesque views.\n2. Lingyin Temple: One of China's most famous Buddhist temples, Lingyin Temple is a place of great cultural and religious significance. The temple complex is home to numerous ancient Buddhist grottos, statues, and other artifacts, as well as a large library and a peaceful garden.\n3. Longjing Tea Plantations: Hangzhou is famous for its green tea, particularly the Longjing tea, which is grown in the surrounding hills. Visitors can learn about the tea-making process and enjoy the beautiful scenery of the tea plantations.\n4. Xixi National Wetland Park: A unique and fascinating natural attraction, Xixi National Wetland Park is a protected area of wetlands, forests, and waterways. Visitors can take a boat ride through the park's canals and enjoy the diverse wildlife and vegetation.\n5. Hefang Street: A popular shopping and dining destination, Hefang Street is a bustling area filled with local snacks, souvenirs, and traditional Chinese medicine shops. Visitors can try the local street food, buy souvenirs, and experience the local culture.\n6. China National Tea Museum: For those interested in tea culture, the China National Tea Museum is a must-visit attraction. The museum showcases the history and culture of tea in China, with exhibits on tea production, tea ceremonies, and tea culture.\n7. Leifeng Pagoda: A historic pagoda located on the south bank of West Lake, Leifeng Pagoda is a beautiful and impressive structure with stunning views of the lake and surrounding hills. Visitors can climb to the top of the pagoda for panoramic views of the area.\n8. Qinghefang Ancient Street: A historic street in the heart of Hangzhou, Qinghefang Ancient Street is a charming area filled with traditional architecture, local shops, and cultural attractions. Visitors can stroll along the street, try local snacks, and experience the local culture.\n\nThese are just a few of the many interesting places to visit in Hangzhou. Whether you're interested in history, culture, nature, or food, there's something for everyone in this beautiful city."
    },
    "usage": {
        "output_tokens": 632,
        "input_tokens": 35
    },
    "request_id":"fbd7e41a-363c-938a-81be-8ae0f9fbdb3d"
}

异常响应示例

在访问请求出错的情况下,输出的结果中会通过codemessage指明出错原因。

{
    "code":"InvalidApiKey",
    "message":"Invalid API-key provided.",
    "request_id":"fb53c4ec-1c12-4fc4-a580-cdb7c3261fc1"
}

状态码说明

DashScope通用状态码请查阅:返回状态码说明