news 2026/9/27 2:22:52

简单使用Laya模型

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
简单使用Laya模型

简单使用Laya模型

1 简单说明

Laya模型主要用于文本分类、判读的多语言、非自回归的系统1(System 1(快思考)-自动、无意识、并行运行)决策模型。给它一个状态(文本、邮件、工单或 JSON)和带类型的提问;它会在一次前向传播中(约33毫秒)返回带数学校准概率的类型化答案,支持100多种语言。

参考地址

# 官网文档,可支持集成到langchain https://nandhakishorm.github.io/laya/ # Github https://github.com/NandhaKishorM/laya # ModelScope网站 https://modelscope.cn/models/convaiinnovations/laya

模型分类

模型编码参数上下文用途
layaModernBERT-large421M512仅支持英语文本、安全护栏、邮件分类
laya-multilingualmmBERT-base322M1024 (最高支持 8,192)支持100多种语言,速度约快2.2倍
laya-typed-decisionsModernBERT-large421M1024代理轨迹可观测性、客户服务、发票处理和安全事件四种类型化决策工作流

三种决策原语说明

  • choice:分类选择,返回每个选项的概率和置信度,类问题的选项数量控制在约 20 个以内。;
  • score:打分,返回有序量表的分布;
  • noul:是非判断,返回 0-1 的成立概率;

2 简单使用

2.1 安装软件包

pip install laya -i https://pypi.tuna.tsinghua.edu.cn/simple

2.2 Laya代码

importosfromlayaimportRouter# 设置离线环境os.environ["HF_HUB_OFFLINE"]="1"# 设置国内的hf-mirror.comos.environ["HF_ENDPOINT"]="https://hf-mirror.com"# 禁用xet,Xet是Hugging Face的新传输协议,国内用不了os.environ["HF_HUB_DISABLE_XET"]="1"# 设置缓存目录os.environ["HF_HOME"]="E:/laya"# 设置单个模型# agent = laya.load("E:/laya", subfolder="multilingual")# 预加载模型,自动选择laya、laya-multilingual、laya-typed-decisions模型router=Router(preload=True)router.preload(["english","multilingual","typed-decisions"])# 设置问题state={"from":"user@acme.com","subject":"Duplicate charge on invoice #4411","body":"Hi, we were billed twice for March. Please refund the duplicate today or we will cancel our plan."}# 设置规则questions={"department":{# 设置选择类型,返回概率和置信度"type":"choice","instructions":"Which department should handle this request?",# 设置分类标准,必须是列表,可以字典和文本列表"criteria":{"billing":"invoices, payments, refunds","technical":"bugs, outages, system errors","sales":"pricing, new contracts","other":"everything else"}},"urgency":{# 设置分数类型,返回概率分布"type":"score","instructions":"How urgent is this request?",# 必须是列表,不能是字典列表"criteria":["not urgent","soon","critical deadline or blocking issue"]},"churn_risk":{# 设置是非判断,返回0-1的成立概率;"type":"noul","instructions":"Does the user threaten to cancel or leave?"},"refund_requested":{"type":"noul","instructions":"Does the user explicitly request a refund?"}}# 预测的返回值为字典# 1. 英文 -> 选择laya英文模型res_en=router.predict(state,questions)print(res_en)print("laya-english")print("Answer:",res_en["answers"])print("Routing:",res_en["routing"])print("=======")# 2. 中文 -> 选择laya-multilingual多语言模型res_zh=router.predict({"body":"我被收取了两次费用,请退还钱款。"},questions)print(res_zh)print("laya-multilingual")print("Answer:",res_zh["answers"])print("Routing:",res_zh["routing"])print("=======")# 3. 设置决策流模型res_td=router.predict(state,questions,model="typed-decisions")print(res_td)print("laya-choice")print("Answer:",res_td["answers"])print("Routing:",res_td["routing"])

2.3 返回值说明

{"model":"laya-rl-agent","answers":{"department":{# 选择类型"type":"choice",# 目标值"choice":"billing",# 概率分布"probabilities":{"billing":0.9653,"technical":0.014,"sales":0.0101,"other":0.0107},# 概率分布的置信度"confidence":0.864,# 目标值的置信度"answer_confidence":0.9653,"action":{"act_probability":1.0}},"urgency":{"type":"score",# 期望得分,表示在1~2之间的序列"score":1.44,# 标签列表"legend":{"0":"not urgent","1":"soon","2":"critical deadline or blocking issue"},# 概率分布"probabilities":{"0":0.1164,"1":0.3271,"2":0.5565},"confidence":0.1425,"answer_confidence":0.5565,"action":{"act_probability":1.0}},"churn_risk":{# 表示是非判断,返回0-1的成立概率"type":"noul",# 概率值"noul":0.8248,"confidence":0.8248,"answer_confidence":0.8248,"action":{"act_probability":1.0}},"refund_requested":{"type":"noul","noul":0.843,"confidence":0.843,"answer_confidence":0.843,"action":{"act_probability":1.0}}},"usage":{"input_tokens":348,"output_tokens":0},# 表示路由选择的模型"routing":{"model":"english","repo":"convaiinnovations/laya","reason":"English Latin text","detection":{"script":"latin","script_profile":{"latin":1.0},"language":"en","is_english":True,"language_undecided":False,"diacritic_rate":0.0,"non_latin_fraction":0.0},"workflow":None}}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/27 2:21:22

STM32F103C8T6核心理论精讲:GPIO、中断、PWM与定时器实战

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华
网站建设 2026/9/27 2:19:14

周末总结(2026/09/26)

工作 人际关系核心实践 及时回应他人:无论是善意还是恶意,都要在5分钟内做出回应 应对尴尬话题:学会谦虚自嘲,真诚赞美他人(避免阴阳怪气) 社交时效性: 30 分钟 朋友圈互动控制在5分钟内 职场社…

作者头像 李华
网站建设 2026/9/27 2:10:18

动环监控可视化:实时性、准确性与可操作性的工程实践

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华
网站建设 2026/9/27 2:06:15

ACE安全中心组件异常修复指南:Windows服务与驱动级排障

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华
网站建设 2026/9/27 2:05:57

论文结果部分AI率高,数据不能删,该怎么降AI率?

论文结果部分AI率高,数据不能删,该怎么降AI率? 结果章节几乎每段都写“由表可知”,报告又把这些段落连续提示出来。你想降低 AI 率,却不敢改动样本数、结果方向和统计信息。最容易走偏的做法,是把数字删掉…

作者头像 李华
网站建设 2026/9/27 2:02:55

免费降AI工具没效果,怎样才能把论文AI率降下来?

免费降AI工具没效果,怎样才能把论文AI率降下来? 把论文放进免费工具,返回的文字确实变了,再检测却还是不符合要求。接着搜索另一个免费网站,同一段又换了一种说法,原来的研究细节反而越来越少。工具没效果…

作者头像 李华