Skip to content
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
4 changes: 4 additions & 0 deletions apps/web/app/api/user/settings/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ async function saveAISettings(options: SaveSettingsBody) {
}
// use bedrock if no api key set
return Model.CLAUDE_3_5_SONNET_BEDROCK;
case Provider.GOOGLE:
return options.aiModel || Model.GEMINI_1_5_PRO;
case Provider.OLLAMA:
return Model.OLLAMA;
Comment on lines +31 to +34
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add model validation for Google provider.

While the default model is appropriate, consider validating that options.aiModel is a valid Google model when provided to prevent runtime errors.

 case Provider.GOOGLE:
-  return options.aiModel || Model.GEMINI_1_5_PRO;
+  const model = options.aiModel || Model.GEMINI_1_5_PRO;
+  if (![Model.GEMINI_1_5_PRO, Model.GEMINI_1_5_FLASH].includes(model)) {
+    throw new Error("Invalid Google model");
+  }
+  return model;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
case Provider.GOOGLE:
return options.aiModel || Model.GEMINI_1_5_PRO;
case Provider.OLLAMA:
return Model.OLLAMA;
case Provider.GOOGLE:
const model = options.aiModel || Model.GEMINI_1_5_PRO;
if (![Model.GEMINI_1_5_PRO, Model.GEMINI_1_5_FLASH].includes(model)) {
throw new Error("Invalid Google model");
}
return model;
case Provider.OLLAMA:
return Model.OLLAMA;

default:
throw new Error("Invalid AI provider");
}
Expand Down
7 changes: 6 additions & 1 deletion apps/web/app/api/user/settings/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { z } from "zod";

export const saveSettingsBody = z
.object({
aiProvider: z.enum([Provider.ANTHROPIC, Provider.OPEN_AI]),
aiProvider: z.enum([
Provider.ANTHROPIC,
Provider.OPEN_AI,
Provider.GOOGLE,
...(Provider.OLLAMA ? [Provider.OLLAMA] : []),
]),
aiModel: z.string(),
aiApiKey: z.string().optional(),
})
Expand Down
1 change: 1 addition & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"dependencies": {
"@ai-sdk/amazon-bedrock": "^1.0.6",
"@ai-sdk/anthropic": "^1.0.6",
"@ai-sdk/google": "^1.0.12",
"@ai-sdk/openai": "^1.0.11",
"@asteasolutions/zod-to-openapi": "^7.3.0",
"@auth/core": "^0.37.4",
Expand Down
2 changes: 1 addition & 1 deletion apps/web/utils/ai/rule/generate-rules-prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Your response should only include the list of general rules. Aim for 3-10 broadl

const args = aiResponse.toolCalls[0].args;

logger.trace(args);
logger.trace("Args", { args });

return parseRulesResponse(args, hasSnippets);
}
Expand Down
14 changes: 14 additions & 0 deletions apps/web/utils/llms/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const supportsOllama = env.NEXT_PUBLIC_OLLAMA_MODEL;
export const Provider = {
OPEN_AI: "openai",
ANTHROPIC: "anthropic",
GOOGLE: "google",
...(supportsOllama ? { OLLAMA: "ollama" } : {}),
};

Expand All @@ -15,12 +16,15 @@ export const Model = {
// BEDROCK_ANTHROPIC_BACKUP_MODEL:
// env.NEXT_PUBLIC_BEDROCK_ANTHROPIC_BACKUP_MODEL,
CLAUDE_3_5_SONNET_ANTHROPIC: "claude-3-5-sonnet-20241022",
GEMINI_1_5_PRO: "gemini-1.5-pro-latest",
GEMINI_1_5_FLASH: "gemini-1.5-flash-latest",
...(supportsOllama ? { OLLAMA: env.NEXT_PUBLIC_OLLAMA_MODEL } : {}),
};

export const providerOptions: { label: string; value: string }[] = [
{ label: "OpenAI", value: Provider.OPEN_AI },
{ label: "Anthropic", value: Provider.ANTHROPIC },
{ label: "Google", value: Provider.GOOGLE },
...(supportsOllama && Provider.OLLAMA
? [{ label: "Ollama", value: Provider.OLLAMA }]
: []),
Expand All @@ -38,6 +42,16 @@ export const modelOptions: Record<string, { label: string; value: string }[]> =
value: "claude-3-5-sonnet", // used in ui only. can be either anthropic or bedrock
},
],
[Provider.GOOGLE]: [
{
label: "Gemini 1.5 Pro",
value: Model.GEMINI_1_5_PRO,
},
{
label: "Gemini 1.5 Flash",
value: Model.GEMINI_1_5_FLASH,
},
],
...(Provider.OLLAMA && Model.OLLAMA
? {
[Provider.OLLAMA]: [{ label: "Ollama", value: Model.OLLAMA }],
Expand Down
12 changes: 12 additions & 0 deletions apps/web/utils/llms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { createOpenAI } from "@ai-sdk/openai";
import { createAnthropic } from "@ai-sdk/anthropic";
import { createAmazonBedrock } from "@ai-sdk/amazon-bedrock";
import { createGoogleGenerativeAI } from "@ai-sdk/google";
import { createOllama } from "ollama-ai-provider";
import { env } from "@/env";
import { saveAiUsage } from "@/utils/usage";
Expand Down Expand Up @@ -70,6 +71,17 @@ function getModel({ aiProvider, aiModel, aiApiKey }: UserAIFields) {
};
}

if (provider === Provider.GOOGLE) {
if (!aiApiKey) throw new Error("Google API key is not set");

const model = aiModel || Model.GEMINI_1_5_PRO;
return {
provider: Provider.GOOGLE,
model,
llmModel: createGoogleGenerativeAI({ apiKey: aiApiKey })(model),
};
}

if (provider === Provider.OLLAMA && env.NEXT_PUBLIC_OLLAMA_MODEL) {
return {
provider: Provider.OLLAMA,
Expand Down
10 changes: 10 additions & 0 deletions apps/web/utils/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ const costs: Record<
input: 0.8 / 1_000_000,
output: 4 / 1_000_000,
},
// https://ai.google.dev/pricing#1_5pro
"gemini-1.5-pro-latest": {
input: 1.25 / 1_000_000,
output: 5 / 1_000_000,
},
// https://ai.google.dev/pricing#1_5flash
"gemini-1.5-flash-latest": {
input: 0.075 / 1_000_000,
output: 0.3 / 1_000_000,
},
};

// returns cost in cents
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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