文档

Llama 系列大语言模型 快速开始

更新时间:

Llama模型

Llama 3.1 系列是Meta推出的最新一代开源大语言模型,其 405B 版本是首个媲美顶级商业模型的开源大语言模型。

当前在大模型服务平台部署的服务来自于ModelScope社区模型:

Llama3系列是来自Meta开发并公开发布的最新大型语言模型(LLMs)。该系列模型提供了多种参数大小(8B、70B等)的版本。相较于Llama2系列模型,Llama3系列在模型结构上没有重大变化,但是训练数据量进行了极大扩充,从 Llama2系列的2T Tokens扩大到了Llama3的15T Tokens,其中代码数据扩充了4倍。

当前在大模型服务平台部署的服务来自于ModelScope社区模型:

Llama 2系列是来自Meta开发并公开发布的大型语言模型(LLMs)。该系列模型提供了多种参数大小(7B、13B和70B等)的版本,并同时提供了预训练和针对对话场景的微调版本。Llama 2系列使用了2T token进行训练,相比于LLama多出40%,上下文长度从LLama的2048增加到4096,可以理解更长的文本,在多个公开基准测试上超过了已有的开源模型。采用了高质量的数据进行微调和基于人工反馈的强化学习训练,具有较高的可靠性和安全性。

当前在大模型服务平台部署的服务来自于ModelScope社区模型:

SDK使用

您可以通过SDK实现单轮对话、多轮对话、流式输出、function call等多种功能。

前提条件

  • DashScope SDK提供了Python和Java两个版本,请确保您已安装最新版SDK安装SDK

  • 已开通服务并获得API-KEY:获取API-KEY

  • 我们推荐您将API-KEY配置到环境变量中以降低API-KEY的泄漏风险,详情可参考通过环境变量配置API-KEY。您也可以在代码中配置API-KEY,但是泄漏风险会增加。

    说明

    当您使用DashScope Java SDK时,为了效率您应该尽可能复用Generation以及其他请求对象,但对象(如Generation)不是线程安全的,您应该采取一定的措施,比如及时关闭进程、管理同步机制等,来确保对象的安全性。

文本生成

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

# 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='llama2-7b-chat-v2',
        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);
  }
}

返回结果

  • 返回结果示例

{
  "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
  }
}