Files
leadchat/enterprise/app/services/captain/llm/embedding_service.rb
YashRaj be721c2b50 feat: add config for embedding model (#12120)
This PR adds the ability to modify the embedding model used by Captain
AI.Previously, the embedding model was hardcoded which led to errors when
you used a different API provider which did not support that specific
embedding model.

Co-authored-by: Shivam Mishra <scm.mymail@gmail.com>
2025-08-25 11:33:00 +05:30

23 lines
634 B
Ruby

require 'openai'
class Captain::Llm::EmbeddingService < Llm::BaseOpenAiService
class EmbeddingsError < StandardError; end
def self.embedding_model
@embedding_model = InstallationConfig.find_by(name: 'CAPTAIN_EMBEDDING_MODEL')&.value.presence || OpenAiConstants::DEFAULT_EMBEDDING_MODEL
end
def get_embedding(content, model: self.class.embedding_model)
response = @client.embeddings(
parameters: {
model: model,
input: content
}
)
response.dig('data', 0, 'embedding')
rescue StandardError => e
raise EmbeddingsError, "Failed to create an embedding: #{e.message}"
end
end