omniai-mistral 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 288f800acd88334ffa7a9f268fde19c4cd9329a987d0b2f6bc2a1900724d2e63
4
- data.tar.gz: f9b79385abf601b0c11e08f1187b8af2dce952cb40ecd66067798771c300332b
3
+ metadata.gz: ecfcd380d66660c2b8cb71d7cec580dd10c9df8030b25c9ee60e78de5181c173
4
+ data.tar.gz: a2230cb09db232a7024acd6677e206e47e345e3daa07081b0c0e2acde3622d96
5
5
  SHA512:
6
- metadata.gz: 3711845567a9d9d11a03c08106953e148c39b9a0e98adf6c45a575f7cdc05f784a2ec2b523418b0fe98595b5ba5d564d51ae6019629bdb1ae36470218550ac52
7
- data.tar.gz: 661ddc23d8f3ec3e56a6c754cb29a3c2435c4a77250cae46d7a59b48f742d3bf0cfa5622ced5b18650d362de0c9f98dda30cab185b8b708281dc99d31a7d4021
6
+ metadata.gz: 34442af7ff4d2c376e6959d86d5f43b7a02bb57397882f1ec834b2f1c40448458573560abec4e21ac8c79b1c42646d073aac058f5be7ad39332cf74db96c1855
7
+ data.tar.gz: 446b60f9e02e37c822bf5d9728db0059a9926485afb961cafc6ff4ae79938baf5e6a10de6e371a4c687ee89956258e78a0f679220e68cafc5e110e6d0fb163b3
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
6
+
7
+ gem 'rake'
8
+
9
+ gem 'rspec'
10
+ gem 'rspec_junit_formatter'
11
+ gem 'rubocop'
12
+ gem 'rubocop-rake'
13
+ gem 'rubocop-rspec'
14
+ gem 'vcr'
15
+ gem 'webmock'
data/README.md CHANGED
@@ -1,31 +1,101 @@
1
- # Omniai::Mistral
1
+ # OmniAI::Mistral
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
4
-
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/omniai/mistral`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ An Mistral implementation of the [OmniAI](https://github.com/ksylvest/omniai) APIs.
6
4
 
7
5
  ## Installation
8
6
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
7
+ ```sh
8
+ gem install omniai-mistral
9
+ ```
10
10
 
11
- Install the gem and add to the application's Gemfile by executing:
11
+ ## Usage
12
12
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
13
+ ### Client
14
14
 
15
- If bundler is not being used to manage dependencies, install the gem by executing:
15
+ A client is setup as follows if `ENV['MISTRAL_API_KEY']` exists:
16
16
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
17
+ ```ruby
18
+ client = OmniAI::Mistral::Client.new
19
+ ```
18
20
 
19
- ## Usage
21
+ A client may also be passed the following options:
22
+
23
+ - `api_key` (required - default is `ENV['MISTRAL_API_KEY']`)
24
+ - `host` (optional)
25
+
26
+ ### Configuration
27
+
28
+ Global configuration is supported for the following options:
29
+
30
+ ```ruby
31
+ OmniAI::Mistral.configure do |config|
32
+ config.api_key = 'sk-...' # default: ENV['MISTRAL_API_KEY']
33
+ config.host = '...' # default: 'https://api.mistral.ai'
34
+ end
35
+ ```
36
+
37
+ ### Chat
38
+
39
+ A chat completion is generated by passing in prompts using any a variety of formats:
40
+
41
+ ```ruby
42
+ completion = client.chat.completion('Tell me a joke!')
43
+ completion.choice.message.content # 'Why did the chicken cross the road? To get to the other side.'
44
+ ```
45
+
46
+ ```ruby
47
+ completion = client.chat.completion({
48
+ role: OmniAI::Mistral::Chat::Role::USER,
49
+ content: 'Is it wise to jump off a bridge?'
50
+ })
51
+ completion.choice.message.content # 'No.'
52
+ ```
53
+
54
+ ```ruby
55
+ completion = client.chat.completion([
56
+ {
57
+ role: OmniAI::Mistral::Chat::Role::SYSTEM,
58
+ content: 'You are a helpful assistant.'
59
+ },
60
+ 'What is the capital of Canada?',
61
+ ])
62
+ completion.choice.message.content # 'The capital of Canada is Ottawa.'
63
+ ```
64
+
65
+ #### Model
66
+
67
+ `model` takes an optional string (default is `mistral-medium-latest`):
68
+
69
+ ```ruby
70
+ completion = client.chat.completion('Provide code for fibonacci', model: OmniAI::Mistral::Chat::Model::CODESTRAL)
71
+ completion.choice.message.content # 'def fibonacci(n)...end'
72
+ ```
73
+
74
+ #### Temperature
75
+
76
+ `temperature` takes an optional float between `0.0` and `1.0` (defaults is `0.7`):
77
+
78
+ ```ruby
79
+ completion = client.chat.completion('Pick a number between 1 and 5', temperature: 1.0)
80
+ completion.choice.message.content # '3'
81
+ ```
20
82
 
21
- TODO: Write usage instructions here
83
+ #### Stream
22
84
 
23
- ## Development
85
+ `stream` takes an optional a proc to stream responses in real-time chunks instead of waiting for a complete response:
24
86
 
25
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
87
+ ```ruby
88
+ stream = proc do |chunk|
89
+ print(chunk.choice.delta.content) # 'Better', 'three', 'hours', ...
90
+ end
91
+ client.chat.completion('Be poetic.', stream:)
92
+ ```
26
93
 
27
- 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).
94
+ #### Format
28
95
 
29
- ## Contributing
96
+ `format` takes an optional symbol (i.e. `:json`) and switches the client to parsing message content as `json`:
30
97
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/omniai-mistral.
98
+ ```ruby
99
+ completion = client.chat.completion('Please provide a color name / hex / hsl as JSON.', format: :json)
100
+ completion.choice.message.content # { "name": "Black", "hex": "#000", "hsl": { "h": 0, "s": 0, "l": 0 } }
101
+ ```
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OmniAI
4
+ module Mistral
5
+ class Chat
6
+ # An implementation of OmniAI::Chat::Request for Mistral.
7
+ class Request < OmniAI::Chat::Request
8
+ protected
9
+
10
+ # @return [Hash]
11
+ def payload
12
+ { messages: }.tap do |payload|
13
+ payload[:model] = @model
14
+ payload[:stream] = !@stream.nil? unless @stream.nil?
15
+ payload[:temperature] = @temperature if @temperature
16
+ payload[:response_format] = { type: 'json_object' } if @format.eql?(:json)
17
+ end
18
+ end
19
+
20
+ # @return [Array<Hash>]
21
+ def messages
22
+ arrayify(@messages).map do |content|
23
+ case content
24
+ when String then { role: OmniAI::Mistral::Chat::Role::USER, content: }
25
+ when Hash then content
26
+ else raise Error, "Unsupported content=#{content.inspect}"
27
+ end
28
+ end
29
+ end
30
+
31
+ # @return [String]
32
+ def path
33
+ "/#{OmniAI::Mistral::Client::VERSION}/chat/completions"
34
+ end
35
+
36
+ private
37
+
38
+ # @param value [Object, Array<Object>]
39
+ # @return [Array<Object>]
40
+ def arrayify(value)
41
+ value.is_a?(Array) ? value : [value]
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OmniAI
4
+ module Mistral
5
+ # A Mistral chat implementation.
6
+ #
7
+ # Usage:
8
+ #
9
+ # chat = OmniAI::Mistral::Chat.new(client: client)
10
+ # chat.completion('Tell me a joke.')
11
+ # chat.completion(['Tell me a joke.'])
12
+ # chat.completion({ role: 'user', content: 'Tell me a joke.' })
13
+ # chat.completion([{ role: 'system', content: 'Tell me a joke.' }])
14
+ class Chat < OmniAI::Chat
15
+ module Model
16
+ SMALL = 'mistral-small-latest'
17
+ MEDIUM = 'mistral-medium-latest'
18
+ LARGE = 'mistral-large-latest'
19
+ CODESTRAL = 'codestral-latest'
20
+ end
21
+
22
+ module Role
23
+ ASSISTANT = 'assistant'
24
+ USER = 'user'
25
+ SYSTEM = 'system'
26
+ end
27
+
28
+ # @raise [OmniAI::Error]
29
+ #
30
+ # @param prompt [String]
31
+ # @param model [String] optional
32
+ # @param format [Symbol] optional :text or :json
33
+ # @param temperature [Float, nil] optional
34
+ # @param stream [Proc, nil] optional
35
+ #
36
+ # @return [OmniAI::Mistral::Chat::Response]
37
+ def completion(messages, model: Model::MEDIUM, temperature: nil, format: nil, stream: nil)
38
+ request = OmniAI::Mistral::Chat::Request.new(client: @client, messages:, model:, temperature:, format:, stream:)
39
+ request.process!
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OmniAI
4
+ module Mistral
5
+ # An Mistral client implementation. Usage:
6
+ #
7
+ # w/ `api_key``:
8
+ # client = OmniAI::Mistral::Client.new(api_key: '...')
9
+ #
10
+ # w/ ENV['MISTRAL_API_KEY']:
11
+ #
12
+ # ENV['MISTRAL_API_KEY'] = '...'
13
+ # client = OmniAI::Mistral::Client.new
14
+ #
15
+ # w/ config:
16
+ #
17
+ # OmniAI::Mistral.configure do |config|
18
+ # config.api_key = '...'
19
+ # end
20
+ #
21
+ # client = OmniAI::Mistral::Client.new
22
+ class Client < OmniAI::Client
23
+ VERSION = 'v1'
24
+
25
+ # @param api_key [String] optional - defaults to `OmniAI::Mistral.config.api_key`
26
+ # @param host [String] optional - defaults to `OmniAI::Mistral.config.host`
27
+ def initialize(
28
+ api_key: OmniAI::Mistral.config.api_key,
29
+ logger: OmniAI::Mistral.config.logger,
30
+ host: OmniAI::Mistral.config.host
31
+ )
32
+ raise(ArgumentError, %(ENV['MISTRAL_API_KEY'] must be defined or `api_key` must be passed)) if api_key.nil?
33
+
34
+ super(api_key:, logger:)
35
+
36
+ @host = host
37
+ end
38
+
39
+ # @return [HTTP::Client]
40
+ def connection
41
+ @connection ||= HTTP.auth("Bearer #{api_key}").persistent(@host)
42
+ end
43
+
44
+ # @return [OmniAI::Mistral::Chat]
45
+ def chat
46
+ Chat.new(client: self)
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OmniAI
4
+ module Mistral
5
+ # Config for the Mistral `api_key` / `host` / `logger`.
6
+ class Config < OmniAI::Config
7
+ def initialize
8
+ super
9
+ @api_key = ENV.fetch('MISTRAL_API_KEY', nil)
10
+ @host = ENV.fetch('MISTRAL_HOST', 'https://api.mistral.ai')
11
+ end
12
+ end
13
+ end
14
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module OmniAI
4
4
  module Mistral
5
- VERSION = "0.1.0"
5
+ VERSION = '0.2.0'
6
6
  end
7
7
  end
@@ -1,10 +1,24 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "mistral/version"
3
+ require 'event_stream_parser'
4
+ require 'omniai'
5
+ require 'zeitwerk'
6
+
7
+ loader = Zeitwerk::Loader.for_gem
8
+ loader.push_dir(__dir__, namespace: OmniAI)
9
+ loader.setup
4
10
 
5
11
  module OmniAI
12
+ # A namespace for everything Mistral.
6
13
  module Mistral
7
- class Error < StandardError; end
8
- # Your code goes here...
14
+ # @return [OmniAI::Mistral::Config]
15
+ def self.config
16
+ @config ||= Config.new
17
+ end
18
+
19
+ # @yield [OmniAI::Mistral::Config]
20
+ def self.configure
21
+ yield config
22
+ end
9
23
  end
10
24
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniai-mistral
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Sylvestre
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-06-10 00:00:00.000000000 Z
11
+ date: 2024-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: omniai
14
+ name: event_stream_parser
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
@@ -25,13 +25,13 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rspec
28
+ name: omniai
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
- type: :development
34
+ type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
@@ -39,13 +39,13 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rubocop
42
+ name: zeitwerk
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
- type: :development
48
+ type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
@@ -59,16 +59,20 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - ".rspec"
63
- - ".rubocop.yml"
62
+ - Gemfile
64
63
  - README.md
65
- - Rakefile
66
64
  - lib/omniai/mistral.rb
65
+ - lib/omniai/mistral/chat.rb
66
+ - lib/omniai/mistral/chat/request.rb
67
+ - lib/omniai/mistral/client.rb
68
+ - lib/omniai/mistral/config.rb
67
69
  - lib/omniai/mistral/version.rb
68
- - sig/omniai/mistral.rbs
69
70
  homepage: https://github.com/ksylvest/omniai-mistral
70
71
  licenses: []
71
- metadata: {}
72
+ metadata:
73
+ homepage_uri: https://github.com/ksylvest/omniai-mistral
74
+ changelog_uri: https://github.com/ksylvest/omniai-mistral/releases
75
+ rubygems_mfa_required: 'true'
72
76
  post_install_message:
73
77
  rdoc_options: []
74
78
  require_paths:
@@ -77,7 +81,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
77
81
  requirements:
78
82
  - - ">="
79
83
  - !ruby/object:Gem::Version
80
- version: 3.0.0
84
+ version: 3.3.0
81
85
  required_rubygems_version: !ruby/object:Gem::Requirement
82
86
  requirements:
83
87
  - - ">="
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
data/.rubocop.yml DELETED
@@ -1,8 +0,0 @@
1
- AllCops:
2
- TargetRubyVersion: 3.0
3
-
4
- Style/StringLiterals:
5
- EnforcedStyle: double_quotes
6
-
7
- Style/StringLiteralsInInterpolation:
8
- EnforcedStyle: double_quotes
data/Rakefile DELETED
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "bundler/gem_tasks"
4
- require "rspec/core/rake_task"
5
-
6
- RSpec::Core::RakeTask.new(:spec)
7
-
8
- require "rubocop/rake_task"
9
-
10
- RuboCop::RakeTask.new
11
-
12
- task default: %i[spec rubocop]
@@ -1,6 +0,0 @@
1
- module Omniai
2
- module Mistral
3
- VERSION: String
4
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
- end
6
- end