文档

API详情

更新时间:
一键部署

百川

说明

支持的领域 / 任务:aigc

baichuan-13B/baichuan2-7B是由百川智能开发的一个开源的大规模预训练模型。基于Transformer结构,在大约1.2万亿tokens上训练的70亿参数模型,支持中英双语,上下文窗口长度为4096。在标准的中文和英文权威benchmark(C-EVAL/MMLU)上均取得同尺寸最好的效果。

模型概览

模型名

模型简介

baichuan-7b-v1

百川模型,仅支持prompt格式输入

baichuan2-7b-chat-v1

baichuan2-13b-chat-v1

百川模型2-7B对话版/百川模型2-13B对话版,支持message和prompt格式输入

SDK使用

前提条件

文本生成

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

说明

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

设置API-KEY

export DASHSCOPE_API_KEY=YOUR_DASHSCOPE_API_KEY

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='baichuan2-7b-chat-v1',
        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
        ))


def call_with_prompt():
    prompt = '介绍下故宫'
    rsp = dashscope.Generation.call(model='baichuan2-7b-chat-v1',
                                    prompt=prompt)
    print(rsp)
    if rsp.status_code == HTTPStatus.OK:
        print(rsp.output)
        print(rsp.usage)
    else:
        print('Failed, status_code: %s, code: %s, message: %s' %
              (rsp.status_code, rsp.code, rsp.message))


if __name__ == '__main__':
    type = 'message'
    if type == 'prompt':
        call_with_prompt()
    elif type == 'message':
        call_with_messages()
    else:
        print("call type not support")
// 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.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 {
    Generation gen = new Generation();
    GenerationParam param = GenerationParam
    .builder()
    .model("baichuan2-7b-chat-v1")
    .prompt("介绍下杭州")
    .build();
    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

-

目前支持 baichuan-7b-v1、baichuan2-7b-chat-v1

prompt

string

-

用户当前输入的期望模型执行指令。

messages

list dict

-

用户输入的内容,dict内主要包含2个key:role和content,其中role支持user、assistant、system,content为对应role的text输入。目前仅baichuan2-7b-chat-v1支持

result_format

string

-

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

返回结果

  • 返回结果示例

{
    "status_code": 200,
    "request_id": "dcb8fe29-6b2a-9797-8e35-f81b4aa7b33c",
    "code": "",
    "message": "",
    "output": {
        "text": "介绍下故宫\n故宫又称紫禁城,位于北京中轴线的中心,占地72万平方米。据史料记载,是明朝、清朝两代帝王的皇宫。自明永乐建成以来,至今已有六百多年的历史。 故宫的建筑按照它的布局和使用功能分成六大学堂,即外朝、内廷。 外朝,包括了太和殿、中和殿和保和殿,这便是人们常说的“三大殿” 内廷以乾清宫、交泰殿、坤宁宫后三宫为中心,以及东西六宫和御花园,是皇帝和皇后居住生活之所。 故宫,自明朝以来居住了24位皇帝,成为明清两朝的皇宫,它也是世界上现存规模最大、保存最为完整的木质结构的古建筑之一,是中国的象征。 它的建筑和陈列艺术的价值,也是中华民族的骄傲! 故宫位于北京城的中心,旧称“紫禁城”,是明、清两代的皇宫,也是我国现存最大最完整的古建筑群,共有殿宇8000多间。 故宫始建于公元1406年,1420年基本建成,历时14年,是明成祖朱棣下令所建。故宫的设计者是明太祖朱元璋的孙子、明成祖朱棣,明代著名建筑学家、解剖学家、天文学家提出了“高台建筑”和“立体规划”的设计思想,所以,故宫是中国古代建筑史上的一个伟大创举。"
    },
    "usage": {
        "input_tokens": 5,
        "output_tokens": 470
    }
}
  • 返回参数说明

返回参数

类型

说明

status_code

int

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

request_Id

string

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

code

string

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

message

string

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

output

dict

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

text

string

模型生成回复。

usage

dict

计量信息,表示本次请求计量数据,当前模型无计量信息,此处为默认值。

input_tokens

int

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

output_tokens

int

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

HTTP调用接口

功能描述

百川模型同时支持 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

X-DashScope-WorkSpace

String

指明本次调用需要使用的workspace;需要注意的是,对于子账号Apikey调用,此参数为必选项,子账号必须归属于某个workspace才能调用;对于主账号Apikey此项为可选项,添加则使用对应的workspace身份,不添加则使用主账号身份。

ws_QTggmeAxxxxx

Body

model

String

指明需要调用的模型,固定值baichuan-7b-v1 、baichuan2-7b-chat-v1

baichuan-7b-v1

input.prompt

String

是,二中格式选其一

文本内容,支持中英文。

登鹳雀楼->王之涣\n夜雨寄北->

input.messages

List

用户输入的内容,dict内主要包含2个key:role和content,其中role支持user、assistant、system,content为对应role的text输入。目前仅baichuan2-7b-chat-v1支持

[

{"role": "system", "content": "You are a helpful assistant."},

{"role": "user",

"content": "你好,请介绍一下故宫"}

]

parameters.result_format

string

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

message

出参描述

字段

类型

描述

示例值

output.text

String

本次请求的算法输出内容,result_format为text时的格式。

我建议你去颐和园

output.choices.message

List

本次请求的算法输出内容,result_format为message时的格式。

"message": {

"role": "assistant",

"content_type": "text",

"content": "故宫,位于... ... 宫殿和建筑。"}

output.choices.finish_reason 或者

output.finish_reason

String

有三种情况:正在生成时为null,生成结束时如果由于停止token导致则为stop,生成结束时如果因为生成长度过长导致则为length。

stop

request_id

String

本次请求的系统唯一码。

7574ee8f-38a3-4b1e-9280-

11c33ab46e51

usage.output_tokens

Integer

本次请求算法输出内容的 token 数目,目前仅baichuan2-7b-chat-v1和baichuan2-13b-chat-v1支持。

104

usage.input_tokens

Integer

本次请求用户输入内容的 token 数目,目前仅baichuan2-7b-chat-v1和baichuan2-13b-chat-v1支持。

41

请求示例

以下示例展示通过CURL命令来调用baichuan2-7b-chat-v1模型的脚本。

说明

需要使用您的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": "baichuan2-13b-chat-v1",
    "input":{
        "messages":[      
            {
                "role": "system",
                "content": "You are a helpful assistant."
            },
            {
                "role": "user",
                "content": "你好,请介绍一下故宫"
            }
        ]
    },
    "parameters": {
        "result_format": "message"
    }
}'

响应示例

{
    "output": {
        "choices": [
            {
                "finish_reason": "stop",
                "message": {
                    "role": "assistant",
                    "content_type": "text",
                    "content": "故宫,位于中国北京市中心,是中国明清两代的皇家宫殿,也是世界上现存规模最大、保存最为完整的木质结构古建筑之一。故宫始建于明成祖永乐四年(1406年),历时15年建成。它是明朝和清朝两代皇帝的皇家居所,共有24位皇帝在此居住和执政。\n\n故宫占地面积72万平方米,建筑面积约10万平方米。它共有宫殿建筑990多座,房屋8960间。整个建筑群按照南北中轴线布局,分为外朝和内廷两部分。外朝天子殿、中和殿和保和殿组成,是皇帝举行盛大典礼和处理政务的地方;内庭包括乾清宫、坤宁宫和储秀宫等,为皇帝和皇后居住的地方,同时也是皇帝处理日常政务的场所。此外,故宫还有御花园、东西六宫等众多宫殿和建筑。"
                }
            }
        ]
    },
    "usage": {
        "output_tokens": 180,
        "input_tokens": 11
    },
    "request_id": "3719bbfa-0ab8-997d-9207-21355f7320b3"
}

异常响应示例

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

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

状态码说明

大模型服务平台通用状态码请查阅:状态码说明

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