Skip to content

Commit df086f2

Browse files
authored
Merge pull request #221 from alphagov/answer-strategy-on-generate-answer
Accept answer_strategy argument for evaluation:generate_answer
2 parents 1b20313 + 422ff6a commit df086f2

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

lib/tasks/evaluation.rake

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,13 @@ namespace :evaluation do
3636
end
3737

3838
desc "Generate a single answer to a question returned as JSON, for 3rd party evaluation tools"
39-
task generate_answer: :environment do
39+
task :generate_answer, %i[answer_strategy] => :environment do |_, args|
4040
raise "requires a QUESTION env var" if ENV["QUESTION"].blank?
4141

42-
question = Question.new(message: ENV["QUESTION"], conversation: Conversation.new)
42+
answer_strategy = args.fetch(:answer_strategy, Rails.configuration.answer_strategy)
43+
warn "No answer strategy argument provided, using #{answer_strategy}" unless args[:answer_strategy]
44+
45+
question = Question.new(message: ENV["QUESTION"], conversation: Conversation.new, answer_strategy:)
4346
answer = AnswerComposition::Composer.call(question)
4447
puts({ message: answer.message }.to_json)
4548
end

spec/lib/tasks/evaluation_spec.rb

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,48 @@
9696

9797
it "outputs the answer as JSON to stdout" do
9898
answer = build(:answer)
99+
question = "What is the current VAT rate?"
100+
answer_strategy = "claude_structured_answer"
99101

100102
allow(AnswerComposition::Composer)
101103
.to receive(:call)
102104
.with(an_instance_of(Question))
103105
.and_return(answer)
104106

105-
ClimateControl.modify(QUESTION: "What is the current VAT rate?") do
107+
ClimateControl.modify(QUESTION: question) do
106108
answer_json = { message: answer.message }.to_json
107-
expect { Rake::Task[task_name].invoke }
109+
expect { Rake::Task[task_name].invoke(answer_strategy) }
108110
.to output("#{answer_json}\n").to_stdout
109111
end
112+
113+
expect(AnswerComposition::Composer)
114+
.to have_received(:call)
115+
.with(an_object_having_attributes(message: question,
116+
conversation: an_instance_of(Conversation),
117+
answer_strategy: answer_strategy))
118+
end
119+
120+
it "warns when an answer_strategy argument isn't given" do
121+
answer = build(:answer)
122+
question = "What is the current VAT rate?"
123+
default_answer_strategy = Rails.configuration.answer_strategy
124+
125+
allow(AnswerComposition::Composer)
126+
.to receive(:call)
127+
.with(an_instance_of(Question))
128+
.and_return(answer)
129+
130+
ClimateControl.modify(QUESTION: question) do
131+
expect { Rake::Task[task_name].invoke }
132+
.to output.to_stdout
133+
.and output("No answer strategy argument provided, using #{default_answer_strategy}\n").to_stderr
134+
end
135+
136+
expect(AnswerComposition::Composer)
137+
.to have_received(:call)
138+
.with(an_object_having_attributes(message: question,
139+
conversation: an_instance_of(Conversation),
140+
answer_strategy: default_answer_strategy))
110141
end
111142
end
112143

0 commit comments

Comments
 (0)