|
| 1 | +RSpec.describe Search::TextToEmbedding::Titan do |
| 2 | + describe ".call" do |
| 3 | + it "returns a single embedding array for a string input" do |
| 4 | + client = stub_bedrock_invoke_model( |
| 5 | + bedrock_titan_embedding_response([1.0, 2.0, 3.0]), |
| 6 | + ) |
| 7 | + |
| 8 | + embedding = described_class.call("text") |
| 9 | + |
| 10 | + expect(client.api_requests.size).to eq(1) |
| 11 | + |
| 12 | + expect(embedding).to eq([1.0, 2.0, 3.0]) |
| 13 | + end |
| 14 | + |
| 15 | + it "returns an array of embedding arrays for an array input" do |
| 16 | + client = stub_bedrock_invoke_model( |
| 17 | + bedrock_titan_embedding_response([1.0, 2.0, 3.0]), |
| 18 | + bedrock_titan_embedding_response([4.0, 5.0, 6.0]), |
| 19 | + ) |
| 20 | + |
| 21 | + embedding = described_class.call(["Embed this", "Embed that"]) |
| 22 | + |
| 23 | + expect(client.api_requests.size).to eq(2) |
| 24 | + |
| 25 | + expect(embedding).to eq([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]) |
| 26 | + end |
| 27 | + |
| 28 | + it "truncates input text to the length limit" do |
| 29 | + client = stub_bedrock_invoke_model( |
| 30 | + bedrock_titan_embedding_response([1.0, 2.0, 3.0]), |
| 31 | + ) |
| 32 | + |
| 33 | + long_text = "a" * (described_class::INPUT_TEXT_LENGTH_LIMIT + 1) |
| 34 | + described_class.call(long_text) |
| 35 | + |
| 36 | + expect(client.api_requests.size).to eq(1) |
| 37 | + |
| 38 | + request_body = JSON.parse( |
| 39 | + client.api_requests.first.dig(:params, :body), |
| 40 | + ) |
| 41 | + |
| 42 | + expect(request_body["inputText"].length) |
| 43 | + .to eq(described_class::INPUT_TEXT_LENGTH_LIMIT) |
| 44 | + end |
| 45 | + end |
| 46 | +end |
0 commit comments