-
Notifications
You must be signed in to change notification settings - Fork 99
feat(llm): MirascopeProvider #1802
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
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| """Mirascope Router provider for routing LLM requests.""" | ||
|
|
||
| from .provider import MirascopeProvider | ||
|
|
||
| __all__ = ["MirascopeProvider"] |
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 |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| """Utility functions for Mirascope Router provider.""" | ||
|
|
||
| import os | ||
| from typing import cast | ||
|
|
||
| from ..base import Provider | ||
| from ..provider_id import ProviderId | ||
|
|
||
|
|
||
| def extract_provider_prefix(model_id: str) -> str | None: | ||
| """Extract provider prefix from model ID. | ||
|
|
||
| Args: | ||
| model_id: Model identifier in the format "provider/model-name" | ||
| e.g., "openai/gpt-4", "anthropic/claude-3", "google/gemini-pro" | ||
|
|
||
| Returns: | ||
| The provider prefix (e.g., "openai", "anthropic", "google") or None if invalid format. | ||
| """ | ||
| if "/" not in model_id: | ||
| return None | ||
| return model_id.split("/", 1)[0] | ||
|
|
||
|
|
||
| def get_default_router_base_url() -> str: | ||
| """Get the default router base URL from environment or use default. | ||
|
|
||
| Returns: | ||
| The router base URL (without trailing provider path). | ||
| """ | ||
| return os.environ.get( | ||
| "MIRASCOPE_ROUTER_BASE_URL", "https://mirascope.com/router/v0" | ||
| ) | ||
|
|
||
|
|
||
| def create_underlying_provider( | ||
| provider_prefix: str, api_key: str, router_base_url: str | ||
| ) -> Provider: | ||
| """Create and cache an underlying provider instance using provider_singleton. | ||
|
|
||
| This function constructs the appropriate router URL for the provider and | ||
| delegates to provider_singleton for caching and instantiation. | ||
|
|
||
| Args: | ||
| provider_prefix: The provider name (e.g., "openai", "anthropic", "google", | ||
| "openai:completions", "openai:responses") | ||
| api_key: The API key to use for authentication | ||
| router_base_url: The base router URL (e.g., "http://mirascope.com/router/v0") | ||
|
|
||
| Returns: | ||
| A cached provider instance configured for the Mirascope Router. | ||
|
|
||
| Raises: | ||
| ValueError: If the provider is unsupported. | ||
| """ | ||
| # Extract base provider name (handles variants like "openai:completions") | ||
| base_provider = provider_prefix.split(":")[0] | ||
|
|
||
| if base_provider not in ["anthropic", "google", "openai"]: | ||
| raise ValueError( | ||
| f"Unsupported provider: {provider_prefix}. " | ||
| f"Mirascope Router currently supports: anthropic, google, openai" | ||
| ) | ||
|
|
||
| base_url = f"{router_base_url}/{base_provider}" | ||
| if base_provider == "openai": # OpenAI expects /v1, which their SDK doesn't add | ||
| base_url = f"{base_url}/v1" | ||
|
|
||
| # Lazy import to avoid circular dependencies | ||
| from ..provider_registry import provider_singleton | ||
|
|
||
| # Use provider_singleton which provides caching | ||
| return provider_singleton( | ||
| cast(ProviderId, provider_prefix), | ||
| api_key=api_key, | ||
| base_url=base_url, | ||
| ) | ||
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.