-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Prefix all models with provider for consistency #593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1ae39da
prefix models with provider
sydney-runkle 87f3d9e
fix test
sydney-runkle 0e030dd
prefix with provider consistently
sydney-runkle 40a74c0
infer tests
sydney-runkle 6606339
parametrized test
sydney-runkle f377083
dynamic imports
sydney-runkle 59cabf7
docs updates from samuel's suggestion
sydney-runkle a63b832
Update docs/models.md
sydney-runkle 78db3db
Merge branch 'main' into anthropic-consistency
sydney-runkle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.