Class: SmartPrompt::ZhipuAIAdapter

Inherits:
LLMAdapter show all
Includes:
HTTPClient, ImagePersistence, MultimodalMessages, OpenAIChatShaping, ZhipuAI::Embed, ZhipuAI::Image, ZhipuAI::Rerank, ZhipuAI::Text, ZhipuAI::Video, ZhipuAI::Voice
Defined in:
lib/smart_prompt/zhipu_adapter.rb

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

Instance Attribute Summary

Attributes inherited from LLMAdapter

#last_response

Instance Method Summary collapse

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