bedrock-models.test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * A test suite to ensure all configured Amazon Bedrock models are usable.
  3. *
  4. * This is here to make sure we got correct model identifiers from models.dev and other sources.
  5. * Because Amazon Bedrock requires cross-region inference in some models,
  6. * plain model identifiers are not always usable and it requires tweaking of model identifiers to use cross-region inference.
  7. * See https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles-support.html#inference-profiles-support-system for more details.
  8. *
  9. * This test suite is not enabled by default unless AWS credentials and `BEDROCK_EXTENSIVE_MODEL_TEST` environment variables are set.
  10. * This test suite takes ~2 minutes to run. Because not all models are available in all regions,
  11. * it's recommended to use `us-west-2` region for best coverage for running this test suite.
  12. *
  13. * You can run this test suite with:
  14. * ```bash
  15. * $ AWS_REGION=us-west-2 BEDROCK_EXTENSIVE_MODEL_TEST=1 AWS_PROFILE=... npm test -- ./test/bedrock-models.test.ts
  16. * ```
  17. */
  18. import { describe, expect, it } from "vitest";
  19. import { getModels } from "../src/models.ts";
  20. import { complete } from "../src/stream.ts";
  21. import type { Context } from "../src/types.ts";
  22. import { hasBedrockCredentials } from "./bedrock-utils.ts";
  23. describe("Amazon Bedrock Models", () => {
  24. const models = getModels("amazon-bedrock");
  25. it("should get all available Bedrock models", () => {
  26. expect(models.length).toBeGreaterThan(0);
  27. console.log(`Found ${models.length} Bedrock models`);
  28. });
  29. if (hasBedrockCredentials() && process.env.BEDROCK_EXTENSIVE_MODEL_TEST) {
  30. for (const model of models) {
  31. it(`should make a simple request with ${model.id}`, { timeout: 10_000 }, async () => {
  32. const context: Context = {
  33. systemPrompt: "You are a helpful assistant. Be extremely concise.",
  34. messages: [
  35. {
  36. role: "user",
  37. content: "Reply with exactly: 'OK'",
  38. timestamp: Date.now(),
  39. },
  40. ],
  41. };
  42. const response = await complete(model, context);
  43. expect(response.role).toBe("assistant");
  44. expect(response.content).toBeTruthy();
  45. expect(response.content.length).toBeGreaterThan(0);
  46. expect(response.usage.input + response.usage.cacheRead).toBeGreaterThan(0);
  47. expect(response.usage.output).toBeGreaterThan(0);
  48. expect(response.errorMessage).toBeFalsy();
  49. const textContent = response.content
  50. .filter((b) => b.type === "text")
  51. .map((b) => (b.type === "text" ? b.text : ""))
  52. .join("")
  53. .trim();
  54. expect(textContent).toBeTruthy();
  55. console.log(`${model.id}: ${textContent.substring(0, 100)}`);
  56. });
  57. }
  58. }
  59. });