Module: Foam::Otel::LLM::AnthropicShim
- Defined in:
- lib/foam/otel/llm/anthropic_shim.rb
Overview
Gap-filler for the OFFICIAL Anthropic Ruby SDK (gem "anthropic") — the Ruby twin of js/otel's anthropic gap-filler. Patches the public non-streaming call site Messages#create with a gen_ai CLIENT span; streaming (#stream/#stream_raw) is a recorded follow-up.
Defined Under Namespace
Modules: MessagesPatch
Constant Summary collapse
- SUPPORTED_BELOW =
Gem::Version.new("2.0.0")
- RESPONSE =
Anthropic::Models::Message → response attributes. The content blocks ride VERBATIM — text AND tool_use blocks (tool calls) in one JSON value, exactly like the js gap-filler.
lambda do |response| usage = response.respond_to?(:usage) ? response.usage : nil LLM.response_attributes( model: response.respond_to?(:model) ? response.model : nil, id: response.respond_to?(:id) ? response.id : nil, finish_reasons: response.respond_to?(:stop_reason) ? [response.stop_reason].compact : nil, input_tokens: usage.respond_to?(:input_tokens) ? usage.input_tokens : nil, output_tokens: usage.respond_to?(:output_tokens) ? usage.output_tokens : nil, output_messages: response.respond_to?(:content) ? response.content : nil ) end
Class Method Summary collapse
- .install! ⇒ Object
- .installed? ⇒ Boolean
- .present? ⇒ Boolean
- .request(params) ⇒ Object
- .supported_version? ⇒ Boolean
Class Method Details
.install! ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/foam/otel/llm/anthropic_shim.rb', line 20 def install! return true if @installed ::Anthropic::Resources::Messages.prepend(MessagesPatch) Diagnostics.info("LLM shim installed: anthropic (messages.create)") @installed = true end |
.installed? ⇒ Boolean
28 |
# File 'lib/foam/otel/llm/anthropic_shim.rb', line 28 def installed? = @installed |
.present? ⇒ Boolean
14 15 16 17 18 |
# File 'lib/foam/otel/llm/anthropic_shim.rb', line 14 def present? defined?(::Anthropic::Resources::Messages) && ::Anthropic::Resources::Messages.method_defined?(:create) && supported_version? end |
.request(params) ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/foam/otel/llm/anthropic_shim.rb', line 45 def request(params) params = params.is_a?(Hash) ? params : {} LLM.request_attributes( provider: "anthropic", operation: "chat", model: params[:model] || params["model"], max_tokens: params[:max_tokens] || params["max_tokens"], input_messages: params[:messages] || params["messages"], system_instructions: params[:system] || params["system"] ) end |
.supported_version? ⇒ Boolean
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/foam/otel/llm/anthropic_shim.rb', line 30 def supported_version? spec = Gem.loaded_specs["anthropic"] return true if spec.nil? supported = spec.version < SUPPORTED_BELOW unless supported || @version_warned @version_warned = true Diagnostics.warn("anthropic #{spec.version} is outside foam's LLM-shim window (< #{SUPPORTED_BELOW}) " \ "— the Anthropic shim stays dark; file this with foam support") end supported rescue StandardError true end |