微信扫码
添加专属顾问
我要投稿
AI助力合同审核,一键识别风险漏洞,让法务工作更高效智能。 核心内容: 1. 合同审核Agent如何解决传统人工审核的三大痛点 2. 使用Dify平台搭建合同审核Agent的完整流程 3. 支持多种文件格式转换与风险批注生成的技术实现
背景介绍
为什么需要合同审核Agent?
合同是商业活动的基石,明确权责、管控风险、保障交易安全。但在传统人工审核模式下,合同管理面临三大核心挑战:
审核盲区与风险漏判: 依赖法务人员个人经验,面对海量非标条款时,难以百分百识别所有隐蔽风险条款(如责任限制、保密范围、争议解决地),为后续履约埋下隐患;
效率瓶颈与协作成本高: 高并发业务场景下,审核请求排队严重,流转耗时漫长,法务团队疲于应付简单重复问题,严重拖慢商务谈判与项目推进节奏;
知识断层与标准不统一: 审核标准因人而异,新手律师易经验不足,而资深专家的风险偏好与判例知识难以快速沉淀和复用,导致组织级的合同风控水平波动大。
合同审核agent通过AI技术精准破局,将法务专家经验转化为实时、在线、可复用的智能服务,实现风险管控的自动化、标准化与前置化。
今天我就带着大家使用dify搭建合同审核Agent。利用它,用户只需轻松上传合同文件,即可获得一份带有详细风险批注的反馈文档,高效识别条款漏洞与潜在风险。
下面的视频是此Agent的使用效果
:
整体的工作流分为三部分:合同文件处理、合同要点审核、生成批注文件。
下面就让我们逐个步骤来看一下是怎么实现的吧!
图1 文件处理部分流程
from collections.abc import Generatorfrom typing import Anyfrom dify_plugin import Toolfrom dify_plugin.entities.tool import ToolInvokeMessagefrom docx import Documentimport tempfileimport ioimport osimport subprocessfrom typing import Optionalimport requestsclass Doc2docxTool(Tool):def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage]:filename = tool_parameters.get('doc_file')filename.url = "http://api:5001"+filename.urlresult_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"),),)returndef 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_iodef get_meta_data(self, mime_type, output_filename):result_filename: Optional[str] = Nonetemp_filename = output_filename.strip() if output_filename else Noneif temp_filename:# ensure extension nameextension = MimeType.get_extension(mime_type)if not temp_filename.lower().endswith(extension):temp_filename = f"{temp_filename}{extension}"result_filename = temp_filenamereturn {"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 Generatorfrom typing import Anyfrom dify_plugin import Toolfrom dify_plugin.entities.tool import ToolInvokeMessageimport jsonimport iofrom docx import Documentfrom docx.oxml.ns import qnfrom docx.oxml import OxmlElementfrom docx.text.run import Runfrom docx.opc.constants import CONTENT_TYPE, RELATIONSHIP_TYPEfrom docx.opc.packuri import PackURIfrom docx.opc.part import Partfrom docx.opc.oxml import parse_xmlfrom datetime import datetimeimport xml.etree.ElementTree as ETfrom pydantic import Field, PositiveInt, PositiveFloat, BaseModelfrom typing import Annotated, Literal, Optionalimport requestsimport jiebafrom sklearn.feature_extraction.text import TfidfVectorizerfrom 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("请上传文件")returnfile_url = "http://api:5001" + uploaded_file.urlprint(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}")returntext_to_comment = tool_parameters.get('comment_list')try:# 先把外层字符串列表转换成Python listjson_strs = json.loads(text_to_comment)except json.JSONDecodeError as e:print(f"外层列表JSON解析错误: {e}")returnprint(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"),),)returndef get_meta_data(self, mime_type, output_filename):result_filename: Optional[str] = Nonetemp_filename = output_filename.strip() if output_filename else Noneif temp_filename:# ensure extension nameextension = MimeType.get_extension(mime_type)if not temp_filename.lower().endswith(extension):temp_filename = f"{temp_filename}{extension}"result_filename = temp_filenamereturn {"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 resultdef 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)# 获取下一个可用的批注IDnext_comment_id = 0for 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 + 1for text_to_comment, comment_text in comments_list:comment_id = next_comment_idnext_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_textcomment_run.append(comment_text_element)comment_paragraph.append(comment_run)comment_element.append(comment_paragraph)comments_xml.append(comment_element)# 查找需要批注的文本并插入批注引用found_in_document = Falsefor paragraph in document.paragraphs:if text_to_comment in paragraph.text or self.similarity(text_to_comment, paragraph.text) > 0.8:# 找到文本在段落中的位置# 创建w:commentRangeStart和w:commentRangeEndcomment_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:commentReferencecomment_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 = Truebreakif found_in_document:breakif 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-10-30
Dify流程暂停与人工干预:3种实现方案+避坑指南
2025-10-16
告别升级噩梦:Dify 二次开发的无缝适配策略与实战案例(基于 v1.9.1)
2025-10-13
用Dify搭建企业知识库:5个实战技巧提升检索准确率90%
2025-10-13
Dify接口调用实战指南:从入门到精通的避坑手册,收藏了!
2025-10-12
Dify1.6.0升级1.9.1步骤及踩坑记
2025-10-10
用 Dify 零代码搭建 AI 用研助理,5分钟完成100个虚拟用户调研
2025-09-30
重大消息,刚刚Dify 1.9.1发布了!我们聊聊带来了哪些吸引人的功能特性?
2025-09-26
内网环境下Dify1.9.0版本镜像构建过程记录
2025-10-13
2025-09-03
2025-09-16
2025-09-06
2025-08-19
2025-09-23
2025-09-02
2025-08-18
2025-09-04
2025-10-12
2025-09-30
2025-09-23
2025-09-06
2025-09-05
2025-08-29
2025-08-18
2025-08-02
2025-07-30