# 1. 接口说明

山海大模型API接口需要先获得授权才可调用,授权获取方法见《API申请说明》

# 2. 请求地址

https://unigpt-api.hivoice.cn/rest/v1.1/chat/completions
1

# 3. 请求接口

1. 提交方式:POST

2. 请求头参数 Header

名称 类型 必须 默认值 描述
appkey String Y N 产品的唯一标识,需通过邮件申请获取
requestId String Y N 请求的唯一标识,可帮助开发人员定位日志排查错误
udid String Y N 设备的唯一标识
sign String Y N 签名
timestamp Long Y N 请求时间戳(毫秒), 如1686621129587 表示 2023-06-13 09:52:09
stream Boolean N false true:数据以流式返回。
false:一次返回所有结果。

签名生成方法:
A)将上述参数按照appkey、udid、timestamp、secret 顺序拼接字符串;
B)将A形成字符串获取SHA256摘要,形成一个64位的十六进制(字母大写)字符串,即为本次请求sign(签名)的值
示例:

public static String getSign(String appkey, String udid, String timestamp, String secret) {
        String originalStr = appkey + udid + timestamp + secret;
        StringBuilder sign = new StringBuilder();
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            byte[] bytes = md.digest(originalStr.getBytes(StandardCharsets.UTF_8));
            for (int i = 0; i < bytes.length; i++) {
                String hex = Integer.toHexString(bytes[i] & 0xFF);
                if (hex.length() == 1) {
                    sign.append("0");
                }
                sign.append(hex.toUpperCase());
            }
        } catch (Exception unused) {
            unused.printStackTrace();
        }
        return sign.toString();
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

3. 请求参数Body

名称 类型 必须 默认值 描述
model String Y N 模型ID,固定值:unigpt-3.5
max_tokens Integer N N 返回字数限制
temperature Double N N 取值 0-2。该值越大每次返回的结果越随机,即相似度越小。
stop Array N N 控制在生成文时,何时停止,可以定义一组短语或单词,遇到这些文字时停止生成
messages List Y N 提交问题
> role String N user 请求角色 assistant 、user
> content String Y N 请求内容

示例:

{
    "messages": [
        {
            "content": "帮我给张三写一封邮件,祝他生日快乐。"
        }
    ]
}
1
2
3
4
5
6
7

# 4. 返回结果

# 4.1 非流式(stream=false)返回结果

名称 类型 必须 默认值 描述
errorCode Integer Y N 状态码:
0: 成功
其它:失败
result Object Y N 返回结果
> id String Y N 应答ID
> model String Y N 应答模型
> choices Array Y N 应答数组
>> message Object Y N 应答对象
>> role String Y assistant 应答角色
>> content String Y N 应答内容

示例:

{
    "errorCode":"0",
    "errorMsg":"请求成功",
    "result":{
        "id":"chatcmpl-6rzRpnOc9zBQmyA3TCevwQm95CQ4z",
        "object":"chat.completion",
        "created":1678325609,
        "model":"unigpt-3.0",
        "choices":[
            {
                "message":{
                    "role":"assistant",
                    "content":"尊敬的张三先生:\n\n今天是您的生日,同时也是您晋升的日子。我要祝贺您同时获得这两份喜悦,恭喜您,祝愿您生日快乐"
                },
                "finish_reason":"stop",
                "index":0
            }
        ]
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# 4.2 流式(stream=true)返回结果

1. 按字数返回:

{
    "choices":[
        {
            "delta":{
                "content":"问题"
            },
            "index":0
        }
    ],
    "created":1678690725,
    "id":"chatcmpl-6tWQnXXphddhXfzX8KkjYR70OX7Mt",
    "model":"unigpt-3.0",
    "object":"chat.completion.chunk"
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

2. 完成输出,会话完成可以关闭当前链接。