2023-11-23 10:58:54 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-01-17 13:08:49 -05:00
|
|
|
require_relative "endpoint_compliance"
|
2023-11-28 23:17:46 -05:00
|
|
|
require "aws-eventstream"
|
|
|
|
require "aws-sigv4"
|
2023-11-23 10:58:54 -05:00
|
|
|
|
2024-01-17 13:08:49 -05:00
|
|
|
class BedrockMock < 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",
|
2023-11-23 10:58:54 -05:00
|
|
|
exception: nil,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2024-01-17 13:08:49 -05:00
|
|
|
def stub_response(prompt, response_content, tool_call: false)
|
2023-11-23 10:58:54 -05:00
|
|
|
WebMock
|
2024-01-17 13:08:49 -05:00
|
|
|
.stub_request(:post, "#{base_url}/invoke")
|
|
|
|
.with(body: model.default_options.merge(prompt: prompt).to_json)
|
|
|
|
.to_return(status: 200, body: JSON.dump(response(response_content)))
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def stream_line(delta, finish_reason: nil)
|
|
|
|
encoder = Aws::EventStream::Encoder.new
|
|
|
|
|
2023-12-12 15:22:44 -05:00
|
|
|
message =
|
|
|
|
Aws::EventStream::Message.new(
|
|
|
|
payload:
|
|
|
|
StringIO.new(
|
|
|
|
{
|
|
|
|
bytes:
|
|
|
|
Base64.encode64(
|
|
|
|
{
|
|
|
|
completion: delta,
|
|
|
|
stop: finish_reason ? "\n\nHuman:" : nil,
|
|
|
|
stop_reason: finish_reason,
|
|
|
|
truncated: false,
|
|
|
|
log_id: "12b029451c6d18094d868bc04ce83f63",
|
2024-01-09 12:10:20 -05:00
|
|
|
model: "claude-2.1",
|
2023-12-12 15:22:44 -05:00
|
|
|
exception: nil,
|
|
|
|
}.to_json,
|
|
|
|
),
|
|
|
|
}.to_json,
|
|
|
|
),
|
|
|
|
)
|
2023-11-23 10:58:54 -05:00
|
|
|
|
|
|
|
encoder.encode(message)
|
|
|
|
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
|
|
|
|
|
|
|
|
WebMock
|
2024-01-17 13:08:49 -05:00
|
|
|
.stub_request(:post, "#{base_url}/invoke-with-response-stream")
|
|
|
|
.with(body: model.default_options.merge(prompt: prompt).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
|
|
|
def base_url
|
|
|
|
"https://bedrock-runtime.#{SiteSetting.ai_bedrock_region}.amazonaws.com/model/anthropic.claude-v2:1"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
RSpec.describe DiscourseAi::Completions::Endpoints::AwsBedrock do
|
|
|
|
subject(:endpoint) { described_class.new("claude-2", DiscourseAi::Tokenizer::AnthropicTokenizer) }
|
|
|
|
|
2024-03-05 10:48:28 -05:00
|
|
|
fab!(:user)
|
2024-01-17 13:08:49 -05:00
|
|
|
|
|
|
|
let(:bedrock_mock) { BedrockMock.new(endpoint) }
|
|
|
|
|
|
|
|
let(:compliance) do
|
|
|
|
EndpointsCompliance.new(self, endpoint, DiscourseAi::Completions::Dialects::Claude, user)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.ai_bedrock_access_key_id = "123456"
|
|
|
|
SiteSetting.ai_bedrock_secret_access_key = "asd-asd-asd"
|
|
|
|
SiteSetting.ai_bedrock_region = "us-east-1"
|
|
|
|
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(bedrock_mock)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with tools" do
|
|
|
|
it "returns a function invocation" do
|
|
|
|
compliance.regular_mode_tools(bedrock_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(bedrock_mock)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with tools" do
|
2024-01-19 06:51:26 -05:00
|
|
|
it "returns a function invocation" do
|
2024-01-17 13:08:49 -05:00
|
|
|
compliance.streaming_mode_tools(bedrock_mock)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|