Class: OxAiWorkers::Tool::Pipeline

Inherits:
Object
  • Object
show all
Includes:
DependencyHelper, LoadI18n, OxAiWorkers::ToolDefinition
Defined in:
lib/oxaiworkers/tool/pipeline.rb

Instance Attribute Summary collapse

Attributes included from LoadI18n

#locale

Attributes included from OxAiWorkers::ToolDefinition

#white_list

Instance Method Summary collapse

Methods included from LoadI18n

#store_locale, #with_locale

Methods included from DependencyHelper

#depends_on

Methods included from OxAiWorkers::ToolDefinition

#define_function, #full_function_name, #function_schemas, #init_white_list_with, #tool_name

Constructor Details

#initialize(only: nil, on_message: nil) ⇒ Pipeline

Returns a new instance of Pipeline.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/oxaiworkers/tool/pipeline.rb', line 10

def initialize(only: nil, on_message: nil)
  store_locale

  init_white_list_with only

  define_function :send_message, description: I18n.t('oxaiworkers.tool.pipeline.send_message.description') do
    property :message, type: 'string', description: I18n.t('oxaiworkers.tool.pipeline.send_message.message'),
                       required: true
    property :result, type: 'string', description: I18n.t('oxaiworkers.tool.pipeline.send_message.result'),
                      required: true
    property :example, type: 'string', description: I18n.t('oxaiworkers.tool.pipeline.send_message.example'),
                       required: false
    property :to_id, type: 'string', description: I18n.t('oxaiworkers.tool.pipeline.send_message.to_id'),
                     required: true
  end

  @messages = []
  @on_message = on_message
end

Instance Attribute Details

#assistantsObject

Returns the value of attribute assistants.



8
9
10
# File 'lib/oxaiworkers/tool/pipeline.rb', line 8

def assistants
  @assistants
end

#messagesObject

Returns the value of attribute messages.



8
9
10
# File 'lib/oxaiworkers/tool/pipeline.rb', line 8

def messages
  @messages
end

Instance Method Details

#add_assistant(assistant) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/oxaiworkers/tool/pipeline.rb', line 52

def add_assistant(assistant)
  unless assistant.id.present? &&
         assistant.title.present? &&
         assistant.role.present? &&
         assistant.description.present? &&
         assistant.capabilities.present?
    raise 'Assistant is not valid: id, title, role, description and capabilities are required'
  end

  assistant.iterator.on_inner_monologue = ->(text:) { recive_monologue(from_id: assistant.id, message: text) }
  assistant.iterator.on_outer_voice = ->(text:) { recive_voice(from_id: assistant.id, message: text) }

  @assistants ||= {}
  @assistants[assistant.id] = assistant
end

#assistants_infoObject



96
97
98
# File 'lib/oxaiworkers/tool/pipeline.rb', line 96

def assistants_info
  @assistants.values.map { |assistant| format_assistant(assistant) }.join("\n\n")
end

#contextObject



92
93
94
# File 'lib/oxaiworkers/tool/pipeline.rb', line 92

def context
  "#{assistants_info}\n\n#{@messages.map { |m| format_message(m) }.join("\n\n")}"
end

#context_for(id) ⇒ Object



87
88
89
90
# File 'lib/oxaiworkers/tool/pipeline.rb', line 87

def context_for(id)
  array = @messages.select { |message| message[:id] == id || message[:type] == 'voice' }
  array.map { |m| format_message(m) }.join("\n\n")
end

#format_assistant(assistant) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/oxaiworkers/tool/pipeline.rb', line 104

def format_assistant(assistant)
  with_locale do
    I18n.t('oxaiworkers.tool.pipeline.assistant_info',
           id: assistant.id,
           title: assistant.title,
           role: assistant.role,
           description: assistant.description,
           capabilities: assistant.capabilities.join(', '))
  end
end

#format_message(message) ⇒ Object



100
101
102
# File 'lib/oxaiworkers/tool/pipeline.rb', line 100

def format_message(message)
  "**#{message[:id]}**\n: #{message[:message]}"
end

#recive_message(id:, type:, message:, color: :grey) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/oxaiworkers/tool/pipeline.rb', line 76

def recive_message(id:, type:, message:, color: :grey)
  puts "#{id} [#{type}]: #{message}".colorize(color)
  @messages ||= []
  m = { id:, type:, message: }
  @messages << m
  @on_message.call(text: format_message(m)) if !@on_message.nil? && @assistants.key?(id)
  # @assistants.each_value do |assistant|
  #   assistant.iterator.add_queue format_message(m), role: :system if assistant.id != id
  # end
end

#recive_monologue(from_id:, message:) ⇒ Object



68
69
70
# File 'lib/oxaiworkers/tool/pipeline.rb', line 68

def recive_monologue(from_id:, message:)
  recive_message(id: from_id, type: 'monologue', message:, color: :yellow)
end

#recive_voice(from_id:, message:) ⇒ Object



72
73
74
# File 'lib/oxaiworkers/tool/pipeline.rb', line 72

def recive_voice(from_id:, message:)
  recive_message(id: from_id, type: 'voice', message:, color: :green)
end

#send_message(message:, result:, to_id:, example: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/oxaiworkers/tool/pipeline.rb', line 30

def send_message(message:, result:, to_id:, example: nil)
  puts "send_message to #{to_id}: #{message}".colorize(:red)
  puts " Result: #{result}"
  puts " Example: #{example}"
  if @assistants.key?(to_id)
    context = context_for(to_id)
    @assistants[to_id].replace_context(context)
    @assistants[to_id].add_task message
    with_locale do
      @assistants[to_id].add_task "#{I18n.t('oxaiworkers.tool.pipeline.send_message.result')}: #{result}"
      unless example.nil?
        @assistants[to_id].add_task "#{I18n.t('oxaiworkers.tool.pipeline.send_message.example')}: #{example}"
      end
    end
    @assistants[to_id].execute
    nil
  else
    OxAiWorkers.logger.error "Assistant #{to_id} not found"
    "Assistant #{to_id} not found"
  end
end