tiny_chat_gpt 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/tiny_chat_gpt.rb +25 -6
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 13551022c05de5ff85240752f609c2a282c5bb5a10b283de0d4c63a6d89a7af6
4
- data.tar.gz: 1fe62992425f0ea98de5930c62acf26df9ed602907b8e1fdaf0ab2d856eef8c8
3
+ metadata.gz: 76e6d7ac36335701339ae739c5f4b4e01359f2e50d613acc9e2575bfcd406a8a
4
+ data.tar.gz: c28f7e4e0ad73791bc343fc0baa1f0aa986b09d1a211113e8203f7cf16d68265
5
5
  SHA512:
6
- metadata.gz: d1675f8bd301df9a523fea30a2eb9bb23d2bfff8809dfcd80ab744aad6216337e1a3e9fac2d4d9bbce7507d987ffb4d2af224a3e6b2fe645f73201d19c0354d4
7
- data.tar.gz: 6e7743d42bd23c9b48024e6058f80c1725fddc2d0a5223bc9634215c1a2695c93668bcd25a7975b307dcc6e2d5bd922ae15fc8b7518e6051d57843d456351ff1
6
+ metadata.gz: ba1d9bd365ca7f33733c5d5fdab9697e56b26a1fd92b9894355a9f55bf4f817762ea192514990d2065ee05df4dcc2515402acff55b99dc1ecc63150783998309
7
+ data.tar.gz: 7d1da302b9fd7e3575062de5444ee9c0a5571cba2ac809319f3e82d62c95a55ec1be90910cce274b1fb164d1880f6112536330c61579a1759c3ff7bae55ce4af
data/lib/tiny_chat_gpt.rb CHANGED
@@ -4,7 +4,9 @@ require 'net/http'
4
4
  require 'tiny_color'
5
5
  require 'tty-markdown'
6
6
 
7
- # TinyChatGpt provides an interface to OpenAI GPT API.
7
+ # TinyChatGpt provides an interface to OpenAI GPT API - every instance of this
8
+ # class starts a new conversation, which is necessary in order to carry on a
9
+ # conversation.
8
10
  #
9
11
  # usage:
10
12
  #
@@ -18,10 +20,20 @@ class TinyChatGpt
18
20
  MODEL_3_5_TURBO = 'gpt-3.5-turbo'
19
21
  MODEL_4 = 'gpt-4-0613'
20
22
 
23
+ attr_reader :prompt_tokens,
24
+ :completion_tokens
25
+
21
26
  def initialize(model, api_key)
22
27
  @model = model
23
28
  @api_key = api_key
24
29
  @msgs = []
30
+
31
+ @prompt_tokens = 0
32
+ @completion_tokens = 0
33
+ end
34
+
35
+ def total_tokens
36
+ prompt_tokens + completion_tokens
25
37
  end
26
38
 
27
39
  def prompt(prompt)
@@ -38,7 +50,17 @@ class TinyChatGpt
38
50
  response = _send_request(request)
39
51
 
40
52
  if response.code == '200'
41
- TTY::Markdown.parse(_parse_ok(response))
53
+ resp = _parse_ok(response)
54
+ completion = resp["choices"].first.dig('message', 'content')
55
+ @msgs << {
56
+ role: 'assistant',
57
+ content: completion
58
+ }
59
+
60
+ @prompt_tokens += resp.dig('usage', 'prompt_tokens')
61
+ @completion_tokens += resp.dig('usage', 'completion_tokens')
62
+
63
+ TTY::Markdown.parse(completion)
42
64
  else
43
65
  "#{'ERR'.light_red}: HTTP status code #{response.code}, #{_parse_error(response)}"
44
66
  end
@@ -56,10 +78,7 @@ class TinyChatGpt
56
78
  end
57
79
 
58
80
  def _parse_ok(response)
59
- JSON
60
- .parse(response.body)["choices"]
61
- .first
62
- .dig('message', 'content')
81
+ JSON.parse(response.body)
63
82
  end
64
83
 
65
84
  def _parse_error(response)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tiny_chat_gpt
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Lunt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-28 00:00:00.000000000 Z
11
+ date: 2023-10-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tiny_color