文档

快速开始

更新时间:

百川

说明

支持的领域 / 任务:aigc

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

快速开始

前提条件

示例代码

以下示例展示了调用百川API对一个用户指令进行响应的代码。

说明

需要使用您的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);
  }
}

调用成功后,将会返回如下示例结果。

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

了解更多

有关百川模型API的详细调用文档可前往API详情页面进行了解。

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