Class: TinyChatGpt
- Inherits:
-
Object
- Object
- TinyChatGpt
- Defined in:
- lib/tiny_chat_gpt.rb
Overview
TinyChatGpt provides an interface to OpenAI GPT API - every instance of this class starts a new conversation, which is necessary in order to carry on a conversation.
usage:
api_key = 'your_api_key'
model = TinyChatGpt::MODEL_3_5_TURBO
chat = TinyChatGpt.new(model, api_key)
response = chat.prompt('Hello, how are you?')
puts response
Defined Under Namespace
Classes: APIError
Constant Summary collapse
- URI =
URI('https://api.openai.com/v1/chat/completions')
- MODEL_3_5_TURBO =
'gpt-3.5-turbo'
- MODEL_4 =
'gpt-4-0613'
Instance Attribute Summary collapse
-
#completion_tokens ⇒ Object
readonly
Returns the value of attribute completion_tokens.
-
#prompt_tokens ⇒ Object
readonly
Returns the value of attribute prompt_tokens.
Instance Method Summary collapse
- #_parse_error(response) ⇒ Object
- #_parse_ok(response) ⇒ Object
- #_send_request(request) ⇒ Object
-
#initialize(model, api_key) ⇒ TinyChatGpt
constructor
A new instance of TinyChatGpt.
- #prompt(prompt) ⇒ Object
- #total_tokens ⇒ Object
Constructor Details
#initialize(model, api_key) ⇒ TinyChatGpt
Returns a new instance of TinyChatGpt.
26 27 28 29 30 31 32 33 |
# File 'lib/tiny_chat_gpt.rb', line 26 def initialize(model, api_key) @model = model @api_key = api_key @msgs = [] @prompt_tokens = 0 @completion_tokens = 0 end |
Instance Attribute Details
#completion_tokens ⇒ Object (readonly)
Returns the value of attribute completion_tokens.
23 24 25 |
# File 'lib/tiny_chat_gpt.rb', line 23 def completion_tokens @completion_tokens end |
#prompt_tokens ⇒ Object (readonly)
Returns the value of attribute prompt_tokens.
23 24 25 |
# File 'lib/tiny_chat_gpt.rb', line 23 def prompt_tokens @prompt_tokens end |
Instance Method Details
#_parse_error(response) ⇒ Object
84 85 86 87 88 |
# File 'lib/tiny_chat_gpt.rb', line 84 def _parse_error(response) JSON .parse(response.body) .dig('error', 'message') end |
#_parse_ok(response) ⇒ Object
80 81 82 |
# File 'lib/tiny_chat_gpt.rb', line 80 def _parse_ok(response) JSON.parse(response.body) end |
#_send_request(request) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/tiny_chat_gpt.rb', line 69 def _send_request(request) Net::HTTP.post( URI, request.to_json, { 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{KEY}" } ) end |
#prompt(prompt) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/tiny_chat_gpt.rb', line 39 def prompt(prompt) @msgs << { role: 'user', content: prompt } request = { model: @model, messages: @msgs } response = _send_request(request) if response.code == '200' resp = _parse_ok(response) completion = resp["choices"].first.dig('message', 'content') @msgs << { role: 'assistant', content: completion } @prompt_tokens += resp.dig('usage', 'prompt_tokens') @completion_tokens += resp.dig('usage', 'completion_tokens') TTY::Markdown.parse(completion) else "#{'ERR'.light_red}: HTTP status code #{response.code}, #{_parse_error(response)}" end end |
#total_tokens ⇒ Object
35 36 37 |
# File 'lib/tiny_chat_gpt.rb', line 35 def total_tokens prompt_tokens + completion_tokens end |