2023-05-11 09:03:03 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class AnthropicCompletionStubs
|
|
|
|
class << self
|
|
|
|
def response(content)
|
|
|
|
{
|
|
|
|
completion: content,
|
|
|
|
stop: "\n\nHuman:",
|
|
|
|
stop_reason: "stop_sequence",
|
|
|
|
truncated: false,
|
|
|
|
log_id: "12dcc7feafbee4a394e0de9dffde3ac5",
|
2023-07-26 21:24:44 -04:00
|
|
|
model: "claude-2",
|
2023-05-11 09:03:03 -04:00
|
|
|
exception: nil,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def stub_response(prompt, response_text, req_opts: {})
|
|
|
|
WebMock
|
|
|
|
.stub_request(:post, "https://api.anthropic.com/v1/complete")
|
2023-06-27 13:42:33 -04:00
|
|
|
.with(
|
2023-07-26 21:24:44 -04:00
|
|
|
body: { model: "claude-2", prompt: prompt, max_tokens_to_sample: 2000 }.merge(
|
2023-06-27 13:42:33 -04:00
|
|
|
req_opts,
|
|
|
|
).to_json,
|
|
|
|
)
|
2023-05-11 09:03:03 -04:00
|
|
|
.to_return(status: 200, body: JSON.dump(response(response_text)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def stream_line(delta, finish_reason: nil)
|
|
|
|
+"data: " << {
|
|
|
|
completion: delta,
|
|
|
|
stop: finish_reason ? "\n\nHuman:" : nil,
|
|
|
|
stop_reason: finish_reason,
|
|
|
|
truncated: false,
|
|
|
|
log_id: "12b029451c6d18094d868bc04ce83f63",
|
2023-07-26 21:24:44 -04:00
|
|
|
model: "claude-2",
|
2023-05-11 09:03:03 -04:00
|
|
|
exception: nil,
|
|
|
|
}.to_json
|
|
|
|
end
|
|
|
|
|
2023-05-31 19:10:33 -04:00
|
|
|
def stub_streamed_response(prompt, deltas, model: nil, req_opts: {})
|
2023-05-11 09:03:03 -04:00
|
|
|
chunks =
|
|
|
|
deltas.each_with_index.map do |_, index|
|
|
|
|
if index == (deltas.length - 1)
|
2023-07-26 21:24:44 -04:00
|
|
|
stream_line(deltas[index], finish_reason: "stop_sequence")
|
2023-05-11 09:03:03 -04:00
|
|
|
else
|
2023-07-26 21:24:44 -04:00
|
|
|
stream_line(deltas[index])
|
2023-05-11 09:03:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
chunks = chunks.join("\n\n")
|
|
|
|
|
|
|
|
WebMock
|
|
|
|
.stub_request(:post, "https://api.anthropic.com/v1/complete")
|
2023-07-26 21:24:44 -04:00
|
|
|
.with(body: { model: model || "claude-2", prompt: prompt }.merge(req_opts).to_json)
|
2023-05-11 09:03:03 -04:00
|
|
|
.to_return(status: 200, body: chunks)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|