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

这篇具有很好参考价值的文章主要介绍了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 评估、衡量在多样化数据上的性能和完整性

LangChain 71 字符串评估器String Evaluation衡量在多样化数据上的性能和完整性,LLM-Large Language Models,langchain,prompt,chatgpt,人工智能,python

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文章来源地址https://www.toymoban.com/news/detail-793348.html

到了这里,关于LangChain 71 字符串评估器String Evaluation衡量在多样化数据上的性能和完整性的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • redis—String字符串

    目录 前言 1.字符串数据类型 2.常见命令 3.典型应用场景 字符串类型是Redis最基础的数据类型,关于字符串需要特别注意: 1)首先Redis中所有的键的类型都是字符串类型,而且其他几种数据结构也都是在字符串类似基础.上构建的,例如列表和集合的 元素类型是字符串类型,所以

    2024年02月02日
    浏览(44)
  • 字符串分割(split),将字符串按照指定字符进行分割。split(String regex)和split(String regex, int limit)

    一、 split(String regex) 字符串分割,将字符串按照指定字符进行分割,返回的是一个字符串数组。 原理:参数名称是 regex 表示的是以某个字符串进行字符分割。 实例1:根据空格切割 输出结果: 实例2:根据特殊字符进行“.”分割 输出结果: 二、 split(String regex, int limit) 字符

    2024年02月11日
    浏览(44)
  • Java Base64字符串与String字符串互转方法

    在使用String转Base64和Base64转String上有点小问题,特此记录。 结果: 也是跟上面差不多的思路,将Base64转为byte数组,再转为String

    2024年02月15日
    浏览(49)
  • 6.string字符串的比较

    比较结果是真或假, 比较:字符串是1和1比较 然后9和2 比较 大后面就不用比了 对应字符比他大就行了。 结果:如果这个是符合比较运算符的就返回真。反之假 跟具不同的目的选择不同的运算符, 结果只有真和假,运算符不是最后的结果。 总结:如果这个是符合比较运算符

    2024年02月15日
    浏览(36)
  • rust 字符串(String)详解

    rust中的 String ,是一个非常常用的 crate ,它的底层涉及到了rust中的所有权概念,不过这不是本章的内容,如果对rust所有权概念感兴趣的,可以查看另一篇文章:rust所有权 本文的目的还是介绍 String 的基本用法,以及有哪些常用的函数可以使用 字符串,也就是由一系列字符

    2024年02月03日
    浏览(40)
  • 【学到一个新名词】String interning(字符串驻留/字符串内部化)

    作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu Github 公众号:一本正经的瞎扯 在阅读 VictoriaMetrics v1.95.1 的命令行手册的时候,发现这样一段: 什么是 String interning 呢?我通过了 wiki 链接学习了一下。 并且,我还找到了一个使用 String interning 技术

    2024年02月05日
    浏览(52)
  • Java的String(字符串详解)

    主要有三种,一种是直接使用常量去构造,要么使用new String来构造,或者还可以使用字符数组的形式。 String 类型本身并不存储数据,而是存储指向该字符串的引用,所以字符串类型是一个类,s1是一个引用,指向这个类。而这个类有两个成员变量,一个名称为value,这也是一

    2024年02月07日
    浏览(52)
  • Java中的String字符串练习

    目录 Java中的String字符串练习 01-用户登录 02-遍历字符串并统计字符个数 03-字符串拼接 04-字符串反转 注意点 05-金额转化(简单) 代码解释: 06-手机号屏蔽 07-身份证号码查看 易错点: 08-敏感词替换 注意点 toCharArray() 是Java中的一个方法,它用于将字符串转换为字符数组。 方法签

    2024年03月28日
    浏览(55)
  • Java中的字符串String

    目录 一、常用方法 1、字符串构造 2、String对象的比较 (1)、equals方法 (2)、compareTo方法 (3)、compareToIgnoreCase方法(忽略大小写进行比较) 3、字符串查找 4、转化 (1)数值和字符串转化 ​编辑 (2)大小写转换 (3)字符串转数组 (4)格式化 5、字符串替换 6、字符串

    2024年02月05日
    浏览(57)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包