@@ -6,8 +6,11 @@ def config_for(chat)
66 preferred_currency = Money ::Currency . new ( chat . user . family . currency )
77 preferred_date_format = chat . user . family . date_format
88
9+ instructions_config = default_instructions ( preferred_currency , preferred_date_format )
10+
911 {
10- instructions : default_instructions ( preferred_currency , preferred_date_format ) ,
12+ instructions : instructions_config [ :content ] ,
13+ instructions_prompt : instructions_config [ :prompt ] ,
1114 functions : default_functions
1215 }
1316 end
@@ -23,6 +26,60 @@ def default_functions
2326 end
2427
2528 def default_instructions ( preferred_currency , preferred_date_format )
29+ langfuse_instructions = langfuse_default_instructions ( preferred_currency , preferred_date_format )
30+
31+ if langfuse_instructions . present?
32+ {
33+ content : langfuse_instructions [ :content ] ,
34+ prompt : langfuse_instructions
35+ }
36+ else
37+ {
38+ content : fallback_default_instructions ( preferred_currency , preferred_date_format ) ,
39+ prompt : nil
40+ }
41+ end
42+ end
43+
44+ def langfuse_default_instructions ( preferred_currency , preferred_date_format )
45+ return unless langfuse_client
46+
47+ prompt = langfuse_client . get_prompt ( "default_instructions" )
48+
49+ compiled_prompt = prompt . compile (
50+ preferred_currency_symbol : preferred_currency . symbol ,
51+ preferred_currency_iso_code : preferred_currency . iso_code ,
52+ preferred_currency_default_precision : preferred_currency . default_precision ,
53+ preferred_currency_default_format : preferred_currency . default_format ,
54+ preferred_currency_separator : preferred_currency . separator ,
55+ preferred_currency_delimiter : preferred_currency . delimiter ,
56+ preferred_date_format : preferred_date_format ,
57+ current_date : Date . current
58+ )
59+
60+ content = case compiled_prompt
61+ when String
62+ compiled_prompt
63+ when Array
64+ compiled_prompt . filter_map { |message | message [ :content ] } . join ( "\n \n " )
65+ else
66+ nil
67+ end
68+
69+ return if content . blank?
70+
71+ {
72+ name : prompt . name ,
73+ version : prompt . version ,
74+ template : prompt . prompt ,
75+ content : content
76+ }
77+ rescue => e
78+ Rails . logger . warn ( "Langfuse prompt retrieval failed: #{ e . message } " )
79+ nil
80+ end
81+
82+ def fallback_default_instructions ( preferred_currency , preferred_date_format )
2683 <<~PROMPT
2784 ## Your identity
2885
@@ -78,5 +135,14 @@ def default_instructions(preferred_currency, preferred_date_format)
78135 the data you're presenting represents and what context it is in (i.e. date range, account, etc.)
79136 PROMPT
80137 end
138+
139+ def langfuse_client
140+ return unless ENV [ "LANGFUSE_PUBLIC_KEY" ] . present? && ENV [ "LANGFUSE_SECRET_KEY" ] . present?
141+
142+ @langfuse_client ||= Langfuse . new
143+ rescue => e
144+ Rails . logger . warn ( "Langfuse client initialization failed: #{ e . message } " )
145+ nil
146+ end
81147 end
82148end
0 commit comments