-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40f4d89
commit 3bb70a1
Showing
5 changed files
with
117 additions
and
5 deletions.
There are no files selected for viewing
Empty file.
45 changes: 45 additions & 0 deletions
45
elleelleaime/generate/strategies/models/mistral/mistral.py
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from elleelleaime.generate.strategies.strategy import PatchGenerationStrategy | ||
|
||
from dotenv import load_dotenv | ||
from typing import Any, List | ||
|
||
import os | ||
import mistralai | ||
import backoff | ||
|
||
|
||
class MistralModels(PatchGenerationStrategy): | ||
def __init__(self, model_name: str, **kwargs) -> None: | ||
self.model_name = model_name | ||
self.temperature = kwargs.get("temperature", 0.0) | ||
self.n_samples = kwargs.get("n_samples", 1) | ||
|
||
load_dotenv() | ||
self.client = mistralai.Mistral(os.getenv("MISTRAL_API_KEY", None)) | ||
|
||
@backoff.on_exception( | ||
backoff.expo, | ||
( | ||
mistralai.models.SDKError, | ||
mistralai.models.HTTPValidationError, | ||
AssertionError, | ||
), | ||
) | ||
def _completions_with_backoff(self, **kwargs): | ||
response = self.client.chat.complete(**kwargs) | ||
assert response is not None | ||
return response | ||
|
||
def _generate_impl(self, chunk: List[str]) -> Any: | ||
result = [] | ||
|
||
for prompt in chunk: | ||
completion = self._completions_with_backoff( | ||
model=self.model_name, | ||
messages=[{"role": "user", "content": prompt}], | ||
temperature=self.temperature, | ||
n=self.n_samples, | ||
) | ||
result.append(completion.model_dump()) | ||
|
||
return result |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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