mistral_rb 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1570bd9c52e22b55ab811b990c3a28c69200ca088200754e1f745f27d5fd3edc
4
+ data.tar.gz: d6a2cae152c2e8c6adddde772638293bd4c17367f6ce63895791f5e1b5457d5f
5
+ SHA512:
6
+ metadata.gz: 8adfea6193cc0e1c45412e20887aeb977cca81540d3702ee2b6b310b769d545bcb7e4301a0250b34841009b98549ddec388baa96e3b8b9879b5a6da4e3dd7022
7
+ data.tar.gz: 97bd65012511c60905c8fbcc0bad080bef2bafef2755b99bc2a05e29f1b48a5861a07f919b9fd49189c575814893b77028292dca5cfdc5e1d17588fd86ac86f8
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-12-22
4
+
5
+ - Initial release
@@ -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 ndzomgafs@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) 2023 Franck Stephane Ndzomga
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.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # MistralRb
2
+
3
+ Welcome to MistralRb, a Ruby gem providing an easy-to-use interface for the Mistral AI API. This gem allows you to interact with the Mistral API to create chat completions, embeddings, and list available models in a Ruby-friendly way.
4
+
5
+ ## Installation
6
+
7
+ To install MistralRb, add it to your application's Gemfile:
8
+
9
+ ```bash
10
+ bundle add mistral_rb
11
+ ```
12
+
13
+ Or install it yourself as:
14
+ ```bash
15
+ gem install mistral_rb
16
+ ``````
17
+
18
+
19
+ ## Usage
20
+
21
+ Here is a quick example to get you started:
22
+
23
+ ```ruby
24
+ require 'mistral_rb'
25
+
26
+ # Initialize the API with your API key
27
+ mistral = MistralAPI.new("your_api_key_here")
28
+
29
+ # Create Chat Completion
30
+ response = mistral.create_chat_completion("mistral-tiny", [{role: "user", content: "Who is Macron?"}])
31
+ puts response.choices.to_s
32
+
33
+ # Create Embeddings
34
+ embedding_response = mistral.create_embeddings("mistral-embed", ["Hello", "world"])
35
+ puts embedding_response.data.first.embedding
36
+
37
+ # List Available Models
38
+ model_list_response = mistral.list_available_models
39
+ model_list_response.data.each do |model|
40
+ puts model.id
41
+ end
42
+ ```
43
+
44
+
45
+ ## Development
46
+
47
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
48
+
49
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
50
+
51
+ ## Contributing
52
+
53
+ Bug reports and pull requests are welcome on GitHub at https://github.com/fsndzomga/mistral_rb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/fsndzomga/mistral_rb/blob/master/CODE_OF_CONDUCT.md).
54
+
55
+ ## License
56
+
57
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
58
+
59
+ ## Code of Conduct
60
+
61
+ Everyone interacting in the MistralRb project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/fsndzomga/mistral_rb/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rubocop/rake_task"
5
+
6
+ RuboCop::RakeTask.new
7
+
8
+ # task default: :rubocop
9
+ task :default do
10
+ # Default task
11
+ end
@@ -0,0 +1,73 @@
1
+ class CompletionResponse
2
+ attr_reader :id, :object, :created, :model, :choices, :usage
3
+
4
+ def initialize(response_hash)
5
+ @id = response_hash["id"]
6
+ @object = response_hash["object"]
7
+ @created = response_hash["created"]
8
+ @model = response_hash["model"]
9
+ @choices = response_hash["choices"].map { |choice| Choice.new(choice) }
10
+ @usage = response_hash["usage"]
11
+ end
12
+ end
13
+
14
+ class Choice
15
+ attr_reader :index, :message
16
+
17
+ def initialize(choice_hash)
18
+ @index = choice_hash["index"]
19
+ @message = Message.new(choice_hash["message"])
20
+ end
21
+ end
22
+
23
+ class Message
24
+ attr_reader :role, :content
25
+
26
+ def initialize(message_hash)
27
+ @role = message_hash["role"]
28
+ @content = message_hash["content"]
29
+ end
30
+ end
31
+
32
+ class EmbeddingResponse
33
+ attr_reader :id, :object, :data, :model, :usage
34
+
35
+ def initialize(response_hash)
36
+ @id = response_hash["id"]
37
+ @object = response_hash["object"]
38
+ @data = response_hash["data"].map { |embedding_data| Embedding.new(embedding_data) }
39
+ @model = response_hash["model"]
40
+ @usage = response_hash["usage"]
41
+ end
42
+ end
43
+
44
+ class Embedding
45
+ attr_reader :object, :embedding, :index
46
+
47
+ def initialize(embedding_hash)
48
+ @object = embedding_hash["object"]
49
+ @embedding = embedding_hash["embedding"]
50
+ @index = embedding_hash["index"]
51
+ end
52
+ end
53
+
54
+ class ModelListResponse
55
+ attr_reader :object, :data
56
+
57
+ def initialize(response_hash)
58
+ @object = response_hash["object"]
59
+ @data = response_hash["data"].map { |model_data| Model.new(model_data) }
60
+ end
61
+ end
62
+
63
+ class Model
64
+ attr_reader :id, :object, :created, :owned_by, :permissions
65
+
66
+ def initialize(model_hash)
67
+ @id = model_hash["id"]
68
+ @object = model_hash["object"]
69
+ @created = model_hash["created"]
70
+ @owned_by = model_hash["owned_by"]
71
+ @permissions = model_hash["permission"] # This could be further parsed into Permission objects if detailed parsing is required
72
+ end
73
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MistralRb
4
+ VERSION = "0.1.0"
5
+ end
data/lib/mistral_rb.rb ADDED
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "mistral_rb/version"
4
+ require "httparty"
5
+ require "json"
6
+ require_relative "mistral_rb/response_models"
7
+
8
+ class MistralAPI
9
+ include HTTParty
10
+ base_uri "https://api.mistral.ai/v1"
11
+
12
+ def initialize(api_key)
13
+ @headers = {
14
+ "Authorization" => "Bearer #{api_key}",
15
+ "Content-Type" => "application/json"
16
+ }
17
+ end
18
+
19
+ def create_chat_completion(model, messages, temperature = 0.7, top_p = 1, max_tokens = nil, stream = false, safe_mode = false, random_seed = nil)
20
+ body = {
21
+ model: model,
22
+ messages: messages,
23
+ temperature: temperature,
24
+ top_p: top_p,
25
+ max_tokens: max_tokens,
26
+ stream: stream,
27
+ safe_mode: safe_mode,
28
+ random_seed: random_seed
29
+ }.compact.to_json # compact to remove nil values
30
+
31
+ response = self.class.post("/chat/completions", body: body, headers: @headers)
32
+ parsed_response = handle_response(response)
33
+ CompletionResponse.new(parsed_response)
34
+ end
35
+
36
+ def create_embeddings(model, input, encoding_format = "float")
37
+ body = {
38
+ model: model,
39
+ input: input,
40
+ encoding_format: encoding_format
41
+ }.to_json
42
+
43
+ response = self.class.post("/embeddings", body: body, headers: @headers)
44
+ parsed_response = handle_response(response)
45
+ EmbeddingResponse.new(parsed_response)
46
+ end
47
+
48
+ def list_available_models
49
+ response = self.class.get("/models", headers: @headers)
50
+ parsed_response = handle_response(response)
51
+ ModelListResponse.new(parsed_response)
52
+ end
53
+
54
+ private
55
+
56
+ def handle_response(response)
57
+ if response.code.between?(200, 299)
58
+ JSON.parse(response.body)
59
+ else
60
+ raise "API Error: #{response.code} - #{response.body}"
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/mistral_rb/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "mistral_rb"
7
+ spec.version = MistralRb::VERSION
8
+ spec.authors = ["Franck Stephane Ndzomga"]
9
+ spec.email = ["ndzomgafs@gmail.com"]
10
+
11
+ spec.summary = "A simple wrapper for the Mistral API"
12
+ spec.description = "This gem provides an easy-to-use interface for the Mistral AI API."
13
+ spec.homepage = "https://github.com/fsndzomga/mistral_rb"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/fsndzomga/mistral_rb"
19
+ spec.metadata["changelog_uri"] = "https://github.com/fsndzomga/mistral_rb/blob/main/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(__dir__) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (File.expand_path(f) == __FILE__) ||
26
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
27
+ end
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ # Specify runtime and development dependencies in gemspec
34
+ spec.add_runtime_dependency "httparty", "~> 0.18"
35
+ spec.add_development_dependency "bundler", "~> 2.0"
36
+ spec.add_development_dependency "rake", "~> 13.0"
37
+
38
+ # For more information and examples about making a new gem, check out our
39
+ # guide at: https://bundler.io/guides/creating_gem.html
40
+ end
@@ -0,0 +1,4 @@
1
+ module MistralRb
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mistral_rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Franck Stephane Ndzomga
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-12-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.18'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.18'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ description: This gem provides an easy-to-use interface for the Mistral AI API.
56
+ email:
57
+ - ndzomgafs@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".rubocop.yml"
63
+ - CHANGELOG.md
64
+ - CODE_OF_CONDUCT.md
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/mistral_rb.rb
69
+ - lib/mistral_rb/response_models.rb
70
+ - lib/mistral_rb/version.rb
71
+ - mistral_rb.gemspec
72
+ - sig/mistral_rb.rbs
73
+ homepage: https://github.com/fsndzomga/mistral_rb
74
+ licenses:
75
+ - MIT
76
+ metadata:
77
+ homepage_uri: https://github.com/fsndzomga/mistral_rb
78
+ source_code_uri: https://github.com/fsndzomga/mistral_rb
79
+ changelog_uri: https://github.com/fsndzomga/mistral_rb/blob/main/CHANGELOG.md
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 2.6.0
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubygems_version: 3.3.7
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: A simple wrapper for the Mistral API
99
+ test_files: []