2023-11-23 10:58:54 -05:00
|
|
|
# frozen_String_literal: true
|
|
|
|
|
2024-01-17 13:08:49 -05:00
|
|
|
require_relative "endpoint_compliance"
|
2024-01-02 09:21:13 -05:00
|
|
|
|
2024-01-17 13:08:49 -05:00
|
|
|
class AnthropicMock < EndpointMock
|
2023-11-23 10:58:54 -05:00
|
|
|
def response(content)
|
|
|
|
{
|
|
|
|
completion: content,
|
|
|
|
stop: "\n\nHuman:",
|
|
|
|
stop_reason: "stop_sequence",
|
|
|
|
truncated: false,
|
|
|
|
log_id: "12dcc7feafbee4a394e0de9dffde3ac5",
|
2024-01-17 13:08:49 -05:00
|
|
|
model: "claude-2",
|
2023-11-23 10:58:54 -05:00
|
|
|
exception: nil,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-12-18 16:06:01 -05:00
|
|
|
def stub_response(prompt, response_text, tool_call: false)
|
2023-11-23 10:58:54 -05:00
|
|
|
WebMock
|
|
|
|
.stub_request(:post, "https://api.anthropic.com/v1/complete")
|
2024-01-17 13:08:49 -05:00
|
|
|
.with(body: model.default_options.merge(prompt: prompt).to_json)
|
2023-11-23 10:58:54 -05: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",
|
|
|
|
model: "claude-2",
|
|
|
|
exception: nil,
|
|
|
|
}.to_json
|
|
|
|
end
|
|
|
|
|
2023-12-18 16:06:01 -05:00
|
|
|
def stub_streamed_response(prompt, deltas, tool_call: false)
|
2023-11-23 10:58:54 -05:00
|
|
|
chunks =
|
|
|
|
deltas.each_with_index.map do |_, index|
|
|
|
|
if index == (deltas.length - 1)
|
|
|
|
stream_line(deltas[index], finish_reason: "stop_sequence")
|
|
|
|
else
|
|
|
|
stream_line(deltas[index])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-12-18 16:06:01 -05:00
|
|
|
chunks = chunks.join("\n\n").split("")
|
2023-11-23 10:58:54 -05:00
|
|
|
|
|
|
|
WebMock
|
|
|
|
.stub_request(:post, "https://api.anthropic.com/v1/complete")
|
2024-01-17 13:08:49 -05:00
|
|
|
.with(body: model.default_options.merge(prompt: prompt, stream: true).to_json)
|
2023-11-23 10:58:54 -05:00
|
|
|
.to_return(status: 200, body: chunks)
|
|
|
|
end
|
2024-01-17 13:08:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
RSpec.describe DiscourseAi::Completions::Endpoints::Anthropic do
|
|
|
|
subject(:endpoint) { described_class.new("claude-2", DiscourseAi::Tokenizer::AnthropicTokenizer) }
|
2023-11-23 10:58:54 -05:00
|
|
|
|
2024-01-17 13:08:49 -05:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2023-12-18 16:06:01 -05:00
|
|
|
|
2024-01-17 13:08:49 -05:00
|
|
|
let(:anthropic_mock) { AnthropicMock.new(endpoint) }
|
2023-12-18 16:06:01 -05:00
|
|
|
|
2024-01-17 13:08:49 -05:00
|
|
|
let(:compliance) do
|
|
|
|
EndpointsCompliance.new(self, endpoint, DiscourseAi::Completions::Dialects::Claude, user)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#perform_completion!" do
|
|
|
|
context "when using regular mode" do
|
|
|
|
context "with simple prompts" do
|
|
|
|
it "completes a trivial prompt and logs the response" do
|
|
|
|
compliance.regular_mode_simple_prompt(anthropic_mock)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with tools" do
|
|
|
|
it "returns a function invocation" do
|
|
|
|
compliance.regular_mode_tools(anthropic_mock)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "when using streaming mode" do
|
|
|
|
context "with simple prompts" do
|
|
|
|
it "completes a trivial prompt and logs the response" do
|
|
|
|
compliance.streaming_mode_simple_prompt(anthropic_mock)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with tools" do
|
|
|
|
it "returns a function invoncation" do
|
|
|
|
compliance.streaming_mode_tools(anthropic_mock)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|