Class: Prescient::Provider::Mistral
- Includes:
- HTTParty
- Defined in:
- lib/prescient/provider/mistral.rb
Overview
Mistral AI API provider adapter.
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#generate_embedding(text, **options) ⇒ Array<Float>
Generate an embedding through Mistral's embeddings API.
-
#generate_response(prompt, context_items = [], **options) ⇒ Hash
Generate a response through Mistral's chat completions API.
-
#health_check ⇒ Hash
Check whether the configured Mistral models are available.
-
#initialize(**options) ⇒ Mistral
constructor
A new instance of Mistral.
-
#list_models ⇒ Array<Hash>
List models available to the configured Mistral API key.
- #validate_configuration! ⇒ Object protected
Methods inherited from Base
#available?, #build_prompt, #clean_text, #default_context_configs, #default_prompt_templates, #extract_embedding_text, #extract_text_values, #format_context_item, #handle_errors, #validate_embedding_dimensions
Constructor Details
#initialize(**options) ⇒ Mistral
Returns a new instance of Mistral.
11 12 13 14 |
# File 'lib/prescient/provider/mistral.rb', line 11 def initialize(**) super self.class.default_timeout(@options[:timeout] || 60) end |
Instance Method Details
#generate_embedding(text, **options) ⇒ Array<Float>
Generate an embedding through Mistral's embeddings API.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/prescient/provider/mistral.rb', line 19 def (text, **) handle_errors do = [:model] || @options[:embedding_model] response = self.class.post( '/v1/embeddings', headers: api_headers, body: { model: , input: clean_text(text), }.to_json, ) validate_response!(response, 'embedding generation') = response.parsed_response.dig('data', 0, 'embedding') raise Prescient::InvalidResponseError, 'No embedding returned' unless .is_a?(Array) expected_dimensions = @options[:embedding_dimensions] expected_dimensions ? (, expected_dimensions) : end end |
#generate_response(prompt, context_items = [], **options) ⇒ Hash
Generate a response through Mistral's chat completions API.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/prescient/provider/mistral.rb', line 45 def generate_response(prompt, context_items = [], **) handle_errors do model = [:model] || @options[:chat_model] response = self.class.post( '/v1/chat/completions', headers: api_headers, body: { model: model, messages: [{ role: 'user', content: build_prompt(prompt, context_items) }], max_tokens: [:max_tokens] || 2000, temperature: [:temperature] || 0.7, top_p: [:top_p] || 0.9, }.to_json, ) validate_response!(response, 'text generation') parsed_response = response.parsed_response content = normalize_content(parsed_response.dig('choices', 0, 'message', 'content')) raise Prescient::InvalidResponseError, 'No response generated' if content.nil? || content.empty? { response: content.strip, model: model, provider: 'mistral', processing_time: nil, metadata: { usage: parsed_response['usage'], finish_reason: parsed_response.dig('choices', 0, 'finish_reason'), }, } end end |
#health_check ⇒ Hash
Check whether the configured Mistral models are available.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/prescient/provider/mistral.rb', line 81 def health_check handle_errors do response = self.class.get('/v1/models', headers: api_headers) if response.success? models = response.parsed_response['data'] || [] = model_available?(models, @options[:embedding_model]) chat_available = model_available?(models, @options[:chat_model]) { status: 'healthy', provider: 'mistral', reachable: true, models_available: models.map { |model| model['id'] }, embedding_model: { name: @options[:embedding_model], available: }, chat_model: { name: @options[:chat_model], available: chat_available }, ready: && chat_available, } else { status: 'unhealthy', provider: 'mistral', reachable: true, error: "HTTP #{response.code}", message: response., ready: false, } end end rescue Prescient::Error => e { status: 'unavailable', provider: 'mistral', reachable: false, error: e.class.name, message: e., ready: false, } end |
#list_models ⇒ Array<Hash>
List models available to the configured Mistral API key.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/prescient/provider/mistral.rb', line 123 def list_models handle_errors do response = self.class.get('/v1/models', headers: api_headers) validate_response!(response, 'model listing') (response.parsed_response['data'] || []).map do |model| { name: model['id'], object: model['object'], created: model['created'], owned_by: model['owned_by'], capabilities: model['capabilities'], max_context_length: model['max_context_length'], }.compact end end end |
#validate_configuration! ⇒ Object (protected)
143 144 145 146 147 148 149 150 |
# File 'lib/prescient/provider/mistral.rb', line 143 def validate_configuration! = [:api_key, :embedding_model, :chat_model] = .select { |option| @options[option].nil? } return unless .any? raise Prescient::Error, "Missing required options: #{.join(', ')}" end |