Class: Broadlistening::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/broadlistening/config.rb

Constant Summary collapse

DEFAULT_CLUSTER_NUMS =
[ 5, 15 ].freeze
DEFAULT_WORKERS =
10
DEFAULT_AZURE_API_VERSION =
"2024-02-15-preview"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Returns a new instance of Config.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/broadlistening/config.rb', line 53

def initialize(options = {})
  @local_llm_address = options[:local_llm_address] || ENV.fetch("LOCAL_LLM_ADDRESS", "localhost:11434")
  @provider_obj = Provider.new(
    options[:provider]&.to_sym || :openai,
    local_llm_address: @local_llm_address
  )
  @provider = @provider_obj.name
  @model = options[:model] || @provider_obj.default_model
  @embedding_model = options[:embedding_model] || @provider_obj.default_embedding_model
  @cluster_nums = options[:cluster_nums] || DEFAULT_CLUSTER_NUMS.dup
  @workers = options[:workers] || DEFAULT_WORKERS
  @prompts = default_prompts.merge(options[:prompts] || {})
  @api_key = options[:api_key] || @provider_obj.api_key
  @enable_source_link = options.fetch(:enable_source_link, false)
  @hidden_properties = options.fetch(:hidden_properties, {}) || {}
  @is_pubcom = options.fetch(:is_pubcom, false)
  @api_base_url = options[:api_base_url] || @provider_obj.base_url
  @azure_api_version = options[:azure_api_version] || ENV.fetch("AZURE_API_VERSION", DEFAULT_AZURE_API_VERSION)
  @input = options[:input]
  @question = options[:question]
  @name = options[:name]
  @intro = options[:intro]

  validate!
end

Instance Attribute Details

#api_base_urlObject (readonly)

Returns the value of attribute api_base_url.



7
8
9
# File 'lib/broadlistening/config.rb', line 7

def api_base_url
  @api_base_url
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



7
8
9
# File 'lib/broadlistening/config.rb', line 7

def api_key
  @api_key
end

#azure_api_versionObject (readonly)

Returns the value of attribute azure_api_version.



7
8
9
# File 'lib/broadlistening/config.rb', line 7

def azure_api_version
  @azure_api_version
end

#cluster_numsObject (readonly)

Returns the value of attribute cluster_nums.



7
8
9
# File 'lib/broadlistening/config.rb', line 7

def cluster_nums
  @cluster_nums
end

#embedding_modelObject (readonly)

Returns the value of attribute embedding_model.



7
8
9
# File 'lib/broadlistening/config.rb', line 7

def embedding_model
  @embedding_model
end

Returns the value of attribute enable_source_link.



7
8
9
# File 'lib/broadlistening/config.rb', line 7

def enable_source_link
  @enable_source_link
end

#hidden_propertiesObject (readonly)

Returns the value of attribute hidden_properties.



7
8
9
# File 'lib/broadlistening/config.rb', line 7

def hidden_properties
  @hidden_properties
end

#inputObject (readonly)

Returns the value of attribute input.



7
8
9
# File 'lib/broadlistening/config.rb', line 7

def input
  @input
end

#introObject (readonly)

Returns the value of attribute intro.



7
8
9
# File 'lib/broadlistening/config.rb', line 7

def intro
  @intro
end

#is_pubcomObject (readonly)

Returns the value of attribute is_pubcom.



7
8
9
# File 'lib/broadlistening/config.rb', line 7

def is_pubcom
  @is_pubcom
end

#local_llm_addressObject (readonly)

Returns the value of attribute local_llm_address.



7
8
9
# File 'lib/broadlistening/config.rb', line 7

def local_llm_address
  @local_llm_address
end

#modelObject (readonly)

Returns the value of attribute model.



7
8
9
# File 'lib/broadlistening/config.rb', line 7

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/broadlistening/config.rb', line 7

def name
  @name
end

#promptsObject (readonly)

Returns the value of attribute prompts.



7
8
9
# File 'lib/broadlistening/config.rb', line 7

def prompts
  @prompts
end

#providerObject (readonly)

Returns the value of attribute provider.



7
8
9
# File 'lib/broadlistening/config.rb', line 7

def provider
  @provider
end

#questionObject (readonly)

Returns the value of attribute question.



7
8
9
# File 'lib/broadlistening/config.rb', line 7

def question
  @question
end

#workersObject (readonly)

Returns the value of attribute workers.



7
8
9
# File 'lib/broadlistening/config.rb', line 7

def workers
  @workers
end

Class Method Details

.from_file(path) ⇒ Object



49
50
51
# File 'lib/broadlistening/config.rb', line 49

def self.from_file(path)
  from_json(File.read(path))
end

.from_hash(hash) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/broadlistening/config.rb', line 21

def self.from_hash(hash)
  prompts = hash[:prompts]&.transform_keys(&:to_sym)

  cluster_nums = hash[:cluster_nums] || hash.dig(:hierarchical_clustering, :cluster_nums)
  workers = hash[:workers] || hash.dig(:extraction, :workers)
  hidden_properties = hash[:hidden_properties] || hash.dig(:aggregation, :hidden_properties)

  new(
    api_key: hash[:api_key],
    model: hash[:model],
    embedding_model: hash[:embedding_model],
    provider: hash[:provider],
    cluster_nums: cluster_nums,
    workers: workers,
    prompts: prompts,
    enable_source_link: hash[:enable_source_link],
    hidden_properties: hidden_properties,
    is_pubcom: hash[:is_pubcom],
    api_base_url: hash[:api_base_url],
    local_llm_address: hash[:local_llm_address],
    azure_api_version: hash[:azure_api_version],
    input: hash[:input],
    question: hash[:question],
    name: hash[:name],
    intro: hash[:intro]
  )
end

.from_json(json_string) ⇒ Object



16
17
18
19
# File 'lib/broadlistening/config.rb', line 16

def self.from_json(json_string)
  data = JSON.parse(json_string, symbolize_names: true)
  from_hash(data)
end

Instance Method Details

#property_namesObject



107
108
109
# File 'lib/broadlistening/config.rb', line 107

def property_names
  hidden_properties.keys
end

#save_to_file(path) ⇒ Object



103
104
105
# File 'lib/broadlistening/config.rb', line 103

def save_to_file(path)
  File.write(path, JSON.pretty_generate(to_h.merge(prompts: prompts)))
end

#to_hObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/broadlistening/config.rb', line 79

def to_h
  {
    model: model,
    embedding_model: embedding_model,
    provider: provider,
    cluster_nums: cluster_nums,
    workers: workers,
    enable_source_link: enable_source_link,
    hidden_properties: hidden_properties,
    is_pubcom: is_pubcom,
    api_base_url: api_base_url,
    local_llm_address: local_llm_address,
    azure_api_version: azure_api_version,
    input: input,
    question: question,
    name: name,
    intro: intro
  }.compact
end

#to_json(*args) ⇒ Object



99
100
101
# File 'lib/broadlistening/config.rb', line 99

def to_json(*args)
  to_h.merge(prompts: prompts).to_json(*args)
end