Class: Langfuse::ChatPromptClient
- Inherits:
-
Object
- Object
- Langfuse::ChatPromptClient
- Defined in:
- lib/langfuse/chat_prompt_client.rb
Overview
Chat prompt client for compiling chat prompts with variable substitution
Handles chat-based prompts from Langfuse, providing Mustache templating for variable substitution in role-based messages.
Constant Summary collapse
- PLACEHOLDER_TYPE =
"placeholder"
Instance Attribute Summary collapse
-
#commit_message ⇒ String?
readonly
Optional commit message for this prompt version.
-
#config ⇒ Hash
readonly
Prompt configuration.
-
#is_fallback ⇒ Boolean
readonly
Whether this client uses caller-provided fallback content.
-
#labels ⇒ Array<String>
readonly
Labels assigned to this prompt.
-
#name ⇒ String
readonly
Prompt name.
-
#prompt ⇒ Array<Hash>
readonly
Raw prompt template (array of role/content message hashes).
-
#resolution_graph ⇒ Hash?
readonly
Optional dependency resolution graph for composed prompts.
-
#tags ⇒ Array<String>
readonly
Tags assigned to this prompt.
-
#version ⇒ Integer
readonly
Prompt version number.
Instance Method Summary collapse
-
#compile(**kwargs) ⇒ Array<Hash>
Compile the chat prompt with variable substitution and message placeholders.
-
#initialize(prompt_data, is_fallback: false) ⇒ ChatPromptClient
constructor
Initialize a new chat prompt client.
-
#type ⇒ String
Prompt type ("chat").
-
#variables ⇒ Array<String>
Return the unique variables referenced by all message templates.
Constructor Details
#initialize(prompt_data, is_fallback: false) ⇒ ChatPromptClient
Initialize a new chat prompt client
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/langfuse/chat_prompt_client.rb', line 58 def initialize(prompt_data, is_fallback: false) validate_prompt_data!(prompt_data) @name = prompt_data["name"] @version = prompt_data["version"] @prompt = prompt_data["prompt"] @labels = prompt_data["labels"] || [] @tags = prompt_data["tags"] || [] @config = prompt_data["config"] || {} @commit_message = prompt_data["commitMessage"] @resolution_graph = prompt_data["resolutionGraph"] @is_fallback = is_fallback end |
Instance Attribute Details
#commit_message ⇒ String? (readonly)
Returns Optional commit message for this prompt version.
45 46 47 |
# File 'lib/langfuse/chat_prompt_client.rb', line 45 def @commit_message end |
#config ⇒ Hash (readonly)
Returns Prompt configuration.
42 43 44 |
# File 'lib/langfuse/chat_prompt_client.rb', line 42 def config @config end |
#is_fallback ⇒ Boolean (readonly)
Returns Whether this client uses caller-provided fallback content.
51 52 53 |
# File 'lib/langfuse/chat_prompt_client.rb', line 51 def is_fallback @is_fallback end |
#labels ⇒ Array<String> (readonly)
Returns Labels assigned to this prompt.
36 37 38 |
# File 'lib/langfuse/chat_prompt_client.rb', line 36 def labels @labels end |
#name ⇒ String (readonly)
Returns Prompt name.
27 28 29 |
# File 'lib/langfuse/chat_prompt_client.rb', line 27 def name @name end |
#prompt ⇒ Array<Hash> (readonly)
Returns Raw prompt template (array of role/content message hashes).
33 34 35 |
# File 'lib/langfuse/chat_prompt_client.rb', line 33 def prompt @prompt end |
#resolution_graph ⇒ Hash? (readonly)
Returns Optional dependency resolution graph for composed prompts.
48 49 50 |
# File 'lib/langfuse/chat_prompt_client.rb', line 48 def resolution_graph @resolution_graph end |
#tags ⇒ Array<String> (readonly)
Returns Tags assigned to this prompt.
39 40 41 |
# File 'lib/langfuse/chat_prompt_client.rb', line 39 def @tags end |
#version ⇒ Integer (readonly)
Returns Prompt version number.
30 31 32 |
# File 'lib/langfuse/chat_prompt_client.rb', line 30 def version @version end |
Instance Method Details
#compile(**kwargs) ⇒ Array<Hash>
Compile the chat prompt with variable substitution and message placeholders
Returns an array of message hashes with roles and compiled content. Placeholder entries are resolved from keyword arguments: arrays are expanded, empty arrays are skipped, unresolved placeholders stay in the output, and malformed values raise before invalid messages are sent to an LLM provider.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/langfuse/chat_prompt_client.rb', line 111 def compile(**kwargs) unresolved = [] compiled = [] prompt.each do || normalized = symbolize_keys() if normalized[:type].to_s == PLACEHOLDER_TYPE append_placeholder(normalized, kwargs, compiled, unresolved) else compiled << (normalized, kwargs) end end warn_unresolved(unresolved) compiled end |
#type ⇒ String
Returns Prompt type ("chat").
73 74 75 |
# File 'lib/langfuse/chat_prompt_client.rb', line 73 def type "chat" end |
#variables ⇒ Array<String>
Return the unique variables referenced by all message templates
Section names are included because callers must provide their values. Message placeholder entries are not Mustache templates and are excluded.
84 85 86 87 88 89 90 91 |
# File 'lib/langfuse/chat_prompt_client.rb', line 84 def variables prompt.each_with_object([]) do |, names| normalized = symbolize_keys() next if normalized[:type].to_s == PLACEHOLDER_TYPE names.concat(PromptVariables.extract(normalized[:content] || "")) end.uniq end |