Class: OxAiWorkers::Tool::Wolfram

Inherits:
Object
  • Object
show all
Includes:
DependencyHelper, LoadI18n, OxAiWorkers::ToolDefinition
Defined in:
lib/oxaiworkers/tool/wolfram.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, access_token: nil, location: nil) ⇒ Wolfram

Returns a new instance of Wolfram.



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

def initialize(only: nil, access_token: nil, location: nil)
  store_locale

  init_white_list_with only

  define_function :ask, description: I18n.t('oxaiworkers.tool.wolfram_alpha.ask.description') do
    property :prompt, type: 'string', description: I18n.t('oxaiworkers.tool.wolfram_alpha.ask.prompt'),
                      required: true
    if location.nil?
      property :location, type: 'string', description: I18n.t('oxaiworkers.tool.wolfram_alpha.ask.location'),
                          required: false
    end
  end

  @access_token = access_token
  @location = location
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



11
12
13
# File 'lib/oxaiworkers/tool/wolfram.rb', line 11

def access_token
  @access_token
end

#locationObject

Returns the value of attribute location.



11
12
13
# File 'lib/oxaiworkers/tool/wolfram.rb', line 11

def location
  @location
end

Instance Method Details

#ask(prompt:, location: nil) ⇒ Object



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

def ask(prompt:, location: nil)
  location ||= @location
  options = { location: }
  api_key = @access_token || OxAiWorkers.configuration.access_token_wolfram
  @wolfram ||= WolframAlpha::Client.new api_key, options.compact
  response = @wolfram.query prompt
  input = response['Input'] # Get the input interpretation pod.
  result = ''
  result = "Input interpretation:\n#{input.subpods.map(&:plaintext).join("\n")}\n\n" unless input.nil?
  result += response.pods.map { |p| [p.title, p.subpods.map(&:plaintext).join("\n")].join(":\n") }.join("\n\n")
  images = []
  %w[Plots Result Image].each do |title|
    plot = response.find { |pod| pod.title == title }
    images += plot.subpods.map { |img| img&.image } unless plot.nil?
  end
  images = images.compact.filter { |i| i[:type] != 'Default' }.uniq
  result += "\n\n" + images.map { |img| "![#{img[:alt]}](\"#{img[:src]}\")" }.join("\n") unless images.empty?
  result
end