-
Notifications
You must be signed in to change notification settings - Fork 353
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prefix all models with provider for consistency (#593)
Co-authored-by: Samuel Colvin <[email protected]>
- Loading branch information
1 parent
34ec8a6
commit 7920cb9
Showing
11 changed files
with
110 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,84 @@ | ||
from importlib import import_module | ||
|
||
import pytest | ||
|
||
from pydantic_ai import UserError | ||
from pydantic_ai.models import infer_model | ||
from pydantic_ai.models.gemini import GeminiModel | ||
|
||
from ..conftest import TestEnv, try_import | ||
from ..conftest import TestEnv | ||
|
||
TEST_CASES = [ | ||
('OPENAI_API_KEY', 'openai:gpt-3.5-turbo', 'openai:gpt-3.5-turbo', 'openai', 'OpenAIModel'), | ||
('OPENAI_API_KEY', 'gpt-3.5-turbo', 'openai:gpt-3.5-turbo', 'openai', 'OpenAIModel'), | ||
('OPENAI_API_KEY', 'o1', 'openai:o1', 'openai', 'OpenAIModel'), | ||
('GEMINI_API_KEY', 'google-gla:gemini-1.5-flash', 'google-gla:gemini-1.5-flash', 'gemini', 'GeminiModel'), | ||
('GEMINI_API_KEY', 'gemini-1.5-flash', 'google-gla:gemini-1.5-flash', 'gemini', 'GeminiModel'), | ||
( | ||
'GEMINI_API_KEY', | ||
'google-vertex:gemini-1.5-flash', | ||
'google-vertex:gemini-1.5-flash', | ||
'vertexai', | ||
'VertexAIModel', | ||
), | ||
( | ||
'GEMINI_API_KEY', | ||
'vertexai:gemini-1.5-flash', | ||
'google-vertex:gemini-1.5-flash', | ||
'vertexai', | ||
'VertexAIModel', | ||
), | ||
( | ||
'ANTHROPIC_API_KEY', | ||
'anthropic:claude-3-5-haiku-latest', | ||
'anthropic:claude-3-5-haiku-latest', | ||
'anthropic', | ||
'AnthropicModel', | ||
), | ||
( | ||
'ANTHROPIC_API_KEY', | ||
'claude-3-5-haiku-latest', | ||
'anthropic:claude-3-5-haiku-latest', | ||
'anthropic', | ||
'AnthropicModel', | ||
), | ||
( | ||
'GROQ_API_KEY', | ||
'groq:llama-3.3-70b-versatile', | ||
'groq:llama-3.3-70b-versatile', | ||
'groq', | ||
'GroqModel', | ||
), | ||
('OLLAMA_API_KEY', 'ollama:llama3', 'ollama:llama3', 'ollama', 'OllamaModel'), | ||
( | ||
'MISTRAL_API_KEY', | ||
'mistral:mistral-small-latest', | ||
'mistral:mistral-small-latest', | ||
'mistral', | ||
'MistralModel', | ||
), | ||
] | ||
|
||
with try_import() as openai_imports_successful: | ||
from pydantic_ai.models.openai import OpenAIModel | ||
|
||
with try_import() as vertexai_imports_successful: | ||
from pydantic_ai.models.vertexai import VertexAIModel | ||
@pytest.mark.parametrize('mock_api_key, model_name, expected_model_name, module_name, model_class_name', TEST_CASES) | ||
def test_infer_model( | ||
env: TestEnv, mock_api_key: str, model_name: str, expected_model_name: str, module_name: str, model_class_name: str | ||
): | ||
try: | ||
model_module = import_module(f'pydantic_ai.models.{module_name}') | ||
expected_model = getattr(model_module, model_class_name) | ||
except ImportError: | ||
pytest.skip(f'{model_name} dependencies not installed') | ||
|
||
env.set(mock_api_key, 'via-env-var') | ||
|
||
@pytest.mark.skipif(not openai_imports_successful(), reason='openai not installed') | ||
def test_infer_str_openai(env: TestEnv): | ||
env.set('OPENAI_API_KEY', 'via-env-var') | ||
m = infer_model('openai:gpt-3.5-turbo') | ||
assert isinstance(m, OpenAIModel) | ||
assert m.name() == 'openai:gpt-3.5-turbo' | ||
m = infer_model(model_name) # pyright: ignore[reportArgumentType] | ||
assert isinstance(m, expected_model) | ||
assert m.name() == expected_model_name | ||
|
||
m2 = infer_model(m) | ||
assert m2 is m | ||
|
||
|
||
def test_infer_str_gemini(env: TestEnv): | ||
env.set('GEMINI_API_KEY', 'via-env-var') | ||
m = infer_model('gemini-1.5-flash') | ||
assert isinstance(m, GeminiModel) | ||
assert m.name() == 'gemini-1.5-flash' | ||
|
||
|
||
@pytest.mark.skipif(not vertexai_imports_successful(), reason='google-auth not installed') | ||
def test_infer_vertexai(env: TestEnv): | ||
m = infer_model('vertexai:gemini-1.5-flash') | ||
assert isinstance(m, VertexAIModel) | ||
assert m.name() == 'vertexai:gemini-1.5-flash' | ||
|
||
|
||
def test_infer_str_unknown(): | ||
with pytest.raises(UserError, match='Unknown model: foobar'): | ||
infer_model('foobar') # pyright: ignore[reportArgumentType] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters