agent-framework-mistral 1.0.0a260604__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- agent_framework_mistral/__init__.py +17 -0
- agent_framework_mistral/_embedding_client.py +250 -0
- agent_framework_mistral/py.typed +1 -0
- agent_framework_mistral-1.0.0a260604.dist-info/METADATA +68 -0
- agent_framework_mistral-1.0.0a260604.dist-info/RECORD +7 -0
- agent_framework_mistral-1.0.0a260604.dist-info/WHEEL +4 -0
- agent_framework_mistral-1.0.0a260604.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Copyright (c) Microsoft. All rights reserved.
|
|
2
|
+
|
|
3
|
+
import importlib.metadata
|
|
4
|
+
|
|
5
|
+
from ._embedding_client import MistralEmbeddingClient, MistralEmbeddingOptions, MistralEmbeddingSettings
|
|
6
|
+
|
|
7
|
+
try:
|
|
8
|
+
__version__ = importlib.metadata.version(__name__)
|
|
9
|
+
except importlib.metadata.PackageNotFoundError:
|
|
10
|
+
__version__ = "0.0.0" # Fallback for development mode
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
"MistralEmbeddingClient",
|
|
14
|
+
"MistralEmbeddingOptions",
|
|
15
|
+
"MistralEmbeddingSettings",
|
|
16
|
+
"__version__",
|
|
17
|
+
]
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
# Copyright (c) Microsoft. All rights reserved.
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
import sys
|
|
7
|
+
from collections.abc import Sequence
|
|
8
|
+
from typing import Any, ClassVar, Generic, TypedDict
|
|
9
|
+
|
|
10
|
+
from agent_framework import (
|
|
11
|
+
BaseEmbeddingClient,
|
|
12
|
+
Embedding,
|
|
13
|
+
EmbeddingGenerationOptions,
|
|
14
|
+
GeneratedEmbeddings,
|
|
15
|
+
UsageDetails,
|
|
16
|
+
load_settings,
|
|
17
|
+
)
|
|
18
|
+
from agent_framework._settings import SecretString
|
|
19
|
+
from agent_framework.observability import EmbeddingTelemetryLayer
|
|
20
|
+
from mistralai.client import Mistral
|
|
21
|
+
|
|
22
|
+
if sys.version_info >= (3, 13):
|
|
23
|
+
from typing import TypeVar # type: ignore # pragma: no cover
|
|
24
|
+
else:
|
|
25
|
+
from typing_extensions import TypeVar # type: ignore # pragma: no cover
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
logger = logging.getLogger("agent_framework.mistral")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class MistralEmbeddingOptions(EmbeddingGenerationOptions, total=False):
|
|
32
|
+
"""Mistral AI-specific embedding options.
|
|
33
|
+
|
|
34
|
+
Extends EmbeddingGenerationOptions with Mistral-specific fields.
|
|
35
|
+
|
|
36
|
+
Examples:
|
|
37
|
+
.. code-block:: python
|
|
38
|
+
|
|
39
|
+
from agent_framework_mistral import MistralEmbeddingOptions
|
|
40
|
+
|
|
41
|
+
options: MistralEmbeddingOptions = {
|
|
42
|
+
"model": "mistral-embed",
|
|
43
|
+
"dimensions": 1024,
|
|
44
|
+
}
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
MistralEmbeddingOptionsT = TypeVar(
|
|
49
|
+
"MistralEmbeddingOptionsT",
|
|
50
|
+
bound=TypedDict, # type: ignore[valid-type]
|
|
51
|
+
default="MistralEmbeddingOptions",
|
|
52
|
+
covariant=True,
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class MistralEmbeddingSettings(TypedDict, total=False):
|
|
57
|
+
"""Mistral AI embedding settings.
|
|
58
|
+
|
|
59
|
+
Fields:
|
|
60
|
+
api_key: Mistral API key. Resolved from ``MISTRAL_API_KEY``.
|
|
61
|
+
embedding_model: Embedding model name. Resolved from ``MISTRAL_EMBEDDING_MODEL``.
|
|
62
|
+
server_url: Optional server URL override. Resolved from ``MISTRAL_SERVER_URL``.
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
api_key: str | None
|
|
66
|
+
embedding_model: str | None
|
|
67
|
+
server_url: str | None
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class RawMistralEmbeddingClient(
|
|
71
|
+
BaseEmbeddingClient[str, list[float], MistralEmbeddingOptionsT],
|
|
72
|
+
Generic[MistralEmbeddingOptionsT],
|
|
73
|
+
):
|
|
74
|
+
"""Raw Mistral AI embedding client without telemetry.
|
|
75
|
+
|
|
76
|
+
Keyword Args:
|
|
77
|
+
model: The Mistral embedding model (e.g. "mistral-embed").
|
|
78
|
+
Can also be set via environment variable ``MISTRAL_EMBEDDING_MODEL``.
|
|
79
|
+
api_key: Mistral API key. Defaults to ``MISTRAL_API_KEY`` environment variable.
|
|
80
|
+
server_url: Optional server URL override. Defaults to ``MISTRAL_SERVER_URL``
|
|
81
|
+
environment variable, or the Mistral default.
|
|
82
|
+
client: Optional pre-configured ``Mistral`` client instance.
|
|
83
|
+
additional_properties: Additional properties stored on the client instance.
|
|
84
|
+
env_file_path: Path to ``.env`` file for settings.
|
|
85
|
+
env_file_encoding: Encoding for ``.env`` file.
|
|
86
|
+
"""
|
|
87
|
+
|
|
88
|
+
INJECTABLE: ClassVar[set[str]] = {"client"}
|
|
89
|
+
|
|
90
|
+
def __init__(
|
|
91
|
+
self,
|
|
92
|
+
*,
|
|
93
|
+
model: str | None = None,
|
|
94
|
+
api_key: str | SecretString | None = None,
|
|
95
|
+
server_url: str | None = None,
|
|
96
|
+
client: Mistral | None = None,
|
|
97
|
+
additional_properties: dict[str, Any] | None = None,
|
|
98
|
+
env_file_path: str | None = None,
|
|
99
|
+
env_file_encoding: str | None = None,
|
|
100
|
+
) -> None:
|
|
101
|
+
"""Initialize a raw Mistral AI embedding client."""
|
|
102
|
+
mistral_settings = load_settings(
|
|
103
|
+
MistralEmbeddingSettings,
|
|
104
|
+
env_prefix="MISTRAL_",
|
|
105
|
+
required_fields=["embedding_model", "api_key"],
|
|
106
|
+
api_key=str(api_key) if isinstance(api_key, SecretString) else api_key,
|
|
107
|
+
embedding_model=model,
|
|
108
|
+
server_url=server_url,
|
|
109
|
+
env_file_path=env_file_path,
|
|
110
|
+
env_file_encoding=env_file_encoding,
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
self.model: str = mistral_settings["embedding_model"] # type: ignore[assignment]
|
|
114
|
+
resolved_api_key: str = mistral_settings["api_key"] # type: ignore[assignment]
|
|
115
|
+
resolved_server_url = mistral_settings.get("server_url")
|
|
116
|
+
|
|
117
|
+
if client is not None:
|
|
118
|
+
self.client = client
|
|
119
|
+
else:
|
|
120
|
+
client_kwargs: dict[str, Any] = {"api_key": resolved_api_key}
|
|
121
|
+
if resolved_server_url:
|
|
122
|
+
client_kwargs["server_url"] = resolved_server_url
|
|
123
|
+
self.client = Mistral(**client_kwargs)
|
|
124
|
+
|
|
125
|
+
self.server_url = resolved_server_url
|
|
126
|
+
super().__init__(additional_properties=additional_properties)
|
|
127
|
+
|
|
128
|
+
def service_url(self) -> str:
|
|
129
|
+
"""Get the URL of the service."""
|
|
130
|
+
return self.server_url or "https://api.mistral.ai"
|
|
131
|
+
|
|
132
|
+
async def get_embeddings(
|
|
133
|
+
self,
|
|
134
|
+
values: Sequence[str],
|
|
135
|
+
*,
|
|
136
|
+
options: MistralEmbeddingOptionsT | None = None,
|
|
137
|
+
) -> GeneratedEmbeddings[list[float], MistralEmbeddingOptionsT]:
|
|
138
|
+
"""Call the Mistral AI embeddings API.
|
|
139
|
+
|
|
140
|
+
Args:
|
|
141
|
+
values: The text values to generate embeddings for.
|
|
142
|
+
options: Optional embedding generation options.
|
|
143
|
+
|
|
144
|
+
Returns:
|
|
145
|
+
Generated embeddings with usage metadata.
|
|
146
|
+
|
|
147
|
+
Raises:
|
|
148
|
+
ValueError: If model is not provided or values is empty.
|
|
149
|
+
"""
|
|
150
|
+
if not values:
|
|
151
|
+
return GeneratedEmbeddings([], options=options)
|
|
152
|
+
|
|
153
|
+
opts: dict[str, Any] = options or {} # type: ignore
|
|
154
|
+
model = opts.get("model") or self.model
|
|
155
|
+
if not model:
|
|
156
|
+
raise ValueError("model is required")
|
|
157
|
+
|
|
158
|
+
kwargs: dict[str, Any] = {"model": model, "inputs": list(values)}
|
|
159
|
+
if "dimensions" in opts:
|
|
160
|
+
kwargs["output_dimension"] = opts["dimensions"]
|
|
161
|
+
|
|
162
|
+
response = await self.client.embeddings.create_async(**kwargs)
|
|
163
|
+
|
|
164
|
+
embeddings: list[Embedding[list[float]]] = []
|
|
165
|
+
if response and response.data:
|
|
166
|
+
items = sorted(response.data, key=lambda d: d.index if d.index is not None else 0)
|
|
167
|
+
for item in items:
|
|
168
|
+
vector = list(item.embedding) if item.embedding else []
|
|
169
|
+
embeddings.append(
|
|
170
|
+
Embedding(
|
|
171
|
+
vector=vector,
|
|
172
|
+
dimensions=len(vector),
|
|
173
|
+
model=response.model or model,
|
|
174
|
+
)
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
usage_dict: UsageDetails | None = None
|
|
178
|
+
if response and response.usage:
|
|
179
|
+
usage_dict = {
|
|
180
|
+
"input_token_count": response.usage.prompt_tokens,
|
|
181
|
+
"total_token_count": response.usage.total_tokens,
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return GeneratedEmbeddings(embeddings, options=options, usage=usage_dict)
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
class MistralEmbeddingClient(
|
|
188
|
+
EmbeddingTelemetryLayer[str, list[float], MistralEmbeddingOptionsT],
|
|
189
|
+
RawMistralEmbeddingClient[MistralEmbeddingOptionsT],
|
|
190
|
+
Generic[MistralEmbeddingOptionsT],
|
|
191
|
+
):
|
|
192
|
+
"""Mistral AI embedding client with telemetry support.
|
|
193
|
+
|
|
194
|
+
Keyword Args:
|
|
195
|
+
model: The Mistral embedding model (e.g. "mistral-embed").
|
|
196
|
+
Can also be set via environment variable ``MISTRAL_EMBEDDING_MODEL``.
|
|
197
|
+
api_key: Mistral API key. Defaults to ``MISTRAL_API_KEY`` environment variable.
|
|
198
|
+
server_url: Optional server URL override. Defaults to ``MISTRAL_SERVER_URL``
|
|
199
|
+
environment variable, or the Mistral default.
|
|
200
|
+
client: Optional pre-configured ``Mistral`` client instance.
|
|
201
|
+
otel_provider_name: Optional telemetry provider name override.
|
|
202
|
+
env_file_path: Path to ``.env`` file for settings.
|
|
203
|
+
env_file_encoding: Encoding for ``.env`` file.
|
|
204
|
+
|
|
205
|
+
Examples:
|
|
206
|
+
.. code-block:: python
|
|
207
|
+
|
|
208
|
+
from agent_framework_mistral import MistralEmbeddingClient
|
|
209
|
+
|
|
210
|
+
# Using environment variables
|
|
211
|
+
# Set MISTRAL_API_KEY=your-key
|
|
212
|
+
# Set MISTRAL_EMBEDDING_MODEL=mistral-embed
|
|
213
|
+
client = MistralEmbeddingClient()
|
|
214
|
+
|
|
215
|
+
# Or passing parameters directly
|
|
216
|
+
client = MistralEmbeddingClient(
|
|
217
|
+
model="mistral-embed",
|
|
218
|
+
api_key="your-api-key",
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
# Generate embeddings
|
|
222
|
+
result = await client.get_embeddings(["Hello, world!"])
|
|
223
|
+
print(result[0].vector)
|
|
224
|
+
"""
|
|
225
|
+
|
|
226
|
+
OTEL_PROVIDER_NAME: ClassVar[str] = "mistralai"
|
|
227
|
+
|
|
228
|
+
def __init__(
|
|
229
|
+
self,
|
|
230
|
+
*,
|
|
231
|
+
model: str | None = None,
|
|
232
|
+
api_key: str | SecretString | None = None,
|
|
233
|
+
server_url: str | None = None,
|
|
234
|
+
client: Mistral | None = None,
|
|
235
|
+
otel_provider_name: str | None = None,
|
|
236
|
+
additional_properties: dict[str, Any] | None = None,
|
|
237
|
+
env_file_path: str | None = None,
|
|
238
|
+
env_file_encoding: str | None = None,
|
|
239
|
+
) -> None:
|
|
240
|
+
"""Initialize a Mistral AI embedding client."""
|
|
241
|
+
super().__init__(
|
|
242
|
+
model=model,
|
|
243
|
+
api_key=api_key,
|
|
244
|
+
server_url=server_url,
|
|
245
|
+
client=client,
|
|
246
|
+
additional_properties=additional_properties,
|
|
247
|
+
otel_provider_name=otel_provider_name,
|
|
248
|
+
env_file_path=env_file_path,
|
|
249
|
+
env_file_encoding=env_file_encoding,
|
|
250
|
+
)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: agent-framework-mistral
|
|
3
|
+
Version: 1.0.0a260604
|
|
4
|
+
Summary: Mistral AI integration for Microsoft Agent Framework.
|
|
5
|
+
Author: Microsoft
|
|
6
|
+
Author-email: Microsoft <af-support@microsoft.com>
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Framework :: Pydantic :: 2
|
|
17
|
+
Classifier: Typing :: Typed
|
|
18
|
+
Requires-Dist: agent-framework-core>=1.8.0,<2
|
|
19
|
+
Requires-Dist: mistralai>=2.0.0,<3
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Project-URL: homepage, https://learn.microsoft.com/en-us/agent-framework/
|
|
22
|
+
Project-URL: issues, https://github.com/microsoft/agent-framework/issues
|
|
23
|
+
Project-URL: release_notes, https://github.com/microsoft/agent-framework/releases?q=tag%3Apython-1&expanded=true
|
|
24
|
+
Project-URL: source, https://github.com/microsoft/agent-framework/tree/main/python
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# Get Started with Microsoft Agent Framework Mistral AI
|
|
28
|
+
|
|
29
|
+
Please install this package:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pip install agent-framework-mistral --pre
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
and see the [README](https://github.com/microsoft/agent-framework/tree/main/python/README.md) for more information.
|
|
36
|
+
|
|
37
|
+
## Embedding Client
|
|
38
|
+
|
|
39
|
+
The `MistralEmbeddingClient` provides embedding generation using Mistral AI models.
|
|
40
|
+
|
|
41
|
+
### Quick Start
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from agent_framework_mistral import MistralEmbeddingClient
|
|
45
|
+
|
|
46
|
+
# Using environment variables (MISTRAL_API_KEY, MISTRAL_EMBEDDING_MODEL)
|
|
47
|
+
client = MistralEmbeddingClient()
|
|
48
|
+
|
|
49
|
+
# Or passing parameters directly
|
|
50
|
+
client = MistralEmbeddingClient(
|
|
51
|
+
model="mistral-embed",
|
|
52
|
+
api_key="your-api-key",
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
# Generate embeddings
|
|
56
|
+
result = await client.get_embeddings(["Hello, world!", "How are you?"])
|
|
57
|
+
for embedding in result:
|
|
58
|
+
print(f"Dimensions: {embedding.dimensions}")
|
|
59
|
+
print(f"Vector: {embedding.vector[:5]}...")
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Configuration
|
|
63
|
+
|
|
64
|
+
| Environment Variable | Description |
|
|
65
|
+
|---|---|
|
|
66
|
+
| `MISTRAL_API_KEY` | Your Mistral AI API key |
|
|
67
|
+
| `MISTRAL_EMBEDDING_MODEL` | Embedding model name (e.g., `mistral-embed`) |
|
|
68
|
+
| `MISTRAL_SERVER_URL` | Optional server URL override |
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
agent_framework_mistral/__init__.py,sha256=o08_LKTNbJBP4wTbxELoEo2qWyg0AYmTIdU7VwfsrMc,476
|
|
2
|
+
agent_framework_mistral/_embedding_client.py,sha256=RQPN9Y7xxIBdbSC2xJKFpbDT8pOS6L_2v-xiGT7arQE,8826
|
|
3
|
+
agent_framework_mistral/py.typed,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
|
4
|
+
agent_framework_mistral-1.0.0a260604.dist-info/licenses/LICENSE,sha256=ws_MuBL-SCEBqPBFl9_FqZkaaydIJmxHrJG2parhU4M,1141
|
|
5
|
+
agent_framework_mistral-1.0.0a260604.dist-info/WHEEL,sha256=eh7sammvW2TypMMMGKgsM83HyA_3qQ5Lgg3ynoecH3M,79
|
|
6
|
+
agent_framework_mistral-1.0.0a260604.dist-info/METADATA,sha256=Zi23Bq8aW7SBnp8EabOvakwvJQywJGTBg7oj1ZvZN6w,2329
|
|
7
|
+
agent_framework_mistral-1.0.0a260604.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Microsoft Corporation.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE
|