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



29
30
31
32
33
34
35
36
37
38
# File 'lib/langfuse/chat_prompt_client.rb', line 29

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

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#labelsObject (readonly)

Returns the value of attribute labels.



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

def labels
  @labels
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#promptObject (readonly)

Returns the value of attribute prompt.



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

def prompt
  @prompt
end

#tagsObject (readonly)

Returns the value of attribute tags.



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

def tags
  @tags
end

#versionObject (readonly)

Returns the value of attribute version.



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

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



55
56
57
58
59
# File 'lib/langfuse/chat_prompt_client.rb', line 55

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