mistral 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.env.example +3 -0
- data/.rubocop.yml +60 -0
- data/.tool-versions +1 -0
- data/CHANGELOG.md +12 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/LICENSE.txt +21 -0
- data/PYTHON_CLIENT_COMPARISON.md +184 -0
- data/README.md +145 -0
- data/Rakefile +12 -0
- data/examples/chat_no_streaming.rb +18 -0
- data/examples/chat_with_streaming.rb +18 -0
- data/examples/chatbot_with_streaming.rb +289 -0
- data/examples/embeddings.rb +16 -0
- data/examples/function_calling.rb +104 -0
- data/examples/json_format.rb +21 -0
- data/examples/list_models.rb +13 -0
- data/lib/http/features/line_iterable_body.rb +35 -0
- data/lib/mistral/client.rb +229 -0
- data/lib/mistral/client_base.rb +126 -0
- data/lib/mistral/constants.rb +6 -0
- data/lib/mistral/exceptions.rb +38 -0
- data/lib/mistral/models/chat_completion.rb +95 -0
- data/lib/mistral/models/common.rb +11 -0
- data/lib/mistral/models/embeddings.rb +21 -0
- data/lib/mistral/models/models.rb +39 -0
- data/lib/mistral/version.rb +5 -0
- data/lib/mistral.rb +24 -0
- metadata +172 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 01bc1534430ff8a86dafca215d434c999337163910c2b67c5a7198bbc4ade7c3
|
4
|
+
data.tar.gz: d97eda8d0b53118de9998dca31041ec14a1e357b7052a6083d4f172408192ebf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 46f9cb65643ff7b805a2fe94181d7d8af3e27afc42ae001d9a6a6a4ef660ae79461508c793734cf46bcf14e470ec2f2e21c480e12d567f2e0c224c6cd3639f62
|
7
|
+
data.tar.gz: a68462d1daf574b786d325ece5f5519d9b3bada3081b1a150eb088e6bdb036b6f455a46a5e516ed6b4f6a79b294e400c21240c0a210181279c5eab16575756e9
|
data/.env.example
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 3.3
|
3
|
+
NewCops: disable
|
4
|
+
SuggestExtensions: false
|
5
|
+
|
6
|
+
Style/StringLiterals:
|
7
|
+
EnforcedStyle: single_quotes
|
8
|
+
|
9
|
+
Style/StringLiteralsInInterpolation:
|
10
|
+
EnforcedStyle: double_quotes
|
11
|
+
|
12
|
+
Layout/ArgumentAlignment:
|
13
|
+
EnforcedStyle: with_fixed_indentation
|
14
|
+
|
15
|
+
Layout/MultilineMethodCallBraceLayout:
|
16
|
+
EnforcedStyle: new_line
|
17
|
+
|
18
|
+
# All cops below exist to maximize compatibility with the Python client source code
|
19
|
+
|
20
|
+
Lint/UnreachableLoop:
|
21
|
+
Enabled: false
|
22
|
+
|
23
|
+
Metrics/CyclomaticComplexity:
|
24
|
+
Enabled: false
|
25
|
+
|
26
|
+
Metrics/AbcSize:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
Metrics/MethodLength:
|
30
|
+
Enabled: false
|
31
|
+
|
32
|
+
Style/Documentation:
|
33
|
+
Enabled: false
|
34
|
+
|
35
|
+
Style/HashSyntax:
|
36
|
+
Enabled: false
|
37
|
+
|
38
|
+
Metrics/ParameterLists:
|
39
|
+
Enabled: false
|
40
|
+
|
41
|
+
Metrics/PerceivedComplexity:
|
42
|
+
Enabled: false
|
43
|
+
|
44
|
+
Metrics/ClassLength:
|
45
|
+
Enabled: false
|
46
|
+
|
47
|
+
Metrics/BlockLength:
|
48
|
+
Enabled: false
|
49
|
+
|
50
|
+
Naming/PredicateName:
|
51
|
+
Enabled: false
|
52
|
+
|
53
|
+
Style/GlobalVars:
|
54
|
+
Exclude:
|
55
|
+
- examples/chatbot_with_streaming.rb
|
56
|
+
|
57
|
+
Style/StringConcatenation:
|
58
|
+
Exclude:
|
59
|
+
- test/test_chat.rb
|
60
|
+
- test/test_helper.rb
|
data/.tool-versions
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby 3.3.0
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.1.1/)
|
6
|
+
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
## [0.1.0] - 2024-05-04
|
9
|
+
|
10
|
+
- Initial release. Feature parity with `v0.1.8` of the [client-python](https://github.com/mistralai/client-python)
|
11
|
+
|
12
|
+
[0.1.0]: https://github.com/wilsonsilva/nostr/compare/28e7c9...v0.1.0
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at wilson.dsigns@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Wilson Silva
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
@@ -0,0 +1,184 @@
|
|
1
|
+
# Comparison with [client-python](https://github.com/mistralai/client-python)
|
2
|
+
|
3
|
+
This Ruby gem aims to maintain 1:1 parity with the Python client. There are just a few differences to make the API
|
4
|
+
more idiomatic to Ruby.
|
5
|
+
|
6
|
+
## Development standards
|
7
|
+
|
8
|
+
In the interest of efficiency and maintainability, this gem __prioritizes parity__ with the Python client over
|
9
|
+
__adherence to my typical development standards__ such as 100% documentation, 100% test coverage, and strict
|
10
|
+
linting. This approach simplifies the process of backporting new features from the Python package in the future.
|
11
|
+
|
12
|
+
## Public API
|
13
|
+
|
14
|
+
The public API remains the same as the Python client. For example, these two examples are equivalent:
|
15
|
+
|
16
|
+
```python
|
17
|
+
import os
|
18
|
+
|
19
|
+
from mistralai.client import MistralClient
|
20
|
+
from mistralai.models.chat_completion import ChatMessage
|
21
|
+
|
22
|
+
|
23
|
+
def main():
|
24
|
+
api_key = os.environ["MISTRAL_API_KEY"]
|
25
|
+
model = "mistral-tiny"
|
26
|
+
|
27
|
+
client = MistralClient(api_key=api_key)
|
28
|
+
|
29
|
+
chat_response = client.chat(
|
30
|
+
model=model,
|
31
|
+
messages=[
|
32
|
+
ChatMessage(role="user", content="What is the best French cheese?")
|
33
|
+
],
|
34
|
+
)
|
35
|
+
print(chat_response.choices[0].message.content)
|
36
|
+
|
37
|
+
|
38
|
+
if __name__ == "__main__":
|
39
|
+
main()
|
40
|
+
```
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
require 'mistral'
|
44
|
+
|
45
|
+
api_key = ENV.fetch('MISTRAL_API_KEY')
|
46
|
+
model = 'mistral-tiny'
|
47
|
+
|
48
|
+
client = Mistral::Client.new(api_key: api_key)
|
49
|
+
|
50
|
+
chat_response = client.chat(
|
51
|
+
model: model,
|
52
|
+
messages: [
|
53
|
+
Mistral::ChatMessage.new(role: 'user', content: 'What is the best French cheese?')
|
54
|
+
]
|
55
|
+
)
|
56
|
+
|
57
|
+
puts chat_response.choices[0].message.content
|
58
|
+
```
|
59
|
+
|
60
|
+
However, since Ruby doesn't have native `async` support, all `async` methods and examples are not implemented.
|
61
|
+
|
62
|
+
### Directory structure
|
63
|
+
|
64
|
+
Excluding the async files, they are roughly the same. The main differences are the presence of `__init__.py` files in
|
65
|
+
the Python package, additional files like `http/features/line_iterable_body.rb` in the Ruby gem and `utils.py`
|
66
|
+
(equivalent to `test_helper.rb`) in the Python package.
|
67
|
+
|
68
|
+
```shell
|
69
|
+
Python package Ruby gem
|
70
|
+
├── examples ├── examples
|
71
|
+
│ ├── chat_no_streaming.py │ ├── chat_no_streaming.rb
|
72
|
+
│ ├── chat_with_streaming.py │ ├── chat_with_streaming.rb
|
73
|
+
│ ├── chatbot_with_streaming.py │ ├── chatbot_with_streaming.rb
|
74
|
+
│ ├── embeddings.py │ ├── embeddings.rb
|
75
|
+
│ ├── function_calling.py │ ├── function_calling.rb
|
76
|
+
│ ├── json_format.py │ ├── json_format.rb
|
77
|
+
│ └── list_models.py │ └── list_models.rb
|
78
|
+
├── src ├── lib
|
79
|
+
│ └── mistralai │ ├── http
|
80
|
+
│ ├── __init__.py │ │ └── features
|
81
|
+
│ ├── client.py │ │ └── line_iterable_body.rb
|
82
|
+
│ ├── client_base.py │ ├── mistral
|
83
|
+
│ ├── constants.py │ │ ├── client.rb
|
84
|
+
│ ├── exceptions.py │ │ ├── client_base.rb
|
85
|
+
│ └── models │ │ ├── constants.rb
|
86
|
+
│ ├── __init__.py │ │ ├── exceptions.rb
|
87
|
+
│ ├── chat_completion.py │ │ ├── models
|
88
|
+
│ ├── common.py │ │ │ ├── chat_completion.rb
|
89
|
+
│ ├── embeddings.py │ │ │ ├── common.rb
|
90
|
+
│ └── models.py │ │ │ ├── embeddings.rb
|
91
|
+
└── tests │ │ │ └── models.rb
|
92
|
+
├── __init__.py │ │ └── version.rb
|
93
|
+
├── test_chat.py │ └── mistral.rb
|
94
|
+
├── test_embedder.py └── test
|
95
|
+
├── test_list_models.py ├── test_chat.rb
|
96
|
+
└── utils.py ├── test_embedder.rb
|
97
|
+
├── test_helper.rb
|
98
|
+
└── test_list_models.rb
|
99
|
+
```
|
100
|
+
|
101
|
+
### Errors and exceptions
|
102
|
+
|
103
|
+
Ruby lacks a native way to import and namespace constants like Python. To address this, exceptions are namespaced,
|
104
|
+
and the top-level exception is named `Error` as per Ruby gem standard practice:
|
105
|
+
|
106
|
+
| Python | Ruby |
|
107
|
+
|------------------------------|----------------------------|
|
108
|
+
| `MistralException` | `Mistral::Error` |
|
109
|
+
| `MistralAPIException` | `Mistral::APIError` |
|
110
|
+
| `MistralAPIStatusException` | `Mistral::APIStatusError` |
|
111
|
+
| `MistralConnectionException` | `Mistral::ConnectionError` |
|
112
|
+
|
113
|
+
## Private API
|
114
|
+
|
115
|
+
Unlike Python, where private methods start with an underscore (`_`), Ruby follows the convention of not having a
|
116
|
+
specific naming pattern for private methods.
|
117
|
+
|
118
|
+
```python
|
119
|
+
def _process_line(self, line: str) -> Optional[Dict[str, Any]]:
|
120
|
+
# Implementation
|
121
|
+
|
122
|
+
def _make_chat_request:
|
123
|
+
# Implementation
|
124
|
+
```
|
125
|
+
|
126
|
+
```ruby
|
127
|
+
def process_line(line)
|
128
|
+
end
|
129
|
+
|
130
|
+
def make_chat_request(line)
|
131
|
+
end
|
132
|
+
```
|
133
|
+
|
134
|
+
## Static typing
|
135
|
+
|
136
|
+
Python's `Pydantic` package for data validation and manipulation is implemented using `dry-types` and `dry-struct`
|
137
|
+
in Ruby. `Pydantic`'s `Model`s are implemented as `Dry::Struct`s, and `model_dump` is replaced with `to_h` for
|
138
|
+
converting structs to hashes.
|
139
|
+
|
140
|
+
Ruby's `RBS` type system was initially explored but not fully implemented due to time constraints. It remains
|
141
|
+
available in a [separate branch](https://github.com/wilsonsilva/mistral/tree/rbs-types).
|
142
|
+
|
143
|
+
## Client version
|
144
|
+
|
145
|
+
Unlike Python, where the version is set in the `Client` class, Ruby follows the convention of defining the version
|
146
|
+
as a `VERSION` constant in the gem's top-level module.
|
147
|
+
|
148
|
+
## HTTP Client
|
149
|
+
|
150
|
+
The Python version uses the package [httpx](https://www.python-httpx.org/) to send HTTP requests. In Ruby, the
|
151
|
+
gem [http](https://github.com/httprb/http) (also called `http.rb`) is used, which is similar. One difference
|
152
|
+
is that while `httpx`'s responses let you easily iterate over each line of the body, `http.rb` doesn't have this
|
153
|
+
functionality built-in.
|
154
|
+
|
155
|
+
To mimic `httpx`'s `iter_lines` behavior, I implemented a plugin (known as a `Feature` in `http.rb`):
|
156
|
+
|
157
|
+
```python
|
158
|
+
for line in response.iter_lines():
|
159
|
+
json_streamed_response = self._process_line(line)
|
160
|
+
if json_streamed_response:
|
161
|
+
yield json_streamed_response
|
162
|
+
```
|
163
|
+
|
164
|
+
```ruby
|
165
|
+
response.body.each_line do |line|
|
166
|
+
processed_line = process_line(line)
|
167
|
+
next if processed_line.nil?
|
168
|
+
|
169
|
+
yielder << processed_line
|
170
|
+
end
|
171
|
+
```
|
172
|
+
|
173
|
+
This code resides in `lib/http/features/line_iterable_body.rb`.
|
174
|
+
|
175
|
+
## Testing
|
176
|
+
|
177
|
+
The Ruby gem aims for 1:1 parity with the Python client. As such, it uses `Minitest` (similar to Python's `pytest`).
|
178
|
+
However, testing was simplified by using and `webmock` for stubbing requests, instead of implementing 100% test
|
179
|
+
coverage and using RSpec, which is usually what I do.
|
180
|
+
|
181
|
+
## Examples
|
182
|
+
|
183
|
+
The `function_calling.rb` example omits the unnecessary `n_rows = data['transaction_id'].length` line present in
|
184
|
+
the Python version.
|
data/README.md
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
# Mistral Ruby Client
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/mistral.svg)](https://badge.fury.io/rb/mistral)
|
4
|
+
![Build](https://github.com/wilsonsilva/mistral/actions/workflows/main.yml/badge.svg)
|
5
|
+
|
6
|
+
Mistral is a Ruby gem to interact with the [Mistral AI API](https://www.mistral.ai).
|
7
|
+
|
8
|
+
This client is a 1:1 port of Mistral's [client-python](https://github.com/mistralai/client-python).
|
9
|
+
For a detailed comparison between the Ruby and Python clients, please refer to the
|
10
|
+
[PYTHON_CLIENT_COMPARISON.md](https://github.com/wilsonsilva/mistral/blob/main/PYTHON_CLIENT_COMPARISON.md) file.
|
11
|
+
|
12
|
+
## 🔑 Key features
|
13
|
+
|
14
|
+
- API parity with the official [Python client](https://github.com/mistralai/client-python)
|
15
|
+
- Full support for all Mistral AI functionalities, including chat completions, embeddings, and function calling
|
16
|
+
- Asynchronous streaming of responses
|
17
|
+
- Comprehensive error handling and retry mechanisms
|
18
|
+
- Configurable client options (e.g., API endpoint, timeout, max retries)
|
19
|
+
- Fully leverages `dry-struct` for type safety and avoids primitive obsession with hashes
|
20
|
+
|
21
|
+
## 📦 Installation
|
22
|
+
|
23
|
+
Install the gem and add to the application's Gemfile by executing:
|
24
|
+
|
25
|
+
```
|
26
|
+
$ bundle add mistral
|
27
|
+
```
|
28
|
+
|
29
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
30
|
+
|
31
|
+
```
|
32
|
+
$ gem install mistral
|
33
|
+
```
|
34
|
+
|
35
|
+
## ⚡️ Quickstart
|
36
|
+
|
37
|
+
Here are a few examples of how to use the Mistral gem:
|
38
|
+
|
39
|
+
### Chat completion
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
require 'mistral'
|
43
|
+
|
44
|
+
api_key = ENV.fetch('MISTRAL_API_KEY')
|
45
|
+
client = Mistral::Client.new(api_key: api_key)
|
46
|
+
|
47
|
+
model = 'mistral-small'
|
48
|
+
|
49
|
+
chat_response = client.chat(
|
50
|
+
model: model,
|
51
|
+
messages: [
|
52
|
+
Mistral::ChatMessage.new(role: 'user', content: 'What is the best French cheese?')
|
53
|
+
]
|
54
|
+
)
|
55
|
+
|
56
|
+
puts chat_response.choices[0].message.content
|
57
|
+
```
|
58
|
+
|
59
|
+
### Chat completion with streaming
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
require 'mistral'
|
63
|
+
|
64
|
+
api_key = ENV.fetch('MISTRAL_API_KEY')
|
65
|
+
client = Mistral::Client.new(api_key: api_key)
|
66
|
+
|
67
|
+
model = 'mistral-small'
|
68
|
+
|
69
|
+
client.chat_stream(
|
70
|
+
model: model,
|
71
|
+
messages: [
|
72
|
+
Mistral::ChatMessage.new(role: 'user', content: 'What is the best French cheese?')
|
73
|
+
]
|
74
|
+
).each do |chunk|
|
75
|
+
print chunk.choices[0].delta.content if chunk.choices[0].delta.content
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
## 📚 Documentation
|
80
|
+
|
81
|
+
In the [`examples`](https://github.com/wilsonsilva/mistral/tree/main/examples) folder, you will find how to do:
|
82
|
+
|
83
|
+
| File Name | Description |
|
84
|
+
|--------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------|
|
85
|
+
| [`chat_no_streaming.rb`](https://github.com/wilsonsilva/mistral/blob/main/examples/chat_no_streaming.rb) | How to use the chat endpoint without streaming |
|
86
|
+
| [`chat_with_streaming.rb`](https://github.com/wilsonsilva/mistral/blob/main/examples/chat_with_streaming.rb) | How to use the chat endpoint with streaming |
|
87
|
+
| [`chatbot_with_streaming.rb`](https://github.com/wilsonsilva/mistral/blob/main/examples/chatbot_with_streaming.rb) | A simple interactive chatbot using streaming |
|
88
|
+
| [`embeddings.rb`](https://github.com/wilsonsilva/mistral/blob/main/examples/embeddings.rb) | How to use the embeddings endpoint |
|
89
|
+
| [`function_calling.rb`](https://github.com/wilsonsilva/mistral/blob/main/examples/function_calling.rb) | How to call functions using the chat endpoint |
|
90
|
+
| [`json_format.rb`](https://github.com/wilsonsilva/mistral/blob/main/examples/json_format.rb) | How to request and parse JSON responses from the chat endpoint |
|
91
|
+
| [`list_models.rb`](https://github.com/wilsonsilva/mistral/blob/main/examples/list_models.rb) | How to list available models |
|
92
|
+
|
93
|
+
## 🔨 Development
|
94
|
+
|
95
|
+
After checking out the repo, run `bin/setup` to install dependencies.
|
96
|
+
|
97
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
98
|
+
|
99
|
+
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
100
|
+
|
101
|
+
To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`,
|
102
|
+
which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file
|
103
|
+
to [rubygems.org](https://rubygems.org).
|
104
|
+
|
105
|
+
The health and maintainability of the codebase is ensured through a set of Rake tasks to test and lint the gem:
|
106
|
+
|
107
|
+
```
|
108
|
+
rake build # Build mistral into the pkg directory
|
109
|
+
rake build:checksum # Generate SHA512 checksum if mistral.gem into the checksums directory
|
110
|
+
rake clean # Remove any temporary products
|
111
|
+
rake clobber # Remove any generated files
|
112
|
+
rake install # Build and install mistral.gem into system gems
|
113
|
+
rake install:local # Build and install mistral.gem into system gems without network access
|
114
|
+
rake release[remote] # Create tag v0.1.0 and build and push mistral.gem to https://rubygems.org
|
115
|
+
rake rubocop # Run RuboCop
|
116
|
+
rake rubocop:autocorrect # Autocorrect RuboCop offenses (only when it's safe)
|
117
|
+
rake rubocop:autocorrect_all # Autocorrect RuboCop offenses (safe and unsafe)
|
118
|
+
rake test # Run the test suite
|
119
|
+
rake test:cmd # Print out the test command
|
120
|
+
rake test:isolated # Show which test files fail when run in isolation
|
121
|
+
rake test:slow # Show bottom 25 tests wrt time
|
122
|
+
```
|
123
|
+
|
124
|
+
## 🐞 Issues & Bugs
|
125
|
+
|
126
|
+
If you find any issues or bugs, please report them [here](https://github.com/wilsonsilva/mistral/issues), I will be happy
|
127
|
+
to have a look at them and fix them as soon as possible.
|
128
|
+
|
129
|
+
Please let me know if the [client-python](https://github.com/mistralai/client-python) introduces any new features,
|
130
|
+
so I can keep this gem in sync with the latest updates.
|
131
|
+
|
132
|
+
## 🤝 Contributing
|
133
|
+
|
134
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/wilsonsilva/mistral.
|
135
|
+
This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere
|
136
|
+
to the [code of conduct](https://github.com/wilsonsilva/mistral/blob/main/CODE_OF_CONDUCT.md).
|
137
|
+
|
138
|
+
## 📜 License
|
139
|
+
|
140
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
141
|
+
|
142
|
+
## 👔 Code of Conduct
|
143
|
+
|
144
|
+
Everyone interacting in the mistral project's codebases, issue trackers, chat rooms and mailing lists is expected
|
145
|
+
to follow the [code of conduct](https://github.com/wilsonsilva/mistral/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'dotenv/load'
|
6
|
+
require 'mistral'
|
7
|
+
|
8
|
+
api_key = ENV.fetch('MISTRAL_API_KEY')
|
9
|
+
client = Mistral::Client.new(api_key: api_key)
|
10
|
+
|
11
|
+
model = 'mistral-tiny'
|
12
|
+
|
13
|
+
chat_response = client.chat(
|
14
|
+
model: model,
|
15
|
+
messages: [Mistral::ChatMessage.new(role: 'user', content: 'What is the best French cheese?')]
|
16
|
+
)
|
17
|
+
|
18
|
+
puts chat_response.choices[0].message.content
|
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'dotenv/load'
|
6
|
+
require 'mistral'
|
7
|
+
|
8
|
+
api_key = ENV.fetch('MISTRAL_API_KEY')
|
9
|
+
client = Mistral::Client.new(api_key: api_key)
|
10
|
+
|
11
|
+
model = 'mistral-tiny'
|
12
|
+
|
13
|
+
client.chat_stream(
|
14
|
+
model: model,
|
15
|
+
messages: [Mistral::ChatMessage.new(role: 'user', content: 'What is the best French cheese?')]
|
16
|
+
).each do |chunk|
|
17
|
+
print chunk.choices[0].delta.content if chunk.choices[0].delta.content
|
18
|
+
end
|