Module: Langfuse::Utils
- Defined in:
- lib/langfuse/utils.rb
Constant Summary collapse
- VERBATIM_BODY_KEYS =
Body keys whose values must be passed through verbatim (user data), everything else nested under them keeps its original key format.
%w[input output metadata usageDetails costDetails modelParameters].freeze
Class Method Summary collapse
- .current_timestamp ⇒ Object
-
.deep_camelize_keys(hash) ⇒ Object
将哈希的键名转换为小驼峰格式.
- .deep_stringify_keys(hash) ⇒ Object
- .deep_symbolize_keys(hash) ⇒ Object
-
.generate_hex_span_id ⇒ Object
W3C-compatible 16-char hex span/observation ID (8 bytes), used for OTel ingestion.
-
.generate_hex_trace_id ⇒ Object
W3C-compatible 32-char hex trace ID (16 bytes), used for OTel ingestion.
- .generate_id ⇒ Object
-
.prepare_event_body(body) ⇒ Object
Prepare an event body for the ingestion API: - top-level keys are camelized (snake_case -> camelCase) - user data values (input/output/metadata/usageDetails/costDetails/modelParameters) are passed through verbatim so user-provided keys are not mangled - the legacy
usageobject keys are camelized (prompt_tokens -> promptTokens). - .url_encode(string) ⇒ Object
Class Method Details
.current_timestamp ⇒ Object
28 29 30 |
# File 'lib/langfuse/utils.rb', line 28 def Time.now.utc.iso8601(3) end |
.deep_camelize_keys(hash) ⇒ Object
将哈希的键名转换为小驼峰格式
75 76 77 78 79 80 81 82 83 |
# File 'lib/langfuse/utils.rb', line 75 def deep_camelize_keys(hash) return hash unless hash.is_a?(Hash) hash.each_with_object({}) do |(key, value), result| new_key = camelize_key(key.to_s) new_value = value.is_a?(Hash) ? deep_camelize_keys(value) : value result[new_key] = new_value end end |
.deep_stringify_keys(hash) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/langfuse/utils.rb', line 46 def deep_stringify_keys(hash) return hash unless hash.is_a?(Hash) hash.each_with_object({}) do |(key, value), result| new_key = camelize_key(key.to_s) new_value = value.is_a?(Hash) ? deep_stringify_keys(value) : value result[new_key] = new_value end end |
.deep_symbolize_keys(hash) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/langfuse/utils.rb', line 36 def deep_symbolize_keys(hash) return hash unless hash.is_a?(Hash) hash.each_with_object({}) do |(key, value), result| new_key = key.is_a?(String) ? key.to_sym : key new_value = value.is_a?(Hash) ? deep_symbolize_keys(value) : value result[new_key] = new_value end end |
.generate_hex_span_id ⇒ Object
W3C-compatible 16-char hex span/observation ID (8 bytes), used for OTel ingestion
24 25 26 |
# File 'lib/langfuse/utils.rb', line 24 def generate_hex_span_id SecureRandom.hex(8) end |
.generate_hex_trace_id ⇒ Object
W3C-compatible 32-char hex trace ID (16 bytes), used for OTel ingestion
19 20 21 |
# File 'lib/langfuse/utils.rb', line 19 def generate_hex_trace_id SecureRandom.hex(16) end |
.generate_id ⇒ Object
14 15 16 |
# File 'lib/langfuse/utils.rb', line 14 def generate_id SecureRandom.uuid end |
.prepare_event_body(body) ⇒ Object
Prepare an event body for the ingestion API:
- top-level keys are camelized (snake_case -> camelCase)
- user data values (input/output/metadata/usageDetails/costDetails/modelParameters) are passed through verbatim so user-provided keys are not mangled
- the legacy
usageobject keys are camelized (prompt_tokens -> promptTokens)
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/langfuse/utils.rb', line 61 def prepare_event_body(body) return body unless body.is_a?(Hash) body.each_with_object({}) do |(key, value), result| new_key = camelize_key(key.to_s) result[new_key] = if !VERBATIM_BODY_KEYS.include?(new_key) && value.is_a?(Hash) deep_stringify_keys(value) else value end end end |
.url_encode(string) ⇒ Object
32 33 34 |
# File 'lib/langfuse/utils.rb', line 32 def url_encode(string) ERB::Util.url_encode(string.to_s) end |