Class: AISpec::Core::Contract

Inherits:
Object
  • Object
show all
Defined in:
lib/aispec/core/contract.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, file_path: nil) ⇒ Contract

Returns a new instance of Contract.



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
48
49
50
51
# File 'lib/aispec/core/contract.rb', line 21

def initialize(hash, file_path: nil)
  @raw_hash = hash
  @file_path = file_path
  @name = hash[:name] || (file_path ? File.basename(file_path, ".*") : "unnamed-contract")
  
  model_config = hash[:model] || {}
  if model_config.is_a?(Hash)
    @provider_name = (model_config[:provider] || AISpec.configuration.default_provider).to_s
    @model = (model_config[:model] || AISpec.configuration.default_model).to_s
  else
    @provider_name = AISpec.configuration.default_provider.to_s
    @model = model_config.to_s
  end

  @prompt_template = hash[:prompt] || "{{input}}"
  
  dataset_config = hash[:dataset]
  if dataset_config.is_a?(Hash)
    @dataset_path = dataset_config[:path]
    @dataset_items = dataset_config[:items] || []
  elsif dataset_config.is_a?(Array)
    @dataset_items = dataset_config
  else
    @dataset_items = []
  end

  load_external_dataset! if @dataset_path && @file_path

  raw_contracts = hash[:contracts] || hash[:assertions] || []
  @assertions = parse_assertions(raw_contracts)
end

Instance Attribute Details

#assertionsObject (readonly)

Returns the value of attribute assertions.



8
9
10
# File 'lib/aispec/core/contract.rb', line 8

def assertions
  @assertions
end

#dataset_itemsObject (readonly)

Returns the value of attribute dataset_items.



8
9
10
# File 'lib/aispec/core/contract.rb', line 8

def dataset_items
  @dataset_items
end

#dataset_pathObject (readonly)

Returns the value of attribute dataset_path.



8
9
10
# File 'lib/aispec/core/contract.rb', line 8

def dataset_path
  @dataset_path
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



8
9
10
# File 'lib/aispec/core/contract.rb', line 8

def file_path
  @file_path
end

#modelObject (readonly)

Returns the value of attribute model.



8
9
10
# File 'lib/aispec/core/contract.rb', line 8

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/aispec/core/contract.rb', line 8

def name
  @name
end

#prompt_templateObject (readonly)

Returns the value of attribute prompt_template.



8
9
10
# File 'lib/aispec/core/contract.rb', line 8

def prompt_template
  @prompt_template
end

#provider_nameObject (readonly)

Returns the value of attribute provider_name.



8
9
10
# File 'lib/aispec/core/contract.rb', line 8

def provider_name
  @provider_name
end

#raw_hashObject (readonly)

Returns the value of attribute raw_hash.



8
9
10
# File 'lib/aispec/core/contract.rb', line 8

def raw_hash
  @raw_hash
end

Class Method Details

.from_hash(hash) ⇒ Object



17
18
19
# File 'lib/aispec/core/contract.rb', line 17

def self.from_hash(hash)
  new(hash)
end

.load_file(path) ⇒ Object

Raises:

  • (ArgumentError)


10
11
12
13
14
15
# File 'lib/aispec/core/contract.rb', line 10

def self.load_file(path)
  raise ArgumentError, "Contract file not found: #{path}" unless File.exist?(path)

  hash = YAML.safe_load(File.read(path), symbolize_names: true, aliases: true) || {}
  new(hash, file_path: path)
end