-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathanswer_relevancy_spec.rb
More file actions
203 lines (182 loc) · 6.25 KB
/
answer_relevancy_spec.rb
File metadata and controls
203 lines (182 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
RSpec.describe AutoEvaluation::AnswerRelevancy, :aws_credentials_stubbed do
describe ".call" do
let(:prompts) { AutoEvaluation::Prompts.config.answer_relevancy }
let(:question_message) { "This is a test question message." }
let(:answer_message) { "This is a test answer message." }
let(:statements) { ["This is the first statement.", "This is the second statement."] }
let(:statements_json) { { statements: }.to_json }
let(:user_prompt_statements) do
sprintf(
prompts.fetch(:statements).fetch(:user_prompt),
answer: answer_message,
)
end
let(:statements_tools) { [prompts.fetch(:statements).fetch(:tool_spec)] }
let!(:statements_stub) do
bedrock_invoke_model_openai_oss_tool_call(
user_prompt_statements,
statements_tools,
statements_json,
)
end
let(:verdicts) do
[
{ "verdict" => "yes" },
{ "verdict" => "no", "reason" => "The statement is irrelevant." },
]
end
let(:verdicts_json) { { verdicts: }.to_json }
let(:user_prompt_verdicts) do
sprintf(
prompts.fetch(:verdicts).fetch(:user_prompt),
question: question_message,
statements:,
)
end
let(:verdicts_tools) { [prompts.fetch(:verdicts).fetch(:tool_spec)] }
let!(:verdicts_stub) do
bedrock_invoke_model_openai_oss_tool_call(
user_prompt_verdicts,
verdicts_tools,
verdicts_json,
)
end
let(:reason) { "This is the reason for the score." }
let(:reason_json) { { reason: }.to_json }
let(:user_prompt_reason) do
sprintf(
prompts.fetch(:reason).fetch(:user_prompt),
score: 0.5,
unsuccessful_verdicts_reasons: ["The statement is irrelevant."],
question: question_message,
)
end
let(:reason_tools) { [prompts.fetch(:reason).fetch(:tool_spec)] }
let!(:reason_stub) do
bedrock_invoke_model_openai_oss_tool_call(
user_prompt_reason,
reason_tools,
reason_json,
)
end
it "returns a results object with the expected attributes" do
allow(Clock).to receive(:monotonic_time)
.and_return(200.0, 202.0, 204.0, 206.0, 208.0, 210.0)
result = described_class.call(
question_message:,
answer_message:,
)
expected_llm_responses = {
statements: JSON.parse(statements_stub.response.body),
verdicts: JSON.parse(verdicts_stub.response.body),
reason: JSON.parse(reason_stub.response.body),
}
shared_expected_metrics_attributes = {
duration: 2.0,
model: AutoEvaluation::BedrockOpenAIOssInvoke::MODEL,
llm_prompt_tokens: 25,
llm_completion_tokens: 35,
llm_cached_tokens: nil,
}
expected_metrics = {
statements: shared_expected_metrics_attributes,
verdicts: shared_expected_metrics_attributes,
reason: shared_expected_metrics_attributes,
}
expect(result)
.to be_a(AutoEvaluation::ScoreResult)
.and have_attributes(
score: 0.5,
reason:,
success: true,
llm_responses: expected_llm_responses,
metrics: expected_metrics,
)
end
context "when 'idk' verdicts are present" do
let(:verdicts) do
[
{ "verdict" => "idk", "reason" => "Cannot determine relevance." },
{ "verdict" => "no", "reason" => "The statement is irrelevant." },
]
end
it "treats 'idk' verdicts as positive in the score" do
result = described_class.call(
question_message:,
answer_message:,
)
expect(result.score).to eq(0.5)
end
end
context "when no statements are extracted from the answer" do
let(:statements_json) { { statements: [] }.to_json }
it "returns a result object with the expected attributes" do
allow(Clock).to receive(:monotonic_time).and_return(200.0, 202.0)
result = described_class.call(
question_message:,
answer_message:,
)
expect(result)
.to be_a(AutoEvaluation::ScoreResult)
.and have_attributes(
score: 1.0,
reason: "No statements were extracted from the answer.",
success: true,
llm_responses: hash_including(statements: anything),
metrics: hash_including(statements: anything),
)
end
end
context "when no verdicts are generated for the extracted statements" do
let(:verdicts_json) { { verdicts: [] }.to_json }
it "returns a result object with the expected attributes" do
allow(Clock).to receive(:monotonic_time)
.and_return(200.0, 202.0, 204.0, 206.0)
result = described_class.call(
question_message:,
answer_message:,
)
expect(result)
.to be_a(AutoEvaluation::ScoreResult)
.and have_attributes(
score: 1.0,
reason: "No verdicts were generated for the extracted statements.",
success: true,
llm_responses: hash_including(
statements: anything,
verdicts: anything,
),
metrics: hash_including(
statements: anything,
verdicts: anything,
),
)
end
end
context "when verdicts are generated and none have a 'no' verdict" do
let(:verdicts_json) { { verdicts: [{ "verdict" => "yes" }, { "verdict" => "yes" }] }.to_json }
it "returns a result object with the expected attributes" do
allow(Clock).to receive(:monotonic_time).and_return(200.0, 202.0, 204.0, 206.0)
result = described_class.call(
question_message:,
answer_message:,
)
expect(result)
.to be_a(AutoEvaluation::ScoreResult)
.and have_attributes(
score: 1.0,
reason: "The response fully addressed the input with no irrelevant statements.",
success: true,
llm_responses: hash_including(
statements: anything,
verdicts: anything,
),
metrics: hash_including(
statements: anything,
verdicts: anything,
),
)
end
end
end
end