Class: AskGpt::GPT

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, model: 'gpt-3.5-turbo', temperature: 0.7, unable_to_get_answer_text: nil) ⇒ GPT

Returns a new instance of GPT.



10
11
12
13
14
15
16
17
# File 'lib/ask_gpt.rb', line 10

def initialize(api_key, model: 'gpt-3.5-turbo', temperature: 0.7, unable_to_get_answer_text: nil)
  @api_key = api_key
  @model = model
  @temperature = temperature
  @messages = []
  @history = []
  @unable_to_get_answer_text = unable_to_get_answer_text || 'Unable to get answer from ChatGPT. Make sure API key is valid and has enough credits.'
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



8
9
10
# File 'lib/ask_gpt.rb', line 8

def api_key
  @api_key
end

#historyObject

Returns the value of attribute history.



8
9
10
# File 'lib/ask_gpt.rb', line 8

def history
  @history
end

#messagesObject

Returns the value of attribute messages.



8
9
10
# File 'lib/ask_gpt.rb', line 8

def messages
  @messages
end

#modelObject

Returns the value of attribute model.



8
9
10
# File 'lib/ask_gpt.rb', line 8

def model
  @model
end

#temperatureObject

Returns the value of attribute temperature.



8
9
10
# File 'lib/ask_gpt.rb', line 8

def temperature
  @temperature
end

#unable_to_get_answer_textObject

Returns the value of attribute unable_to_get_answer_text.



8
9
10
# File 'lib/ask_gpt.rb', line 8

def unable_to_get_answer_text
  @unable_to_get_answer_text
end

Instance Method Details

#ask(question) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ask_gpt.rb', line 19

def ask(question)
  return if question.empty?

  replay_history
  update_messages(question)
  answer = get_completion

  return unable_to_get_answer_text unless answer

  update_history(question, answer)
  answer
end