Loading [MathJax]/jax/input/TeX/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >聊聊Spring AI的Evaluator

聊聊Spring AI的Evaluator

作者头像
code4it
发布于 2025-04-15 06:07:48
发布于 2025-04-15 06:07:48
6500
代码可运行
举报
文章被收录于专栏:码匠的流水账码匠的流水账
运行总次数:0
代码可运行

本文主要研究一下Spring AI的Evaluator

Evaluator

spring-ai-client-chat/src/main/java/org/springframework/ai/evaluation/Evaluator.java

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@FunctionalInterface
public interface Evaluator {

	EvaluationResponse evaluate(EvaluationRequest evaluationRequest);

	default String doGetSupportingData(EvaluationRequest evaluationRequest) {
		List<Document> data = evaluationRequest.getDataList();
		return data.stream()
			.map(Document::getText)
			.filter(StringUtils::hasText)
			.collect(Collectors.joining(System.lineSeparator()));
	}

}

Evaluator接口定义了evaluate方法,用于对ai生成的内容进行评估,避免AI没有产生幻觉式的响应,它有两个实现,分别是RelevancyEvaluator、FactCheckingEvaluator

EvaluationRequest

org/springframework/ai/evaluation/EvaluationRequest.java

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class EvaluationRequest {

	private final String userText;

	private final List<Document> dataList;

	private final String responseContent;

	public EvaluationRequest(String userText, String responseContent) {
		this(userText, Collections.emptyList(), responseContent);
	}

	public EvaluationRequest(List<Document> dataList, String responseContent) {
		this("", dataList, responseContent);
	}

	public EvaluationRequest(String userText, List<Document> dataList, String responseContent) {
		this.userText = userText;
		this.dataList = dataList;
		this.responseContent = responseContent;
	}

	//......
}	

EvaluationRequest定义了userText、dataList、responseContent属性,其中userText是用户的输入,dataList是上下文数据,比如RAG追加的内容,responseContent是AI模型的响应

EvaluationResponse

org/springframework/ai/evaluation/EvaluationResponse.java

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class EvaluationResponse {

	private final boolean pass;

	private final float score;

	private final String feedback;

	private final Map<String, Object> metadata;

	@Deprecated
	public EvaluationResponse(boolean pass, float score, String feedback, Map<String, Object> metadata) {
		this.pass = pass;
		this.score = score;
		this.feedback = feedback;
		this.metadata = metadata;
	}

	public EvaluationResponse(boolean pass, String feedback, Map<String, Object> metadata) {
		this.pass = pass;
		this.score = 0;
		this.feedback = feedback;
		this.metadata = metadata;
	}

	//......
}	

EvaluationResponse定义了pass、score、feedback、metadata属性

RelevancyEvaluator

org/springframework/ai/evaluation/RelevancyEvaluator.java

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class RelevancyEvaluator implements Evaluator {

	private static final String DEFAULT_EVALUATION_PROMPT_TEXT = """
				Your task is to evaluate if the response for the query
				is in line with the context information provided.\\n
				You have two options to answer. Either YES/ NO.\\n
				Answer - YES, if the response for the query
				is in line with context information otherwise NO.\\n
				Query: \\n {query}\\n
				Response: \\n {response}\\n
				Context: \\n {context}\\n
				Answer: "
			""";

	private final ChatClient.Builder chatClientBuilder;

	public RelevancyEvaluator(ChatClient.Builder chatClientBuilder) {
		this.chatClientBuilder = chatClientBuilder;
	}

	@Override
	public EvaluationResponse evaluate(EvaluationRequest evaluationRequest) {

		var response = evaluationRequest.getResponseContent();
		var context = doGetSupportingData(evaluationRequest);

		String evaluationResponse = this.chatClientBuilder.build()
			.prompt()
			.user(userSpec -> userSpec.text(DEFAULT_EVALUATION_PROMPT_TEXT)
				.param("query", evaluationRequest.getUserText())
				.param("response", response)
				.param("context", context))
			.call()
			.content();

		boolean passing = false;
		float score = 0;
		if (evaluationResponse.toLowerCase().contains("yes")) {
			passing = true;
			score = 1;
		}

		return new EvaluationResponse(passing, score, "", Collections.emptyMap());
	}

}

RelevancyEvaluator让AI去评估响应是否与上下文信息一致,给出yes或者no的结果,如果是yes则passing为true,score为1,否则默认passing为false,score为0

示例

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@Test
void testEvaluation() {

    dataController.delete();
    dataController.load();

    String userText = "What is the purpose of Carina?";

    ChatResponse response = ChatClient.builder(chatModel)
            .build().prompt()
            .advisors(new QuestionAnswerAdvisor(vectorStore))
            .user(userText)
            .call()
            .chatResponse();
    String responseContent = response.getResult().getOutput().getContent();

    var relevancyEvaluator = new RelevancyEvaluator(ChatClient.builder(chatModel));

    EvaluationRequest evaluationRequest = new EvaluationRequest(userText,
            (List<Content>) response.getMetadata().get(QuestionAnswerAdvisor.RETRIEVED_DOCUMENTS), responseContent);

    EvaluationResponse evaluationResponse = relevancyEvaluator.evaluate(evaluationRequest);

    assertTrue(evaluationResponse.isPass(), "Response is not relevant to the question");

}

这里先用userText去问下AI,然后将responseContent、QuestionAnswerAdvisor.RETRIEVED_DOCUMENTS一起丢给relevancyEvaluator,再用AI去评估一下

FactCheckingEvaluator

org/springframework/ai/evaluation/FactCheckingEvaluator.java

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class FactCheckingEvaluator implements Evaluator {

	private static final String DEFAULT_EVALUATION_PROMPT_TEXT = """
				Evaluate whether or not the following claim is supported by the provided document.
				Respond with "yes" if the claim is supported, or "no" if it is not.
				Document: \\n {document}\\n
				Claim: \\n {claim}
			""";

	private static final String BESPOKE_EVALUATION_PROMPT_TEXT = """
				Document: \\n {document}\\n
				Claim: \\n {claim}
			""";

	private final ChatClient.Builder chatClientBuilder;

	private final String evaluationPrompt;

	/**
	 * Constructs a new FactCheckingEvaluator with the provided ChatClient.Builder. Uses
	 * the default evaluation prompt suitable for general purpose LLMs.
	 * @param chatClientBuilder The builder for the ChatClient used to perform the
	 * evaluation
	 */
	public FactCheckingEvaluator(ChatClient.Builder chatClientBuilder) {
		this(chatClientBuilder, DEFAULT_EVALUATION_PROMPT_TEXT);
	}

	/**
	 * Constructs a new FactCheckingEvaluator with the provided ChatClient.Builder and
	 * evaluation prompt.
	 * @param chatClientBuilder The builder for the ChatClient used to perform the
	 * evaluation
	 * @param evaluationPrompt The prompt text to use for evaluation
	 */
	public FactCheckingEvaluator(ChatClient.Builder chatClientBuilder, String evaluationPrompt) {
		this.chatClientBuilder = chatClientBuilder;
		this.evaluationPrompt = evaluationPrompt;
	}

	/**
	 * Creates a FactCheckingEvaluator configured for use with the Bespoke Minicheck
	 * model.
	 * @param chatClientBuilder The builder for the ChatClient used to perform the
	 * evaluation
	 * @return A FactCheckingEvaluator configured for Bespoke Minicheck
	 */
	public static FactCheckingEvaluator forBespokeMinicheck(ChatClient.Builder chatClientBuilder) {
		return new FactCheckingEvaluator(chatClientBuilder, BESPOKE_EVALUATION_PROMPT_TEXT);
	}

	/**
	 * Evaluates whether the response content in the EvaluationRequest is factually
	 * supported by the context provided in the same request.
	 * @param evaluationRequest The request containing the response to be evaluated and
	 * the supporting context
	 * @return An EvaluationResponse indicating whether the claim is supported by the
	 * document
	 */
	@Override
	public EvaluationResponse evaluate(EvaluationRequest evaluationRequest) {
		var response = evaluationRequest.getResponseContent();
		var context = doGetSupportingData(evaluationRequest);

		String evaluationResponse = this.chatClientBuilder.build()
			.prompt()
			.user(userSpec -> userSpec.text(this.evaluationPrompt).param("document", context).param("claim", response))
			.call()
			.content();

		boolean passing = evaluationResponse.equalsIgnoreCase("yes");
		return new EvaluationResponse(passing, "", Collections.emptyMap());
	}

}

FactCheckingEvaluator旨在评估AI生成的响应在给定上下文中的事实准确性。该评估器通过验证给定的声明(claim)是否逻辑上支持提供的上下文(document),帮助检测和减少AI输出中的幻觉现象;在使用FactCheckingEvaluator时,claim和document会被提交给AI模型进行评估。为了更高效地完成这一任务,可以使用更小且更高效的AI模型,例如Bespoke的Minicheck。Minicheck 是一种专门设计用于事实核查的小型高效模型,它通过分析事实信息片段和生成的输出,验证声明是否与文档相符。如果文档能够证实声明的真实性,模型将回答“是”,否则回答“否”。这种模型特别适用于检索增强型生成(RAG)应用,确保生成的答案基于上下文信息。

示例

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@Test
void testFactChecking() {
  // Set up the Ollama API
  OllamaApi ollamaApi = new OllamaApi("http://localhost:11434");

  ChatModel chatModel = new OllamaChatModel(ollamaApi,
				OllamaOptions.builder().model(BESPOKE_MINICHECK).numPredict(2).temperature(0.0d).build())


  // Create the FactCheckingEvaluator
  var factCheckingEvaluator = new FactCheckingEvaluator(ChatClient.builder(chatModel));

  // Example context and claim
  String context = "The Earth is the third planet from the Sun and the only astronomical object known to harbor life.";
  String claim = "The Earth is the fourth planet from the Sun.";

  // Create an EvaluationRequest
  EvaluationRequest evaluationRequest = new EvaluationRequest(context, Collections.emptyList(), claim);

  // Perform the evaluation
  EvaluationResponse evaluationResponse = factCheckingEvaluator.evaluate(evaluationRequest);

  assertFalse(evaluationResponse.isPass(), "The claim should not be supported by the context");

}

这里使用ollama调用bespoke-minicheck模型,其temperature设置为0.0,之后把context与claim都传递给factCheckingEvaluator去评估

小结

Spring AI提供了Evaluator接口定义了evaluate方法,用于对ai生成的内容进行评估,避免AI没有产生幻觉式的响应,它有两个实现,分别是RelevancyEvaluator、FactCheckingEvaluator。RelevancyEvaluator用于评估相关性,FactCheckingEvaluator用于评估事实准确性。

doc

  • testing
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2025-04-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
redis与mysql的数据一致性问题(事务一致性)
案例:考虑一个在线购物应用,其中有一个购物车服务,购物车信息存储在MySQL中,同时为了提高性能,购物车中的商品数量也被缓存到了Redis。用户在购物车中添加商品时,需要保证购物车数量在MySQL和Redis中的更新是原子性的,以避免不一致的情况。
GeekLiHua
2025/01/21
1370
【详解】为已安装nginx动态添加模块
Nginx是一款轻量级、高性能的HTTP和反向代理服务器,也是IMAP/POP3/SMTP代理服务器。在Nginx的使用过程中,我们可能需要添加一些额外的模块来扩展其功能。虽然Nginx官方在编译时提供了很多模块供我们选择,但有时候我们可能需要添加一些第三方的模块。在Nginx版本1.9.11之后,Nginx支持了动态模块加载,这使得我们可以在不重新编译Nginx的情况下添加新的模块。
大盘鸡拌面
2024/12/28
4540
Django 实现购物车功能
  购物车思路:使用 session 功能识别不同浏览器用户,使得用户不管是否登录了网站,均能够把想要购买的产品放在某个地方,之后随时可以显示或修改要购买的产品,等确定了之后再下订单,购物车可以用来暂存商品。
希希里之海
2018/08/02
2.7K0
Django 实现购物车功能
数据库的事务
原子性(Atomicity):事务中的所有操作要么全部完成,要么全部不完成,不会结束在中间某个环节。
GeekLiHua
2024/08/30
1130
Python缓存技术(Memcached、Redis)面试题解析
缓存技术在现代软件开发中扮演着至关重要的角色,能够显著提升系统的性能与响应速度。Memcached与Redis作为两种广泛使用的内存键值存储系统,常被应用于Python项目中以实现高效的缓存解决方案。本篇博客将深入浅出地探讨Python面试中关于Memcached与Redis的常见问题、易错点以及应对策略,并结合实例代码进行讲解。
Jimaks
2024/04/21
1370
小程序电商平台开发指南:从产品设计到技术实现
小程序电商平台已经成为新的电商趋势。本文将全面介绍如何开发一个小程序电商平台,包括产品设计、交互设计、技术架构等方面的内容。我们将详细分析电商平台的各个功能模块,并提供一些技术实现的建议和注意事项。
陆业聪
2024/08/07
5670
小程序电商平台开发指南:从产品设计到技术实现
C语言之超市商品管理系统
本文介绍了一个基于C语言实现的超市商品管理系统,旨在为管理员和消费者提供高效的商品管理与购物体验。系统通过简洁的设计和实用的功能模块,满足了小型超市或零售店的日常运营需求。
LucianaiB
2025/01/21
1920
C语言之超市商品管理系统
woocommerce shortcode短代码调用
WooCommerce配备了很多shortcode短代码(简码),可以直接在post帖子和page页面内插入内容,方便展示产品、分类等。比如直接在文章编辑时直接插入[products],或者在php文
ytkah
2023/03/14
12.7K0
woocommerce shortcode短代码调用
集成测试的实践与思考
前面的文章聊过测试过程效率提升和演变,也分享了我对于单元测试的一些实践和思考。这篇文章接着上篇单元测试的内容,聊聊集成测试的特点,要解决什么问题,以及实践的注意事项。
老_张
2023/11/30
2390
集成测试的实践与思考
【Java 进阶篇】Session 使用详解
欢迎来到这篇关于Java Session的详尽解释,我们将从基础开始,深入研究Session的概念、用法和最佳实践。不管您是一个新手还是一个有经验的Java开发人员,这篇博客都将为您提供有关Session的全面指南。
繁依Fanyi
2023/11/07
1.5K0
【Java 进阶篇】Session 使用详解
如何一步一步用DDD设计一个电商网站(四)—— 把商品卖给用户
上篇中我们讲述了“把商品卖给用户”中的商品和用户的初步设计。现在把剩余的“卖”这个动作给做了。这里提醒一下,正常情况下,我们的每一步业务设计都需要和领域专家进行沟通,尽可能的符合通用语言的表述。这里的领域专家包括但不限于当前开发团队中对这块业务最了解的开发人员、系统实际的使用人等。
Zachary_ZF
2018/09/10
9520
如何一步一步用DDD设计一个电商网站(四)—— 把商品卖给用户
【详解】ElasticSearchQuery查询方式
Elasticsearch(ES)是一个基于Lucene的高性能、分布式、开源搜索引擎,提供了多种灵活的查询方式以满足不同场景下的需求。在本文中,我们将深入探讨Elasticsearch的查询方式,并通过实例展示其用法。
大盘鸡拌面
2024/12/29
8490
Redux 快速上手指南
如果要用一句话来概括Redux,那么可以使用官网的这句话:**Redux是针对JavaScript应用的可预测状态容器。**此句话虽然简单,但包含了以下几个含义:
xiangzhihong
2022/11/30
1.4K0
从零到部署:用 Vue 和 Express 实现迷你全栈电商应用(四)
随着前端应用的日渐复杂,状态和数据管理成为了构建大型应用的关键。受 Redux 等项目的启发,Vue.js 团队也量身定做了状态管理库 Vuex。在这篇教程中,我们将带你熟悉 Store、Mutation 和 Action 三大关键概念,并升级迷你商城应用的前端代码。
一只图雀
2020/04/07
2.1K0
Spring Boot电商项目
前言:这是我学SpringBoot以来第一个实际应用性的项目,还是有必要写个笔记记录一下,以便之后复习和部分知识重复利用。
害恶细君
2022/11/22
1.5K0
Spring Boot电商项目
SQL 进阶指南:掌握这些高阶技巧,让你的数据分析快人一步!
你以为 SQL 只是简单的 SELECT * FROM table?那你可就大错特错了! 在这个数据驱动的时代,真正的高手早已抛弃低效的查询方式,玩转窗口函数、递归 CTE、动态透视表,甚至用近似计算让百亿级数据秒出结果! 还在为 SQL 运行慢、分析难、报表卡死而苦恼? 别担心,这篇文章带你深入 SQL 世界,从业务思维出发,手把手拆解 用户增长、行为分析、订单转化、留存率 等核心场景,让你的数据分析能力直接起飞!
睡前大数据
2025/03/25
1180
SQL 进阶指南:掌握这些高阶技巧,让你的数据分析快人一步!
什么是Redux的三大原则?
Redux 是一个用于 JavaScript 应用程序的状态管理库,特别是在 React 应用中广泛使用。它通过一系列的核心原则来管理应用的状态,使得状态管理变得更加可预测和可维护。Redux 的三大原则是:
王小婷
2025/05/25
770
【工作篇】接口幂等问题探究
最近遇到一些问题,表单重复提交,导致插入重复数据到数据库,这里查询一些通用的方案,自己都实践一下,以后好回顾。
玖柒的小窝
2021/12/17
7610
美多商城项目(八)
It is our attitude at the beginning of a difficult task which, more than anything else, will affect its successful outcome.
小闫同学啊
2019/07/18
1.1K0
美多商城项目(八)
秒杀技术瓶颈与解决之道
秒杀(Spike)是电子商务领域的一项重要业务,指的是在短时间内,大量用户竞相购买某一特定商品或服务。秒杀活动常常伴随着高并发、高延迟故障、商品售罄等挑战。本文将深入讨论秒杀技术瓶颈的原因,并提出一些解决之道,帮助您更好地应对秒杀活动中的挑战。
疯狂的KK
2023/09/25
4780
秒杀技术瓶颈与解决之道
相关推荐
redis与mysql的数据一致性问题(事务一致性)
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验