Class: ChatgptRb::Conversation

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, model: "gpt-3.5-turbo", functions: [], temperature: 0.7, max_tokens: 1024, top_p: 1.0, frequency_penalty: 0.0, presence_penalty: 0.0, messages: [], prompt: nil, base_uri: "https://api.openai.com/v1", &configuration) ⇒ Conversation

Returns a new instance of Conversation.

Parameters:

  • api_key (String) (defaults to: nil)
  • model (String) (defaults to: "gpt-3.5-turbo")
  • functions (Array<Hash>, Array<ChatgptRb::Function>) (defaults to: [])
  • temperature (Float) (defaults to: 0.7)
  • max_tokens (Integer) (defaults to: 1024)
  • top_p (Float) (defaults to: 1.0)
  • frequency_penalty (Float) (defaults to: 0.0)
  • presence_penalty (Float) (defaults to: 0.0)
  • messages (Array<Hash>) (defaults to: [])
  • prompt (String, nil) (defaults to: nil)

    instructions that the model can use to inform its responses, for example: “Act like a sullen teenager.”

  • base_uri (String) (defaults to: "https://api.openai.com/v1")


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/chatgpt_rb/conversation.rb', line 22

def initialize(api_key: nil, model: "gpt-3.5-turbo", functions: [], temperature: 0.7, max_tokens: 1024, top_p: 1.0, frequency_penalty: 0.0, presence_penalty: 0.0, messages: [], prompt: nil, base_uri: "https://api.openai.com/v1", &configuration)
  @api_key = api_key
  @model = model
  @functions = functions.each_with_object({}) do |function, hash|
    func = if function.is_a?(ChatgptRb::Function)
             function
           else
             parameters = function.dig(:parameters, :properties)&.map do |name, definition|
               required = function.dig(:parameters, :required)&.include?(name)
               ChatgptRb::Parameter.new(name:, required:, **definition)
             end || []
             ChatgptRb::Function.new(parameters:, **function.except(:parameters))
           end
    hash[func.name] = func
  end
  @temperature = temperature
  @max_tokens = max_tokens
  @top_p = top_p
  @frequency_penalty = frequency_penalty
  @presence_penalty = presence_penalty
  @messages = messages.map { |message| message.transform_keys(&:to_sym) }
  @prompt = prompt
  @base_uri = base_uri
  ChatgptRb::DSL::Conversation.configure(self, &configuration) if block_given?
  @messages.unshift(role: "system", content: prompt) if prompt
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#base_uriObject

Returns the value of attribute base_uri.



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

def base_uri
  @base_uri
end

#frequency_penaltyObject

Returns the value of attribute frequency_penalty.



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

def frequency_penalty
  @frequency_penalty
end

#functionsObject

Returns the value of attribute functions.



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

def functions
  @functions
end

#max_tokensObject

Returns the value of attribute max_tokens.



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

def max_tokens
  @max_tokens
end

#messagesObject (readonly)

Returns the value of attribute messages.



9
10
11
# File 'lib/chatgpt_rb/conversation.rb', line 9

def messages
  @messages
end

#modelObject

Returns the value of attribute model.



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

def model
  @model
end

#presence_penaltyObject

Returns the value of attribute presence_penalty.



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

def presence_penalty
  @presence_penalty
end

#promptObject

Returns the value of attribute prompt.



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

def prompt
  @prompt
end

#temperatureObject

Returns the value of attribute temperature.



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

def temperature
  @temperature
end

#top_pObject

Returns the value of attribute top_p.



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

def top_p
  @top_p
end

Instance Method Details

#ask(content) {|the| ... } ⇒ String

Returns the response.

Parameters:

  • content (String)

Yield Parameters:

  • the (String)

    response, but streamed

Returns:

  • (String)

    the response



52
53
54
55
# File 'lib/chatgpt_rb/conversation.rb', line 52

def ask(content, &block)
  @messages << { role: "user", content: }
  get_next_response(&block)
end

#ask_with_function(content, function) {|the| ... } ⇒ String

Returns the response.

Parameters:

  • content (String)
  • function (ChatgptRb::Function)

    temporarily enhance the next response with the provided function

Yield Parameters:

  • the (String)

    response, but streamed

Returns:

  • (String)

    the response



61
62
63
64
65
66
# File 'lib/chatgpt_rb/conversation.rb', line 61

def ask_with_function(content, function, &block)
  function_was = functions[function.name]
  functions[function.name] = function
  get_next_response(content, &block)
  functions[function.name] = function_was
end