Module: SmartPrompt::ZhipuAI::Image

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

Overview

Text-to-image (CogView / GLM-Image). save_image comes from the ImagePersistence concern.

Instance Method Summary collapse

Instance Method Details

#generate_image(prompt, params = {}) ⇒ Object

Text-to-image. The Zhipu response is NESTED: data.images[].url (not OpenAI’s data[]), so we parse defensively. Returns an Array of b64_json:.

Raises:



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

def generate_image(prompt, params = {})
  SmartPrompt.logger.info "ZhipuAIAdapter: generating image"
  raise Error, "Prompt cannot be empty" if prompt.nil? || prompt.to_s.strip.empty?

  model_name = params[:model] || @config["image_model"] || @config["model"]
  raise Error, "No model configured for image generation" if model_name.nil? || model_name.to_s.strip.empty?

  body = { "model" => model_name, "prompt" => prompt.to_s }
  body["size"]            = params[:size]            if params[:size]
  body["user"]            = params[:user]            if params[:user]
  body["response_format"] = params[:response_format] if params[:response_format]

  SmartPrompt.logger.info "Zhipu image params: #{body.except('prompt').inspect}"
  response =
    begin
      http_post_json(@image_url, body)
    rescue LLMAPIError, Error
      raise
    rescue => e
      raise Error, "Failed to call Zhipu image generation: #{e.message}"
    end

  images = parse_image_response(response)
  SmartPrompt.logger.info "ZhipuAIAdapter: generated #{images.size} image(s)"
  images
end