Class: Gpt3::Builder::Gpt3Builder

Inherits:
Object
  • Object
show all
Includes:
KLog::Logging
Defined in:
lib/gpt3/builder/gpt3_builder.rb

Overview

GPT3 Builder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGpt3Builder

assigns a builder hash and defines builder methods configuration = nil)



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gpt3/builder/gpt3_builder.rb', line 49

def initialize
  @access_token               = ENV.fetch('OPENAI_ACCESS_TOKEN', nil) # ENV['OPENAI_SECRET_KEY']
  @client                     = OpenAI::Client.new(access_token: access_token)

  @default_default_engine     = 'code-davinci-001'
  @default_max_tokens         = 100
  @default_temperature        = 0
  @default_top_p              = 1
  @default_frequency_penalty  = 0
  @default_presence_penalty   = 0

  @prompt                     = ''

  # @target_folders = configuration.target_folders.clone
  # @template_folders = configuration.template_folders.clone
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



22
23
24
# File 'lib/gpt3/builder/gpt3_builder.rb', line 22

def access_token
  @access_token
end

#clientObject (readonly)

Prompt builder Pre-prompt sanitization (before applying to prompt, you want to remove useless stuff)

- This is essentially a pre-processor or it might be a filter within prompts

Executor (run the prompt and store the results)



16
17
18
# File 'lib/gpt3/builder/gpt3_builder.rb', line 16

def client
  @client
end

#default_engineObject

Returns the value of attribute default_engine.



24
25
26
# File 'lib/gpt3/builder/gpt3_builder.rb', line 24

def default_engine
  @default_engine
end

#default_frequency_penaltyObject

Returns the value of attribute default_frequency_penalty.



28
29
30
# File 'lib/gpt3/builder/gpt3_builder.rb', line 28

def default_frequency_penalty
  @default_frequency_penalty
end

#default_max_tokensObject

Returns the value of attribute default_max_tokens.



25
26
27
# File 'lib/gpt3/builder/gpt3_builder.rb', line 25

def default_max_tokens
  @default_max_tokens
end

#default_presence_penaltyObject

Returns the value of attribute default_presence_penalty.



29
30
31
# File 'lib/gpt3/builder/gpt3_builder.rb', line 29

def default_presence_penalty
  @default_presence_penalty
end

#default_temperatureObject

Returns the value of attribute default_temperature.



26
27
28
# File 'lib/gpt3/builder/gpt3_builder.rb', line 26

def default_temperature
  @default_temperature
end

#default_top_pObject

Returns the value of attribute default_top_p.



27
28
29
# File 'lib/gpt3/builder/gpt3_builder.rb', line 27

def default_top_p
  @default_top_p
end

#promptObject

Returns the value of attribute prompt.



20
21
22
# File 'lib/gpt3/builder/gpt3_builder.rb', line 20

def prompt
  @prompt
end

#responseObject (readonly)

Returns the value of attribute response.



17
18
19
# File 'lib/gpt3/builder/gpt3_builder.rb', line 17

def response
  @response
end

#response_bodyObject (readonly)

Returns the value of attribute response_body.



18
19
20
# File 'lib/gpt3/builder/gpt3_builder.rb', line 18

def response_body
  @response_body
end

Class Method Details

.init {|builder| ... } ⇒ Builder

Create and initialize the builder.

configuration = nil)

Yields:

  • (builder)

Returns:

  • (Builder)

    Returns the builder via fluent interface



39
40
41
42
43
44
45
# File 'lib/gpt3/builder/gpt3_builder.rb', line 39

def self.init
  builder = new # configuration)

  yield(builder) if block_given?

  builder
end

Instance Method Details

#add_target_folder(folder_key, value) ⇒ Object

Fluent adder for target folder (KBuilder::NamedFolders)



233
234
235
236
237
# File 'lib/gpt3/builder/gpt3_builder.rb', line 233

def add_target_folder(folder_key, value)
  target_folders.add(folder_key, value)

  self
end

#buildHash/StrongType

Returns data object, can be a hash or strong typed object that you have wrapped around the hash

Returns:

  • (Hash/StrongType)

    Returns data object, can be a hash or strong typed object that you have wrapped around the hash

Raises:

  • (NotImplementedError)


69
70
71
# File 'lib/gpt3/builder/gpt3_builder.rb', line 69

def build
  raise NotImplementedError
end

#complete(engine: default_engine, max_tokens: default_max_tokens, temperature: default_temperature, top_p: default_top_p, frequency_penalty: default_frequency_penalty, presence_penalty: default_presence_penalty, suffix: nil) ⇒ Object

rubocop:disable Metrics/ParameterLists



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/gpt3/builder/gpt3_builder.rb', line 134

def complete(
  engine: default_engine,
  max_tokens: default_max_tokens,
  temperature: default_temperature,
  top_p: default_top_p,
  frequency_penalty: default_frequency_penalty,
  presence_penalty: default_presence_penalty,
  suffix: nil
)
  parameters = {
    prompt: prompt,
    max_tokens: max_tokens,
    temperature: temperature,
    top_p: top_p,
    frequency_penalty: frequency_penalty,
    presence_penalty: presence_penalty
  }

  parameters[:suffix] = suffix if suffix

  @response = client.completions(engine: engine, parameters: parameters)

  @response_body = JSON.parse(response.body)

  self
end

#debugObject

system(“code -d #file #Gpt3::Builder::Gpt3Builder.temp_filetemp_file.path”)

  sleep 2
end

end



302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/gpt3/builder/gpt3_builder.rb', line 302

def debug
  puts '----------------------------------------------------------------------'
  puts prompt
  puts '----------------------------------------------------------------------'

  # puts '- Pretty JSON-----------------------------------------------------------'
  # puts JSON.pretty_generate(run_commands.response_body)
  return unless response_body

  puts '- JSON----------------------------------------------------------------'
  puts response_body['choices'].first['text']
end

#example(example = nil, file: nil) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/gpt3/builder/gpt3_builder.rb', line 124

def example(example = nil, file: nil)
  example ||= ''
  example = File.read(file) if file

  add_block(example)

  self
end

#file_listObject

rubocop:enable Metrics/ParameterLists



162
163
164
165
166
167
168
# File 'lib/gpt3/builder/gpt3_builder.rb', line 162

def file_list
  @response = client.files.list

  @response_body = JSON.parse(response.body)

  self
end

#human(message) ⇒ Object Also known as: dude



104
105
106
107
108
109
# File 'lib/gpt3/builder/gpt3_builder.rb', line 104

def human(message)
  @human_question = true
  add_block("You: #{message}")

  self
end

#line(message) ⇒ Object



118
119
120
121
122
# File 'lib/gpt3/builder/gpt3_builder.rb', line 118

def line(message)
  add_line(message)

  self
end

#message(message) ⇒ Object



112
113
114
115
116
# File 'lib/gpt3/builder/gpt3_builder.rb', line 112

def message(message)
  add_block(message)

  self
end

#start(message) ⇒ Object


Fluent interface




97
98
99
100
101
102
# File 'lib/gpt3/builder/gpt3_builder.rb', line 97

def start(message)
  @started = true
  add_block(message)

  self
end

#write_result(file) ⇒ Object



170
171
172
173
174
# File 'lib/gpt3/builder/gpt3_builder.rb', line 170

def write_result(file)
  File.write(file, response_text)

  self
end