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.
-
#json ⇒ Object
Returns the value of attribute json.
-
#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.
-
#seed ⇒ Object
Returns the value of attribute seed.
-
#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", json: false, seed: nil, &configuration) ⇒ Conversation
constructor
A new instance of Conversation.
- #to_json ⇒ Object
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", json: false, seed: nil, &configuration) ⇒ Conversation
Returns a new instance of Conversation.
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 52 |
# File 'lib/chatgpt_rb/conversation.rb', line 25 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", json: false, seed: nil, &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 @json = json @seed = seed 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.
9 10 11 |
# File 'lib/chatgpt_rb/conversation.rb', line 9 def api_key @api_key end |
#base_uri ⇒ Object
Returns the value of attribute base_uri.
9 10 11 |
# File 'lib/chatgpt_rb/conversation.rb', line 9 def base_uri @base_uri end |
#frequency_penalty ⇒ Object
Returns the value of attribute frequency_penalty.
9 10 11 |
# File 'lib/chatgpt_rb/conversation.rb', line 9 def frequency_penalty @frequency_penalty end |
#functions ⇒ Object
Returns the value of attribute functions.
9 10 11 |
# File 'lib/chatgpt_rb/conversation.rb', line 9 def functions @functions end |
#json ⇒ Object
Returns the value of attribute json.
9 10 11 |
# File 'lib/chatgpt_rb/conversation.rb', line 9 def json @json end |
#max_tokens ⇒ Object
Returns the value of attribute max_tokens.
9 10 11 |
# File 'lib/chatgpt_rb/conversation.rb', line 9 def max_tokens @max_tokens end |
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
10 11 12 |
# File 'lib/chatgpt_rb/conversation.rb', line 10 def @messages end |
#model ⇒ Object
Returns the value of attribute model.
9 10 11 |
# File 'lib/chatgpt_rb/conversation.rb', line 9 def model @model end |
#presence_penalty ⇒ Object
Returns the value of attribute presence_penalty.
9 10 11 |
# File 'lib/chatgpt_rb/conversation.rb', line 9 def presence_penalty @presence_penalty end |
#prompt ⇒ Object
Returns the value of attribute prompt.
9 10 11 |
# File 'lib/chatgpt_rb/conversation.rb', line 9 def prompt @prompt end |
#seed ⇒ Object
Returns the value of attribute seed.
9 10 11 |
# File 'lib/chatgpt_rb/conversation.rb', line 9 def seed @seed end |
#temperature ⇒ Object
Returns the value of attribute temperature.
9 10 11 |
# File 'lib/chatgpt_rb/conversation.rb', line 9 def temperature @temperature end |
#top_p ⇒ Object
Returns the value of attribute top_p.
9 10 11 |
# File 'lib/chatgpt_rb/conversation.rb', line 9 def top_p @top_p end |
Instance Method Details
#ask(content) {|the| ... } ⇒ String
Returns the response.
61 62 63 64 |
# File 'lib/chatgpt_rb/conversation.rb', line 61 def ask(content, &block) @messages << { role: "user", content: } get_next_response(&block) end |
#ask_with_function(content, function) {|the| ... } ⇒ String
Returns the response.
70 71 72 73 74 75 |
# File 'lib/chatgpt_rb/conversation.rb', line 70 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 |
#to_json ⇒ Object
54 55 56 |
# File 'lib/chatgpt_rb/conversation.rb', line 54 def to_json .to_json end |