chore: Update LLM formatter classes to include additional details (#11491)
This PR introduces support for optionally exposing more data during LLM function calls. This will be useful as we expand Copilot’s capabilities. Changes included: - Add support for ArticleLlmFormatter - Add missing specs for ContactLLMFormatter and ArticleLLMFormatter - Add additional spec for ConversationLLMFormatter based on config
This commit is contained in:
@@ -33,6 +33,7 @@
|
||||
#
|
||||
class Article < ApplicationRecord
|
||||
include PgSearch::Model
|
||||
include LlmFormattable
|
||||
|
||||
has_many :associated_articles,
|
||||
class_name: :Article,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
module LlmFormattable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
def to_llm_text
|
||||
LlmFormatter::LlmTextFormatterService.new(self).format
|
||||
def to_llm_text(config = {})
|
||||
LlmFormatter::LlmTextFormatterService.new(self).format(config)
|
||||
end
|
||||
end
|
||||
|
||||
22
app/services/llm_formatter/article_llm_formatter.rb
Normal file
22
app/services/llm_formatter/article_llm_formatter.rb
Normal file
@@ -0,0 +1,22 @@
|
||||
class LlmFormatter::ArticleLlmFormatter
|
||||
attr_reader :article
|
||||
|
||||
def initialize(article)
|
||||
@article = article
|
||||
end
|
||||
|
||||
def format(*)
|
||||
<<~TEXT
|
||||
Title: #{article.title}
|
||||
ID: #{article.id}
|
||||
Status: #{article.status}
|
||||
Category: #{article.category&.name || 'Uncategorized'}
|
||||
Author: #{article.author&.name || 'Unknown'}
|
||||
Views: #{article.views}
|
||||
Created At: #{article.created_at}
|
||||
Updated At: #{article.updated_at}
|
||||
Content:
|
||||
#{article.content}
|
||||
TEXT
|
||||
end
|
||||
end
|
||||
@@ -1,5 +1,5 @@
|
||||
class LlmFormatter::ContactLlmFormatter < LlmFormatter::DefaultLlmFormatter
|
||||
def format
|
||||
def format(*)
|
||||
sections = []
|
||||
sections << "Contact ID: ##{@record.id}"
|
||||
sections << 'Contact Attributes:'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
class LlmFormatter::ConversationLlmFormatter < LlmFormatter::DefaultLlmFormatter
|
||||
def format
|
||||
def format(config = {})
|
||||
sections = []
|
||||
sections << "Conversation ID: ##{@record.display_id}"
|
||||
sections << "Channel: #{@record.inbox.channel.name}"
|
||||
@@ -10,6 +10,7 @@ class LlmFormatter::ConversationLlmFormatter < LlmFormatter::DefaultLlmFormatter
|
||||
'No messages in this conversation'
|
||||
end
|
||||
|
||||
sections << "Contact Details: #{@record.contact.to_llm_text}" if config[:include_contact_details]
|
||||
sections.join("\n")
|
||||
end
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ class LlmFormatter::DefaultLlmFormatter
|
||||
@record = record
|
||||
end
|
||||
|
||||
def format
|
||||
def format(*)
|
||||
# override this
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,9 +3,9 @@ class LlmFormatter::LlmTextFormatterService
|
||||
@record = record
|
||||
end
|
||||
|
||||
def format
|
||||
def format(config = {})
|
||||
formatter_class = find_formatter
|
||||
formatter_class.new(@record).format
|
||||
formatter_class.new(@record).format(config)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
Reference in New Issue
Block a user