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(variables: { 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



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/langfuse/chat_prompt_client.rb', line 57

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



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

def commit_message
  @commit_message
end

#configHash (readonly)

Returns Prompt configuration.

Returns:

  • (Hash)

    Prompt configuration



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

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



50
51
52
# File 'lib/langfuse/chat_prompt_client.rb', line 50

def is_fallback
  @is_fallback
end

#labelsArray<String> (readonly)

Returns Labels assigned to this prompt.

Returns:

  • (Array<String>)

    Labels assigned to this prompt



32
33
34
# File 'lib/langfuse/chat_prompt_client.rb', line 32

def labels
  @labels
end

#nameString (readonly)

Returns Prompt name.

Returns:

  • (String)

    Prompt name



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

def name
  @name
end

#promptArray<Hash> (readonly)

Returns Array of message hashes and placeholder entries.

Returns:

  • (Array<Hash>)

    Array of message hashes and placeholder entries



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

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



47
48
49
# File 'lib/langfuse/chat_prompt_client.rb', line 47

def resolution_graph
  @resolution_graph
end

#tagsArray<String> (readonly)

Returns Tags assigned to this prompt.

Returns:

  • (Array<String>)

    Tags assigned to this prompt



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

def tags
  @tags
end

#versionInteger (readonly)

Returns Prompt version number.

Returns:

  • (Integer)

    Prompt version number



29
30
31
# File 'lib/langfuse/chat_prompt_client.rb', line 29

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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/langfuse/chat_prompt_client.rb', line 94

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”)



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

def type
  "chat"
end