Class: Langfuse::TextPromptClient

Inherits:
Object
  • Object
show all
Defined in:
lib/langfuse/text_prompt_client.rb

Overview

Text prompt client for compiling text prompts with variable substitution

Handles text-based prompts from Langfuse, providing Mustache templating for variable substitution.

Examples:

Basic usage

prompt_data = api_client.get_prompt("greeting")
text_prompt = Langfuse::TextPromptClient.new(prompt_data)
text_prompt.compile(name: "Alice")
# => "Hello Alice!"

Accessing metadata

text_prompt.name      # => "greeting"
text_prompt.version   # => 1
text_prompt.labels    # => ["production"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prompt_data, is_fallback: false) ⇒ TextPromptClient

Initialize a new text 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



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

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



43
44
45
# File 'lib/langfuse/text_prompt_client.rb', line 43

def commit_message
  @commit_message
end

#configHash (readonly)

Returns Prompt configuration.

Returns:

  • (Hash)

    Prompt configuration



37
38
39
# File 'lib/langfuse/text_prompt_client.rb', line 37

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



49
50
51
# File 'lib/langfuse/text_prompt_client.rb', line 49

def is_fallback
  @is_fallback
end

#labelsArray<String> (readonly)

Returns Labels assigned to this prompt.

Returns:

  • (Array<String>)

    Labels assigned to this prompt



31
32
33
# File 'lib/langfuse/text_prompt_client.rb', line 31

def labels
  @labels
end

#nameString (readonly)

Returns Prompt name.

Returns:

  • (String)

    Prompt name



25
26
27
# File 'lib/langfuse/text_prompt_client.rb', line 25

def name
  @name
end

#promptString (readonly)

Returns Raw prompt template.

Returns:

  • (String)

    Raw prompt template



40
41
42
# File 'lib/langfuse/text_prompt_client.rb', line 40

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



46
47
48
# File 'lib/langfuse/text_prompt_client.rb', line 46

def resolution_graph
  @resolution_graph
end

#tagsArray<String> (readonly)

Returns Tags assigned to this prompt.

Returns:

  • (Array<String>)

    Tags assigned to this prompt



34
35
36
# File 'lib/langfuse/text_prompt_client.rb', line 34

def tags
  @tags
end

#versionInteger (readonly)

Returns Prompt version number.

Returns:

  • (Integer)

    Prompt version number



28
29
30
# File 'lib/langfuse/text_prompt_client.rb', line 28

def version
  @version
end

Instance Method Details

#compile(**kwargs) ⇒ String

Compile the prompt with variable substitution

Examples:

text_prompt.compile(name: "Alice", greeting: "Hi")
# => "Hi Alice! Welcome."

Parameters:

  • kwargs (Hash)

    Variables to substitute in the template (as keyword arguments)

Returns:

  • (String)

    The compiled prompt text

Raises:

  • (ArgumentError)

    if variables cannot be rendered



95
96
97
98
99
# File 'lib/langfuse/text_prompt_client.rb', line 95

def compile(**kwargs)
  return prompt if kwargs.empty?

  PromptRenderer.render(prompt, kwargs)
end

#typeString

Returns Prompt type ("text").

Returns:

  • (String)

    Prompt type ("text")



71
72
73
# File 'lib/langfuse/text_prompt_client.rb', line 71

def type
  "text"
end

#variablesArray<String>

Return the unique variables referenced by the prompt template

Section names are included because callers must provide their values. Variables inside sections include the full section path.

Returns:

  • (Array<String>)

    Referenced variable names in source order

Raises:

  • (Mustache::Parser::SyntaxError)

    if the prompt contains invalid Mustache syntax



82
83
84
# File 'lib/langfuse/text_prompt_client.rb', line 82

def variables
  PromptVariables.extract(prompt)
end