Skip to content

Commit 9dad16d

Browse files
committed
add doc for tool-calling and evaluation
1 parent 37bab47 commit 9dad16d

File tree

6 files changed

+731
-0
lines changed

6 files changed

+731
-0
lines changed
128 KB
Loading

docs/statics/tc-core-component.png

112 KB
Loading

docs/statics/tc-returnDirect.png

104 KB
Loading

docs/statics/tool-context.png

122 KB
Loading

spring-ai-evaluation/README.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
## 模型评估
2+
在测试 AI 应用时,需要对生成的内容进行评估,以避免模型输出“幻觉”信息(即虚构、不准确的内容)。一种常见的评估方式是直接借助 AI 模型本身来判断答案的质量。此时应选用一个更适合评估任务的模型,它未必和用于生成答案的模型相同。在 Spring AI 中,专门用于结果评估的接口是 `Evaluator`,它的定义如下:
3+
4+
```java
5+
@FunctionalInterface
6+
public interface Evaluator {
7+
EvaluationResponse evaluate(EvaluationRequest evaluationRequest);
8+
}
9+
```
10+
11+
评估时传入的是一个特定的请求类型 EvaluationRequest,它包括几个关键的属性,如下:
12+
13+
```java
14+
public class EvaluationRequest {
15+
// 用户输入的原始问题
16+
private final String userText;
17+
// 上下文信息,可以是文档,历史对话等等
18+
private final List<Content> dataList;
19+
// 模型针对用户问题返回的结果
20+
private final String responseContent;
21+
22+
public EvaluationRequest(String userText, List<Content> dataList, String responseContent) {
23+
this.userText = userText;
24+
this.dataList = dataList;
25+
this.responseContent = responseContent;
26+
}
27+
...
28+
}
29+
```
30+
31+
32+
33+
# 关联评估
34+
`RelevancyEvaluator` 是对 `Evaluator` 接口的具体实现,主要用于判断 AI 所生成的回答是否与检索到的上下文信息相关联。这项评估机制常被用来检测 RAG(检索增强生成)流程中的响应质量,确保模型的回复能贴合用户提问及相关内容。评估过程依赖三个要素:**用户输入、AI 模型的回答****以及检索得到的上下文内容**。系统会通过提示词模板向 AI 发出提问,然后判断模型回答是否具有上下文相关性。下面是 `RelevancyEvaluator` 默认使用的提示模板内容:
35+
36+
```java
37+
Your task is to evaluate if the response for the query
38+
is in line with the context information provided.
39+
40+
You have two options to answer. Either YES or NO.
41+
42+
Answer YES, if the response for the query
43+
is in line with context information otherwise NO.
44+
45+
Query:
46+
{query}
47+
48+
Response:
49+
{response}
50+
51+
Context:
52+
{context}
53+
54+
Answer:
55+
```
56+
57+
58+
59+
## 案例介绍
60+
这里我们使用 deepseek 来评估豆包的输出结果,关于如何在项目中使用多个 chatmodel 可以参考 chat client api 部分。
61+
62+
```java
63+
/**
64+
* 评估消息内容
65+
*
66+
* @param message
67+
* @return
68+
*/
69+
public EvaluationResponse evaluate(String message) {
70+
// 使用 OpenAI 模型进行评估
71+
String openAiResponse = openAiChatClient.prompt().user(message).call().content();
72+
String question = message;
73+
EvaluationRequest evaluationRequest = new EvaluationRequest(
74+
// The original user question
75+
question,
76+
// context data
77+
Collections.emptyList(),
78+
// The AI model's response
79+
openAiResponse);
80+
RelevancyEvaluator evaluator = new RelevancyEvaluator(ChatClient.builder(this.deepSeekChatModel));
81+
EvaluationResponse evaluationResponse = evaluator.evaluate(evaluationRequest);
82+
return evaluationResponse;
83+
}
84+
```
85+
86+
87+
88+
这里主要看下 EvaluationResponse 的几个属性
89+
90+
```java
91+
// 表示是否通过评估
92+
private final boolean pass;
93+
// 评估打分 0~1
94+
private final float score;
95+
// 评估反馈
96+
private final String feedback;
97+
// 一些元数据信息
98+
private final Map<String, Object> metadata;
99+
```
100+
101+
评估结果主要是针对 pass 和 score,如果对于结果要求不是很高,直接关注 pass 即可;如果对结果有求较高,需要结合评估打分来控制。
102+

0 commit comments

Comments
 (0)