Class: SmartPrompt::ZhipuAIAdapter

Overview

Adapter for 智谱 AI (BigModel / GLM) — one adapter owns the whole provider: every category shares the base URL open.bigmodel.cn/api/paas/v4 and Bearer auth.

Per-modality behavior lives in capability modules under adapters/zhipu/ (Text / Embed / Image / Video / Voice / Rerank); cross-provider plumbing (HTTP, multimodal normalization, chat shaping, image saving) comes from the shared concerns. This class wires them together + holds config/credentials.

chat/vision — POST {base}/chat/completions (OpenAI-compatible; reasoning_content)
embeddings  — POST {base}/embeddings        (embedding-3, custom dimensions)
image       — POST {base}/images/generations (nested data.images[].url)
video       — POST {base}/videos/generations -> GET {base}/async-result (async)
tts         — POST {base}/audio/speech       (glm-tts)
asr         — POST {base}/audio/transcriptions (multipart)
rerank      — POST {base}/rerank

Constant Summary collapse

DEFAULT_BASE_URL =
"https://open.bigmodel.cn/api/paas/v4".freeze
DEFAULT_CODING_BASE_URL =

CodeGeeX-4 / coding models use a separate base.

"https://open.bigmodel.cn/api/coding/paas/v4".freeze

Constants included from SmartPrompt::ZhipuAI::Text

SmartPrompt::ZhipuAI::Text::CHAT_OPTIONAL_KEYS

Constants included from MultimodalMessages

MultimodalMessages::SUPPORTED_IMAGE_FORMATS

Instance Attribute Summary

Attributes inherited from LLMAdapter

#last_response

Instance Method Summary collapse

Methods included from SmartPrompt::ZhipuAI::Rerank

#rerank

Methods included from SmartPrompt::ZhipuAI::Voice

#synthesize_speech, #synthesize_to_file, #transcribe_audio

Methods included from SmartPrompt::ZhipuAI::Video

#check_video_status, #download_video, #generate_video, #wait_for_video_completion

Methods included from SmartPrompt::ZhipuAI::Image

#generate_image

Methods included from SmartPrompt::ZhipuAI::Embed

#embeddings

Methods included from SmartPrompt::ZhipuAI::Text

#send_request

Methods included from HTTPClient

#http_get_json, #http_post_binary, #http_post_json, #stream_chat

Methods included from MultimodalMessages

#normalize_content_item, #normalize_image_url, #normalize_input_image, #normalize_media_part, #normalize_media_url, #process_multimodal_messages, #stringify_hash

Methods included from OpenAIChatShaping

#build_completion_response, #build_stream_chunk, #extra_top_level_fields, #reasoning_field_name

Methods included from ImagePersistence

#save_image, #save_single_image

Methods inherited from LLMAdapter

#send_request

Constructor Details

#initialize(config) ⇒ ZhipuAIAdapter

Returns a new instance of ZhipuAIAdapter.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/smart_prompt/zhipu_adapter.rb', line 61

def initialize(config)
  super
  SmartPrompt.logger.info "Start create the SmartPrompt ZhipuAIAdapter."

  api_key = @config["api_key"]
  if api_key.is_a?(String) && api_key.start_with?("ENV[") && api_key.end_with?("]")
    api_key = eval(api_key)
  end
  # Tolerate a missing key at construction so examples/config can load without a
  # live key; the first request fails with a clear auth error.
  SmartPrompt.logger.warn "Zhipu api_key is empty — API calls will fail until it is set." if api_key.nil? || api_key.to_s.strip.empty?

  @api_key     = api_key
  @base_url    = (@config["url"] || DEFAULT_BASE_URL).to_s.chomp("/")
  @coding_base = (@config["coding_url"] || DEFAULT_CODING_BASE_URL).to_s.chomp("/")
  # Optional per-method URL overrides (default to the standard paths off @base_url).
  @image_url  = (@config["image_url"]  || "#{@base_url}/images/generations").to_s
  @video_url  = (@config["video_url"]  || "#{@base_url}/videos/generations").to_s
  @query_url  = (@config["query_url"]  || "#{@base_url}/async-result").to_s
  SmartPrompt.logger.info "Zhipu base_url=#{@base_url}"
end

Instance Method Details

#default_image_prefixObject



57
58
59
# File 'lib/smart_prompt/zhipu_adapter.rb', line 57

def default_image_prefix
  "zhipu_image"
end

#provider_labelObject

—- hooks for shared concerns ——————————————-



53
54
55
# File 'lib/smart_prompt/zhipu_adapter.rb', line 53

def provider_label
  "Zhipu"
end