Class: Langfuse::ChatPromptClient

Inherits:
Object
  • Object
show all
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.

Examples:

Basic usage

prompt_data = api_client.get_prompt("support_chat")
chat_prompt = Langfuse::ChatPromptClient.new(prompt_data)
chat_prompt.compile(user_name: "Alice", issue: "login")
# => [{ role: "system", content: "You are a support agent..." }, ...]

Accessing metadata

chat_prompt.name      # => "support_chat"
chat_prompt.version   # => 1
chat_prompt.labels    # => ["production"]

Constant Summary collapse

PLACEHOLDER_TYPE =
"placeholder"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prompt_data, is_fallback: false) ⇒ ChatPromptClient

Initialize a new chat prompt client

Parameters:

  • prompt_data (Hash)

    The prompt data from the API

  • is_fallback (Boolean) (defaults to: false)

    Whether this client wraps caller-provided fallback content

Raises:

  • (ArgumentError)

    if prompt data is invalid



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_messageString? (readonly)

Returns Optional commit message for this prompt version.

Returns:

  • (String, nil)

    Optional commit message for this prompt version



45
46
47
# File 'lib/langfuse/chat_prompt_client.rb', line 45

def commit_message
  @commit_message
end

#configHash (readonly)

Returns Prompt configuration.

Returns:

  • (Hash)

    Prompt configuration



42
43
44
# File 'lib/langfuse/chat_prompt_client.rb', line 42

def config
  @config
end

#is_fallbackBoolean (readonly)

Returns Whether this client uses caller-provided fallback content.

Returns:

  • (Boolean)

    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

#labelsArray<String> (readonly)

Returns Labels assigned to this prompt.

Returns:

  • (Array<String>)

    Labels assigned to this prompt



36
37
38
# File 'lib/langfuse/chat_prompt_client.rb', line 36

def labels
  @labels
end

#nameString (readonly)

Returns Prompt name.

Returns:

  • (String)

    Prompt name



27
28
29
# File 'lib/langfuse/chat_prompt_client.rb', line 27

def name
  @name
end

#promptArray<Hash> (readonly)

Returns Raw prompt template (array of role/content message hashes).

Returns:

  • (Array<Hash>)

    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_graphHash? (readonly)

Returns Optional dependency resolution graph for composed prompts.

Returns:

  • (Hash, nil)

    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

#tagsArray<String> (readonly)

Returns Tags assigned to this prompt.

Returns:

  • (Array<String>)

    Tags assigned to this prompt



39
40
41
# File 'lib/langfuse/chat_prompt_client.rb', line 39

def tags
  @tags
end

#versionInteger (readonly)

Returns Prompt version number.

Returns:

  • (Integer)

    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.

Examples:

chat_prompt.compile(name: "Alice", topic: "Ruby")
# => [
#   { role: :system, content: "You are a helpful assistant." },
#   { role: :user, content: "Hello Alice, let's discuss Ruby!" }
# ]

Parameters:

  • kwargs (Hash)

    Variables and placeholder values to compile

Returns:

  • (Array<Hash>)

    Array of compiled messages and unresolved placeholders

Raises:

  • (ArgumentError)

    if a placeholder value is malformed



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 |message|
    normalized = symbolize_keys(message)
    if normalized[:type].to_s == PLACEHOLDER_TYPE
      append_placeholder(normalized, kwargs, compiled, unresolved)
    else
      compiled << compile_message(normalized, kwargs)
    end
  end
  warn_unresolved(unresolved)
  compiled
end

#typeString

Returns Prompt type ("chat").

Returns:

  • (String)

    Prompt type ("chat")



73
74
75
# File 'lib/langfuse/chat_prompt_client.rb', line 73

def type
  "chat"
end

#variablesArray<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.

Returns:

  • (Array<String>)

    Referenced variable names in message and source order

Raises:

  • (Mustache::Parser::SyntaxError)

    if a message contains invalid Mustache syntax



84
85
86
87
88
89
90
91
# File 'lib/langfuse/chat_prompt_client.rb', line 84

def variables
  prompt.each_with_object([]) do |message, names|
    normalized = symbolize_keys(message)
    next if normalized[:type].to_s == PLACEHOLDER_TYPE

    names.concat(PromptVariables.extract(normalized[:content] || ""))
  end.uniq
end