Skip to content

Commit 7a61396

Browse files
committed
Refactors model provider logic and updates dependencies
1 parent 1511381 commit 7a61396

File tree

6 files changed

+50
-23
lines changed

6 files changed

+50
-23
lines changed

samples/mastra-nextjs/.defang/lab

Lines changed: 0 additions & 7 deletions
This file was deleted.

samples/mastra-nextjs/app/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,5 @@ yarn-error.log*
4141
next-env.d.ts
4242

4343
.mastra
44+
../.defang
45+

samples/mastra-nextjs/app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
},
1212
"dependencies": {
1313
"@ai-sdk/amazon-bedrock": "^3.0.67",
14-
"@ai-sdk/google": "^2.0.44",
1514
"@ai-sdk/google-vertex": "^3.0.86",
15+
"@ai-sdk/provider": "^2.0.0",
1616
"@assistant-ui/react": "^0.11.39",
1717
"@assistant-ui/react-ai-sdk": "^1.1.11",
1818
"@assistant-ui/react-markdown": "^0.11.4",

samples/mastra-nextjs/app/pnpm-lock.yaml

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

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

@@ -12,27 +13,56 @@ import { getRepositoryCommits } from "../tools/getRepositoryCommits";
1213
import { getRepositoryPullRequests } from "../tools/getRepositoryPullRequests";
1314
import { getRepositoryStars } from "../tools/getRepositoryStars";
1415

15-
function AWSModelProvider(modelName: string) {
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 {
1618
return createAmazonBedrock({
1719
credentialProvider: fromNodeProviderChain(),
1820
})(modelName);
1921
}
2022

21-
function GCPModelProvider(model: string) {
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+
}
2237
return createVertex({
23-
project: "cloudbuildtest-468719",
24-
location: "us-central1",
25-
})(model);
38+
project: projectId,
39+
location: location,
40+
})(modelName);
41+
}
42+
43+
function getModel(): LanguageModelV2 {
44+
const provider = process.env.DEFANG_PROVIDER;
45+
const modelName = process.env.LLM_MODEL;
46+
if (modelName === undefined) {
47+
throw new Error("LLM_MODEL is not defined in environment variables");
48+
}
49+
50+
if (provider === "aws") {
51+
return createAWSModelProvider(modelName);
52+
}
53+
54+
if (provider === "gcp") {
55+
return createGCPModelProvider(modelName);
56+
}
57+
58+
throw new Error(`Unsupported DEFANG_PROVIDER: ${provider}`);
2659
}
2760

2861
export const agent = new Agent({
2962
name: "agent",
3063
instructions,
3164
memory: getMemory,
32-
model:
33-
process.env.DEFANG_PROVIDER === "aws"
34-
? AWSModelProvider(process.env.LLM_MODEL || "us.amazon.nova-lite-v1:0")
35-
: GCPModelProvider(process.env.LLM_MODEL || "gemini-2.5-pro"),
65+
model: getModel,
3666
tools: {
3767
getFilePaths,
3868
getFileContent,

samples/mastra-nextjs/compose.yaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ services:
77
environment:
88
- DB_URL=postgres://postgres:${POSTGRES_PASSWORD}@database:5432/postgres
99
- DB_SSL=true #set this to false if deploying to defang playground
10-
- GITHUB_TOKEN
11-
- DEFANG_PROVIDER=gcp
12-
- LLM_MODEL=gemini-2.5-flash #anthropic.claude-3-5-sonnet-20241022-v2:0
10+
- GITHUB_TOKEN #optional, remove this line if unused
11+
- DEFANG_PROVIDER=aws # change to gcp when deploying to Google Cloud
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
1315
restart: unless-stopped
1416
ports:
1517
- mode: ingress

0 commit comments

Comments
 (0)