资讯 社区 文档
技术能力
语音技术
文字识别
人脸与人体
图像技术
语言与知识
视频技术

ChatLaw

ChatLaw由壹万卷公司与北大深研院研发的法律行业大模型,在开源版本基础上进行了进一步架构升级,融入了法律意图识别、法律关键词提取、CoT推理增强等模块,实现了效果提升,以满足法律问答、法条检索等应用需求。本文介绍了相关API。

说明: 当前服务版本仅支持体验试用,暂时无法支持大规模调用,如有更高的并发需求请提交工单与我们联系。

接口描述

调用本接口,发起一次对话请求。

在线调试

平台提供了 API在线调试平台-示例代码 ,用于帮助开发者调试接口,平台集成快速检索、查看开发文档、查看在线调用的请求内容和返回结果、复制和下载示例代码等功能,简单易用,更多内容请查看API在线调试介绍

鉴权说明

本文API,支持2种鉴权方式。不同鉴权方式,调用方式不同,使用Header、Query参数不同,详见本文请求说明。开发者可以选择以下任一种方式进行鉴权。

请求结构

以访问凭证access_token鉴权方式为例,说明调用API请求结构,示例如下。

POST /rpc/2.0/ai_custom/v1/wenxinworkshop/chat/chatlaw?access_token=24.4a3a19b******18992 HTTP/1.1
Host: aip.baidubce.com
Content-Type: application/json

{
   "messages": [
    {"role":"user","content": "如果一个人故意驾车向人群冲撞,但没有造成重大损失,会被怎样处罚?"}
  ],
  "extra_parameters":{
      "use_keyword":true,
      "use_reference":true
  }
}

请求头域

除公共头域外,无其它特殊头域。注意:鉴权方式不同,对应请求头域不同。

请求参数

  • Query参数

只有访问凭证access_token鉴权方式,需使用Query参数。

访问凭证access_token鉴权

名称 类型 必填 描述
access_token string 通过API Key和Secret Key获取的access_token,参考Access Token获取
  • Body参数
名称 类型 必填 描述
messages List(message) 聊天上下文信息。说明:
(1)messages成员不能为空,1个成员表示单轮对话,多个成员表示多轮对话
(2)最后一个message为当前请求的信息,前面的message为历史对话信息
(3)必须为奇数个成员,成员中message的role必须依次为user、assistant
(4)message中的content总长度不能超过8000 个字符
stream bool 是否以流式接口的形式返回数据,默认false
temperature float 要使用的采样温度,说明:
(1)较高的数值会使输出更加随机,而较低的数值会使其更集中,更具确定性
(2)范围 [0,2.0],默认值为 1
top_p float 温度采样的替代方法,核采样,说明:
(1)取值范围 [0,1.0],其中模型考虑具有 top_p 概率质量的令牌的结果
(2)默认值1
user_id string 表示最终用户的唯一标识符
extra_parameters Object 第三方大模型推理高级参数,依据第三方大模型厂商不同而变化
metadata map<string,string> 说明:
(1)元素个数最大支持16个
(2)key和value必须都是string类型

message说明

名称 类型 描述
role string 当前支持以下:
user: 表示用户
assistant: 表示对话助手
content string 对话内容,不能为空

extra_parameters说明

名称 类型 必须 描述
use_keyword bool 表示是否提取问答关键词以加强模型推理。使用use_keyword参数,可能会导致以下情况:
(1)模型回答通识性问题下降
(2)在接口响应前,获取到提取的问答关键词,并将内容拼接到prompt中,最终接口返回参数prompt_tokens数会比用户输入多
use_reference bool 表示是否使用参考法条以加强推理。使用use_reference参数,可能会导致以下情况:
(1)模型回答通识性问题下降
(2)在接口响应前,获取到参考法条,并将内容拼接到prompt中,最终接口返回参数prompt_tokens数会比用户输入多

响应头域

除公共头域外,无其它特殊头域。

响应参数

名称 类型 描述
id string 本轮对话的id
object string 回包类型。
chat.completion:多轮对话返回
created int 时间戳
sentence_id int 表示当前子句的序号。只有在流式接口模式下会返回该字段
is_end bool 表示当前子句是否是最后一句。只有在流式接口模式下会返回该字段
result string 对话返回结果,当前对话返回结果限制在1000个token
need_clear_history bool 表示用户输入是否存在安全,是否关闭当前会话,清理历史会话信息
true:是,表示用户输入存在安全风险,建议关闭当前会话,清理历史会话信息
false:否,表示用户输入无安全风险
ban_round int 当need_clear_history为true时,此字段会告知第几轮对话有敏感信息,如果是当前问题,ban_round=-1
usage usage token统计信息,由第三方模型本身负责计算

usage说明

名称 类型 描述
prompt_tokens int 问题tokens数(包含历史QA)
completion_tokens int 回答tokens数
total_tokens int tokens总数

注意 :同步模式和流式模式,响应参数返回不同,详细内容参考示例描述。

  • 同步模式下,响应参数为以上字段的完整json包。
  • 流式模式下,各字段的响应参数为 data: {响应参数}。

请求示例(单轮)

以访问凭证access_token鉴权方式为例,说明如何调用API,示例如下。

# 步骤一,获取access_token,替换下列示例中的API Key与Secret Key
curl 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=[API Key]&client_secret=[Secret Key]'

# 步骤二,调用本文API,使用步骤一获取的access_token,替换下列示例中的”调用接口获取的access_token“
curl -X POST https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/chatlaw?access_token=[步骤一调用接口获取的access_token] -d '{
  "messages": [
    {"role":"user","content": "如果一个人故意驾车向人群冲撞,但没有造成重大损失,会被怎样处罚?"}
  ],
  "extra_parameters":{
      "use_keyword":true,
      "use_reference":true
  }
}'
import requests
import json

def get_access_token():
    """
    使用 API Key,Secret Key 获取access_token,替换下列示例中的应用API Key、应用Secret Key
    """
        
    url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=[应用API Key]&client_secret=[应用Secret Key]"
    
    payload = json.dumps("")
    headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }
    
    response = requests.request("POST", url, headers=headers, data=payload)
    return response.json().get("access_token")


def main():
        
    url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/chatlaw?access_token=" + get_access_token()
    
    payload = json.dumps({
        "messages": [{
        "role": "user",
        "content": "如果一个人故意驾车向人群冲撞,但没有造成重大损失,会被怎样处罚?"
    }],
    "extra_parameters": {
        "use_keyword": True,
        "use_reference": True
    }
    })
    headers = {
        'Content-Type': 'application/json'
    }
    
    response = requests.request("POST", url, headers=headers, data=payload)
    
    print(response.text)
    

if __name__ == '__main__':
    main()

响应示例(单轮)

HTTP/1.1 200 OK
Date: Mon, 12 Apr 2021 06:27:55 GMT
Content-Type: application/json;charset=utf-8
X-Aipe-Self-Def: eb_total_tokens:741-id:as-z046y3u8p9
Statement: AI-generated

{
    "id":"as-z046y3u8p9",
    "object":"chat.completion",
    "created":1701325384,
    "result":"根据问题描述和相关法律,即使没有造成死亡等重伤后果,仍然属于危险驾驶,情况也构成犯罪危险驾驶。从道路交通安全法来看,司乘人员严禁随意跳车和钻车窗口。无论何时,都需要提前落实转车准备工作,包括系紧安全带,关好门窗及天窗,确认手刹和灯光状态,摆正坐姿与座椅角度等。同时需要警惕闯红灯,乱变道,超速行驶等恶习。",
    "is_truncated":false,
    "need_clear_history":false,
    "is_safe":0,
    "usage":{
        "prompt_tokens":587,
        "completion_tokens":154,
        "total_tokens":741
    }
}

请求示例(多轮)

# 步骤一,获取access_token,替换下列示例中的API Key与Secret Key
curl 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=[API Key]&client_secret=[Secret Key]'

# 步骤二,调用本文API,使用步骤一获取的access_token,替换下列示例中的”调用接口获取的access_token“
curl -X POST https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/chatlaw?access_token=[步骤一调用接口获取的access_token] -d '{
  "messages": [
    {"role":"user","content":"如果一个人故意驾车向人群冲撞,但没有造成重大损失,会被怎样处罚?"},
    {"role":"assistant","content":"根据问题描述和相关法律,即使没有造成死亡等重伤后果,仍然属于危险驾驶,情况也构成犯罪危险驾驶。从道路交通安全法来看,司乘人员严禁随意跳车和钻车窗口。无论何时,都需要提前落实转车准备工作,包括系紧安全带,关好门窗及天窗,确认手刹和灯光状态,摆正坐姿与座椅角度等。同时需要警惕闯红灯,乱变道,超速行驶等恶习。"},
    {"role":"user","content": "如果司机处于醉酒驾驶状态呢"}
  ],
  "extra_parameters":{
      "use_keyword":true,
      "use_reference":true
  }
}'
import requests
import json

def get_access_token():
    """
    使用 API Key,Secret Key 获取access_token,替换下列示例中的应用API Key、应用Secret Key
    """
        
    url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=[应用API Key]&client_secret=[应用Secret Key]"
    
    payload = json.dumps("")
    headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }
    
    response = requests.request("POST", url, headers=headers, data=payload)
    return response.json().get("access_token")

def main():
        
    url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/chatlaw?access_token=" + get_access_token()
    
    payload = json.dumps({
        "messages": [
            {
                "role": "user",
                "content": "如果一个人故意驾车向人群冲撞,但没有造成重大损失,会被怎样处罚?"
            },
            {
                "role": "assistant",
                "content": "根据问题描述和相关法律,即使没有造成死亡等重伤后果,仍然属于危险驾驶,情况也构成犯罪危险驾驶。从道路交通安全法来看,司乘人员严禁随意跳车和钻车窗口。无论何时,都需要提前落实转车准备工作,包括系紧安全带,关好门窗及天窗,确认手刹和灯光状态,摆正坐姿与座椅角度等。同时需要警惕闯红灯,乱变道,超速行驶等恶习。"
            },
            {
                "role": "user",
                "content": "如果司机处于醉酒驾驶状态呢"
            }
        ],
        "extra_parameters":{
            "use_keyword":True,
            "use_reference":True
        }
    })
    headers = {
        'Content-Type': 'application/json'
    }
    
    response = requests.request("POST", url, headers=headers, data=payload)
    
    print(response.text)
    

if __name__ == '__main__':
    main()

响应示例(多轮)

HTTP/1.1 200 OK
Date: Mon, 12 Apr 2021 06:27:55 GMT
Content-Type: application/json;charset=utf-8
X-Aipe-Self-Def: eb_total_tokens:1450-id:as-f3shsukvc2
Statement: AI-generated

{
    "id":"as-f3shsukvc2",
    "object":"chat.completion",
    "created":1701325502,
    "result":"根据这三种规定,饮酒驾驶机动车将面临相应的刑事责任及相应的资格吊销和业务禁止。如果该驾人造成了严重的交通事故,还可能涉嫌危害运输安全罪等。对于交通事故责任的责任判断,一般适用“机械比对说”原则:首先,判断谁是事故责任方,须考虑双方都做出过错事实的推导。那么最为直接、明晰的办法就是比较双方行为之间事故产生的机械联系程度,认定哪一个最小化了事故发生概率。换言之,把双方作为单独试探主体所做的决策排除掉外界干扰,分析这种决策如何减少行为者承担危险的必要性。而对于饮酒驾驶,已经足够地解释清楚其引起交通事故的风险了。",
    "is_truncated":false,
    "need_clear_history":false,
    "is_safe":0,
    "usage":{
        "prompt_tokens":1193,
        "completion_tokens":257,
        "total_tokens":1450
    }
}

请求示例(流式单轮)

# 步骤一,获取access_token,替换下列示例中的API Key与Secret Key
curl 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=[API Key]&client_secret=[Secret Key]'

# 步骤二,调用本文API,使用步骤一获取的access_token,替换下列示例中的”调用接口获取的access_token“
curl -X POST 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/chatlaw?access_token=[步骤一调用接口获取的access_token]' -d '{
  "messages": [
    {"role":"user", "content": "如果一个人故意驾车向人群冲撞,但没有造成重大损失,会被怎样处罚?"}
  ],
  "stream": true,
  "extra_parameters":{
      "use_keyword":true,
      "use_reference":true
  }
}'
import requests
import json

def get_access_token():
    """
    使用 API Key,Secret Key 获取access_token,替换下列示例中的应用API Key、应用Secret Key
    """
        
    url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=[应用API Key]&client_secret=[应用Secret Key]"
    
    payload = json.dumps("")
    headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }
    
    response = requests.request("POST", url, headers=headers, data=payload)
    return response.json().get("access_token")

def main():
        
    url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/chatlaw?access_token=" + get_access_token()
    
    payload = json.dumps({
        "messages": [
            {
                "role": "user",
                "content": "如果一个人故意驾车向人群冲撞,但没有造成重大损失,会被怎样处罚?"
            }
        ],
        "stream": True,
        "extra_parameters":{
           "use_keyword":True,
           "use_reference":True
        }        
    })
    headers = {
        'Content-Type': 'application/json'
    }
    
    response = requests.request("POST", url, headers=headers, data=payload, stream=True)
    
    for line in response.iter_lines():
        print(line.decode("UTF-8"))
    
if __name__ == '__main__':
    main()

响应示例(流式单轮)

HTTP/1.1 200 OK
Date: Mon, 12 Apr 2021 06:27:55 GMT
Content-Type: text/event-stream;charset=utf-8
Cache-Control: no-cache
Statement: AI-generated


data: {"id":"as-7dn3ehrnh3","object":"chat.completion","created":1701325259,"sentence_id":0,"is_end":false,"is_truncated":false,"result":"按照《中华","need_clear_history":false,"is_safe":0,"usage":{"prompt_tokens":580,"completion_tokens":7,"total_tokens":587}}

data: {"id":"as-7dn3ehrnh3","object":"chat.completion","created":1701325260,"sentence_id":1,"is_end":false,"is_truncated":false,"result":"人民共和国刑法》的规定,故意犯罪要受到刑事制裁。","need_clear_history":false,"is_safe":0,"usage":{"prompt_tokens":580,"completion_tokens":32,"total_tokens":612}}

data: {"id":"as-7dn3ehrnh3","object":"chat.completion","created":1701325261,"sentence_id":2,"is_end":false,"is_truncated":false,"result":"根据司法解释和相关条文,行为人只需造成他人轻伤,就属于触犯故意伤害罪。","need_clear_history":false,"is_safe":0,"usage":{"prompt_tokens":580,"completion_tokens":72,"total_tokens":652}}

data: {"id":"as-7dn3ehrnh3","object":"chat.completion","created":1701325263,"sentence_id":3,"is_end":false,"is_truncated":false,"result":"因此,对于驾车冲人案,即使肇事者没有造成重大损失,也不妨碍将该行为视为故意犯罪追究刑事责任。","need_clear_history":false,"is_safe":0,"usage":{"prompt_tokens":580,"completion_tokens":113,"total_tokens":693}}

data: {"id":"as-7dn3ehrnh3","object":"chat.completion","created":1701325265,"sentence_id":4,"is_end":false,"is_truncated":false,"result":"同时,由于该行为符合《中华人民共和国治安管理处罚法》“偷开他人机动车”的条件,又构成犯罪,还须承担行","need_clear_history":false,"is_safe":0,"usage":{"prompt_tokens":580,"completion_tokens":163,"total_tokens":743}}

data: {"id":"as-7dn3ehrnh3","object":"chat.completion","created":1701325266,"sentence_id":5,"is_end":false,"is_truncated":false,"result":"政主体作出的行政措施。","need_clear_history":false,"is_safe":0,"usage":{"prompt_tokens":580,"completion_tokens":173,"total_tokens":753}}

data: {"id":"as-7dn3ehrnh3","object":"chat.completion","created":1701325266,"sentence_id":6,"is_end":true,"is_truncated":false,"result":"","need_clear_history":false,"is_safe":0,"usage":{"prompt_tokens":580,"completion_tokens":173,"total_tokens":753}}

请求示例(流式多轮)

# 步骤一,获取access_token,替换下列示例中的API Key与Secret Key
curl 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=[API Key]&client_secret=[Secret Key]'

# 步骤二,调用本文API,使用步骤一获取的access_token,替换下列示例中的”调用接口获取的access_token“

curl -X POST 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/chatlaw?access_token=[步骤一调用接口获取的access_token]' -d '--data '{
  "messages": [
    {"role":"user","content":"如果一个人故意驾车向人群冲撞,但没有造成重大损失,会被怎样处罚?"},
    {"role":"assistant","content":"根据问题描述和相关法律,即使没有造成死亡等重伤后果,仍然属于危险驾驶,情况也构成犯罪危险驾驶。从道路交通安全法来看,司乘人员严禁随意跳车和钻车窗口。无论何时,都需要提前落实转车准备工作,包括系紧安全带,关好门窗及天窗,确认手刹和灯光状态,摆正坐姿与座椅角度等。同时需要警惕闯红灯,乱变道,超速行驶等恶习。"},
    {"role":"user","content": "如果司机处于醉酒驾驶状态呢"}
  ],
  "stream": true,
  "extra_parameters":{
      "use_keyword":true,
      "use_reference":true
  }
}'
import requests
import json

def get_access_token():
    """
    使用 API Key,Secret Key 获取access_token,替换下列示例中的应用API Key、应用Secret Key
    """
        
    url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=[应用API Key]&client_secret=[应用Secret Key]"
    
    payload = json.dumps("")
    headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }
    
    response = requests.request("POST", url, headers=headers, data=payload)
    return response.json().get("access_token")

def main():

    url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/chatlaw?access_token=" + get_access_token()

    payload = json.dumps({
        "messages": [{
            "role": "user",
            "content": "如果一个人故意驾车向人群冲撞,但没有造成重大损失,会被怎样处罚?"
        }, {
            "role":
            "assistant",
            "content":
            "根据问题描述和相关法律,即使没有造成死亡等重伤后果,仍然属于危险驾驶,情况也构成犯罪危险驾驶。从道路交通安全法来看,司乘人员严禁随意跳车和钻车窗口。无论何时,都需要提前落实转车准备工作,包括系紧安全带,关好门窗及天窗,确认手刹和灯光状态,摆正坐姿与座椅角度等。同时需要警惕闯红灯,乱变道,超速行驶等恶习。"
        }, {
            "role": "user",
            "content": "如果司机处于醉酒驾驶状态呢"
        }],
        "stream":True,
        "extra_parameters": {
            "use_keyword":True,
            "use_reference":True
        }
    })
    headers = {
        'Content-Type': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload, stream=True)

    for line in response.iter_lines():
        print(line.decode("UTF-8"))

if __name__ == '__main__':
    main()

响应示例(流式多轮)

HTTP/1.1 200 OK
Date: Mon, 12 Apr 2021 06:27:55 GMT
Content-Type: text/event-stream;charset=utf-8
Cache-Control: no-cache
Statement: AI-generated

data: {"id":"as-1emij971ms","object":"chat.completion","created":1701325621,"sentence_id":0,"is_end":false,"is_truncated":false,"result":"民法典第九","need_clear_history":false,"is_safe":0,"usage":{"prompt_tokens":1189,"completion_tokens":7,"total_tokens":1196}}

data: {"id":"as-1emij971ms","object":"chat.completion","created":1701325623,"sentence_id":1,"is_end":false,"is_truncated":false,"result":"百四十三条:醉酒之后驾车上高速公路会导致机动车肇事的风险极大,应该受到严厉打击。","need_clear_history":false,"is_safe":0,"usage":{"prompt_tokens":1189,"completion_tokens":48,"total_tokens":1237}}

data: {"id":"as-1emij971ms","object":"chat.completion","created":1701325625,"sentence_id":2,"is_end":false,"is_truncated":false,"result":"同时,这项罪名只适用在普遍公众有关社会秩序的高级形象问题,例如长途旅客可以参考汽车事故法庭案例。","need_clear_history":false,"is_safe":0,"usage":{"prompt_tokens":1189,"completion_tokens":96,"total_tokens":1285}}

data: {"id":"as-1emij971ms","object":"chat.completion","created":1701325627,"sentence_id":3,"is_end":false,"is_truncated":false,"result":"而不是像醉驾那样面临威胁到自身或第三方生命/财产问题,所以醉驾的刑数要比高速公路醉驾地区要低些。","need_clear_history":false,"is_safe":0,"usage":{"prompt_tokens":1189,"completion_tokens":143,"total_tokens":1332}}

data: {"id":"as-1emij971ms","object":"chat.completion","created":1701325627,"sentence_id":4,"is_end":true,"is_truncated":false,"result":"","need_clear_history":false,"is_safe":0,"usage":{"prompt_tokens":1189,"completion_tokens":143,"total_tokens":1332}}

错误码

如果请求错误,服务器返回的JSON文本包含以下参数。

名称 描述
error_code 错误码
error_msg 错误描述信息,帮助理解和解决发生的错误

例如Access Token失效返回以下内容,需要重新获取新的Access Token再次请求。

{
  "error_code": 110,
  "error_msg": "Access token invalid or no longer valid"
}

更多相关错误码,请查看错误码说明

上一篇
DISC-MedLLM
下一篇
Falcon-7B