-
Notifications
You must be signed in to change notification settings - Fork 992
Add Mistral provider #1118
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
Add Mistral provider #1118
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from __future__ import annotations as _annotations | ||
|
||
import os | ||
from typing import overload | ||
|
||
from httpx import AsyncClient as AsyncHTTPClient | ||
|
||
from pydantic_ai.models import cached_async_http_client | ||
|
||
try: | ||
from mistralai import Mistral | ||
except ImportError as e: # pragma: no cover | ||
raise ImportError( | ||
'Please install the `mistral` package to use the Mistral provider, ' | ||
"you can use the `mistral` optional group — `pip install 'pydantic-ai-slim[mistral]'`" | ||
) from e | ||
|
||
|
||
from . import Provider | ||
|
||
|
||
class MistralProvider(Provider[Mistral]): | ||
"""Provider for Mistral API.""" | ||
|
||
@property | ||
def name(self) -> str: | ||
return 'mistral' | ||
|
||
@property | ||
def base_url(self) -> str: | ||
return self.client.sdk_configuration.get_server_details()[0] | ||
|
||
@property | ||
def client(self) -> Mistral: | ||
return self._client | ||
|
||
@overload | ||
def __init__(self, *, mistral_client: Mistral | None = None) -> None: ... | ||
|
||
@overload | ||
def __init__(self, *, api_key: str | None = None, http_client: AsyncHTTPClient | None = None) -> None: ... | ||
|
||
def __init__( | ||
self, | ||
*, | ||
api_key: str | None = None, | ||
mistral_client: Mistral | None = None, | ||
http_client: AsyncHTTPClient | None = None, | ||
) -> None: | ||
"""Create a new Mistral provider. | ||
|
||
Args: | ||
api_key: The API key to use for authentication, if not provided, the `MISTRAL_API_KEY` environment variable | ||
will be used if available. | ||
mistral_client: An existing `Mistral` client to use, if provided, `api_key` and `http_client` must be `None`. | ||
http_client: An existing async client to use for making HTTP requests. | ||
""" | ||
api_key = api_key or os.environ.get('MISTRAL_API_KEY') | ||
|
||
if api_key is None and mistral_client is None: | ||
raise ValueError( | ||
'Set the `MISTRAL_API_KEY` environment variable or pass it via `MistralProvider(api_key=...)`' | ||
'to use the Mistral provider.' | ||
) | ||
|
||
if mistral_client is not None: | ||
assert http_client is None, 'Cannot provide both `mistral_client` and `http_client`' | ||
assert api_key is None, 'Cannot provide both `mistral_client` and `api_key`' | ||
self._client = mistral_client | ||
elif http_client is not None: | ||
self._client = Mistral(api_key=api_key, async_client=http_client) | ||
else: | ||
self._client = Mistral(api_key=api_key, async_client=cached_async_http_client()) |
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.