Class: ChatgptRb::Conversation
- Inherits:
-
Object
- Object
- ChatgptRb::Conversation
- Defined in:
- lib/chatgpt_rb/conversation.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#base_uri ⇒ Object
Returns the value of attribute base_uri.
-
#frequency_penalty ⇒ Object
Returns the value of attribute frequency_penalty.
-
#functions ⇒ Object
Returns the value of attribute functions.
-
#max_tokens ⇒ Object
Returns the value of attribute max_tokens.
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#model ⇒ Object
Returns the value of attribute model.
-
#presence_penalty ⇒ Object
Returns the value of attribute presence_penalty.
-
#prompt ⇒ Object
Returns the value of attribute prompt.
-
#temperature ⇒ Object
Returns the value of attribute temperature.
-
#top_p ⇒ Object
Returns the value of attribute top_p.
Instance Method Summary collapse
-
#ask(content) {|the| ... } ⇒ String
The response.
-
#ask_with_function(content, function) {|the| ... } ⇒ String
The response.
-
#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
constructor
A new instance of Conversation.
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.
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 = .map { || .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_key ⇒ Object
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_uri ⇒ Object
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_penalty ⇒ Object
Returns the value of attribute frequency_penalty.
8 9 10 |
# File 'lib/chatgpt_rb/conversation.rb', line 8 def frequency_penalty @frequency_penalty end |
#functions ⇒ Object
Returns the value of attribute functions.
8 9 10 |
# File 'lib/chatgpt_rb/conversation.rb', line 8 def functions @functions end |
#max_tokens ⇒ Object
Returns the value of attribute max_tokens.
8 9 10 |
# File 'lib/chatgpt_rb/conversation.rb', line 8 def max_tokens @max_tokens end |
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
9 10 11 |
# File 'lib/chatgpt_rb/conversation.rb', line 9 def @messages end |
#model ⇒ Object
Returns the value of attribute model.
8 9 10 |
# File 'lib/chatgpt_rb/conversation.rb', line 8 def model @model end |
#presence_penalty ⇒ Object
Returns the value of attribute presence_penalty.
8 9 10 |
# File 'lib/chatgpt_rb/conversation.rb', line 8 def presence_penalty @presence_penalty end |
#prompt ⇒ Object
Returns the value of attribute prompt.
8 9 10 |
# File 'lib/chatgpt_rb/conversation.rb', line 8 def prompt @prompt end |
#temperature ⇒ Object
Returns the value of attribute temperature.
8 9 10 |
# File 'lib/chatgpt_rb/conversation.rb', line 8 def temperature @temperature end |
#top_p ⇒ Object
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.
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.
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 |