Skip to content

Commit 41bdb03

Browse files
committed
Simplifies model provider logic and updates environment variables
1 parent 1ce140a commit 41bdb03

File tree

3 files changed

+21
-45
lines changed

3 files changed

+21
-45
lines changed

samples/mastra-nextjs/app/src/mastra/agent/index.ts

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { Agent } from "@mastra/core/agent";
22
import { createAmazonBedrock } from "@ai-sdk/amazon-bedrock";
3-
import type { LanguageModelV2 } from "@ai-sdk/provider";
43
import { fromNodeProviderChain } from "@aws-sdk/credential-providers";
5-
import { createVertex } from "@ai-sdk/google-vertex";
4+
import { vertex } from "@ai-sdk/google-vertex";
65

76
import { getMemory } from "../memory";
87
import { instructions } from "./instructions";
@@ -13,49 +12,25 @@ import { getRepositoryCommits } from "../tools/getRepositoryCommits";
1312
import { getRepositoryPullRequests } from "../tools/getRepositoryPullRequests";
1413
import { getRepositoryStars } from "../tools/getRepositoryStars";
1514

16-
// https://ai-sdk.dev/providers/ai-sdk-providers/amazon-bedrock#using-aws-sdk-credentials-chain-instance-profiles-instance-roles-ecs-roles-eks-service-accounts-etc
17-
function createAWSModelProvider(modelName: string): LanguageModelV2 {
18-
return createAmazonBedrock({
19-
credentialProvider: fromNodeProviderChain(),
20-
})(modelName);
21-
}
22-
23-
// https://ai-sdk.dev/providers/ai-sdk-providers/google-vertex
24-
function createGCPModelProvider(modelName: string): LanguageModelV2 {
25-
const projectId = process.env.DEFANG_GCP_PROJECT;
26-
if (projectId === undefined) {
27-
throw new Error(
28-
"DEFANG_GCP_PROJECT is not defined in environment variables"
29-
);
30-
}
31-
const location = process.env.DEFANG_GCP_LOCATION;
32-
if (location === undefined) {
33-
throw new Error(
34-
"DEFANG_GCP_LOCATION is not defined in environment variables"
35-
);
36-
}
37-
return createVertex({
38-
project: projectId,
39-
location: location,
40-
})(modelName);
41-
}
42-
43-
function getModel(): LanguageModelV2 {
44-
const provider = process.env.DEFANG_PROVIDER;
15+
function getModel() {
16+
const provider = process.env.AWS_REGION ? "aws" : "gcp";
4517
const modelName = process.env.LLM_MODEL;
4618
if (modelName === undefined) {
4719
throw new Error("LLM_MODEL is not defined in environment variables");
4820
}
4921

50-
if (provider === "aws") {
51-
return createAWSModelProvider(modelName);
52-
}
53-
54-
if (provider === "gcp") {
55-
return createGCPModelProvider(modelName);
22+
switch (provider) {
23+
case "aws":
24+
// https://ai-sdk.dev/providers/ai-sdk-providers/amazon-bedrock#using-aws-sdk-credentials-chain-instance-profiles-instance-roles-ecs-roles-eks-service-accounts-etc
25+
return createAmazonBedrock({
26+
credentialProvider: fromNodeProviderChain(),
27+
})(modelName);
28+
case "gcp":
29+
// https://ai-sdk.dev/providers/ai-sdk-providers/google-vertex
30+
return vertex(modelName);
31+
default:
32+
return modelName;
5633
}
57-
58-
throw new Error(`Unsupported DEFANG_PROVIDER: ${provider}`);
5934
}
6035

6136
export const agent = new Agent({

samples/mastra-nextjs/compose.dev.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ services:
66
environment:
77
- DB_URL=postgres://postgres:password@database:5432/postgres
88
- DB_SSL=false #set this to true if deploying to defang playground
9+
- LLM_MODEL=gemini-2.5-flash
910
- GOOGLE_GENERATIVE_AI_API_KEY=xxx
1011
- GITHUB_TOKEN=xxx # remove this line if unused
1112

samples/mastra-nextjs/compose.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
services:
22
app:
3+
domainname: example.com
34
x-defang-llm: true
45
build:
56
context: ./app
67
dockerfile: Dockerfile
78
environment:
89
- DB_URL=postgres://postgres:${POSTGRES_PASSWORD}@database:5432/postgres
9-
- DB_SSL=true #set this to false if deploying to defang playground
10-
- GITHUB_TOKEN #optional, remove this line if unused
11-
- DEFANG_PROVIDER # use "gcp" when deploying to Google Cloud, "aws" when deploying to AWS
12-
- DEFANG_GCP_PROJECT # change to your GCP project when deploying to Google Cloud
13-
- DEFANG_GCP_LOCATION #should be us-central1 when deploying to Google Cloud, else change to your perferred location
14-
- LLM_MODEL=anthropic.claude-3-5-sonnet-20241022-v2:0 #gemini-2.5-flash for GCP, anthroipc.claude-3-5-sonnet-20241022-v2 for AWS
10+
- DB_SSL=true # set this to false if deploying to defang playground, true otherwise
11+
- GOOGLE_VERTEX_PROJECT= # your GCP project ID
12+
- GOOGLE_VERTEX_LOCATION= # your GCP region, e.g., us-central1
13+
- LLM_MODEL # gemini-2.5-flash for GCP, anthropic.claude-3-5-sonnet-20241022-v2:0 for AWS
14+
- GITHUB_TOKEN # optional, remove this line if unused
1515
restart: unless-stopped
1616
ports:
1717
- mode: ingress

0 commit comments

Comments
 (0)