-
Notifications
You must be signed in to change notification settings - Fork 94
Fetch assistant prompt configuration from Langfuse #177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,8 +6,11 @@ | |
| preferred_currency = Money::Currency.new(chat.user.family.currency) | ||
| preferred_date_format = chat.user.family.date_format | ||
|
|
||
| instructions_config = default_instructions(preferred_currency, preferred_date_format) | ||
|
|
||
| { | ||
| instructions: default_instructions(preferred_currency, preferred_date_format), | ||
| instructions: instructions_config[:content], | ||
| instructions_prompt: instructions_config[:prompt], | ||
| functions: default_functions | ||
| } | ||
| end | ||
|
|
@@ -23,6 +26,60 @@ | |
| end | ||
|
|
||
| def default_instructions(preferred_currency, preferred_date_format) | ||
| langfuse_instructions = langfuse_default_instructions(preferred_currency, preferred_date_format) | ||
|
|
||
| if langfuse_instructions.present? | ||
| { | ||
| content: langfuse_instructions[:content], | ||
| prompt: langfuse_instructions | ||
| } | ||
| else | ||
| { | ||
| content: fallback_default_instructions(preferred_currency, preferred_date_format), | ||
| prompt: nil | ||
| } | ||
| end | ||
| end | ||
|
|
||
| def langfuse_default_instructions(preferred_currency, preferred_date_format) | ||
| return unless langfuse_client | ||
|
|
||
| prompt = langfuse_client.get_prompt("default_instructions") | ||
|
|
||
| compiled_prompt = prompt.compile( | ||
| preferred_currency_symbol: preferred_currency.symbol, | ||
| preferred_currency_iso_code: preferred_currency.iso_code, | ||
| preferred_currency_default_precision: preferred_currency.default_precision, | ||
| preferred_currency_default_format: preferred_currency.default_format, | ||
| preferred_currency_separator: preferred_currency.separator, | ||
| preferred_currency_delimiter: preferred_currency.delimiter, | ||
| preferred_date_format: preferred_date_format, | ||
| current_date: Date.current | ||
| ) | ||
|
|
||
| content = case compiled_prompt | ||
| when String | ||
| compiled_prompt | ||
| when Array | ||
| compiled_prompt.filter_map { |message| message[:content] }.join("\n\n") | ||
| else | ||
| nil | ||
| end | ||
|
|
||
| return if content.blank? | ||
|
|
||
| { | ||
| name: prompt.name, | ||
| version: prompt.version, | ||
| template: prompt.prompt, | ||
| content: content | ||
| } | ||
| rescue => e | ||
| Rails.logger.warn("Langfuse prompt retrieval failed: #{e.message}") | ||
| nil | ||
| end | ||
|
|
||
| def fallback_default_instructions(preferred_currency, preferred_date_format) | ||
| <<~PROMPT | ||
| ## Your identity | ||
|
|
@@ -78,5 +135,14 @@ | |
| the data you're presenting represents and what context it is in (i.e. date range, account, etc.) | ||
| PROMPT | ||
| end | ||
|
|
||
| def langfuse_client | ||
| return unless ENV["LANGFUSE_PUBLIC_KEY"].present? && ENV["LANGFUSE_SECRET_KEY"].present? | ||
|
|
||
| @langfuse_client ||= Langfuse.new | ||
| rescue => e | ||
| Rails.logger.warn("Langfuse client initialization failed: #{e.message}") | ||
| nil | ||
| end | ||
|
Comment on lines
+183
to
+190
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Extract duplicated This method is nearly identical to Create a new concern at module LangfuseClientConcern
extend ActiveSupport::Concern
class_methods do
def langfuse_client
return unless ENV["LANGFUSE_PUBLIC_KEY"].present? && ENV["LANGFUSE_SECRET_KEY"].present?
@langfuse_client ||= Langfuse.new
rescue => e
Rails.logger.warn("Langfuse client initialization failed: #{e.message}")
nil
end
end
endThen include it in both module Assistant::Configurable
extend ActiveSupport::Concern
+ include LangfuseClientConcern
class_methods do
# ...
-
- def langfuse_client
- return unless ENV["LANGFUSE_PUBLIC_KEY"].present? && ENV["LANGFUSE_SECRET_KEY"].present?
-
- @langfuse_client ||= Langfuse.new
- rescue => e
- Rails.logger.warn("Langfuse client initialization failed: #{e.message}")
- nil
- end
end
endAs per coding guidelines. 🤖 Prompt for AI Agents |
||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -62,6 +62,7 @@ | |||||
| prompt, | ||||||
| model:, | ||||||
| instructions: nil, | ||||||
| instructions_prompt: nil, | ||||||
| functions: [], | ||||||
| function_results: [], | ||||||
| streamer: nil, | ||||||
|
|
@@ -112,6 +113,7 @@ | |||||
| model: model, | ||||||
| input: input_payload, | ||||||
| output: response.messages.map(&:output_text).join("\n"), | ||||||
| prompt: instructions_prompt, | ||||||
| session_id: session_id, | ||||||
| user_identifier: user_identifier | ||||||
| ) | ||||||
|
|
@@ -123,6 +125,7 @@ | |||||
| model: model, | ||||||
| input: input_payload, | ||||||
| output: parsed.messages.map(&:output_text).join("\n"), | ||||||
| prompt: instructions_prompt, | ||||||
| usage: raw_response["usage"], | ||||||
| session_id: session_id, | ||||||
| user_identifier: user_identifier | ||||||
|
|
@@ -141,26 +144,41 @@ | |||||
| @langfuse_client = Langfuse.new | ||||||
| end | ||||||
|
|
||||||
| def log_langfuse_generation(name:, model:, input:, output:, usage: nil, session_id: nil, user_identifier: nil) | ||||||
| return unless langfuse_client | ||||||
|
|
||||||
| trace = langfuse_client.trace( | ||||||
| name: "openai.#{name}", | ||||||
| input: input, | ||||||
| session_id: session_id, | ||||||
| user_id: user_identifier | ||||||
| ) | ||||||
| trace.generation( | ||||||
| name: name, | ||||||
| model: model, | ||||||
| input: input, | ||||||
| output: output, | ||||||
| usage: usage, | ||||||
| session_id: session_id, | ||||||
| user_id: user_identifier | ||||||
| ) | ||||||
| trace.update(output: output) | ||||||
| rescue => e | ||||||
| Rails.logger.warn("Langfuse logging failed: #{e.message}") | ||||||
| def log_langfuse_generation(name:, model:, input:, output:, usage: nil, session_id: nil, user_identifier: nil, prompt: nil) | ||||||
|
||||||
| def log_langfuse_generation(name:, model:, input:, output:, usage: nil, session_id: nil, user_identifier: nil, prompt: nil) | |
| def log_langfuse_generation(name:, model:, input:, output:, usage: nil, session_id: nil, user_identifier: nil, prompt: nil) |
🧰 Tools
🪛 GitHub Check: ci / lint
[failure] 147-147:
Layout/IndentationConsistency: Inconsistent indentation detected.
🤖 Prompt for AI Agents
In app/models/provider/openai.rb around line 147, the method definition `def
log_langfuse_generation...` is indented inconsistently; reformat the line to
follow Ruby style (2-space indentation) so it aligns with the other method
definitions in the class/module scope — move the `def` left to the same
indentation level as neighboring methods and ensure the parameter list and
closing `end` remain aligned accordingly.
Uh oh!
There was an error while loading. Please reload this page.