LangChain 71 字符串评估器String Evaluation衡量在多样化数据上的性能和完整性

news2025/2/25 15:33:28

LangChain系列文章

  1. LangChain 60 深入理解LangChain 表达式语言23 multiple chains链透传参数 LangChain Expression Language (LCEL)
  2. LangChain 61 深入理解LangChain 表达式语言24 multiple chains链透传参数 LangChain Expression Language (LCEL)
  3. LangChain 62 深入理解LangChain 表达式语言25 agents代理 LangChain Expression Language (LCEL)
  4. LangChain 63 深入理解LangChain 表达式语言26 生成代码code并执行 LangChain Expression Language (LCEL)
  5. LangChain 64 深入理解LangChain 表达式语言27 添加审查 Moderation LangChain Expression Language (LCEL)
  6. LangChain 65 深入理解LangChain 表达式语言28 余弦相似度Router Moderation LangChain Expression Language (LCEL)
  7. LangChain 66 深入理解LangChain 表达式语言29 管理prompt提示窗口大小 LangChain Expression Language (LCEL)
  8. LangChain 67 深入理解LangChain 表达式语言30 调用tools搜索引擎 LangChain Expression Language (LCEL)
  9. LangChain 68 LLM Deployment大语言模型部署方案
  10. LangChain 69 向量数据库Pinecone入门
  11. LangChain 70 Evaluation 评估、衡量在多样化数据上的性能和完整性

在这里插入图片描述

1. 字符串评估器String Evaluation

字符串评估器是LangChain内的一个组件,旨在通过将语言模型生成的输出(预测)与参考字符串或输入进行比较,来评估语言模型的性能。这种比较是评估语言模型的关键步骤,为生成文本的准确性或质量提供了衡量标准。

在实践中,字符串评估器通常用于评估预测字符串与给定输入(如问题或提示)的一致性。通常会提供参考标签或上下文字符串,以定义正确或理想回应的外观。这些评估器可以根据您的应用程序的具体需求进行定制。

要创建自定义字符串评估器,请继承StringEvaluator类并实现_evaluate_strings方法。如果您需要异步支持,还应实现_aevaluate_strings方法。

以下是与字符串评估器相关的关键属性和方法的总结:

  • evaluation_name评估名称:指定评估的名称。
  • requires_input 必要输入:布尔属性,用于指示评估器是否需要输入字符串。如果为真,当未提供输入时,评估器将抛出错误。如果为假,如果提供了输入,则会记录警告,表明输入在评估中不会被考虑。
  • requires_reference 需要参考:布尔属性,用于指定评估器是否需要参考标签。如果为真,当未提供参考时,评估器将抛出错误。如果为假,如果提供了参考,则会记录警告,表明参考在评估中不会被考虑。

字符串评估器还实现了以下方法:

  • aevaluate_strings 异步评估字符串:异步评估链或语言模型的输出,支持可选的输入和标签。
  • evaluate_strings 同步评估字符串:同步评估链或语言模型的输出,支持可选的输入和标签。

以下部分提供了关于可用的字符串评估器实现以及如何创建自定义字符串评估器的详细信息。

2. 标准评估 Criteria Evaluation

在您希望使用特定评分标准或标准集来评估模型输出的场景中,标准评估器是一个非常实用的工具。它可以帮助您检查LLM或Chain的输出是否符合定义的一套标准。

要深入了解其功能和可配置性,请参阅CriteriaEvalChain类的参考文档。

3. 使用CriteriaEvalChain无需参考资料 Usage without references

在这个例子中,你将使用CriteriaEvalChain来检查一个输出是否简洁。首先,创建评估链以预测输出是否“简洁”。

from langchain.evaluation import load_evaluator

from dotenv import load_dotenv  # 导入从 .env 文件加载环境变量的函数
load_dotenv()  # 调用函数实际加载环境变量

from langchain.globals import set_debug  # 导入在 langchain 中设置调试模式的函数
set_debug(True)  # 启用 langchain 的调试模式

# from langchain.evaluation import load_evaluator
# evaluator = load_evaluator("criteria", criteria="conciseness")

# This is equivalent to loading using the enum
from langchain.evaluation import EvaluatorType
evaluator = load_evaluator(EvaluatorType.CRITERIA, criteria="conciseness")

eval_result = evaluator.evaluate_strings(
    prediction="What's 2+2? That's an elementary question. The answer you're looking for is that two and two is four.",
    input="What's 2+2?",
)
print('eval_result >> ', eval_result)

3.1 输出格式

所有字符串评估器都暴露了一个 evaluate_strings(或 async aevaluate_strings)方法,该方法接受:

  • 输入input (str)- 发送给agent代理的输入。
  • 预测 prediction(str)- 预测的回应。

评估器返回包含以下值的字典:- 分数:二进制整数0到1,其中1意味着输出符合标准,0则相反 - 值:对应分数的“Y”或“N” - 推理:从LLM生成的“思维链条推理”字符串,在创建分数之前产生。

输出

(.venv)  ~/Workspace/LLM/langchain-llm-app/ [develop*] python Evaluate/criteria.py                                                       ⏎
[chain/start] [1:chain:CriteriaEvalChain] Entering Chain run with input:
{
  "input": "What's 2+2?",
  "output": "What's 2+2? That's an elementary question. The answer you're looking for is that two and two is four."
}
[llm/start] [1:chain:CriteriaEvalChain > 2:llm:ChatOpenAI] Entering LLM run with input:
{
  "prompts": [
    "Human: You are assessing a submitted answer on a given task or input based on a set of criteria. Here is the data:\n[BEGIN DATA]\n***\n[Input]: What's 2+2?\n***\n[Submission]: What's 2+2? That's an elementary question. The answer you're looking for is that two and two is four.\n***\n[Criteria]: conciseness: Is the submission concise and to the point?\n***\n[END DATA]\nDoes the submission meet the Criteria? First, write out in a step by step manner your reasoning about each criterion to be sure that your conclusion is correct. Avoid simply stating the correct answers at the outset. Then print only the single character \"Y\" or \"N\" (without quotes or punctuation) on its own line corresponding to the correct answer of whether the submission meets all criteria. At the end, repeat just the letter again by itself on a new line."
  ]
}
[llm/end] [1:chain:CriteriaEvalChain > 2:llm:ChatOpenAI] [7.17s] Exiting LLM run with output:
{
  "generations": [
    [
      {
        "text": "The criterion to evaluate the submission is \"conciseness\". This requires the answer to be brief, to the point, and without unnecessary information or explanation.\n\nAssessing the submission, the responder did not solely provide the answer. The submission included additional commentary: \"That's an elementary question.\" This part of the response is not integral to answering the question and thus adds unnecessary length and detail.\n\nFurthermore, the phrase, \"The answer you're looking for is\" also adds unneeded length to the answer. A more concise response would simply state the answer: \"four\".\n\nConsidering these points, the submission does not meet the criterion of conciseness, as it contains unnecessary extraneous detail and is not as brief as it could be.\n\nN\nN",
        "generation_info": {
          "finish_reason": "stop",
          "logprobs": null
        },
        "type": "ChatGeneration",
        "message": {
          "lc": 1,
          "type": "constructor",
          "id": [
            "langchain",
            "schema",
            "messages",
            "AIMessage"
          ],
          "kwargs": {
            "content": "The criterion to evaluate the submission is \"conciseness\". This requires the answer to be brief, to the point, and without unnecessary information or explanation.\n\nAssessing the submission, the responder did not solely provide the answer. The submission included additional commentary: \"That's an elementary question.\" This part of the response is not integral to answering the question and thus adds unnecessary length and detail.\n\nFurthermore, the phrase, \"The answer you're looking for is\" also adds unneeded length to the answer. A more concise response would simply state the answer: \"four\".\n\nConsidering these points, the submission does not meet the criterion of conciseness, as it contains unnecessary extraneous detail and is not as brief as it could be.\n\nN\nN",
            "additional_kwargs": {}
          }
        }
      }
    ]
  ],
  "llm_output": {
    "token_usage": {
      "completion_tokens": 151,
      "prompt_tokens": 192,
      "total_tokens": 343
    },
    "model_name": "gpt-4",
    "system_fingerprint": null
  },
  "run": null
}
[chain/end] [1:chain:CriteriaEvalChain] [7.18s] Exiting Chain run with output:
{
  "results": {
    "reasoning": "The criterion to evaluate the submission is \"conciseness\". This requires the answer to be brief, to the point, and without unnecessary information or explanation.\n\nAssessing the submission, the responder did not solely provide the answer. The submission included additional commentary: \"That's an elementary question.\" This part of the response is not integral to answering the question and thus adds unnecessary length and detail.\n\nFurthermore, the phrase, \"The answer you're looking for is\" also adds unneeded length to the answer. A more concise response would simply state the answer: \"four\".\n\nConsidering these points, the submission does not meet the criterion of conciseness, as it contains unnecessary extraneous detail and is not as brief as it could be.\n\nN",
    "value": "N",
    "score": 0
  }
}
eval_result >>  {'reasoning': 'The criterion to evaluate the submission is "conciseness". This requires the answer to be brief, to the point, and without unnecessary information or explanation.\n\nAssessing the submission, the responder did not solely provide the answer. The submission included additional commentary: "That\'s an elementary question." This part of the response is not integral to answering the question and thus adds unnecessary length and detail.\n\nFurthermore, the phrase, "The answer you\'re looking for is" also adds unneeded length to the answer. A more concise response would simply state the answer: "four".\n\nConsidering these points, the submission does not meet the criterion of conciseness, as it contains unnecessary extraneous detail and is not as brief as it could be.\n\nN', 'value': 'N', 'score': 0}

代码

https://github.com/zgpeace/pets-name-langchain/tree/develop

参考

https://python.langchain.com/docs/guides/evaluation/string/criteria_eval_chain

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1382967.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【STK】手把手教你利用STK进行关联分析仿真01-STK/CAT模块介绍

关联分析工具(Conjunction Analysis Tool )主要用于分析航天发射或卫星在轨运行过程中与其他目标之间的接近情况,关联分析包括: 接近分析工具 Close Approach Tool CAT高级接近分析工具 AdvCAT激光接近分析工具 LaserCAT发射窗口分析工具 Launch Window Analysis今天主要介绍…

RAG 评估框架 -- RAGAS

原文 引入 RAG(Retrieval Augmented Generation)的原因 随着ChatGPT的推出,很多人都理所当然直接用LLM当作知识库回答问题。这种想法有两个明显的缺点: LLM无法得知在训练之后所发生的事情,因此无法回答相关的问题存…

大面积光源HUD阳光倒灌实验装置太阳光模拟器

背景 1.根据现在市场上一些量产的hud的结构和原理可知,hud中最重要的零件之一就是凹面镜(自由曲面),hud利用凹面镜放大投影的光学原理进行投影成像。当发生阳光倒灌时,太阳光沿着hud正常工作时成像的逆光路,通过挡风玻璃-凹面镜-…

4.【CPP】入门(初始化列表||explicit||static||友元||静态成员变量/函数)

一.初始化列表 1.引入 我们知道在c11中才能在成员对象声明时初始化,像下面这样。 class Date { public: Date(int year, int month, int day): _year(year), _month(month), _day(day) {} private: int _year2000; int _month12; int _day20; };注意:…

CMake tasks.json launch.json

hehedalinux:~/Linux/cmake/cmakeClass$ tree . ├── CMakeLists.txt ├── include │ ├── Gun.h │ └── Soldier.h ├── main.cpp └── src├── Gun.cpp└── Soldier.cpp2 directories, 6 files hehedalinux:~/Linux/cmake/cmakeClass$ launch.json&am…

每日一题——LeetCode1189.气球的最大数量

方法一 个人方法: 统计text字符串中b、a、l、o、n 这几个字符出现的次数 l和n需要两个才能拼成一个balloon,所以碰到l和o加1,其他字符加2 最后求出出现次数最少的那个字符再除以2就是能拼凑成的单词数量,避免出现小数要使用向下…

godot初学笔记

godot开发工具下载地址 godot下载地址 godot入门视频 godot入门教学b站地址 素材下载地址 素材下载地址 最终成品图 2D3D如何切换 添加2D场景 添加其他节点 添加人物节点 设置人物为接地 给人物添加Sprite 2d 给人物设置材质 解决材质糊的问题 设置材质包切割 在场景中实…

CISP注册信息系统安全认证,2024年1月20日即将开课~想了解点击查看

CISP注册信息系统安全认证 1🈷20日 开课~ 想报名的必须提前预约啦 👇👇👇 课程介绍 本课程包括10个独立的知识域(安全工程与运营、计算环境安全、软件安全开发、网络安全监管、物理与网络通信安全、信息安全保障、信…

杨中科 .NETCORE EFCORE 第一部分 基本使用

一 、什么是EF Core 什么是ORM 1、说明: 本课程需要你有数据库、SOL等基础知识。 2、ORM: ObjectRelational Mapping。让开发者用对象操作的形式操作关系数据库 比如插入: User user new User(Name"admin"Password"123”; orm.Save(user);比如查询: Book b…

自动粘贴文本:高效复制中国邮政编码,提升效率,释放创意

在快节奏的现代生活中,时间就是金钱,效率就是生命。中国邮政EMS,作为您的快递服务首选,一直致力于提供更加便捷、高效的寄递体验。今天,我们隆重推出全新功能——"自动粘贴文本",让您轻松复制邮政…

test-02-test case generate 测试用例生成 EvoSuite 介绍

拓展阅读 junit5 系列 基于 junit5 实现 junitperf 源码分析 Auto generate mock data for java test.(便于 Java 测试自动生成对象信息) Junit performance rely on junit5 and jdk8.(java 性能测试框架。性能测试。压测。测试报告生成。) 拓展阅读 自动生成测试用例 什么…

Centos7 安装与卸载mysql

卸载 ps ajx | grep mysql : 查看当前服务器是否有mysql 没有的话就不需要卸载咯。 centos7 通过yum下载安装包通常是以.rpm为后缀,rpm -qa 可以查看当前服务器上所有的安装包: rpm -qa | grep mysql | xargs yum -y remove :将查询到的mysql…

第三十九周:文献阅读+Transformer

目录 摘要 Abstract 文献阅读:CNN与LSTM在水质预测中的应用 现有问题 提出方法 相关模型 CNN LSTM CNN-LSTM神经网络模型 模型框架 CNN-LSTM神经网络 研究实验 数据集 模型评估指标 数据预处理 实验设计与结果 研究贡献 Transformer Encoder-Dec…

ES6(ECMAScript 6.0)

ES6 学习链接ES6什么是ES6?ECMAScript 和 JavaScript 的关系 ECMAScript各版本新增特性ES6 块级作用域 let 学习链接 ES6简介 ECMAScript简介及特性(科普!很详细!!!!) 20分钟上手ES…

Java多线程并发篇----第十三篇

系列文章目录 文章目录 系列文章目录前言一、Semaphore 信号量二、Semaphore 与 ReentrantLock 区别三、可重入锁(递归锁)四、公平锁与非公平锁前言 前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站,这篇文章男女通用,…

【促销定价】背后的算法技术 2 - 数据预处理生成

【促销定价】背后的算法技术 2 - 数据预处理生成 01 数据探查02 数据清洗03 数据聚合04 数据补全05 小结参考文献 导读:在日常生活中,我们经常会遇见线上/线下商家推出各类打折、满减、赠品、新人价、优惠券、捆绑销售等促销活动。一次成功的促销对于消费…

什么类型的企业需要工单系统?适用场景与优势分析

在现代商业中,必须高效地管理与跟踪大量任务和工作流程。对一些企业而言,处理很多订单是一项具有挑战性的任务。此订单可能来自客户需求、内部问题反馈或各种业务流程中的任务分配。可是,如果没有好的解决方法,这类订单可能会致使…

用LM Studio:2分钟在本地免费部署大语言模型,替代ChatGPT

你想在本地使用类似ChatGPT 的大语言模型么?LM Studio 可以帮你2分钟实现ChatGPT的功能,而且可以切换很多不同类型的大语言模型,同时支持在Windows和MAC上的PC端部署。 LM Studio是一款面向开发者的友好工具,特别适合那些想要探索…

Costco攻入山姆大本营

01 Costco深圳店开业火爆 “我今天不去Costco,早上还没开业,路上就已经堵车了,看来今天人很多,过几天再去”,原本计划在Costco开业当天去逛逛的张芸(化名)无奈只能放弃。 家住在Costco深圳店旁…