-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcoherence.rb
More file actions
52 lines (41 loc) · 1.3 KB
/
coherence.rb
File metadata and controls
52 lines (41 loc) · 1.3 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
module AutoEvaluation
class Coherence
THRESHOLD = 0.75
def self.call(...) = new(...).call
def initialize(question_message:, answer_message:)
@question_message = question_message
@answer_message = answer_message
end
def call
result = BedrockOpenAIOssInvoke.call(user_prompt, tools)
score = normalise_rubric_score(result.evaluation_data.fetch("score"))
AutoEvaluation::ScoreResult.new(
score:,
reason: result.evaluation_data.fetch("reason").strip,
success: score >= THRESHOLD,
llm_responses: { coherence: result.llm_response },
metrics: { coherence: result.metrics },
)
end
private
attr_reader :question_message, :answer_message
def llm_prompts
Prompts.config.coherence
end
def user_prompt
sprintf(
llm_prompts.fetch(:user_prompt),
answer: answer_message,
question: question_message,
)
end
def tools
[llm_prompts.fetch(:tool_spec)]
end
def normalise_rubric_score(rubric_score)
min_rubric_score = llm_prompts.fetch(:config).fetch(:min_rubric_score)
max_rubric_score = llm_prompts.fetch(:config).fetch(:max_rubric_score)
(rubric_score.to_d - min_rubric_score) / (max_rubric_score - min_rubric_score)
end
end
end