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(cli): Use Anthropic via Amazon Bedrock #268

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"dependencies": {
"@ai-sdk/anthropic": "^0.0.50",
"@ai-sdk/openai": "^0.0.61",
"@anthropic-ai/bedrock-sdk": "^0.12.0",
"@clerk/nextjs": "^5.6.0",
"@faker-js/faker": "^8.4.1",
"@octokit/rest": "^21.0.2",
Expand Down
33 changes: 24 additions & 9 deletions packages/shortest/src/ai/client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import AnthropicBedrock from "@anthropic-ai/bedrock-sdk";
import Anthropic from "@anthropic-ai/sdk";
import pc from "picocolors";
import { BashTool } from "../browser/core/bash-tool";
Expand All @@ -9,22 +10,36 @@ import { SYSTEM_PROMPT } from "./prompts";
import { AITools } from "./tools";

export class AIClient {
private client: Anthropic;
private client: Anthropic | AnthropicBedrock;
Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for contributing, Taylor!

One thing here: do Anthropic and AnthropicBedrock share different interfaces? If so, it will be nice to create abstract class around them to make our code more scalable when we decide add more AI clients?

Copy link
Author

Choose a reason for hiding this comment

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

this is a good idea. i wrote this commit originally for my personal fork but saw someone else wanted it as well.

if i have the time, ill implement this. thanks for the feedback!

Copy link
Contributor

@gladyshcodes gladyshcodes Jan 12, 2025

Choose a reason for hiding this comment

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

nice. we could actually merge this pr as is if it works as expected, and then you could open another pr with that impl if you have time

private model: string;
private maxMessages: number;
private debugMode: boolean;

constructor(config: AIConfig, debugMode: boolean = false) {
if (!config.apiKey) {
throw new Error(
"Anthropic API key is required. Set it in shortest.config.ts or ANTHROPIC_API_KEY env var",
);
if (config.useBedrock) {
if (!config.awsAccessKey || !config.awsSecretKey || !config.awsRegion) {
throw new Error(
"AWS credentials required when using Bedrock. Set awsAccessKey, awsSecretKey and awsRegion in shortest.config.ts",
);
}
this.client = new AnthropicBedrock({
awsAccessKey: config.awsAccessKey,
awsSecretKey: config.awsSecretKey,
awsRegion: config.awsRegion,
});
this.model = "us.anthropic.claude-3-5-sonnet-20241022-v2:0";
} else {
if (!config.apiKey) {
throw new Error(
"Anthropic API key is required. Set it in shortest.config.ts or ANTHROPIC_API_KEY env var",
);
}
this.client = new Anthropic({
apiKey: config.apiKey,
});
this.model = "claude-3-5-sonnet-20241022";
}

this.client = new Anthropic({
apiKey: config.apiKey,
});
this.model = "claude-3-5-sonnet-20241022";
this.maxMessages = 10;
this.debugMode = debugMode;
}
Expand Down
20 changes: 17 additions & 3 deletions packages/shortest/src/core/runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,31 @@ export class TestRunner {
},
});

// this may never happen as the config is initialized before this code is executed
if (!this.config.anthropicKey) {
// this may never happen as the config is initlized before this code is executed
if (this.config.useBedrock) {
if (
!this.config.awsAccessKey ||
!this.config.awsSecretKey ||
!this.config.awsRegion
) {
throw new Error("AWS credentials required when using Bedrock");
}
} else if (!this.config.anthropicKey) {
throw new Error("ANTHROPIC_KEY is not set");
}

const aiClient = new AIClient(
{
apiKey: this.config.anthropicKey,
model: "claude-3-5-sonnet-20241022",
awsAccessKey: this.config.awsAccessKey,
awsSecretKey: this.config.awsSecretKey,
awsRegion: this.config.awsRegion,
model: this.config.useBedrock
? "us.anthropic.claude-3-5-sonnet-20241022-v2:0"
: "claude-3-5-sonnet-20241022",
maxMessages: 10,
debug: this.debugAI,
useBedrock: this.config.useBedrock,
},
this.debugAI,
);
Expand Down
14 changes: 12 additions & 2 deletions packages/shortest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,18 @@ function validateConfig(config: Partial<ShortestConfig>) {
if (config.headless === undefined) missingFields.push("headless");
if (!config.baseUrl) missingFields.push("baseUrl");
if (!config.testPattern) missingFields.push("testPattern");
if (!config.anthropicKey && !process.env.ANTHROPIC_API_KEY)
missingFields.push("anthropicKey");

if (config.useBedrock) {
if (!config.awsAccessKey && !process.env.AWS_ACCESS_KEY_ID)
missingFields.push("awsAccessKey");
if (!config.awsSecretKey && !process.env.AWS_SECRET_ACCESS_KEY)
missingFields.push("awsSecretKey");
if (!config.awsRegion && !process.env.AWS_REGION)
missingFields.push("awsRegion");
} else {
if (!config.anthropicKey && !process.env.ANTHROPIC_API_KEY)
missingFields.push("anthropicKey");
}

if (missingFields.length > 0) {
throw new Error(
Expand Down
6 changes: 5 additions & 1 deletion packages/shortest/src/types/ai.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { ActionInput } from "./browser";

export interface AIConfig {
apiKey: string;
apiKey?: string;
model?: string;
maxMessages?: number;
debug?: boolean;
useBedrock?: boolean;
awsAccessKey?: string;
awsSecretKey?: string;
awsRegion?: string;
}

export interface AIResponse {
Expand Down
4 changes: 4 additions & 0 deletions packages/shortest/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ export interface ShortestConfig {
baseUrl: string;
testPattern: string;
anthropicKey?: string;
useBedrock?: boolean;
awsAccessKey?: string;
awsSecretKey?: string;
awsRegion?: string;
mailosaur?: {
apiKey?: string;
serverId?: string;
Expand Down
Loading