tiny_chat_gpt 1.0.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/tiny_chat_gpt.rb +63 -33
  3. metadata +31 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 65e3af15974d0088f1cc53eacb0957e600a324ce7862d4e4656bb9022b097773
4
- data.tar.gz: 5a01788191a33bccc7cdb77b2183f249ea3c576c7f2ecb11667ed636a9447d8a
3
+ metadata.gz: 76e6d7ac36335701339ae739c5f4b4e01359f2e50d613acc9e2575bfcd406a8a
4
+ data.tar.gz: c28f7e4e0ad73791bc343fc0baa1f0aa986b09d1a211113e8203f7cf16d68265
5
5
  SHA512:
6
- metadata.gz: 1f4dbb55d0e27478b28619db2ec83f2a1bd801d3631686c2d11ac27ef14c5dd03fe8d88037da07988de1c8203fbaef20aaeb57b8b0401cec86ebd16bfd4f9efc
7
- data.tar.gz: 12b646bc6db077e7407e7499bab7b6d0f33102a92b81f888d252a8e38a2b0effaf8123e09132e63167f786248cd07a4e78e18ef1d4ce92736967bb18e8016bee
6
+ metadata.gz: ba1d9bd365ca7f33733c5d5fdab9697e56b26a1fd92b9894355a9f55bf4f817762ea192514990d2065ee05df4dcc2515402acff55b99dc1ecc63150783998309
7
+ data.tar.gz: 7d1da302b9fd7e3575062de5444ee9c0a5571cba2ac809319f3e82d62c95a55ec1be90910cce274b1fb164d1880f6112536330c61579a1759c3ff7bae55ce4af
data/lib/tiny_chat_gpt.rb CHANGED
@@ -1,60 +1,90 @@
1
1
  require 'json'
2
+ require 'uri'
2
3
  require 'net/http'
4
+ require 'tiny_color'
5
+ require 'tty-markdown'
3
6
 
4
- # the TinyChatGPT class is an API adapter for OpenAI's GPT-3 based ChatGPT
5
- # model. this class makes it easy to send prompts to the ChatGPT API and
6
- # receive responses.
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.
7
10
  #
8
11
  # usage:
9
- # chatbot = ChatGPT.new("davinci", "your_api_key")
10
- # puts chatbot.ask("Hello, how are you today?")
11
12
  #
12
- # if there is any non-200 HTTP response from the API then an instance of
13
- # TinyChatGpt::APIError will be raised with a helpful error message.
14
- #
15
- # NOTE: this version does not maintain any context from one prompt to the next,
16
- # so having a longer conversation with ChatGPT via this client is not yet
17
- # supported. every use of the #ask method is sent as a separate API request,
18
- # and does not include the context of previous prompts and replies.
13
+ # api_key = 'your_api_key'
14
+ # model = TinyChatGpt::MODEL_3_5_TURBO
15
+ # chat = TinyChatGpt.new(model, api_key)
16
+ # response = chat.prompt('Hello, how are you?')
17
+ # puts response
19
18
  class TinyChatGpt
20
- API_URL = "https://api.openai.com/v1/engines/davinci/jobs".freeze
19
+ URI = URI('https://api.openai.com/v1/chat/completions')
20
+ MODEL_3_5_TURBO = 'gpt-3.5-turbo'
21
+ MODEL_4 = 'gpt-4-0613'
22
+
23
+ attr_reader :prompt_tokens,
24
+ :completion_tokens
21
25
 
22
26
  def initialize(model, api_key)
23
27
  @model = model
24
28
  @api_key = api_key
29
+ @msgs = []
30
+
31
+ @prompt_tokens = 0
32
+ @completion_tokens = 0
25
33
  end
26
34
 
27
- def ask(prompt)
35
+ def total_tokens
36
+ prompt_tokens + completion_tokens
37
+ end
38
+
39
+ def prompt(prompt)
40
+ @msgs << {
41
+ role: 'user',
42
+ content: prompt
43
+ }
44
+
28
45
  request = {
29
- prompt: prompt,
30
- max_tokens: 100,
31
- n: 1,
32
- stop: "",
33
- temperature: 0.5,
34
- model: @model
46
+ model: @model,
47
+ messages: @msgs
35
48
  }
49
+
36
50
  response = _send_request(request)
37
51
 
38
- if response.code == 200
39
- _parse_response(response)
52
+ if response.code == '200'
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)
40
64
  else
41
- raise TinyChatGpt::APIError.new("ERR: non-200 HTTP status to ChatGPT: #{response.code}")
65
+ "#{'ERR'.light_red}: HTTP status code #{response.code}, #{_parse_error(response)}"
42
66
  end
43
67
  end
44
68
 
45
69
  def _send_request(request)
46
- uri = URI(API_URL)
47
- http = Net::HTTP.new(uri.host, uri.port)
48
- http.use_ssl = true
49
- request = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
50
- request.basic_auth(@api_key, '')
51
- request.body = request.to_json
52
- http.request(request)
70
+ Net::HTTP.post(
71
+ URI,
72
+ request.to_json,
73
+ {
74
+ 'Content-Type' => 'application/json',
75
+ 'Authorization' => "Bearer #{KEY}"
76
+ }
77
+ )
78
+ end
79
+
80
+ def _parse_ok(response)
81
+ JSON.parse(response.body)
53
82
  end
54
83
 
55
- def _parse_response(response)
56
- response = JSON.parse(response)
57
- response["choices"].first["text"]
84
+ def _parse_error(response)
85
+ JSON
86
+ .parse(response.body)
87
+ .dig('error', 'message')
58
88
  end
59
89
  end
60
90
 
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tiny_chat_gpt
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.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-02-10 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2023-10-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tiny_color
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: tty-markdown
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
13
41
  description: a tiny ChatGPT client
14
42
  email: jefflunt@gmail.com
15
43
  executables: []