Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bedrock): add messages API #305

Merged
merged 1 commit into from
Mar 4, 2024
Merged
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
20 changes: 10 additions & 10 deletions packages/bedrock-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ yarn add @anthropic-ai/bedrock-sdk

<!-- prettier-ignore -->
```js
import Anthropic from '@anthropic-ai/sdk';
import { AnthropicBedrock } from '@anthropic-ai/bedrock-sdk';

// Note: this assumes you have configured AWS credentials in a way
Expand All @@ -29,16 +28,17 @@ import { AnthropicBedrock } from '@anthropic-ai/bedrock-sdk';
const anthropic = new AnthropicBedrock();

async function main() {
const completion = await anthropic.completions.create({
model: 'anthropic.claude-3-opus-20240229-v1:0',
prompt: `${Anthropic.HUMAN_PROMPT} how does a court case get to the Supreme Court? ${Anthropic.AI_PROMPT}`,
stop_sequences: [Anthropic.HUMAN_PROMPT],
max_tokens_to_sample: 800,
temperature: 0.5,
top_k: 250,
top_p: 0.5,
const message = await anthropic.messages.create({
model: 'anthropic.claude-3-sonnet-20240229-v1:0',
messages: [
{
role: 'user',
content: 'Hello!',
},
],
max_tokens: 1024,
});
console.log(completion);
console.log(message);
}

main();
Expand Down
20 changes: 10 additions & 10 deletions packages/bedrock-sdk/examples/demo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env -S npm run tsn -T

import Anthropic from '@anthropic-ai/sdk';
import { AnthropicBedrock } from '@anthropic-ai/bedrock-sdk';

// Note: this assumes you have configured AWS credentials in a way
Expand All @@ -11,16 +10,17 @@ import { AnthropicBedrock } from '@anthropic-ai/bedrock-sdk';
const anthropic = new AnthropicBedrock();

async function main() {
const completion = await anthropic.completions.create({
model: 'anthropic.claude-3-opus-20240229-v1:0',
prompt: `${Anthropic.HUMAN_PROMPT} how does a court case get to the Supreme Court? ${Anthropic.AI_PROMPT}`,
stop_sequences: [Anthropic.HUMAN_PROMPT],
max_tokens_to_sample: 800,
temperature: 0.5,
top_k: 250,
top_p: 0.5,
const message = await anthropic.messages.create({
model: 'anthropic.claude-3-sonnet-20240229-v1:0',
messages: [
{
role: 'user',
content: 'Hello!',
},
],
max_tokens: 1024,
});
console.log(completion);
console.log(message);
}

main();
23 changes: 14 additions & 9 deletions packages/bedrock-sdk/examples/streaming.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env -S npm run tsn -T

import Anthropic from '@anthropic-ai/sdk';
import { AnthropicBedrock } from '@anthropic-ai/bedrock-sdk';

// Note: this assumes you have configured AWS credentials in a way
Expand All @@ -11,18 +10,24 @@ import { AnthropicBedrock } from '@anthropic-ai/bedrock-sdk';
const client = new AnthropicBedrock();

async function main() {
const question = 'Hey Claude! How can I recursively list all files in a directory in Rust?';

const stream = await client.completions.create({
prompt: `${Anthropic.HUMAN_PROMPT}${question}${Anthropic.AI_PROMPT}:`,
model: 'anthropic.claude-v2:1',
const stream = await client.messages.create({
model: 'anthropic.claude-3-sonnet-20240229-v1:0',
messages: [
{
role: 'user',
content: 'Hello!',
},
],
max_tokens: 1024,
stream: true,
max_tokens_to_sample: 500,
});

for await (const completion of stream) {
process.stdout.write(completion.completion);
for await (const event of stream) {
if (event.type === 'content_block_delta' && event.delta.type === 'text_delta') {
process.stdout.write(event.delta.text);
}
}
process.stdout.write('\n');
}

main();
4 changes: 3 additions & 1 deletion packages/bedrock-sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getAuthHeaders } from './auth';
import { Stream } from './streaming';

const DEFAULT_VERSION = 'bedrock-2023-05-31';
const MODEL_ENDPOINTS = new Set<string>(['/v1/complete', '/v1/messages']);

export type ClientOptions = Omit<API.ClientOptions, 'apiKey' | 'authToken'> & {
awsSecretKey?: string | null | undefined;
Expand Down Expand Up @@ -73,6 +74,7 @@ export class AnthropicBedrock extends Core.APIClient {
this.awsSessionToken = awsSessionToken;
}

messages: Resources.Messages = new Resources.Messages(this);
completions: Resources.Completions = new Resources.Completions(this);

protected override defaultQuery(): Core.DefaultQuery | undefined {
Expand Down Expand Up @@ -120,7 +122,7 @@ export class AnthropicBedrock extends Core.APIClient {
}
}

if (options.path === '/v1/complete' && options.method === 'post') {
if (MODEL_ENDPOINTS.has(options.path) && options.method === 'post') {
if (!Core.isObj(options.body)) {
throw new Error('Expected request body to be an object for post /v1/messages');
}
Expand Down