文档

API详情

更新时间:
一键部署

月之暗面

说明

支持的领域 / 任务:aigc

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

模型概览

模型名

模型简介

moonshot-v1-8k

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

moonshot-v1-32k

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

moonshot-v1-128k

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

SDK使用

前提条件

文本生成

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

说明

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

参数配置

参数

类型

默认值

说明

model

string

-

模型名称。

messages

list dict

-

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

parameters.temperature

float

0.3

取值范围: [.0f, 1.0f]。 多样性,越高,多样性越好, 缺省 0.3。

parameters.top_p

float

0.85

取值范围: [.0f, 1.0f)。值越小,越容易出头部, 缺省 0.85

parameters.presence_penalty

float

0

存在惩罚,介于-2.0到2.0之间的数字。正值会根据新生成的词汇是否出现在文本中来进行惩罚,增加模型讨论新话题的可能性。

parameters.frequency_penalty

float

0

频率惩罚,介于-2.0到2.0之间的数字。正值会根据新生成的词汇在文本中现有的频率来进行惩罚,减少模型一字不差重复同样话语的可能性。

parameters.stop

string,list[string]

null

停止词,当全匹配这个(组)词后会停止输出,这个(组)词本身不会输出。最多不能超过5个字符串,每个字符串不得超过32个字节。

parameters.max_tokens

int

1024

这个值建议按需给个合理的值,如果不给的话,我们会给一个不错的整数比如 1024。特别要注意的是,这个max_tokens是指您期待我们返回的 token 长度,而不是输入 + 输出的总长度。比如对一个moonshot-v1-8k模型,它的最大输入 + 输出总长度是 8192,当输入 messages 总长度为 4096 的时候,您最多只能设置为 4096,否则我们服务会返回不合法的输入参数。

返回结果

  • 返回结果示例

{
    "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"
}
  • 返回参数说明

返回参数

类型

说明

status_code

int

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

request_Id

string

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

code

string

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

message

string

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

output

dict

调用结果信息。

output.choise[list]

list

返回结果列表

output.choise[x].finish_reason

string

停止原因

output.choise[x].message

string

返回结果

output.choise[x].message.role

string

返回结果role

output.choise[x].message.content

string

返回结果内容

usage

dict

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

usage.input_tokens

int

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

usage.output_tokens

int

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

usage.total_tokens

int

用户输入+模型生成回复转换为Token后的长度。

HTTP调用接口

功能描述

Moonshot-v1模型同时支持 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

指明需要调用的模型,固定值moonshot-v1-8k,moonshot-v1-32k,moonshot-v1-128k

moonshot-v1-8k

input.messages

list

包含迄今为止对话的消息列表。

这是一个结构体的列表,每个元素类似如下:{"role": "user", "content": "你好"} role 只支持 system,user,assistant 其一,content 不得为空。

parameters.temperature

float

采样温度,介于 0 和 1 之间。较高的值(如 0.7)将使输出更加随机,而较低的值(如 0.2)将使其更加集中和确定性。

0.3

parameters.top_p

float

取值范围: [.0f, 1.0f)。值越小,越容易出头部, 缺省 0.85

0.85

parameters.presence_penalty

float

存在惩罚,介于-2.0到2.0之间的数字。正值会根据新生成的词汇是否出现在文本中来进行惩罚,增加模型讨论新话题的可能性。

0

parameters.frequency_penalty

float

频率惩罚,介于-2.0到2.0之间的数字。正值会根据新生成的词汇在文本中现有的频率来进行惩罚,减少模型一字不差重复同样话语的可能性。

0

parameters.stop

string,list[string]

停止词,当全匹配这个(组)词后会停止输出,这个(组)词本身不会输出。最多不能超过 5 个字符串,每个字符串不得超过 32 字节。

null

parameters.max_tokens

int

这个值建议按需给个合理的值,如果不给的话,我们会给一个不错的整数比如 1024。特别要注意的是,这个max_tokens是指您期待我们返回的 token 长度,而不是输入 + 输出的总长度。比如对一个moonshot-v1-8k模型,它的最大输入 + 输出总长度是 8192,当输入 messages 总长度为 4096 的时候,您最多只能设置为 4096,否则我们服务会返回不合法的输入参数。

1024

出参描述

字段

类型

描述

示例值

status_code

int

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

200

request_Id

string

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

23066782-2f2b-9f66-82f0-7c26a186b470

output.choise[x].finish_reason

string

停止原因

stop

output.choise[x].message.role

string

返回结果role

assistant

output.choise[x].message.content

string

返回结果内容

你好!今天我能帮你什么?

usage

dict

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

"usage": {

"total_tokens": 15,

"input_tokens": 8,

"output_tokens": 7

}

usage.input_tokens

int

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

8

usage.output_tokens

int

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

7

usage.total_tokens

int

用户输入+模型生成的回复转换为Token后的长度。

15

请求示例

以下示例展示通过CURL命令来调用moonshot-v1-8k模型的脚本。

说明

需要使用您的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": "moonshot-v1-8k",
    "input":{
        "messages":[      
            {
                "role": "system",
                "content": "You are a helpful assistant."
            },
            {
                "role": "user",
                "content": "你好"
            }
        ]
    }
}'

响应示例

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

异常响应示例

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

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

状态码说明

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

  • 本页导读 (1)