微信扫码
添加专属顾问
我要投稿
AI助力合同审核,一键识别风险漏洞,让法务工作更高效智能。 核心内容: 1. 合同审核Agent如何解决传统人工审核的三大痛点 2. 使用Dify平台搭建合同审核Agent的完整流程 3. 支持多种文件格式转换与风险批注生成的技术实现
背景介绍
为什么需要合同审核Agent?
合同是商业活动的基石,明确权责、管控风险、保障交易安全。但在传统人工审核模式下,合同管理面临三大核心挑战:
审核盲区与风险漏判: 依赖法务人员个人经验,面对海量非标条款时,难以百分百识别所有隐蔽风险条款(如责任限制、保密范围、争议解决地),为后续履约埋下隐患;
效率瓶颈与协作成本高: 高并发业务场景下,审核请求排队严重,流转耗时漫长,法务团队疲于应付简单重复问题,严重拖慢商务谈判与项目推进节奏;
知识断层与标准不统一: 审核标准因人而异,新手律师易经验不足,而资深专家的风险偏好与判例知识难以快速沉淀和复用,导致组织级的合同风控水平波动大。
合同审核agent通过AI技术精准破局,将法务专家经验转化为实时、在线、可复用的智能服务,实现风险管控的自动化、标准化与前置化。
今天我就带着大家使用dify搭建合同审核Agent。利用它,用户只需轻松上传合同文件,即可获得一份带有详细风险批注的反馈文档,高效识别条款漏洞与潜在风险。
下面的视频是此Agent的使用效果:
整体的工作流分为三部分:合同文件处理、合同要点审核、生成批注文件。
下面就让我们逐个步骤来看一下是怎么实现的吧!
图1 文件处理部分流程
from collections.abc import Generator
from typing import Any
from dify_plugin import Tool
from dify_plugin.entities.tool import ToolInvokeMessage
from docx import Document
import tempfile
import io
import os
import subprocess
from typing import Optional
import requests
class Doc2docxTool(Tool):
def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage]:
filename = tool_parameters.get('doc_file')
filename.url = "http://api:5001"+filename.url
result_bytes_io = self.convert_doc_to_docx(filename.url)
result_file_bytes = result_bytes_io.getvalue()
print(f"Converted DOCX file size: {len(result_file_bytes)} bytes")
yield self.create_blob_message(
blob=result_file_bytes,
meta=self.get_meta_data(
mime_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
output_filename=tool_parameters.get("output_filename"),
),
)
return
def convert_doc_to_docx(self, url):
with tempfile.TemporaryDirectory() as tmpdir:
response = requests.get(url)
doc_path = os.path.join(tmpdir, "input.doc")
with open(doc_path, "wb") as f:
f.write(response.content)
# 执行转换
subprocess.run([
"libreoffice", "--headless", "--convert-to", "docx", doc_path, "--outdir", tmpdir
], check=True)
docx_path = os.path.join(tmpdir, "input.docx")
result_bytes_io = io.BytesIO()
document = Document(docx_path)
document.save(result_bytes_io)
print(f"Converted DOCX file size: {result_bytes_io.getbuffer().nbytes} bytes")
return result_bytes_io
def get_meta_data(self, mime_type, output_filename):
result_filename: Optional[str] = None
temp_filename = output_filename.strip() if output_filename else None
if temp_filename:
# ensure extension name
extension = MimeType.get_extension(mime_type)
if not temp_filename.lower().endswith(extension):
temp_filename = f"{temp_filename}{extension}"
result_filename = temp_filename
return {
"mime_type": mime_type,
"filename": result_filename,
}
apt updateapt install libreoffice -y
第二部分是我们合同审核任务的关键(这一步非常重要哦),目标就是根据用户的需求在合同中进行逐条审核。下面是我们合同要点审查实现流程:
图5 审查部分流程
售后部分和其他节点有一些区别,因为不同的产品对应的售后条款不同,所以针对这一点,我们使用【代码】节点结合【开始】节点中的“product_type”进行判断,然后将需要遵守的售后规则传入【售后条款审核】节点。
从图中可以看到,我们在条款审核这一部分主要是依靠大模型的能力来实现的。所以这部分的调优除了大模型的选型,就是我们对prompt的调整。下面我们以【赔偿责任】节点为例进行讲解。 【赔偿责任】节点的prompt如下图6所示。
我们设置prompt的主要目的是让大模型明白三件事:
1. 你是谁?(确定角色以及立场)
2. 你要做什么?(审查的要点和遵守的规则)
3. 你最终要输出什么?(输出内容和格式)
Ok,带着这三点要求我们来看一下prompt中具体是怎么实现的。
prompt解读:
在prompt开始的部分我们给大模型进行了身份和立场的确认,因为我们所做的合同审核工具主要是为采购合同的乙方服务,所以我们希望大模型能够站在乙方的角度思考,“屏蔽”训练过程中数据、loss function或reward model教给它的“中立客观”的记忆。
第二段我们告诉了大模型他的具体任务是什么。 接着我们给出了审核中要关注的要点和始终坚持的目标。审核要点很好理解,这就是用户针对“赔偿责任”所要重点关注的内容,模型需要关注合同中有关“赔偿责任”的条款,认真研判这些条款是否侵害了乙方的权益。
最后就是对模型输出的格式和内容的要求了。为了方便后续文档批注的插入,我们需要模型以json的格式输出“问题原文”、“风险类型”和“修订建议”。
各位看到这里可能有疑问了:“你的合同审核关注要点和我的不同,怎么办啊?”
答:好办,将你的审查要点和我所提供给你的prompt输入到大模型中,告诉他“按照我提供给你的模板生成一份!”,这样就可以生成适合你的审查要点的prompt了。
在得到审核内容之后,我们使用一个大模型节点对审核内容进行二次判断,主要目标是为了将重复的“问题原文”进行合并,具体prompt如下图。
因为这个节点所实现的目标相比审查节点来说要简单的多,所以我们使用了qwen3-4B的模型来实现,从测试效果来看可以满足我们的需求。不过话说回来,如果我们在审查节点时使用235B的模型,是否可以省略二次审查的节点呢?欢迎大家评论区留言讨论
通过前两步的处理我们对上传的合同文件进行了审查,得到了审查的结果,万事俱备只欠展示。
针对用户经常使用的合同审核方法,我们选择使用批注的形式输出最终的审查结果。
针对三种上传的文件格式,我们编写了对应的【批注插入】插件。插件的构建参考我们前面给出的链接,下面我们以docx文件插入批注为例给出tool部分代码。
from collections.abc import Generator
from typing import Any
from dify_plugin import Tool
from dify_plugin.entities.tool import ToolInvokeMessage
import json
import io
from docx import Document
from docx.oxml.ns import qn
from docx.oxml import OxmlElement
from docx.text.run import Run
from docx.opc.constants import CONTENT_TYPE, RELATIONSHIP_TYPE
from docx.opc.packuri import PackURI
from docx.opc.part import Part
from docx.opc.oxml import parse_xml
from datetime import datetime
import xml.etree.ElementTree as ET
from pydantic import Field, PositiveInt, PositiveFloat, BaseModel
from typing import Annotated, Literal, Optional
import requests
import jieba
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
_COMMENTS_PART_DEFAULT_XML_BYTES = b"""
<w:comments xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" />
"""
class InsertCommentTool(Tool):
def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage]:
# 提取参数
uploaded_file = tool_parameters.get('docx')
if not uploaded_file:
yield self.create_text_message("请上传文件")
return
file_url = "http://api:5001" + uploaded_file.url
print(f"Processing uploaded file: {file_url}")
response = requests.get(file_url)
print(f"the response info : {response.content}")
print(f"the response info type : {type(response.content)}")
if response.status_code != 200:
yield self.create_text_message(f"文件下载失败,状态码: {response.status_code}")
return
text_to_comment = tool_parameters.get('comment_list')
try:
# 先把外层字符串列表转换成Python list
json_strs = json.loads(text_to_comment)
except json.JSONDecodeError as e:
print(f"外层列表JSON解析错误: {e}")
return
print(f"Parsed JSON strings: {json_strs}")
# 转化为元组列表
final_text_comments = []
for json_str in json_strs:
processed = self.process_json_string(json_str)
final_text_comments.extend(processed)
print(f"Final text comments to insert: {final_text_comments}")
outfile_path, document = self.insert_multiple_comments_into_docx(response.content, uploaded_file.filename, final_text_comments, "AI审查助手")
result_bytes_io = io.BytesIO()
document.save(result_bytes_io)
result_file_bytes = result_bytes_io.getvalue()
yield self.create_blob_message(
blob=result_file_bytes,
meta=self.get_meta_data(
mime_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
output_filename=tool_parameters.get("output_filename"),
),
)
return
def get_meta_data(self, mime_type, output_filename):
result_filename: Optional[str] = None
temp_filename = output_filename.strip() if output_filename else None
if temp_filename:
# ensure extension name
extension = MimeType.get_extension(mime_type)
if not temp_filename.lower().endswith(extension):
temp_filename = f"{temp_filename}{extension}"
result_filename = temp_filename
return {
"mime_type": mime_type,
"filename": result_filename,
}
def process_json_string(self, json_str):
try:
json_node = json.loads(json_str) # 先把字符串转为字典
except json.JSONDecodeError as e:
print(f"JSON解析错误: {e}")
return []
result = []
question_list = json_node.get("问题条款", [])
for item in question_list:
original_text = item.get("问题原文", "")
suggestion = item.get("修订建议", "")
error_type = item.get("风险类型", "")
result.append((original_text, f"风险类型:{error_type}。修订建议:{suggestion}"))
return result
def similarity(self, text1, text2):
texts = [' '.join(jieba.lcut(text1)), ' '.join(jieba.lcut(text2))]
vectorizer = TfidfVectorizer()
tfidf = vectorizer.fit_transform(texts)
similarity = cosine_similarity(tfidf[0:1], tfidf[1:2])
return similarity[0][0]
def insert_multiple_comments_into_docx(self, io_content, file_path, comments_list, author="Author", initials="A"):
"""
在DOCX文件中插入多个批注。
Args:
io_content (str): DOCX内容的二进制流。
comments_list (list): 包含 (text_to_comment, comment_text) 元组的列表。
author (str): 批注作者。
initials (str): 批注作者缩写。
"""
doc_file = io.BytesIO(io_content)
document = Document(doc_file)
# 确保comments part存在
try:
comments_part = document.part.part_related_by(RELATIONSHIP_TYPE.COMMENTS)
except KeyError:
comments_part = Part(
partname=PackURI("/word/comments.xml"),
content_type=CONTENT_TYPE.WML_COMMENTS,
blob=_COMMENTS_PART_DEFAULT_XML_BYTES,
package=document.part.package,
)
document.part.relate_to(comments_part, RELATIONSHIP_TYPE.COMMENTS)
ET.register_namespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main")
comments_xml = parse_xml(comments_part.blob)
# 获取下一个可用的批注ID
next_comment_id = 0
for comment in comments_xml.findall(qn("w:comment")):
current_id = int(comment.get(qn("w:id")))
if current_id >= next_comment_id:
next_comment_id = current_id + 1
for text_to_comment, comment_text in comments_list:
comment_id = next_comment_id
next_comment_id += 1
# 创建批注XML元素
comment_element = OxmlElement("w:comment")
comment_element.set(qn("w:date"), datetime.now().isoformat())
comment_element.set(qn("w:id"), str(comment_id))
comment_element.set(qn("w:author"), author)
comment_element.set(qn("w:initials"), initials)
comment_paragraph = OxmlElement("w:p")
comment_run = OxmlElement("w:r")
comment_text_element = OxmlElement("w:t")
comment_text_element.text = comment_text
comment_run.append(comment_text_element)
comment_paragraph.append(comment_run)
comment_element.append(comment_paragraph)
comments_xml.append(comment_element)
# 查找需要批注的文本并插入批注引用
found_in_document = False
for paragraph in document.paragraphs:
if text_to_comment in paragraph.text or self.similarity(text_to_comment, paragraph.text) > 0.8:
# 找到文本在段落中的位置
# 创建w:commentRangeStart和w:commentRangeEnd
comment_range_start = OxmlElement("w:commentRangeStart")
comment_range_start.set(qn("w:id"), str(comment_id))
comment_range_end = OxmlElement("w:commentRangeEnd")
comment_range_end.set(qn("w:id"), str(comment_id))
# 将w:commentRangeStart插入到run之前
paragraph.runs[0]._element.addprevious(comment_range_start)
# 将w:commentRangeEnd插入到run之后
paragraph.runs[-1]._element.addnext(comment_range_end)
# 创建w:r元素,包含w:commentReference
comment_reference_run = OxmlElement("w:r")
comment_reference = OxmlElement("w:commentReference")
comment_reference.set(qn("w:id"), str(comment_id))
comment_reference_run.append(comment_reference)
# 将w:commentReference插入到run中
paragraph.runs[-1]._element.append(comment_reference_run)
found_in_document = True
break
if found_in_document:
break
if not found_in_document:
print(f"Warning: Text \"{text_to_comment}\" not found in the document. Comment not added for this text.")
comments_part._blob = ET.tostring(comments_xml)
output_file_path = file_path.replace(".docx", "_with_multiple_comments.docx")
document.save(output_file_path)
print(f"批注已成功插入到 {output_file_path} 中。")
return output_file_path, document
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2025-08-23
Dify集成MCP服务
2025-08-23
Dify v1.7.2 实战爆破:6 大特性颠覆开发,23 处修复稳如老狗
2025-08-20
深度实战:我用 Dify 复刻了 1688 的 AI 搜索,“多路召回”才是灵魂
2025-08-20
Dify Java Client
2025-08-19
5 分钟搭建你的 AI 应用-Dify 全流程指南
2025-08-19
【万字长文】Dify 知识库全链路图解:7 个关键节点,彻底拆解 RAG 黑盒
2025-08-18
Dify功能解析四:Dify父子模式分段解决普通分段的什么问题?
2025-08-17
Dify 实战篇| 配置参数实战优化
2025-06-04
2025-06-25
2025-05-29
2025-06-03
2025-06-02
2025-06-29
2025-06-05
2025-06-24
2025-06-10
2025-06-30
2025-08-18
2025-08-02
2025-07-30
2025-06-26
2025-06-17
2025-05-29
2025-05-28
2025-05-22