Module: SmartPrompt::ZhipuAI::Embed

Included in:
SmartPrompt::ZhipuAIAdapter
Defined in:
lib/smart_prompt/adapters/zhipu/embed.rb

Overview

Embeddings (embedding-3, custom dimensions).

Instance Method Summary collapse

Instance Method Details

#embeddings(text, model) ⇒ Object

embedding-3 (default 2048 dims); supports a custom ‘dimensions` (256/512/1024/2048) via config. Returns the first embedding vector.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/smart_prompt/adapters/zhipu/embed.rb', line 7

def embeddings(text, model)
  model_name = model || @config["embedding_model"] || @config["model"]
  SmartPrompt.logger.info "ZhipuAIAdapter: embeddings model=#{model_name}"

  body = { "model" => model_name, "input" => text.is_a?(Array) ? text : [text.to_s] }
  body["dimensions"] = @config["dimensions"] if @config["dimensions"]
  body["encoding_format"] = @config["encoding_format"] if @config["encoding_format"]

  response =
    begin
      http_post_json("#{@base_url}/embeddings", body)
    rescue LLMAPIError, Error
      raise
    rescue => e
      raise LLMAPIError, "Failed to call Zhipu embeddings: #{e.message}"
    end

  items = response["data"]
  unless items.is_a?(Array) && items.any? && items[0]["embedding"]
    raise LLMAPIError, "No embedding vector in Zhipu response: #{response.inspect}"
  end
  items[0]["embedding"]
end