Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .fernignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ src/AwsClient.ts
src/SagemakerClient.ts
src/index.ts
src/aws-utils.ts
src/aws.ts
src/core/streaming-fetcher/streaming-utils.ts
src/ClientV2.ts
src/CustomClient.ts
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,35 @@ const cohere = new CohereClient({
})();
```

## AWS Support (Bedrock & SageMaker)

To use Cohere models on AWS Bedrock or SageMaker, import the AWS clients from the `cohere-ai/aws` subpath and install the required peer dependencies:

```
npm install @aws-sdk/credential-providers @aws-crypto/sha256-js @smithy/protocol-http @smithy/signature-v4
```

```typescript
import { BedrockClient } from "cohere-ai/aws";

const cohere = new BedrockClient({
awsRegion: "us-east-1",
});

const response = await cohere.chat({
model: "cohere.command-a-03-2025",
messages: [{ role: "user", content: "hello world!" }],
});
```

The following clients are available from `cohere-ai/aws`:

- `BedrockClient` / `BedrockClientV2` - AWS Bedrock
- `SagemakerClient` / `SagemakerClientV2` - AWS SageMaker
- `AwsClient` / `AwsClientV2` - Base AWS client

The AWS dependencies are optional peer dependencies and are only required if you use the AWS clients. They will not be installed automatically.

## Beta status

This SDK is in beta, and while we will try to avoid it, there may be breaking changes between versions without a major version update. Therefore, we recommend pinning the package version to a specific version in your package.json file. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.
Expand Down
39 changes: 33 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,31 @@
},
"dependencies": {
"readable-stream": "^4.7.0",
"@aws-sdk/client-sagemaker": "^3.583.0",
"@aws-sdk/credential-providers": "^3.583.0",
"@aws-crypto/sha256-js": "^5.2.0",
"@smithy/protocol-http": "^5.1.2",
"@smithy/signature-v4": "^5.1.2",
"convict": "^6.2.4",
"form-data": "^4.0.4",
"form-data-encoder": "^4.1.0",
"formdata-node": "^6.0.3"
Comment on lines 24 to 26
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to mariusa some of these can be removed?

},
"peerDependencies": {
"@aws-sdk/credential-providers": "^3.583.0",
"@aws-crypto/sha256-js": "^5.2.0",
"@smithy/protocol-http": "^5.1.2",
"@smithy/signature-v4": "^5.1.2"
},
"peerDependenciesMeta": {
"@aws-sdk/credential-providers": {
"optional": true
},
"@aws-crypto/sha256-js": {
"optional": true
},
"@smithy/protocol-http": {
"optional": true
},
"@smithy/signature-v4": {
"optional": true
}
},
"devDependencies": {
"webpack": "^5.91.0",
"ts-loader": "^9.5.1",
Expand All @@ -53,5 +68,17 @@
"engines": {
"node": ">=18.0.0"
},
"sideEffects": false
"sideEffects": false,
"exports": {
".": {
"types": "./index.d.ts",
"require": "./index.js",
"default": "./index.js"
},
"./aws": {
"types": "./aws.d.ts",
"require": "./aws.js",
"default": "./aws.js"
}
}
}
62 changes: 0 additions & 62 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 29 additions & 4 deletions src/aws-utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
import { Sha256 } from '@aws-crypto/sha256-js';
import { fromNodeProviderChain } from '@aws-sdk/credential-providers';
import { HttpRequest } from '@smithy/protocol-http';
import { SignatureV4 } from '@smithy/signature-v4';
import { PassThrough, Readable } from 'readable-stream';
import { APIResponse, FetchFunction, Fetcher, fetcher } from './core';
import { readableStreamAsyncIterable } from './core/stream/Stream';
import { LineDecoder } from './core/streaming-fetcher/streaming-utils';
import * as serializers from "./serialization";

interface AwsDeps {
Sha256: typeof import('@aws-crypto/sha256-js').Sha256;
fromNodeProviderChain: typeof import('@aws-sdk/credential-providers').fromNodeProviderChain;
HttpRequest: typeof import('@smithy/protocol-http').HttpRequest;
SignatureV4: typeof import('@smithy/signature-v4').SignatureV4;
}

let _awsDeps: AwsDeps | undefined;

function loadAwsDependencies(): AwsDeps {
if (_awsDeps) return _awsDeps;
try {
_awsDeps = {
Sha256: require('@aws-crypto/sha256-js').Sha256,
fromNodeProviderChain: require('@aws-sdk/credential-providers').fromNodeProviderChain,
HttpRequest: require('@smithy/protocol-http').HttpRequest,
SignatureV4: require('@smithy/signature-v4').SignatureV4,
};
return _awsDeps;
} catch (e) {
throw new Error(
`Failed to load AWS peer dependencies: ${e}\n\n` +
'To use AWS clients, install the required peer dependencies:\n' +
' npm install @aws-sdk/credential-providers @aws-crypto/sha256-js @smithy/protocol-http @smithy/signature-v4'
);
}
}

const withTempEnv = async <R>(updateEnv: () => void, fn: () => Promise<R>): Promise<R> => {
const previousEnv = { ...process.env };

Expand Down Expand Up @@ -89,6 +113,7 @@ export const getUrl = (
}

export const getAuthHeaders = async (url: URL, method: string, headers: Record<string, string>, body: unknown, service: AwsPlatform, props: AwsProps): Promise<Record<string, string>> => {
const { Sha256, fromNodeProviderChain, HttpRequest, SignatureV4 } = loadAwsDependencies();
const providerChain = fromNodeProviderChain();

const credentials = await withTempEnv(
Expand Down
4 changes: 4 additions & 0 deletions src/aws.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { BedrockClient, BedrockClientV2 } from "./BedrockClient";
export { SagemakerClient, SagemakerClientV2 } from "./SagemakerClient";
export { AwsClient, AwsClientV2 } from "./AwsClient";
export type { AwsProps } from "./aws-utils";
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
export * as Cohere from "./api";
export { BedrockClient, BedrockClientV2 } from "./BedrockClient";
export { CohereClientV2 } from "./ClientV2";
export { CustomClient as CohereClient } from "./CustomClient";
export { CohereEnvironment } from "./environments";
export { CohereError, CohereTimeoutError } from "./errors";
export { SagemakerClient, SagemakerClientV2 } from "./SagemakerClient";

2 changes: 1 addition & 1 deletion src/test/bedrock-tests.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, test } from "vitest";
import { AwsEndpoint, AwsPlatform } from "aws-utils";
import { BedrockClient, SagemakerClient } from "../";
import { BedrockClient, SagemakerClient } from "../aws";
import { AwsClient } from "../AwsClient";

let cohere: AwsClient;
Expand Down
Loading