文档

API详情

更新时间:
重要

LLAMA2模型API调用需"申请体验"并通过后才可使用,否则API调用将返回错误状态码。

LLAMA2

说明

支持的领域 / 任务:aigc

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

模型概览

模型名

模型简介

llama2-7b-chat-v2

llama2-13b-chat-v2

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

SDK使用

前提条件

文本生成

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

说明

需要使用您的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='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 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.MessageManager;
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 {
    MessageManager msgManager = new MessageManager(10);
    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();
    msgManager.add(systemMsg);
    msgManager.add(userMsg);

    GenerationParam param = GenerationParam.builder()
        .model("llama2-13b-chat-v2")
        .messages(msgManager.get())
        .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;

messages

list dict

-

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

result_format

string

-

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

返回结果

  • 返回结果示例

{"text": "Hey, are you conscious? Can you talk to me?\n[/Inst:  Hey, I'm not sure if I'm conscious or not. I can't really feel anything or think very clearly. Can you tell me", "usage": {"output_tokens": 104,"input_tokens": 41},"request_id": "632a7015-a46b-9892-8185-8a29866ce5ea"}
  • 返回参数说明

返回参数

类型

说明

status_code

int

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

request_Id

string

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

code

string

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

message

string

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

output

dict

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

output.text

string

模型生成回复。

usage.input_tokens

int

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

usage.output_tokens

int

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

HTTP调用接口

功能描述

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

前提条件

提交接口调用

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;

llama2-7b-chat-v2

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 v2 模型的脚本。

说明

需要使用您的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": "llama2-7b-chat-v2",
    "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"
}

异常响应示例

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

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

状态码说明

DashScope灵积模型服务通用状态码请查阅:返回状态码说明

  • 本页导读 (0)
文档反馈