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"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prompt_data) ⇒ ChatPromptClient

Initialize a new chat prompt client

Parameters:

  • prompt_data (Hash)

    The prompt data from the API

Raises:

  • (ArgumentError)

    if prompt data is invalid



45
46
47
48
49
50
51
52
53
54
# File 'lib/langfuse/chat_prompt_client.rb', line 45

def initialize(prompt_data)
  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"] || {}
end

Instance Attribute Details

#configHash (readonly)

Returns Prompt configuration.

Returns:

  • (Hash)

    Prompt configuration



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

def config
  @config
end

#labelsArray<String> (readonly)

Returns Labels assigned to this prompt.

Returns:

  • (Array<String>)

    Labels assigned to this prompt



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

def labels
  @labels
end

#nameString (readonly)

Returns Prompt name.

Returns:

  • (String)

    Prompt name



24
25
26
# File 'lib/langfuse/chat_prompt_client.rb', line 24

def name
  @name
end

#promptArray<Hash> (readonly)

Returns Array of message hashes with role and content.

Returns:

  • (Array<Hash>)

    Array of message hashes with role and content



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

def prompt
  @prompt
end

#tagsArray<String> (readonly)

Returns Tags assigned to this prompt.

Returns:

  • (Array<String>)

    Tags assigned to this prompt



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

def tags
  @tags
end

#versionInteger (readonly)

Returns Prompt version number.

Returns:

  • (Integer)

    Prompt version number



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

def version
  @version
end

Instance Method Details

#compile(**kwargs) ⇒ Array<Hash>

Compile the chat prompt with variable substitution

Returns an array of message hashes with roles and compiled content. Each message in the prompt will have its content compiled with the provided variables using Mustache templating.

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 to substitute in message templates (as keyword arguments)

Returns:

  • (Array<Hash>)

    Array of compiled messages with :role and :content keys



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

def compile(**kwargs)
  prompt.map do |message|
    compile_message(message, kwargs)
  end
end