mistral_common 1.0.0__tar.gz
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.
Potentially problematic release.
This version of mistral_common might be problematic. Click here for more details.
- mistral_common-1.0.0/PKG-INFO +110 -0
- mistral_common-1.0.0/README.md +91 -0
- mistral_common-1.0.0/pyproject.toml +58 -0
- mistral_common-1.0.0/src/mistral_common/__init__.py +0 -0
- mistral_common-1.0.0/src/mistral_common/base.py +9 -0
- mistral_common-1.0.0/src/mistral_common/data/mistral_instruct_tokenizer_240216.model.v2 +0 -0
- mistral_common-1.0.0/src/mistral_common/data/mistral_instruct_tokenizer_240216.model.v3 +0 -0
- mistral_common-1.0.0/src/mistral_common/data/tokenizer.model.v1 +0 -0
- mistral_common-1.0.0/src/mistral_common/exceptions.py +67 -0
- mistral_common-1.0.0/src/mistral_common/protocol/__init__.py +0 -0
- mistral_common-1.0.0/src/mistral_common/protocol/base.py +9 -0
- mistral_common-1.0.0/src/mistral_common/protocol/embedding/request.py +10 -0
- mistral_common-1.0.0/src/mistral_common/protocol/embedding/response.py +20 -0
- mistral_common-1.0.0/src/mistral_common/protocol/instruct/__init__.py +0 -0
- mistral_common-1.0.0/src/mistral_common/protocol/instruct/messages.py +56 -0
- mistral_common-1.0.0/src/mistral_common/protocol/instruct/request.py +29 -0
- mistral_common-1.0.0/src/mistral_common/protocol/instruct/response.py +66 -0
- mistral_common-1.0.0/src/mistral_common/protocol/instruct/tool_calls.py +36 -0
- mistral_common-1.0.0/src/mistral_common/protocol/instruct/validator.py +282 -0
- mistral_common-1.0.0/src/mistral_common/protocol/utils.py +5 -0
- mistral_common-1.0.0/src/mistral_common/py.typed +0 -0
- mistral_common-1.0.0/src/mistral_common/tokens/__init__.py +0 -0
- mistral_common-1.0.0/src/mistral_common/tokens/instruct/normalize.py +141 -0
- mistral_common-1.0.0/src/mistral_common/tokens/instruct/request.py +15 -0
- mistral_common-1.0.0/src/mistral_common/tokens/tokenizers/base.py +61 -0
- mistral_common-1.0.0/src/mistral_common/tokens/tokenizers/mistral.py +117 -0
- mistral_common-1.0.0/src/mistral_common/tokens/tokenizers/sentencepiece.py +334 -0
- mistral_common-1.0.0/src/mistral_common/tokens/tokenizers/utils.py +6 -0
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: mistral_common
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary:
|
|
5
|
+
Author: bam4d
|
|
6
|
+
Author-email: bam4d@mistral.ai
|
|
7
|
+
Requires-Python: >=3.8.10,<4.0.0
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Requires-Dist: jsonschema (==4.21.1)
|
|
14
|
+
Requires-Dist: pydantic (==2.6.1)
|
|
15
|
+
Requires-Dist: sentencepiece (==0.1.99)
|
|
16
|
+
Requires-Dist: typing-extensions (>=4.11.0,<5.0.0)
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
|
|
19
|
+
# Mistral Common
|
|
20
|
+
|
|
21
|
+
## What is it?
|
|
22
|
+
mistral-common is a set of tools to help you work with Mistral models.
|
|
23
|
+
|
|
24
|
+
Our first release contains tokenization. Our tokenizers go beyond the usual text <-> tokens, adding parsing of tools and structured conversation. We also release the validation and normalization code that is used in our API.
|
|
25
|
+
|
|
26
|
+
We are releasing three versions of our tokenizer powering different sets of models.
|
|
27
|
+
|
|
28
|
+
- v1: open-mistral-7b, open-mixtral-8x7b, mistral-embed
|
|
29
|
+
- v2: mistral-small-latest, mistral-large-latest
|
|
30
|
+
- v3: open-mixtral-8x22b
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
|
|
34
|
+
### pip
|
|
35
|
+
You can install `mistral-common` via pip:
|
|
36
|
+
```
|
|
37
|
+
pip install mistral-common
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### From Source
|
|
41
|
+
Alternatively, you can install from source directly. This repo uses poetry as a dependency and virtual environment manager.
|
|
42
|
+
|
|
43
|
+
You can install poetry with
|
|
44
|
+
```
|
|
45
|
+
pip install poetry
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
poetry will set up a virtual environment and install dependencies with the following command:
|
|
49
|
+
```
|
|
50
|
+
poetry install
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Examples
|
|
54
|
+
<a target="_blank" href="https://colab.research.google.com/github/mistralai/mistral-common/blob/main/examples/tokenizer.ipynb">
|
|
55
|
+
<img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/>
|
|
56
|
+
</a>
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
```py
|
|
61
|
+
# Import needed packages:
|
|
62
|
+
from mistral_common.protocol.instruct.messages import UserMessage
|
|
63
|
+
from mistral_common.protocol.instruct.request import ChatCompletionRequest
|
|
64
|
+
from mistral_common.protocol.instruct.tool_calls import Function, Tool
|
|
65
|
+
from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
|
|
66
|
+
|
|
67
|
+
# Load Mistral tokenizer
|
|
68
|
+
|
|
69
|
+
model_name = "open-mixtral-8x22B"
|
|
70
|
+
|
|
71
|
+
tokenizer = MistralTokenizer.from_model(model_name)
|
|
72
|
+
|
|
73
|
+
# Tokenize a list of messages
|
|
74
|
+
tokenized = tokenizer.encode_chat_completion(
|
|
75
|
+
ChatCompletionRequest(
|
|
76
|
+
tools=[
|
|
77
|
+
Tool(
|
|
78
|
+
function=Function(
|
|
79
|
+
name="get_current_weather",
|
|
80
|
+
description="Get the current weather",
|
|
81
|
+
parameters={
|
|
82
|
+
"type": "object",
|
|
83
|
+
"properties": {
|
|
84
|
+
"location": {
|
|
85
|
+
"type": "string",
|
|
86
|
+
"description": "The city and state, e.g. San Francisco, CA",
|
|
87
|
+
},
|
|
88
|
+
"format": {
|
|
89
|
+
"type": "string",
|
|
90
|
+
"enum": ["celsius", "fahrenheit"],
|
|
91
|
+
"description": "The temperature unit to use. Infer this from the users location.",
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
"required": ["location", "format"],
|
|
95
|
+
},
|
|
96
|
+
)
|
|
97
|
+
)
|
|
98
|
+
],
|
|
99
|
+
messages=[
|
|
100
|
+
UserMessage(content="What's the weather like today in Paris"),
|
|
101
|
+
],
|
|
102
|
+
model=model_name,
|
|
103
|
+
)
|
|
104
|
+
)
|
|
105
|
+
tokens, text = tokenized.tokens, tokenized.text
|
|
106
|
+
|
|
107
|
+
# Count the number of tokens
|
|
108
|
+
print(len(tokens))
|
|
109
|
+
```
|
|
110
|
+
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# Mistral Common
|
|
2
|
+
|
|
3
|
+
## What is it?
|
|
4
|
+
mistral-common is a set of tools to help you work with Mistral models.
|
|
5
|
+
|
|
6
|
+
Our first release contains tokenization. Our tokenizers go beyond the usual text <-> tokens, adding parsing of tools and structured conversation. We also release the validation and normalization code that is used in our API.
|
|
7
|
+
|
|
8
|
+
We are releasing three versions of our tokenizer powering different sets of models.
|
|
9
|
+
|
|
10
|
+
- v1: open-mistral-7b, open-mixtral-8x7b, mistral-embed
|
|
11
|
+
- v2: mistral-small-latest, mistral-large-latest
|
|
12
|
+
- v3: open-mixtral-8x22b
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
### pip
|
|
17
|
+
You can install `mistral-common` via pip:
|
|
18
|
+
```
|
|
19
|
+
pip install mistral-common
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### From Source
|
|
23
|
+
Alternatively, you can install from source directly. This repo uses poetry as a dependency and virtual environment manager.
|
|
24
|
+
|
|
25
|
+
You can install poetry with
|
|
26
|
+
```
|
|
27
|
+
pip install poetry
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
poetry will set up a virtual environment and install dependencies with the following command:
|
|
31
|
+
```
|
|
32
|
+
poetry install
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Examples
|
|
36
|
+
<a target="_blank" href="https://colab.research.google.com/github/mistralai/mistral-common/blob/main/examples/tokenizer.ipynb">
|
|
37
|
+
<img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/>
|
|
38
|
+
</a>
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
```py
|
|
43
|
+
# Import needed packages:
|
|
44
|
+
from mistral_common.protocol.instruct.messages import UserMessage
|
|
45
|
+
from mistral_common.protocol.instruct.request import ChatCompletionRequest
|
|
46
|
+
from mistral_common.protocol.instruct.tool_calls import Function, Tool
|
|
47
|
+
from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
|
|
48
|
+
|
|
49
|
+
# Load Mistral tokenizer
|
|
50
|
+
|
|
51
|
+
model_name = "open-mixtral-8x22B"
|
|
52
|
+
|
|
53
|
+
tokenizer = MistralTokenizer.from_model(model_name)
|
|
54
|
+
|
|
55
|
+
# Tokenize a list of messages
|
|
56
|
+
tokenized = tokenizer.encode_chat_completion(
|
|
57
|
+
ChatCompletionRequest(
|
|
58
|
+
tools=[
|
|
59
|
+
Tool(
|
|
60
|
+
function=Function(
|
|
61
|
+
name="get_current_weather",
|
|
62
|
+
description="Get the current weather",
|
|
63
|
+
parameters={
|
|
64
|
+
"type": "object",
|
|
65
|
+
"properties": {
|
|
66
|
+
"location": {
|
|
67
|
+
"type": "string",
|
|
68
|
+
"description": "The city and state, e.g. San Francisco, CA",
|
|
69
|
+
},
|
|
70
|
+
"format": {
|
|
71
|
+
"type": "string",
|
|
72
|
+
"enum": ["celsius", "fahrenheit"],
|
|
73
|
+
"description": "The temperature unit to use. Infer this from the users location.",
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
"required": ["location", "format"],
|
|
77
|
+
},
|
|
78
|
+
)
|
|
79
|
+
)
|
|
80
|
+
],
|
|
81
|
+
messages=[
|
|
82
|
+
UserMessage(content="What's the weather like today in Paris"),
|
|
83
|
+
],
|
|
84
|
+
model=model_name,
|
|
85
|
+
)
|
|
86
|
+
)
|
|
87
|
+
tokens, text = tokenized.tokens, tokenized.text
|
|
88
|
+
|
|
89
|
+
# Count the number of tokens
|
|
90
|
+
print(len(tokens))
|
|
91
|
+
```
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "mistral_common"
|
|
3
|
+
version = "v1.0.0"
|
|
4
|
+
description = ""
|
|
5
|
+
authors = ["bam4d <bam4d@mistral.ai>"]
|
|
6
|
+
readme = "README.md"
|
|
7
|
+
packages = [{ include = "mistral_common", from = "src" }]
|
|
8
|
+
|
|
9
|
+
[tool.ruff]
|
|
10
|
+
lint.select = ["E", "F", "W", "Q", "I"]
|
|
11
|
+
lint.ignore = ["E203"]
|
|
12
|
+
lint.fixable = ["ALL"]
|
|
13
|
+
lint.unfixable = []
|
|
14
|
+
line-length = 120
|
|
15
|
+
exclude = ["docs", "build"]
|
|
16
|
+
|
|
17
|
+
[tool.mypy]
|
|
18
|
+
disallow_untyped_defs = true
|
|
19
|
+
show_error_codes = true
|
|
20
|
+
no_implicit_optional = true
|
|
21
|
+
warn_return_any = true
|
|
22
|
+
warn_unused_ignores = true
|
|
23
|
+
exclude = ["docs", "tools", "build"]
|
|
24
|
+
|
|
25
|
+
[[tool.mypy.overrides]]
|
|
26
|
+
module = "sentencepiece.*"
|
|
27
|
+
ignore_missing_imports = true
|
|
28
|
+
|
|
29
|
+
[tool.poetry.dependencies]
|
|
30
|
+
python = "^3.8.10"
|
|
31
|
+
pydantic = "2.6.1"
|
|
32
|
+
jsonschema = "4.21.1"
|
|
33
|
+
sentencepiece = "0.1.99"
|
|
34
|
+
typing-extensions = "^4.11.0"
|
|
35
|
+
|
|
36
|
+
[tool.poetry.group.dev.dependencies]
|
|
37
|
+
types-jsonschema = "4.21.0.20240118"
|
|
38
|
+
types-protobuf = "4.24.0.20240129"
|
|
39
|
+
mypy-protobuf = "^3.5.0"
|
|
40
|
+
pytest = "7.4.4"
|
|
41
|
+
ruff = "^0.2.2"
|
|
42
|
+
mypy = "^1.8.0"
|
|
43
|
+
pytest-cov = "^4.1.0"
|
|
44
|
+
diff-cover = "^8.0.3"
|
|
45
|
+
coverage = {extras = ["toml"], version = "^7.4.4"}
|
|
46
|
+
|
|
47
|
+
[build-system]
|
|
48
|
+
requires = ["poetry-core"]
|
|
49
|
+
build-backend = "poetry.core.masonry.api"
|
|
50
|
+
|
|
51
|
+
[tool.pytest.ini_options]
|
|
52
|
+
testpaths = ["./tests"]
|
|
53
|
+
|
|
54
|
+
[tool.coverage.run]
|
|
55
|
+
omit = ["tests", "*src/mistral_common/data*"]
|
|
56
|
+
|
|
57
|
+
[tool.coverage.report]
|
|
58
|
+
skip_covered = true
|
|
File without changes
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class MistralCommonException(Exception):
|
|
5
|
+
message: str = "Internal server error"
|
|
6
|
+
|
|
7
|
+
def __init__(
|
|
8
|
+
self,
|
|
9
|
+
message: Optional[str] = None,
|
|
10
|
+
) -> None:
|
|
11
|
+
if message:
|
|
12
|
+
self.message = message
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class TokenizerException(MistralCommonException):
|
|
16
|
+
def __init__(self, message: str) -> None:
|
|
17
|
+
super().__init__(message)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class UnsupportedTokenizerFeatureException(MistralCommonException):
|
|
21
|
+
def __init__(self, message: str) -> None:
|
|
22
|
+
super().__init__(message)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class InvalidRequestException(MistralCommonException):
|
|
26
|
+
def __init__(self, message: str) -> None:
|
|
27
|
+
super().__init__(message)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class InvalidSystemPromptException(MistralCommonException):
|
|
31
|
+
def __init__(self, message: str) -> None:
|
|
32
|
+
super().__init__(message)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class InvalidMessageStructureException(MistralCommonException):
|
|
36
|
+
def __init__(self, message: str) -> None:
|
|
37
|
+
super().__init__(message)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class InvalidAssistantMessageException(MistralCommonException):
|
|
41
|
+
def __init__(self, message: str) -> None:
|
|
42
|
+
super().__init__(message)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class InvalidToolMessageException(MistralCommonException):
|
|
46
|
+
def __init__(self, message: str) -> None:
|
|
47
|
+
super().__init__(message)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class InvalidToolSchemaException(MistralCommonException):
|
|
51
|
+
def __init__(self, message: str) -> None:
|
|
52
|
+
super().__init__(message)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class InvalidUserMessageException(MistralCommonException):
|
|
56
|
+
def __init__(self, message: str) -> None:
|
|
57
|
+
super().__init__(message)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class InvalidFunctionCallException(MistralCommonException):
|
|
61
|
+
def __init__(self, message: str) -> None:
|
|
62
|
+
super().__init__(message)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class InvalidToolException(MistralCommonException):
|
|
66
|
+
def __init__(self, message: str) -> None:
|
|
67
|
+
super().__init__(message)
|
|
File without changes
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
from typing import List, Optional, Union
|
|
2
|
+
|
|
3
|
+
from mistral_common.base import MistralBase
|
|
4
|
+
from pydantic import Field
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class EmbeddingRequest(MistralBase):
|
|
8
|
+
input: Union[str, List[str]] = Field(description="Text to embed.")
|
|
9
|
+
model: str = Field(description="ID of the model to use.")
|
|
10
|
+
encoding_format: Optional[str] = Field(default="float", description="The format to return the embeddings in.")
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from typing import List
|
|
2
|
+
|
|
3
|
+
from mistral_common.base import MistralBase
|
|
4
|
+
from mistral_common.protocol.base import UsageInfo
|
|
5
|
+
from mistral_common.protocol.utils import random_uuid
|
|
6
|
+
from pydantic import Field
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class EmbeddingObject(MistralBase):
|
|
10
|
+
object: str = Field(default="embedding", description="The type of the object returned.")
|
|
11
|
+
embedding: List[float] = Field(description="The type of the object returned.")
|
|
12
|
+
index: int = Field(description="The index of the embedding in the input text.")
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class EmbeddingResponse(MistralBase):
|
|
16
|
+
id: str = Field(default_factory=lambda: f"embd-{random_uuid()}")
|
|
17
|
+
object: str = Field(default="list", description="The type of the object returned.")
|
|
18
|
+
data: List[EmbeddingObject] = Field(description="List of embeddings.")
|
|
19
|
+
model: str = Field(description="The model used to generate the embeddings.")
|
|
20
|
+
usage: UsageInfo
|
|
File without changes
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
from enum import Enum
|
|
2
|
+
from typing import List, Literal, Optional, Union
|
|
3
|
+
|
|
4
|
+
from pydantic import Field
|
|
5
|
+
from typing_extensions import Annotated # compatibility with 3.8
|
|
6
|
+
|
|
7
|
+
from mistral_common.base import MistralBase
|
|
8
|
+
from mistral_common.protocol.instruct.tool_calls import ToolCall
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Roles(str, Enum):
|
|
12
|
+
system = "system"
|
|
13
|
+
user = "user"
|
|
14
|
+
assistant = "assistant"
|
|
15
|
+
tool = "tool"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class ChunkTypes(str, Enum):
|
|
19
|
+
text = "text"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class ContentChunk(MistralBase):
|
|
23
|
+
type: ChunkTypes = ChunkTypes.text
|
|
24
|
+
text: str
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class BaseMessage(MistralBase):
|
|
28
|
+
role: Literal[Roles.system, Roles.user, Roles.assistant, Roles.tool]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class UserMessage(BaseMessage):
|
|
32
|
+
role: Literal[Roles.user] = Roles.user
|
|
33
|
+
content: Union[str, List[ContentChunk]]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class SystemMessage(BaseMessage):
|
|
37
|
+
role: Literal[Roles.system] = Roles.system
|
|
38
|
+
content: Union[str, List[ContentChunk]]
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class AssistantMessage(BaseMessage):
|
|
42
|
+
role: Literal[Roles.assistant] = Roles.assistant
|
|
43
|
+
content: Optional[str] = None
|
|
44
|
+
tool_calls: Optional[List[ToolCall]] = None
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class ToolMessage(BaseMessage):
|
|
48
|
+
content: str
|
|
49
|
+
role: Literal[Roles.tool] = Roles.tool
|
|
50
|
+
tool_call_id: Optional[str] = None
|
|
51
|
+
|
|
52
|
+
# Deprecated in V3 tokenization
|
|
53
|
+
name: Optional[str] = None
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
ChatMessage = Annotated[Union[SystemMessage, UserMessage, AssistantMessage, ToolMessage], Field(discriminator="role")]
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from enum import Enum
|
|
2
|
+
from typing import List, Optional
|
|
3
|
+
|
|
4
|
+
from pydantic import Field
|
|
5
|
+
|
|
6
|
+
from mistral_common.base import MistralBase
|
|
7
|
+
from mistral_common.protocol.instruct.messages import ChatMessage
|
|
8
|
+
from mistral_common.protocol.instruct.tool_calls import Tool, ToolChoice
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class ResponseFormats(str, Enum):
|
|
12
|
+
text: str = "text"
|
|
13
|
+
json: str = "json_object"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ResponseFormat(MistralBase):
|
|
17
|
+
type: ResponseFormats = ResponseFormats.text
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class ChatCompletionRequest(MistralBase):
|
|
21
|
+
model: Optional[str] = None
|
|
22
|
+
messages: List[ChatMessage]
|
|
23
|
+
response_format: ResponseFormat = Field(default_factory=ResponseFormat)
|
|
24
|
+
tools: List[Tool] = Field(default_factory=list)
|
|
25
|
+
tool_choice: ToolChoice = ToolChoice.auto
|
|
26
|
+
temperature: float = Field(default=0.7, ge=0.0, le=1.0)
|
|
27
|
+
top_p: float = Field(default=1.0, ge=0.0, le=1.0)
|
|
28
|
+
max_tokens: Optional[int] = Field(default=None, ge=0)
|
|
29
|
+
random_seed: Optional[int] = Field(default=None, ge=0)
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import time
|
|
2
|
+
from enum import Enum
|
|
3
|
+
from typing import List, Optional
|
|
4
|
+
|
|
5
|
+
from pydantic import Field
|
|
6
|
+
|
|
7
|
+
from mistral_common.base import MistralBase
|
|
8
|
+
from mistral_common.protocol.base import UsageInfo
|
|
9
|
+
from mistral_common.protocol.instruct.tool_calls import ToolCall
|
|
10
|
+
from mistral_common.protocol.utils import random_uuid
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class FinishReason(str, Enum):
|
|
14
|
+
stop: str = "stop"
|
|
15
|
+
length: str = "length"
|
|
16
|
+
model_length: str = "model_length"
|
|
17
|
+
error: str = "error"
|
|
18
|
+
tool_call: str = "tool_calls"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class ChatCompletionTokenLogprobs(MistralBase):
|
|
22
|
+
token: str
|
|
23
|
+
logprob: float
|
|
24
|
+
bytes: List[int]
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class ChatCompletionResponseChoiceLogprobs(MistralBase):
|
|
28
|
+
content: List[ChatCompletionTokenLogprobs]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class DeltaMessage(MistralBase):
|
|
32
|
+
role: Optional[str] = None
|
|
33
|
+
content: Optional[str] = None
|
|
34
|
+
tool_calls: Optional[List[ToolCall]] = None
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class ChatCompletionResponseChoice(MistralBase):
|
|
38
|
+
index: int
|
|
39
|
+
message: DeltaMessage
|
|
40
|
+
finish_reason: Optional[FinishReason] = None
|
|
41
|
+
logprobs: Optional[ChatCompletionResponseChoiceLogprobs] = None
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class ChatCompletionResponse(MistralBase):
|
|
45
|
+
id: str = Field(default_factory=lambda: f"chatcmpl-{random_uuid()}")
|
|
46
|
+
object: str = "chat.completion"
|
|
47
|
+
created: int = Field(default_factory=lambda: int(time.time()))
|
|
48
|
+
model: str
|
|
49
|
+
choices: List[ChatCompletionResponseChoice]
|
|
50
|
+
usage: UsageInfo
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class ChatCompletionResponseStreamChoice(MistralBase):
|
|
54
|
+
index: int
|
|
55
|
+
delta: DeltaMessage
|
|
56
|
+
finish_reason: Optional[FinishReason] = None
|
|
57
|
+
logprobs: Optional[ChatCompletionResponseChoiceLogprobs] = None
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class ChatCompletionStreamResponse(MistralBase):
|
|
61
|
+
id: str = Field(default_factory=lambda: f"chatcmpl-{random_uuid()}")
|
|
62
|
+
object: str = "chat.completion.chunk"
|
|
63
|
+
created: int = Field(default_factory=lambda: int(time.time()))
|
|
64
|
+
model: str
|
|
65
|
+
choices: List[ChatCompletionResponseStreamChoice]
|
|
66
|
+
usage: Optional[UsageInfo] = None
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
from enum import Enum
|
|
2
|
+
from typing import Any, Dict
|
|
3
|
+
|
|
4
|
+
from mistral_common.base import MistralBase
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Function(MistralBase):
|
|
8
|
+
name: str
|
|
9
|
+
description: str = ""
|
|
10
|
+
parameters: Dict[str, Any]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class ToolType(str, Enum):
|
|
14
|
+
function = "function"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class ToolChoice(str, Enum):
|
|
18
|
+
auto: str = "auto"
|
|
19
|
+
none: str = "none"
|
|
20
|
+
any: str = "any"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class Tool(MistralBase):
|
|
24
|
+
type: ToolType = ToolType.function
|
|
25
|
+
function: Function
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class FunctionCall(MistralBase):
|
|
29
|
+
name: str
|
|
30
|
+
arguments: str
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class ToolCall(MistralBase):
|
|
34
|
+
id: str = "null" # required for V3 tokenization
|
|
35
|
+
type: ToolType = ToolType.function
|
|
36
|
+
function: FunctionCall
|