feat(clients): bedrock: SigV4 signer, Converse, event streams, guardrails, agents, KB, batch
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
// Routes: GET /ai/bedrock (page), /status, /calls (no credential needed)
|
||||
#include <cstdlib>
|
||||
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
setenv("MOLO_ENV_FILES", "off", 1);
|
||||
for (const char* v : {"AWS_BEARER_TOKEN_BEDROCK", "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN", "AWS_REGION", "AWS_DEFAULT_REGION"})
|
||||
unsetenv(v);
|
||||
Fixture f;
|
||||
auto page = f.client->get("/ai/bedrock");
|
||||
check(page.status == 200, "page 200");
|
||||
check(contains(page.headers.get("Content-Type"), "text/html"), "html");
|
||||
check(contains(page.body, "Amazon Bedrock") && contains(page.body, "viewport"), "title and mobile viewport");
|
||||
check(contains(page.body, "/embedded/ai_bedrock.js"), "script wired");
|
||||
check(contains(page.body, "<select"), "route picker is a dropdown, not a button row");
|
||||
check(f.client->post("/ai/bedrock", "{}", "application/json").status == 405, "post page is 405");
|
||||
|
||||
auto script = f.client->get("/embedded/ai_bedrock.js");
|
||||
check(script.status == 200 && contains(script.body, "/ai/bedrock/"), "script is embedded");
|
||||
|
||||
auto st = f.client->get("/ai/bedrock/status");
|
||||
check(st.status == 200 && contains(st.body, "\"configured\":false"), "status without a credential");
|
||||
check(contains(st.body, "\"auth_mode\":\"none\"") && contains(st.body, "\"region\":\"us-east-1\""), "status names mode and default region");
|
||||
check(contains(st.body, "AWS_BEARER_TOKEN_BEDROCK") && contains(st.body, "AWS_ACCESS_KEY_ID") &&
|
||||
contains(st.body, "AWS_SECRET_ACCESS_KEY") && contains(st.body, "AWS_SESSION_TOKEN") && contains(st.body, "AWS_REGION"),
|
||||
"status names the credential env vars");
|
||||
check(contains(st.body, "bedrock-runtime.us-east-1.amazonaws.com") && contains(st.body, "bedrock.us-east-1.amazonaws.com") &&
|
||||
contains(st.body, "bedrock-agent-runtime.us-east-1.amazonaws.com"),
|
||||
"status lists the three hosts");
|
||||
check(f.client->post("/ai/bedrock/status", "{}", "application/json").status == 405, "post status is 405");
|
||||
|
||||
auto calls = f.client->get("/ai/bedrock/calls");
|
||||
check(calls.status == 200 && contains(calls.body, "Converse") && contains(calls.body, "/model/{modelId}/converse-stream") &&
|
||||
contains(calls.body, "ApplyGuardrail") && contains(calls.body, "/knowledgebases/{knowledgeBaseId}/retrieve") &&
|
||||
contains(calls.body, "CreateModelInvocationJob"),
|
||||
"call table listed");
|
||||
check(contains(calls.body, "\"bearer_ok\":false"), "agent runtime marked as SigV4 only");
|
||||
return finish("ai/bedrock/page");
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
// Routes: POST /ai/bedrock/* (405, 422, dry runs, 503 without a credential)
|
||||
#include <cstdlib>
|
||||
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
setenv("MOLO_ENV_FILES", "off", 1);
|
||||
for (const char* v : {"AWS_BEARER_TOKEN_BEDROCK", "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN", "AWS_REGION", "AWS_DEFAULT_REGION"})
|
||||
unsetenv(v);
|
||||
Fixture f;
|
||||
auto post = [&](const std::string& route, const std::string& body) {
|
||||
return f.client->post("/ai/bedrock/" + route, body, "application/json");
|
||||
};
|
||||
const char* all[] = {"converse", "converse-stream", "invoke", "invoke-stream", "count-tokens", "guardrail",
|
||||
"agent", "retrieve", "models", "profiles", "guardrails", "batch", "sign"};
|
||||
for (const char* r : all) {
|
||||
std::string route = std::string("/ai/bedrock/") + r;
|
||||
check(f.client->get(route).status == 405, route + " get is 405");
|
||||
check(post(r, "not json").status == 422, route + " bad json is 422");
|
||||
check(post(r, "[1]").status == 422, route + " non-object is 422");
|
||||
}
|
||||
|
||||
// 422: input rules run before anything else (also without a credential)
|
||||
check(post("converse", "{}").status == 422, "converse needs prompt");
|
||||
check(post("converse", "{\"prompt\":\"hi\",\"temperature\":1.5}").status == 422, "temperature over 1");
|
||||
check(post("converse", "{\"prompt\":\"hi\",\"top_p\":-1}").status == 422, "top_p under 0");
|
||||
check(post("converse", "{\"prompt\":\"hi\",\"max_tokens\":0}").status == 422, "max_tokens 0");
|
||||
check(post("converse", "{\"prompt\":\"hi\",\"model\":\"a b\"}").status == 422, "model with a space");
|
||||
check(post("converse", "{\"prompt\":\"hi\",\"model\":\"../x\"}").status == 422, "model with dots");
|
||||
check(post("converse", "{\"prompt\":\"hi\",\"service_tier\":\"gold\"}").status == 422, "bad service tier");
|
||||
check(post("converse", "{\"prompt\":\"hi\",\"guardrail_id\":\"g\"}").status == 422, "guardrail needs a version");
|
||||
check(post("converse", "{\"prompt\":\"hi\",\"stop\":[1]}").status == 422, "stop must be strings");
|
||||
check(post("converse-stream", "{\"prompt\":\"hi\",\"latency\":\"warp\"}").status == 422, "bad latency");
|
||||
check(post("invoke", "{\"model\":\"m\"}").status == 422, "invoke needs a body");
|
||||
check(post("invoke", "{\"model\":\"m\",\"body\":{},\"trace\":\"LOUD\"}").status == 422, "bad trace");
|
||||
check(post("invoke-stream", "{\"body\":{}}").status == 422, "invoke-stream needs a model");
|
||||
check(post("count-tokens", "{}").status == 422, "count-tokens needs a prompt");
|
||||
check(post("guardrail", "{\"text\":\"x\"}").status == 422, "guardrail needs an id");
|
||||
check(post("guardrail", "{\"guardrail_id\":\"g\",\"text\":\"x\",\"source\":\"BOTH\"}").status == 422, "bad source");
|
||||
check(post("guardrail", "{\"guardrail_id\":\"g\",\"text\":\"x\",\"qualifiers\":[\"weird\"]}").status == 422, "bad qualifier");
|
||||
check(post("agent", "{\"agent_id\":\"A\",\"alias_id\":\"B\",\"session_id\":\"x\",\"prompt\":\"hi\"}").status == 422, "session id under 2 chars");
|
||||
check(post("agent", "{\"agent_id\":\"WAYTOOLONGAGENT\",\"alias_id\":\"B\",\"session_id\":\"sess-1\",\"prompt\":\"hi\"}").status == 422, "agent id over 10");
|
||||
check(post("retrieve", "{\"knowledge_base_id\":\"short\",\"query\":\"q\"}").status == 422, "kb id must be 10 chars");
|
||||
check(post("retrieve", "{\"knowledge_base_id\":\"KB12345678\",\"query\":\"q\",\"number_of_results\":101}").status == 422, "over 100 results");
|
||||
check(post("retrieve", "{\"knowledge_base_id\":\"KB12345678\",\"query\":\"q\",\"search_type\":\"FUZZY\"}").status == 422, "bad search type");
|
||||
check(post("models", "{\"action\":\"fly\"}").status == 422, "bad models action");
|
||||
check(post("models", "{\"action\":\"get\"}").status == 422, "models get needs id");
|
||||
check(post("models", "{\"action\":\"get\",\"id\":\"../x\"}").status == 422, "bad model id");
|
||||
check(post("profiles", "{\"max_results\":0}").status == 422, "max_results 0");
|
||||
check(post("profiles", "{\"max_results\":1001}").status == 422, "max_results over 1000");
|
||||
check(post("guardrails", "{\"action\":\"get\"}").status == 422, "guardrails get needs id");
|
||||
check(post("batch", "{}").status == 422, "batch needs an action");
|
||||
check(post("batch", "{\"action\":\"nope\"}").status == 422, "bad batch action");
|
||||
check(post("batch", "{\"action\":\"stop\",\"id\":\"../x\"}").status == 422, "bad job id");
|
||||
check(post("batch", "{\"action\":\"create\",\"job_name\":\"j\",\"role_arn\":\"role\",\"model\":\"m\",\"input_s3_uri\":\"s3://b/i\",\"output_s3_uri\":\"s3://b/o\"}").status == 422,
|
||||
"bad role arn");
|
||||
check(post("batch", "{\"action\":\"create\",\"job_name\":\"j\",\"role_arn\":\"arn:aws:iam::123456789012:role/R\",\"model\":\"m\",\"input_s3_uri\":\"s3://b/i\",\"output_s3_uri\":\"s3://b/o\",\"timeout_hours\":12}").status == 422,
|
||||
"timeout under 24 hours");
|
||||
|
||||
// dry runs: paid or state-changing routes show the exact request
|
||||
auto dry = post("converse", "{\"prompt\":\"hi\",\"model\":\"anthropic.claude-3-sonnet-20240229-v1:0\",\"max_tokens\":50}");
|
||||
check(dry.status == 200 && contains(dry.body, "\"dry_run\":true") && contains(dry.body, "\"maxTokens\":50") &&
|
||||
contains(dry.body, "POST /model/anthropic.claude-3-sonnet-20240229-v1%3A0/converse"),
|
||||
"converse is a dry run without confirm");
|
||||
check(contains(post("converse-stream", "{\"prompt\":\"hi\"}").body, "/converse-stream"), "converse-stream dry run");
|
||||
check(contains(post("invoke", "{\"model\":\"amazon.titan-embed-text-v2:0\",\"body\":{\"inputText\":\"x\"}}").body, "\"dry_run\":true"), "invoke dry run");
|
||||
check(contains(post("invoke-stream", "{\"model\":\"m\",\"body\":{}}").body, "invoke-with-response-stream"), "invoke-stream dry run");
|
||||
check(contains(post("guardrail", "{\"guardrail_id\":\"gr1\",\"text\":\"x\"}").body, "/guardrail/gr1/version/DRAFT/apply"), "guardrail dry run defaults to DRAFT");
|
||||
check(contains(post("agent", "{\"agent_id\":\"AGENT12345\",\"alias_id\":\"ALIAS12345\",\"session_id\":\"sess-1\",\"prompt\":\"hi\"}").body,
|
||||
"/agents/AGENT12345/agentAliases/ALIAS12345/sessions/sess-1/text"),
|
||||
"agent dry run");
|
||||
check(contains(post("retrieve", "{\"knowledge_base_id\":\"KB12345678\",\"query\":\"What is AWS?\"}").body, "\"retrievalQuery\":{\"text\":\"What is AWS?\"}"),
|
||||
"retrieve dry run shows the documented body");
|
||||
check(contains(post("batch", "{\"action\":\"stop\",\"id\":\"abcdefghijkl\"}").body, "\"dry_run\":true"), "batch stop is a dry run");
|
||||
check(contains(post("batch", "{\"action\":\"create\",\"job_name\":\"nightly\",\"role_arn\":\"arn:aws:iam::123456789012:role/R\",\"model\":\"amazon.nova-micro-v1:0\",\"input_s3_uri\":\"s3://b/i.jsonl\",\"output_s3_uri\":\"s3://b/o/\"}").body,
|
||||
"\"dry_run\":true"),
|
||||
"batch create is a dry run");
|
||||
|
||||
// 503: free calls and confirmed calls need the credential
|
||||
for (const char* r : {"count-tokens"}) {
|
||||
auto res = post(r, "{\"prompt\":\"hi\"}");
|
||||
check(res.status == 503 && contains(res.body, "not_configured"), std::string(r) + " without a credential is 503");
|
||||
}
|
||||
check(post("models", "{}").status == 503, "models list without a credential is 503");
|
||||
check(post("models", "{\"action\":\"get\",\"id\":\"amazon.nova-micro-v1:0\"}").status == 503, "models get without a credential is 503");
|
||||
check(post("profiles", "{}").status == 503, "profiles without a credential is 503");
|
||||
check(post("guardrails", "{}").status == 503, "guardrails without a credential is 503");
|
||||
check(post("batch", "{\"action\":\"list\"}").status == 503, "batch list without a credential is 503");
|
||||
check(post("batch", "{\"action\":\"get\",\"id\":\"abcdefghijkl\"}").status == 503, "batch get without a credential is 503");
|
||||
check(post("converse", "{\"prompt\":\"hi\",\"confirm\":true}").status == 503, "confirmed converse without a credential is 503");
|
||||
check(post("converse-stream", "{\"prompt\":\"hi\",\"confirm\":true}").status == 503, "confirmed converse-stream is 503");
|
||||
check(post("invoke", "{\"model\":\"m\",\"body\":{},\"confirm\":true}").status == 503, "confirmed invoke is 503");
|
||||
check(post("invoke-stream", "{\"model\":\"m\",\"body\":{},\"confirm\":true}").status == 503, "confirmed invoke-stream is 503");
|
||||
check(post("guardrail", "{\"guardrail_id\":\"g\",\"text\":\"x\",\"confirm\":true}").status == 503, "confirmed guardrail is 503");
|
||||
check(post("agent", "{\"agent_id\":\"A1\",\"alias_id\":\"B1\",\"session_id\":\"sess-1\",\"prompt\":\"hi\",\"confirm\":true}").status == 503, "confirmed agent is 503");
|
||||
check(post("retrieve", "{\"knowledge_base_id\":\"KB12345678\",\"query\":\"q\",\"confirm\":true}").status == 503, "confirmed retrieve is 503");
|
||||
check(post("batch", "{\"action\":\"stop\",\"id\":\"abcdefghijkl\",\"confirm\":true}").status == 503, "confirmed batch stop is 503");
|
||||
return finish("ai/bedrock/routes");
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// Route: POST /ai/bedrock/sign is local (no credential, no network). With the
|
||||
// request of AWS's get-vanilla test case it must give the suite's signature.
|
||||
#include <cstdlib>
|
||||
|
||||
#include "support/Assert.hpp"
|
||||
|
||||
using namespace molo::example::test;
|
||||
|
||||
int main() {
|
||||
setenv("MOLO_ENV_FILES", "off", 1);
|
||||
for (const char* v : {"AWS_BEARER_TOKEN_BEDROCK", "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN"}) unsetenv(v);
|
||||
Fixture f;
|
||||
auto sign = [&](const std::string& body) { return f.client->post("/ai/bedrock/sign", body, "application/json"); };
|
||||
|
||||
// aws4_testsuite get-vanilla: GET /, host example.amazonaws.com, 20150830T123600Z, us-east-1, service "service".
|
||||
auto r = sign("{\"service\":\"service\",\"region\":\"us-east-1\"}");
|
||||
check(r.status == 200, "sign answers without a credential");
|
||||
check(contains(r.body, "5fa00fa31553b73ebf1942676e86291e8372ff2a2260956d9b8aae1d763fbf31"), "signature equals AWS's get-vanilla vector");
|
||||
check(contains(r.body, "AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request"), "authorization header shape");
|
||||
check(contains(r.body, "documentation credentials"), "says whose credentials it used");
|
||||
check(!contains(r.body, "wJalrXUtnFEMI"), "the secret is not echoed");
|
||||
|
||||
auto q = sign("{\"service\":\"service\",\"query\":\"Param-3=Value3&Param=Value2\"}");
|
||||
check(q.status == 200 && contains(q.body, "Param=Value2&Param-3=Value3"), "query is sorted in the canonical request");
|
||||
|
||||
auto bedrock = sign("{\"method\":\"POST\",\"path\":\"/model/anthropic.claude-3-sonnet-20240229-v1%3A0/converse\",\"host\":\"bedrock-runtime.us-east-1.amazonaws.com\",\"body\":\"{}\"}");
|
||||
check(bedrock.status == 200 && contains(bedrock.body, "v1%253A0"), "a Bedrock model path is encoded twice in the canonical request");
|
||||
|
||||
check(sign("{\"method\":\"TRACE\"}").status == 422, "unsupported method");
|
||||
check(sign("{\"path\":\"no-slash\"}").status == 422, "path must start with /");
|
||||
check(sign("{\"region\":\"US EAST\"}").status == 422, "bad region");
|
||||
check(sign("{\"date\":\"yesterday\"}").status == 422, "bad date");
|
||||
return finish("ai/bedrock/sign");
|
||||
}
|
||||
Reference in New Issue
Block a user