Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

improvement: use fully qualified model ids (#6003)

Myles Scolnicka58ec287f126
diff --git a/frontend/src/components/app-config/constants.ts b/frontend/src/components/app-config/constants.ts
index 4eaa4719f4..912815df6b 100644
--- a/frontend/src/components/app-config/constants.ts
+++ b/frontend/src/components/app-config/constants.ts
@@ -1,39 +1,37 @@
/* Copyright 2024 Marimo. All rights reserved. */
export const KNOWN_AI_MODELS = [
// Anthropic
- "claude-opus-4-20250514",
- "claude-sonnet-4-20250514",
- "claude-3-7-sonnet-latest",
- "claude-3-5-sonnet-latest",
- "claude-3-5-haiku-latest",
+ "anthropic/claude-opus-4-1-20250805",
+ "anthropic/claude-opus-4-20250514",
+ "anthropic/claude-sonnet-4-20250514",
+ "anthropic/claude-3-7-sonnet-latest",
+ "anthropic/claude-3-5-sonnet-latest",
+ "anthropic/claude-3-5-haiku-latest",
// DeepSeek
- "deepseek-v3",
- "deepseek-r1",
+ "deepseek/deepseek-v3",
+ "deepseek/deepseek-r1",
// Google
- "gemini-2.5-flash-preview-05-20",
- "gemini-2.5-pro-preview-06-05",
- "gemini-2.0-flash",
- "gemini-2.0-flash-lite",
+ "google/gemini-2.5-flash-preview-05-20",
+ "google/gemini-2.5-pro-preview-06-05",
+ "google/gemini-2.0-flash",
+ "google/gemini-2.0-flash-lite",
// OpenAI
- "o3",
- "o4-mini",
- "gpt-4.5-preview",
- "gpt-4.1",
- "gpt-4o",
- "gpt-3.5-turbo",
+ "openai/o3",
+ "openai/o4-mini",
+ "openai/gpt-4.5-preview",
+ "openai/gpt-4.1",
+ "openai/gpt-4o",
+ "openai/gpt-3.5-turbo",
// AWS Bedrock Models
- "bedrock/us.anthropic.claude-3-7-sonnet-20250219-v1:0",
- "bedrock/anthropic.claude-3-sonnet-20240229",
- "bedrock/anthropic.claude-3-haiku-20240307",
- "bedrock/meta.llama3-8b-instruct-v1:0",
- "bedrock/amazon.titan-text-express-v1",
- "bedrock/us.amazon.nova-pro-v1:0",
+ "bedrock/anthropic.claude-3-5-haiku-20241022-v1:0",
+ "bedrock/anthropic.claude-3-5-sonnet-20241022-v2:0",
+ "bedrock/anthropic.claude-3-7-sonnet-20250219-v1:0",
+ "bedrock/meta.llama3-3-70b-instruct-v1:0",
"bedrock/cohere.command-r-plus-v1",
- "bedrock/ai21.j2-ultra-v1",
] as const;
/**
diff --git a/frontend/src/components/app-config/incorrect-model-id.tsx b/frontend/src/components/app-config/incorrect-model-id.tsx
new file mode 100644
index 0000000000..2ecb0bd2fc
--- /dev/null
+++ b/frontend/src/components/app-config/incorrect-model-id.tsx
@@ -0,0 +1,41 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+import React from "react";
+import { Banner } from "@/plugins/impl/common/error-banner";
+import { AiModelId, type QualifiedModelId } from "@/utils/ai/ids";
+
+interface IncorrectModelIdProps {
+ value: string | null | undefined;
+}
+
+export const IncorrectModelId: React.FC<IncorrectModelIdProps> = ({
+ value,
+}) => {
+ if (!value) {
+ return null;
+ }
+
+ // Only incorrect if missing a slash
+ if (value.includes("/")) {
+ return null;
+ }
+
+ // Try to "correct" by guessing provider
+ const parsed = AiModelId.parse(value as QualifiedModelId);
+ const suggestion = parsed.id;
+
+ return (
+ <Banner kind="danger" className="mt-1">
+ <span>
+ Model id should be in the form{" "}
+ <code className="font-bold">provider/model</code>. {value} is missing a
+ provider.
+ </span>
+ <br />
+ {suggestion && (
+ <span>
+ Did you mean <code className="font-bold">{suggestion}</code>?
+ </span>
+ )}
+ </Banner>
+ );
+};
diff --git a/frontend/src/components/app-config/user-config-form.tsx b/frontend/src/components/app-config/user-config-form.tsx
index fc0d9d8445..a0e092f2eb 100644
--- a/frontend/src/components/app-config/user-config-form.tsx
+++ b/frontend/src/components/app-config/user-config-form.tsx
@@ -54,6 +54,7 @@ import { Textarea } from "../ui/textarea";
import { Tooltip } from "../ui/tooltip";
import { SettingSubtitle, SQL_OUTPUT_SELECT_OPTIONS } from "./common";
import { AWS_REGIONS, KNOWN_AI_MODELS } from "./constants";
+import { IncorrectModelId } from "./incorrect-model-id";
import { useIsConfigOverridden } from "./is-overridden";
import { OptionalFeatures } from "./optional-features";
@@ -192,20 +193,23 @@ export const UserConfigForm: React.FC = () => {
control={form.control}
name="completion.model"
render={({ field }) => (
- <FormItem className={formItemClasses}>
- <FormLabel>Model</FormLabel>
- <FormControl>
- <Input
- data-testid="custom-model-input"
- className="m-0 inline-flex"
- placeholder="Qwen2.5-Coder-7B"
- {...field}
- value={field.value || ""}
- />
- </FormControl>
- <FormMessage />
- <IsOverridden userConfig={config} name="completion.model" />
- </FormItem>
+ <>
+ <FormItem className={formItemClasses}>
+ <FormLabel>Model</FormLabel>
+ <FormControl>
+ <Input
+ data-testid="custom-model-input"
+ className="m-0 inline-flex"
+ placeholder="Qwen2.5-Coder-7B"
+ {...field}
+ value={field.value || ""}
+ />
+ </FormControl>
+ <FormMessage />
+ <IsOverridden userConfig={config} name="completion.model" />
+ </FormItem>
+ <IncorrectModelId value={field.value} />
+ </>
)}
/>
<FormField
@@ -1605,14 +1609,15 @@ export const UserConfigForm: React.FC = () => {
</option>
))}
</datalist>
+ <IncorrectModelId value={field.value} />
<FormDescription>
- If the model starts with "claude-", we will use your
- Anthropic API key. If the model starts with "gemini-", we
- will use your Google AI API key. If the model starts with
- a "bedrock/" prefix followed by a model id (e.g.,
- "bedrock/anthropic.claude-3-sonnet-20240229"), we will use
- your AWS Bedrock configuration. Otherwise, we will use
- your OpenAI API key.
+ Models should include the provider name and model name
+ separated by a slash. For example,
+ "anthropic/claude-3-7-sonnet-latest" or
+ "google/gemini-2.5-flash-preview-05-20".
+ <br />
+ Depending on the provider, we will use the respective API
+ key and additional configuration.
</FormDescription>
</div>
)}
diff --git a/frontend/src/utils/ai/__tests__/ids.test.ts b/frontend/src/utils/ai/__tests__/ids.test.ts
new file mode 100644
index 0000000000..231212dadc
--- /dev/null
+++ b/frontend/src/utils/ai/__tests__/ids.test.ts
@@ -0,0 +1,184 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+import { describe, expect, it } from "vitest";
+import { AiModelId, type ProviderId, type ShortModelId } from "../ids";
+
+describe("AiModelId", () => {
+ describe("constructor", () => {
+ it("should create an instance with providerId and shortModelId", () => {
+ const modelId = new AiModelId("openai", "gpt-4" as ShortModelId);
+ expect(modelId.providerId).toBe("openai");
+ expect(modelId.shortModelId).toBe("gpt-4");
+ });
+ });
+
+ describe("id getter", () => {
+ it("should return qualified model id", () => {
+ const modelId = new AiModelId("anthropic", "claude-3" as ShortModelId);
+ expect(modelId.id).toBe("anthropic/claude-3");
+ });
+
+ it("should handle all provider types", () => {
+ const providers: ProviderId[] = [
+ "openai",
+ "anthropic",
+ "google",
+ "ollama",
+ "bedrock",
+ ];
+
+ providers.forEach((provider) => {
+ const modelId = new AiModelId(provider, "test-model" as ShortModelId);
+ expect(modelId.id).toBe(`${provider}/test-model`);
+ });
+ });
+ });
+
+ describe("parse", () => {
+ describe("with qualified ids (containing '/')", () => {
+ it("should parse openai qualified id", () => {
+ const modelId = AiModelId.parse("openai/gpt-4");
+ expect(modelId.providerId).toBe("openai");
+ expect(modelId.shortModelId).toBe("gpt-4");
+ });
+
+ it("should parse anthropic qualified id", () => {
+ const modelId = AiModelId.parse("anthropic/claude-3-sonnet");
+ expect(modelId.providerId).toBe("anthropic");
+ expect(modelId.shortModelId).toBe("claude-3-sonnet");
+ });
+
+ it("should parse google qualified id", () => {
+ const modelId = AiModelId.parse("google/gemini-pro");
+ expect(modelId.providerId).toBe("google");
+ expect(modelId.shortModelId).toBe("gemini-pro");
+ });
+
+ it("should parse ollama qualified id", () => {
+ const modelId = AiModelId.parse("ollama/llama2");
+ expect(modelId.providerId).toBe("ollama");
+ expect(modelId.shortModelId).toBe("llama2");
+ });
+
+ it("should parse bedrock qualified id", () => {
+ const modelId = AiModelId.parse("bedrock/titan-text");
+ expect(modelId.providerId).toBe("bedrock");
+ expect(modelId.shortModelId).toBe("titan-text");
+ });
+
+ it("should handle multiple slashes", () => {
+ const modelId = AiModelId.parse("openai/gpt-4/turbo");
+ expect(modelId.providerId).toBe("openai");
+ expect(modelId.shortModelId).toBe("gpt-4/turbo");
+ });
+ });
+
+ describe("without qualified ids (no '/')", () => {
+ describe("openai models", () => {
+ it("should guess openai for gpt models", () => {
+ const modelId = AiModelId.parse("gpt-4");
+ expect(modelId.providerId).toBe("openai");
+ expect(modelId.shortModelId).toBe("gpt-4");
+ });
+
+ it("should guess openai for gpt-3.5 models", () => {
+ const modelId = AiModelId.parse("gpt-3.5-turbo");
+ expect(modelId.providerId).toBe("openai");
+ expect(modelId.shortModelId).toBe("gpt-3.5-turbo");
+ });
+
+ it("should guess openai for o3 models", () => {
+ const modelId = AiModelId.parse("o3-mini");
+ expect(modelId.providerId).toBe("openai");
+ expect(modelId.shortModelId).toBe("o3-mini");
+ });
+
+ it("should guess openai for o1 models", () => {
+ const modelId = AiModelId.parse("o1-preview");
+ expect(modelId.providerId).toBe("openai");
+ expect(modelId.shortModelId).toBe("o1-preview");
+ });
+ });
+
+ describe("anthropic models", () => {
+ it("should guess anthropic for claude models", () => {
+ const modelId = AiModelId.parse("claude-3-sonnet");
+ expect(modelId.providerId).toBe("anthropic");
+ expect(modelId.shortModelId).toBe("claude-3-sonnet");
+ });
+
+ it("should guess anthropic for claude-instant", () => {
+ const modelId = AiModelId.parse("claude-instant");
+ expect(modelId.providerId).toBe("anthropic");
+ expect(modelId.shortModelId).toBe("claude-instant");
+ });
+ });
+
+ describe("google models", () => {
+ it("should guess google for gemini models", () => {
+ const modelId = AiModelId.parse("gemini-pro");
+ expect(modelId.providerId).toBe("google");
+ expect(modelId.shortModelId).toBe("gemini-pro");
+ });
+
+ it("should guess google for google-prefixed models", () => {
+ const modelId = AiModelId.parse("google-palm");
+ expect(modelId.providerId).toBe("google");
+ expect(modelId.shortModelId).toBe("google-palm");
+ });
+ });
+
+ describe("ollama fallback", () => {
+ it("should default to ollama for unknown models", () => {
+ const modelId = AiModelId.parse("llama2");
+ expect(modelId.providerId).toBe("ollama");
+ expect(modelId.shortModelId).toBe("llama2");
+ });
+
+ it("should default to ollama for custom models", () => {
+ const modelId = AiModelId.parse("my-custom-model");
+ expect(modelId.providerId).toBe("ollama");
+ expect(modelId.shortModelId).toBe("my-custom-model");
+ });
+
+ it("should default to ollama for empty string", () => {
+ const modelId = AiModelId.parse("");
+ expect(modelId.providerId).toBe("ollama");
+ expect(modelId.shortModelId).toBe("");
+ });
+ });
+ });
+ });
+
+ describe("round-trip parsing", () => {
+ it("should maintain consistency when parsing generated ids", () => {
+ const original = new AiModelId(
+ "anthropic",
+ "claude-3-opus" as ShortModelId,
+ );
+ const parsed = AiModelId.parse(original.id);
+
+ expect(parsed.providerId).toBe(original.providerId);
+ expect(parsed.shortModelId).toBe(original.shortModelId);
+ expect(parsed.id).toBe(original.id);
+ });
+
+ it("should work for all providers", () => {
+ const providers: ProviderId[] = [
+ "openai",
+ "anthropic",
+ "google",
+ "ollama",
+ "bedrock",
+ ];
+
+ providers.forEach((provider) => {
+ const original = new AiModelId(provider, "test-model" as ShortModelId);
+ const parsed = AiModelId.parse(original.id);
+
+ expect(parsed.providerId).toBe(provider);
+ expect(parsed.shortModelId).toBe("test-model");
+ });
+ });
+ });
+});
diff --git a/frontend/src/utils/ai/ids.ts b/frontend/src/utils/ai/ids.ts
new file mode 100644
index 0000000000..369ec3826e
--- /dev/null
+++ b/frontend/src/utils/ai/ids.ts
@@ -0,0 +1,61 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+import type { TypedString } from "../typed";
+
+/**
+ * Supported providers by the marimo server.
+ */
+export type ProviderId =
+ | "openai"
+ | "anthropic"
+ | "google"
+ | "ollama"
+ | "bedrock";
+
+export type ShortModelId = TypedString<"ShortModelId">;
+
+/**
+ * Qualified model id
+ * `provider_id/short_model_id`;
+ */
+export type QualifiedModelId = `${ProviderId}/${ShortModelId}`;
+
+export class AiModelId {
+ readonly providerId: ProviderId;
+ readonly shortModelId: ShortModelId;
+
+ constructor(providerId: ProviderId, shortModelId: ShortModelId) {
+ this.providerId = providerId;
+ this.shortModelId = shortModelId;
+ }
+
+ get id(): QualifiedModelId {
+ return `${this.providerId}/${this.shortModelId}`;
+ }
+
+ static parse(id: string): AiModelId {
+ if (!id.includes("/")) {
+ const providerId = guessProviderId(id);
+ return new AiModelId(providerId, id as ShortModelId);
+ }
+
+ const [providerId, ...shortModelId] = id.split("/");
+ return new AiModelId(
+ providerId as ProviderId,
+ shortModelId.join("/") as ShortModelId,
+ );
+ }
+}
+
+function guessProviderId(id: string): ProviderId {
+ if (id.startsWith("gpt") || id.startsWith("o3") || id.startsWith("o1")) {
+ return "openai";
+ }
+ if (id.startsWith("claude")) {
+ return "anthropic";
+ }
+ if (id.startsWith("gemini") || id.startsWith("google")) {
+ return "google";
+ }
+ return "ollama";
+}
diff --git a/marimo/_server/ai/ids.py b/marimo/_server/ai/ids.py
new file mode 100644
index 0000000000..fca30eea7c
--- /dev/null
+++ b/marimo/_server/ai/ids.py
@@ -0,0 +1,62 @@
+from dataclasses import dataclass
+from typing import NewType
+
+from marimo import _loggers
+
+LOGGER = _loggers.marimo_logger()
+
+AiProviderId = NewType("AiProviderId", str)
+QualifiedModelId = NewType("QualifiedModelId", str)
+ShortModelId = NewType("ShortModelId", str)
+
+
+@dataclass
+class AiModelId:
+ provider: AiProviderId
+ model: ShortModelId
+
+ def __str__(self) -> QualifiedModelId:
+ return QualifiedModelId(f"{self.provider}/{self.model}")
+
+ def __repr__(self) -> str:
+ return f"AiModelId(provider={self.provider}, model={self.model})"
+
+ @staticmethod
+ def from_model(model_id: str) -> "AiModelId":
+ if "/" not in model_id:
+ LOGGER.warning(
+ f"Invalid model ID: {model_id}. Model ID must be in the format <provider>/<model>"
+ )
+ guess = _guess_provider(model_id)
+ LOGGER.warning(f"Guessing provider for {model_id} as {guess}")
+ return AiModelId(provider=guess, model=ShortModelId(model_id))
+
+ provider, short_id = model_id.split("/", 1)
+
+ return AiModelId(
+ provider=AiProviderId(provider), model=ShortModelId(short_id)
+ )
+
+
+def _guess_provider(model: str) -> AiProviderId:
+ def is_google(model: str) -> bool:
+ return model.startswith("google") or model.startswith("gemini")
+
+ def is_anthropic(model: str) -> bool:
+ return model.startswith("claude")
+
+ def is_openai(model: str) -> bool:
+ return (
+ model.startswith("gpt")
+ or model.startswith("o3")
+ or model.startswith("o1")
+ )
+
+ if is_google(model):
+ return AiProviderId("google")
+ elif is_anthropic(model):
+ return AiProviderId("anthropic")
+ elif is_openai(model):
+ return AiProviderId("openai")
+ else:
+ return AiProviderId("ollama")
diff --git a/marimo/_server/ai/providers.py b/marimo/_server/ai/providers.py
index 301093e871..7adf293acc 100644
--- a/marimo/_server/ai/providers.py
+++ b/marimo/_server/ai/providers.py
@@ -38,6 +38,7 @@ from marimo._config.config import (
MarimoConfig,
)
from marimo._dependencies.dependencies import DependencyManager
+from marimo._server.ai.ids import AiModelId
from marimo._server.api.status import HTTPStatus
if TYPE_CHECKING:
@@ -200,11 +201,12 @@ class AnyProviderConfig:
@staticmethod
def for_model(model: str, config: AiConfig) -> AnyProviderConfig:
- if _model_is_anthropic(model):
+ model_id = AiModelId.from_model(model)
+ if model_id.provider == "anthropic":
return AnyProviderConfig.for_anthropic(config)
- elif _model_is_google(model):
+ elif model_id.provider == "google":
return AnyProviderConfig.for_google(config)
- elif _model_is_bedrock(model):
+ elif model_id.provider == "bedrock":
return AnyProviderConfig.for_bedrock(config)
else:
# OpenAI has a default API that ollama also uses, that is
@@ -1031,29 +1033,19 @@ class BedrockProvider(
return None
-def _model_is_google(model: str) -> bool:
- return model.startswith("google") or model.startswith("gemini")
-
-
-def _model_is_anthropic(model: str) -> bool:
- return model.startswith("claude")
-
-
-def _model_is_bedrock(model: str) -> bool:
- return model.startswith("bedrock/")
-
-
def get_completion_provider(
config: AnyProviderConfig, model: str
) -> CompletionProvider[Any, Any]:
- if _model_is_anthropic(model):
- return AnthropicProvider(model, config)
- elif _model_is_google(model):
- return GoogleProvider(model, config)
- elif _model_is_bedrock(model):
- return BedrockProvider(model, config)
+ model_id = AiModelId.from_model(model)
+
+ if model_id.provider == "anthropic":
+ return AnthropicProvider(model_id.model, config)
+ elif model_id.provider == "google":
+ return GoogleProvider(model_id.model, config)
+ elif model_id.provider == "bedrock":
+ return BedrockProvider(model_id.model, config)
else:
- return OpenAIProvider(model, config)
+ return OpenAIProvider(model_id.model, config)
def get_model(config: AiConfig) -> str:
diff --git a/tests/_server/ai/test_ai_ids.py b/tests/_server/ai/test_ai_ids.py
new file mode 100644
index 0000000000..b11434093b
--- /dev/null
+++ b/tests/_server/ai/test_ai_ids.py
@@ -0,0 +1,175 @@
+from unittest.mock import Mock, patch
+
+from marimo._server.ai.ids import (
+ AiModelId,
+ AiProviderId,
+ QualifiedModelId,
+ ShortModelId,
+ _guess_provider,
+)
+
+
+class TestAiModelId:
+ def test_init(self):
+ """Test AiModelId initialization."""
+ model_id = AiModelId(
+ provider=AiProviderId("openai"), model=ShortModelId("gpt-4")
+ )
+ assert model_id.provider == "openai"
+ assert model_id.model == "gpt-4"
+
+ def test_str_conversion(self):
+ """Test string conversion returns qualified model ID."""
+ model_id = AiModelId(
+ provider=AiProviderId("anthropic"), model=ShortModelId("claude-3")
+ )
+ result = str(model_id)
+ assert result == "anthropic/claude-3"
+ assert isinstance(result, str)
+
+ def test_repr(self):
+ """Test repr returns proper representation."""
+ model_id = AiModelId(
+ provider=AiProviderId("google"), model=ShortModelId("gemini-pro")
+ )
+ result = repr(model_id)
+ assert result == "AiModelId(provider=google, model=gemini-pro)"
+
+ def test_from_model_valid_format(self):
+ """Test from_model with valid provider/model format."""
+ model_id = AiModelId.from_model("openai/gpt-4")
+ assert model_id.provider == "openai"
+ assert model_id.model == "gpt-4"
+
+ def test_from_model_complex_model_name(self):
+ """Test from_model with complex model names containing slashes."""
+ model_id = AiModelId.from_model(
+ "huggingface/microsoft/DialoGPT-medium"
+ )
+ assert model_id.provider == "huggingface"
+ assert model_id.model == "microsoft/DialoGPT-medium"
+
+ @patch("marimo._server.ai.ids.LOGGER")
+ def test_from_model_invalid_format_openai(self, mock_logger: Mock):
+ """Test from_model with invalid format - should guess OpenAI."""
+ model_id = AiModelId.from_model("gpt-4")
+
+ assert model_id.provider == "openai"
+ assert model_id.model == "gpt-4"
+
+ # Verify warning was logged
+ mock_logger.warning.assert_any_call(
+ "Invalid model ID: gpt-4. Model ID must be in the format <provider>/<model>"
+ )
+ mock_logger.warning.assert_any_call(
+ "Guessing provider for gpt-4 as openai"
+ )
+
+ @patch("marimo._server.ai.ids.LOGGER")
+ def test_from_model_invalid_format_anthropic(self, mock_logger: Mock):
+ """Test from_model with invalid format - should guess Anthropic."""
+ model_id = AiModelId.from_model("claude-3-opus")
+
+ assert model_id.provider == "anthropic"
+ assert model_id.model == "claude-3-opus"
+
+ mock_logger.warning.assert_any_call(
+ "Invalid model ID: claude-3-opus. Model ID must be in the format <provider>/<model>"
+ )
+
+ @patch("marimo._server.ai.ids.LOGGER")
+ def test_from_model_invalid_format_google(self, mock_logger: Mock):
+ del mock_logger
+ """Test from_model with invalid format - should guess Google."""
+ model_id = AiModelId.from_model("gemini-pro")
+
+ assert model_id.provider == "google"
+ assert model_id.model == "gemini-pro"
+
+ @patch("marimo._server.ai.ids.LOGGER")
+ def test_from_model_invalid_format_ollama_fallback(
+ self, mock_logger: Mock
+ ):
+ del mock_logger
+ """Test from_model with invalid format - should fallback to Ollama."""
+ model_id = AiModelId.from_model("llama2")
+
+ assert model_id.provider == "ollama"
+ assert model_id.model == "llama2"
+
+
+class TestGuessProvider:
+ def test_guess_openai_gpt(self):
+ """Test guessing OpenAI provider for GPT models."""
+ assert _guess_provider("gpt-4") == "openai"
+ assert _guess_provider("gpt-3.5-turbo") == "openai"
+ assert _guess_provider("gpt-4o") == "openai"
+
+ def test_guess_openai_o3(self):
+ """Test guessing OpenAI provider for O3 models."""
+ assert _guess_provider("o3-mini") == "openai"
+ assert _guess_provider("o3-max") == "openai"
+
+ def test_guess_openai_o1(self):
+ """Test guessing OpenAI provider for O1 models."""
+ assert _guess_provider("o1-preview") == "openai"
+ assert _guess_provider("o1-mini") == "openai"
+
+ def test_guess_anthropic_claude(self):
+ """Test guessing Anthropic provider for Claude models."""
+ assert _guess_provider("claude-3-opus") == "anthropic"
+ assert _guess_provider("claude-3-sonnet") == "anthropic"
+ assert _guess_provider("claude-3-haiku") == "anthropic"
+ assert _guess_provider("claude-2") == "anthropic"
+
+ def test_guess_google_gemini(self):
+ """Test guessing Google provider for Gemini models."""
+ assert _guess_provider("gemini-pro") == "google"
+ assert _guess_provider("gemini-1.5-pro") == "google"
+ assert _guess_provider("gemini-flash") == "google"
+
+ def test_guess_google_google_prefix(self):
+ """Test guessing Google provider for models with google prefix."""
+ assert _guess_provider("google-palm") == "google"
+ assert _guess_provider("google-bard") == "google"
+
+ def test_guess_ollama_fallback(self):
+ """Test fallback to Ollama for unknown models."""
+ assert _guess_provider("llama2") == "ollama"
+ assert _guess_provider("mistral") == "ollama"
+ assert _guess_provider("codellama") == "ollama"
+ assert _guess_provider("unknown-model") == "ollama"
+
+ def test_guess_provider_edge_cases(self):
+ """Test edge cases for provider guessing."""
+ # Empty string
+ assert _guess_provider("") == "ollama"
+
+ # Models that might be ambiguous
+ assert _guess_provider("gpt") == "openai" # Starts with gpt
+ assert _guess_provider("claude") == "anthropic" # Starts with claude
+ assert _guess_provider("gemini") == "google" # Starts with gemini
+
+ # Case sensitivity (should work as expected)
+ assert (
+ _guess_provider("GPT-4") == "ollama"
+ ) # Doesn't start with lowercase "gpt"
+ assert (
+ _guess_provider("Claude-3") == "ollama"
+ ) # Doesn't start with lowercase "claude"
+
+
+class TestTypeAliases:
+ def test_type_aliases_are_strings(self):
+ """Test that type aliases behave as strings."""
+ provider_id = AiProviderId("test-provider")
+ qualified_id = QualifiedModelId("test/model")
+ short_id = ShortModelId("model")
+
+ assert isinstance(provider_id, str)
+ assert isinstance(qualified_id, str)
+ assert isinstance(short_id, str)
+
+ assert provider_id == "test-provider"
+ assert qualified_id == "test/model"
+ assert short_id == "model"