Class: Durable::Llm::Providers::Mistral
- Defined in:
- lib/durable/llm/providers/mistral.rb
Overview
Mistral AI provider for accessing Mistral AI’s language models
This class provides a complete interface to Mistral AI’s API, supporting text completions, embeddings, model listing, and streaming responses. It handles authentication, HTTP communication, error management, and response normalization to provide a consistent API experience.
Defined Under Namespace
Classes: MistralChoice, MistralEmbeddingResponse, MistralMessage, MistralResponse, MistralStreamChoice, MistralStreamDelta, MistralStreamResponse
Constant Summary collapse
- BASE_URL =
Base URL for Mistral AI API
'https://api.mistral.ai/v1'
Instance Attribute Summary collapse
-
#api_key ⇒ String?
The API key used for Mistral AI authentication.
Class Method Summary collapse
-
.stream? ⇒ Boolean
Indicates whether this provider supports streaming responses.
Instance Method Summary collapse
-
#completion(options) ⇒ MistralResponse
Performs a chat completion request to Mistral AI.
-
#default_api_key ⇒ String?
Returns the default API key for Mistral AI.
-
#embedding(model:, input:, **options) ⇒ MistralEmbeddingResponse
Generates embeddings for the given input text.
-
#initialize(api_key: nil) ⇒ Mistral
constructor
Initializes a new Mistral provider instance.
-
#models ⇒ Array<String>
Retrieves the list of available models from Mistral AI.
-
#stream(options) {|MistralStreamResponse| ... } ⇒ nil
Performs a streaming chat completion request to Mistral AI.
Methods inherited from Base
Constructor Details
#initialize(api_key: nil) ⇒ Mistral
Initializes a new Mistral provider instance
55 56 57 58 59 60 61 62 63 |
# File 'lib/durable/llm/providers/mistral.rb', line 55 def initialize(api_key: nil) super() @api_key = api_key || default_api_key @conn = Faraday.new(url: BASE_URL) do |faraday| faraday.request :json faraday.response :json faraday.adapter Faraday.default_adapter end end |
Instance Attribute Details
#api_key ⇒ String?
Returns The API key used for Mistral AI authentication.
50 51 52 |
# File 'lib/durable/llm/providers/mistral.rb', line 50 def api_key @api_key end |
Class Method Details
.stream? ⇒ Boolean
Indicates whether this provider supports streaming responses
122 123 124 |
# File 'lib/durable/llm/providers/mistral.rb', line 122 def self.stream? true end |
Instance Method Details
#completion(options) ⇒ MistralResponse
Performs a chat completion request to Mistral AI
77 78 79 80 81 82 83 84 |
# File 'lib/durable/llm/providers/mistral.rb', line 77 def completion() response = @conn.post('chat/completions') do |req| req.headers['Authorization'] = "Bearer #{@api_key}" req.body = end handle_response(response) end |
#default_api_key ⇒ String?
Returns the default API key for Mistral AI
Checks the configuration object first, then falls back to the MISTRAL_API_KEY environment variable.
40 41 42 43 44 45 46 |
# File 'lib/durable/llm/providers/mistral.rb', line 40 def default_api_key begin Durable::Llm.configuration.mistral&.api_key rescue NoMethodError nil end || ENV['MISTRAL_API_KEY'] end |
#embedding(model:, input:, **options) ⇒ MistralEmbeddingResponse
Generates embeddings for the given input text
96 97 98 99 100 101 102 103 |
# File 'lib/durable/llm/providers/mistral.rb', line 96 def (model:, input:, **) response = @conn.post('embeddings') do |req| req.headers['Authorization'] = "Bearer #{@api_key}" req.body = { model: model, input: input, ** } end handle_response(response, MistralEmbeddingResponse) end |
#models ⇒ Array<String>
Retrieves the list of available models from Mistral AI
111 112 113 114 115 116 117 |
# File 'lib/durable/llm/providers/mistral.rb', line 111 def models response = @conn.get('models') do |req| req.headers['Authorization'] = "Bearer #{@api_key}" end handle_response(response).data.map { |model| model['id'] } end |
#stream(options) {|MistralStreamResponse| ... } ⇒ nil
Performs a streaming chat completion request to Mistral AI
Yields response chunks as they arrive from the API.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/durable/llm/providers/mistral.rb', line 137 def stream() [:stream] = true response = @conn.post('chat/completions') do |req| req.headers['Authorization'] = "Bearer #{@api_key}" req.headers['Accept'] = 'text/event-stream' ['temperature'] = ['temperature'].to_f if ['temperature'] req.body = user_proc = proc do |chunk, _size, _total| yield MistralStreamResponse.new(chunk) end req..on_data = to_json_stream(user_proc: user_proc) end handle_response(response) end |