client.mjs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { BaseAnthropic } from '@anthropic-ai/sdk/client';
  2. import * as Resources from '@anthropic-ai/sdk/resources/index';
  3. import { getAuthHeaders } from "./core/auth.mjs";
  4. import { Stream } from "./core/streaming.mjs";
  5. import { readEnv } from "./internal/utils/env.mjs";
  6. import { isObj } from "./internal/utils/values.mjs";
  7. import { buildHeaders } from "./internal/headers.mjs";
  8. import { path } from "./internal/utils/path.mjs";
  9. export { BaseAnthropic } from '@anthropic-ai/sdk/client';
  10. const DEFAULT_VERSION = 'bedrock-2023-05-31';
  11. const MODEL_ENDPOINTS = new Set(['/v1/complete', '/v1/messages', '/v1/messages?beta=true']);
  12. /** API Client for interfacing with the Anthropic Bedrock API. */
  13. export class AnthropicBedrock extends BaseAnthropic {
  14. /**
  15. * API Client for interfacing with the Anthropic Bedrock API.
  16. *
  17. * @param {string | null | undefined} [opts.awsSecretKey]
  18. * @param {string | null | undefined} [opts.awsAccessKey]
  19. * @param {string | undefined} [opts.awsRegion=process.env['AWS_REGION'] ?? us-east-1]
  20. * @param {string | null | undefined} [opts.awsSessionToken]
  21. * @param {(() => Promise<AwsCredentialIdentityProvider>) | null} [opts.providerChainResolver] - Custom provider chain resolver for AWS credentials. Useful for non-Node environments.
  22. * @param {string} [opts.baseURL=process.env['ANTHROPIC_BEDROCK_BASE_URL'] ?? https://bedrock-runtime.${this.awsRegion}.amazonaws.com] - Override the default base URL for the API.
  23. * @param {number} [opts.timeout=10 minutes] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out.
  24. * @param {MergedRequestInit} [opts.fetchOptions] - Additional `RequestInit` options to be passed to `fetch` calls.
  25. * @param {Fetch} [opts.fetch] - Specify a custom `fetch` function implementation.
  26. * @param {number} [opts.maxRetries=2] - The maximum number of times the client will retry a request.
  27. * @param {HeadersLike} opts.defaultHeaders - Default headers to include with every request to the API.
  28. * @param {Record<string, string | undefined>} opts.defaultQuery - Default query parameters to include with every request to the API.
  29. * @param {boolean} [opts.dangerouslyAllowBrowser=false] - By default, client-side use of this library is not allowed, as it risks exposing your secret API credentials to attackers.
  30. * @param {boolean} [opts.skipAuth=false] - Skip authentication for this request. This is useful if you have an internal proxy that handles authentication for you.
  31. */
  32. constructor({ awsRegion = readEnv('AWS_REGION') ?? 'us-east-1', baseURL = readEnv('ANTHROPIC_BEDROCK_BASE_URL') ?? `https://bedrock-runtime.${awsRegion}.amazonaws.com`, awsSecretKey = null, awsAccessKey = null, awsSessionToken = null, providerChainResolver = null, ...opts } = {}) {
  33. super({
  34. baseURL,
  35. ...opts,
  36. });
  37. this.skipAuth = false;
  38. this.messages = makeMessagesResource(this);
  39. this.completions = new Resources.Completions(this);
  40. this.beta = makeBetaResource(this);
  41. this.awsSecretKey = awsSecretKey;
  42. this.awsAccessKey = awsAccessKey;
  43. this.awsRegion = awsRegion;
  44. this.awsSessionToken = awsSessionToken;
  45. this.skipAuth = opts.skipAuth ?? false;
  46. this.providerChainResolver = providerChainResolver;
  47. }
  48. validateHeaders() {
  49. // auth validation is handled in prepareRequest since it needs to be async
  50. }
  51. async prepareRequest(request, { url, options }) {
  52. if (this.skipAuth) {
  53. return;
  54. }
  55. const regionName = this.awsRegion;
  56. if (!regionName) {
  57. throw new Error('Expected `awsRegion` option to be passed to the client or the `AWS_REGION` environment variable to be present');
  58. }
  59. const headers = await getAuthHeaders(request, {
  60. url,
  61. regionName,
  62. awsAccessKey: this.awsAccessKey,
  63. awsSecretKey: this.awsSecretKey,
  64. awsSessionToken: this.awsSessionToken,
  65. fetchOptions: this.fetchOptions,
  66. providerChainResolver: this.providerChainResolver,
  67. });
  68. request.headers = buildHeaders([headers, request.headers]).values;
  69. }
  70. async buildRequest(options) {
  71. options.__streamClass = Stream;
  72. if (isObj(options.body)) {
  73. // create a shallow copy of the request body so that code that mutates it later
  74. // doesn't mutate the original user-provided object
  75. options.body = { ...options.body };
  76. }
  77. if (isObj(options.body)) {
  78. if (!options.body['anthropic_version']) {
  79. options.body['anthropic_version'] = DEFAULT_VERSION;
  80. }
  81. if (options.headers && !options.body['anthropic_beta']) {
  82. const betas = buildHeaders([options.headers]).values.get('anthropic-beta');
  83. if (betas != null) {
  84. options.body['anthropic_beta'] = betas.split(',');
  85. }
  86. }
  87. }
  88. if (MODEL_ENDPOINTS.has(options.path) && options.method === 'post') {
  89. if (!isObj(options.body)) {
  90. throw new Error('Expected request body to be an object for post /v1/messages');
  91. }
  92. const model = options.body['model'];
  93. options.body['model'] = undefined;
  94. const stream = options.body['stream'];
  95. options.body['stream'] = undefined;
  96. if (stream) {
  97. options.path = path `/model/${model}/invoke-with-response-stream`;
  98. }
  99. else {
  100. options.path = path `/model/${model}/invoke`;
  101. }
  102. }
  103. return super.buildRequest(options);
  104. }
  105. }
  106. function makeMessagesResource(client) {
  107. const resource = new Resources.Messages(client);
  108. // @ts-expect-error we're deleting non-optional properties
  109. delete resource.batches;
  110. // @ts-expect-error we're deleting non-optional properties
  111. delete resource.countTokens;
  112. return resource;
  113. }
  114. function makeBetaResource(client) {
  115. const resource = new Resources.Beta(client);
  116. // @ts-expect-error we're deleting non-optional properties
  117. delete resource.promptCaching;
  118. // @ts-expect-error we're deleting non-optional properties
  119. delete resource.messages.batches;
  120. // @ts-expect-error we're deleting non-optional properties
  121. delete resource.messages.countTokens;
  122. return resource;
  123. }
  124. //# sourceMappingURL=client.mjs.map