Class: MistralEmbeddingCreator
- Inherits:
-
Object
- Object
- MistralEmbeddingCreator
- Defined in:
- lib/embedding_engines/mistral_embeddings.rb
Instance Method Summary collapse
- #call(text, pages_mode = true) ⇒ Object
-
#initialize(api_key = nil, chunker = BasicTextChunker.new, model = "mistral-embed") ⇒ MistralEmbeddingCreator
constructor
A new instance of MistralEmbeddingCreator.
Constructor Details
#initialize(api_key = nil, chunker = BasicTextChunker.new, model = "mistral-embed") ⇒ MistralEmbeddingCreator
Returns a new instance of MistralEmbeddingCreator.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/embedding_engines/mistral_embeddings.rb', line 8 def initialize(api_key = nil, chunker = BasicTextChunker.new, model = "mistral-embed") @chunker = chunker @model = model @api_key = api_key || ENV['MISTRAL_API_KEY'] raise 'API key not found. Please set the MISTRAL_API_KEY environment variable.' if api_key.nil? || api_key.empty? if @api_key @llm = MistralAPI.new(api_key: @api_key) else Rails.logger.error "MISTRAL AI API key not provided. Set the MISTRAL_API_KEY in the ENV variables or pass it as an argument." end end |
Instance Method Details
#call(text, pages_mode = true) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/embedding_engines/mistral_embeddings.rb', line 21 def call(text, pages_mode=true) if pages_mode vectors = [] return [] unless @llm # Return empty if the API client isn't set up # Divide the text into chunks for each page text.each_with_index do |page_content, page_index| chunks = @chunker.split_into_chunks(page_content) # Create embeddings for each chunk chunks.each_with_index do |chunk, index| response = @llm.( model: @model, input: [chunk] ) # Extract the embeddings from the response = response.data.first. # Create vector data for the chunk and keep page numbers for reference vector_data = { id: "vec #{index + 1}", values: , metadata: { text: chunk, page: page_index + 1, } } # storing each chunk vector data in an array vectors << vector_data end end vectors else response = @llm.( model: @model, input: [text] ) response.data.first. end end |