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

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_evaluatorfrom 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.mzph.cn/news/615325.shtml

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

相关文章

010集:with as 代码块读写关闭文件—python基础入门实例

接009集: 读写文本文件的相关方法如下。 read ( size-1 ):从文件中读取字符串, size 限制读取的字符数, si ze-1 指对读取的字符数没有限制。 readline ( size-1 ):在…

react hooks 高德地图的应用

一、准备 1.登录控制台 登录 高德开放平台控制台,如果没有开发者账号,请 注册开发者。 2.创建 key 进入应用管理,创建新应用,新应用中添加 key,服务平台选择 Web端(JS API)。 3.获取 key 和密钥 创建成功后&#x…

Mysql的in与exits

Mysql的in与exits IN和EXISTS是MySQL中用于子查询的两种不同的条件操作符。它们在使用和实现上有一些区别。 IN 操作符: IN操作符用于判断一个值是否在一个集合内。它可以用于子查询中,检查主查询的某一列是否在子查询返回的结果集中。 SELECT colum…

linux 测试网络传输速度

在linux和macos中看不到文件复制速度,往往不清楚smb或者afp的传输速度。 dd命令可以测试磁盘io速度,当然也可以测试网络传输速度。 首先要挂载afp或者smb,此步略过。 然后准备好一定体积的测试文件(最好大点,比如1G以上),使用以下命令测试传输速度: dd if=/smb/TestI…

四、C++内存管理

1 C/C内存分布 在学习C的内存管理方式之前&#xff0c;我们先来看一道有关C/C内存分布的题目&#xff1a; 阅读下面的代码&#xff0c;回答相关问题&#xff1a; #include <iostream> using namespace std; int globalVar 1; static int staticGlobalVar 1; int main…

【C++进阶06】红黑树图文详解及C++模拟实现红黑树

一、红黑树的概念及性质 1.1 红黑树的概念 AVL树用平衡因子让树达到高度平衡 红黑树可以认为是AVL树的改良 通过给每个节点标记颜色让树接近平衡 以减少树在插入节点的旋转 在每个结点新增一个存储位表示结点颜色 可以是Red或Black 通过对任何一条从根到叶子的路径上 各个结点…

PaddleSeg学习4——paddle模型使用TensorRT推理(c++)

paddle模型使用TensorRT推理 1 模型末端添加softmax和argmax算子2 paddle模型转onnx模型3 onnx模型转TensorRT模型3.1 安装TensorRT-8.5.3.13.2 使用 trtexec 将onnx模型编译优化导出为engine模型 4 TensorRT模型推理测试5 完整代码6 测试结果 1 模型末端添加softmax和argmax算…

谁是linux SIGKILL的凶手---linux审计日志排障的杀手锏

一、背景描述 在我们的技术生涯中&#xff0c;总会碰到那精彩的一瞬间&#xff0c;哪怕是一瞬间&#xff0c;曾经的熟悉php内核的同事和今天碰到的排障瞬间&#xff0c;都是我技术生涯中那精彩的一瞬间&#xff0c;写一篇日志记录一下。 今天碰到一个问题&#xff0c;就是采集…

2022 年全国职业院校技能大赛高职组云计算赛项试卷

【赛程名称】云计算赛项第一场-私有云 某企业拟使用OpenStack 搭建一个企业云平台&#xff0c;以实现资源池化弹性管理、企业应用集中管理、统一安全认证和授权等管理。 系统架构如图 1 所示&#xff0c;IP 地址规划如表 1 所示。 图 1 系统架构图 表 1 IP 地址规划 设备…

docker 利用特权模式逃逸并拿下主机

docker 利用特权模式逃逸并拿下主机 在溯源反制过程中&#xff0c;会经常遇到一些有趣的玩法&#xff0c;这里给大家分享一种docker在特权模式下逃逸&#xff0c;并拿下主机权限的玩法。 前言 在一次溯源反制过程中&#xff0c;发现了一个主机&#xff0c;经过资产收集之后&…

[手写爬虫框架],从回忆Python到爬虫原理解析

手写爬虫框架&#xff0c;又名&#xff1a;手写爬虫框架的第1天 数据存储&#xff08;无数据库版&#xff09; HTML正文抽取多媒体文件抽取Email提醒 实战项目&#xff1a;基础爬虫 基础爬虫架构及运行流程URL管理器HTML下载器HTML解析器数据存储器爬虫调度器 从爬虫底层 —&…

网站开发第一弹---HTML01

&#x1f389;欢迎您来到我的MySQL基础复习专栏 ☆* o(≧▽≦)o *☆哈喽~我是小小恶斯法克&#x1f379; ✨博客主页&#xff1a;小小恶斯法克的博客 &#x1f388;该系列文章专栏&#xff1a;网站开发flask框架 &#x1f379;文章作者技术和水平很有限&#xff0c;如果文中出现…

基于pytorch的循环神经网络情感分析系统

任务目标 基于给定数据集&#xff0c;进行数据预处理&#xff0c;搭建以LSTM为基本单元的模型&#xff0c;以Adam优化器对模型进行训练&#xff0c;使用训练后的模型进行预测并计算预测分类的准确率。 数据简介 IMDB数据集是一个对电影评论标注为正向评论与负向评论的数据集…

Spring Boot Starter介绍和实战

引言 Spring Boot Starter 是 Spring Boot 提供的一种机制&#xff0c;用于简化和集成应用程序的依赖管理。通过创建自定义的 Starter&#xff0c;可以将一组相关的依赖打包成一个简单的、可重用的模块&#xff0c;使应用程序的配置和依赖管理更加方便。在本文中&#xff0c;我…

【AI视野·今日NLP 自然语言处理论文速览 第七十二期】Mon, 8 Jan 2024

AI视野今日CS.NLP 自然语言处理论文速览 Mon, 8 Jan 2024 Totally 17 papers &#x1f449;上期速览✈更多精彩请移步主页 Daily Computation and Language Papers DeepSeek LLM: Scaling Open-Source Language Models with Longtermism Authors DeepSeek AI Xiao Bi, Deli Ch…

深度卷积神经网络

目录 1.AlexNet 2. 代码实现 1.AlexNet (1)特征提取 (2)选择核函数来计算相关性&#xff1a;怎么判断在高维空间里面两个点是如何相关的&#xff0c;如果是线性模型就是做内积。 (3)凸优化问题 (4)漂亮的定理 丢弃法的作用就是因为模型太大了&#xff0c;使用它来对模型做…

python连接sqlite3工具类

简单使用python连接sqlite3工具类&#xff0c;代码可根据场景自行抽象 #!/usr/bin/env python # -*- coding: utf-8 -*-import sqlite3class SQLiteHandler:# 示例用法default_db_path "example.db"def __init__(self, db_fileNone):self.db_file db_file or self…

Spring面试整理-Spring的AOP

Spring的面向切面编程(AOP)是其核心功能之一,它允许开发者在不改变原有代码的情况下,增加额外的行为(如日志记录、事务管理、安全检查等)。AOP 通过定义"切面"(aspects)和"通知"(advice)来实现这一功能。 AOP概念 切面(Aspect):切面是模块化的…

TCP 和 UDP 的区别

TCP&#xff08;Transmission Control Protocol&#xff09;和UDP&#xff08;User Datagram Protocol&#xff09;是两种主要的传输层协议&#xff0c;它们在网络通信中有一些关键的区别&#xff1a; &#xff08;1&#xff09;http1 / http2 基于 tcp 协议&#xff0c;https …

XCODE IOS 静态链接库替换升级

XCODE 版本15.2. 一个很久需求没更新的IOS 应用&#xff0c;近来有新需求要开发。 拉下代码运行&#xff0c;出现了个BAD_ACCESS错误。出错的位置位于一个调用的第三方的.a静态库内部。因为调用代码并没有修改&#xff0c;很容易想到可能XCODE相关升级&#xff0c;导致的问题。…