快速开始

月之暗面

说明

支持的领域 / 任务:aigc

Moonshot-v1Moonshot AI 推出了一款千亿参数的语言模型,具备优秀的语义理解、指令遵循和文本生成能力。支持多种长度的上下文窗口,适合长文本的理解和内容生成场景。随着性能的迭代,模型会持续更新

快速开始

前提条件

示例代码

以下示例展示了调用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='moonshot-v1-8k',
        messages=messages,
    )
    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.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 GenerationResult callWithMessage() throws ApiException, NoApiKeyException, InputRequiredException {
        Generation gen = new Generation();

        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();

        GenerationParam param = GenerationParam.builder()
            .model("moonshot-v1-8k")
            .messages(Arrays.asList(systemMsg, userMsg))
            .build();

        return gen.call(param);
    }
    public static void main(String[] args) {
        try {
            GenerationResult result = callWithMessage();
            System.out.println(result);
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            // 使用日志框架记录异常信息
            // Logger.error("An error occurred while calling the generation service", e);
            System.err.println("An error occurred while calling the generation service: " + e.getMessage());
        }
        System.exit(0);
    }
}

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

{
    "output": {
        "choices": [
            {
                "message": {
                    "content": "你好!今天我能帮你什么?",
                    "role": "assistant"
                },
                "index": 0,
                "finish_reason": "stop"
            }
        ]
    },
    "usage": {
        "total_tokens": 15,
        "input_tokens": 8,
        "output_tokens": 7
    },
    "request_id": "23066782-2f2b-9f66-82f0-7c26a186b470"
}

了解更多

有关月之暗面大语言模型API的详细调用文档可前往API详情页面进行了解。