tiny_chat_gpt 1.0.0 → 1.1.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 +44 -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: 13551022c05de5ff85240752f609c2a282c5bb5a10b283de0d4c63a6d89a7af6
4
+ data.tar.gz: 1fe62992425f0ea98de5930c62acf26df9ed602907b8e1fdaf0ab2d856eef8c8
5
5
  SHA512:
6
- metadata.gz: 1f4dbb55d0e27478b28619db2ec83f2a1bd801d3631686c2d11ac27ef14c5dd03fe8d88037da07988de1c8203fbaef20aaeb57b8b0401cec86ebd16bfd4f9efc
7
- data.tar.gz: 12b646bc6db077e7407e7499bab7b6d0f33102a92b81f888d252a8e38a2b0effaf8123e09132e63167f786248cd07a4e78e18ef1d4ce92736967bb18e8016bee
6
+ metadata.gz: d1675f8bd301df9a523fea30a2eb9bb23d2bfff8809dfcd80ab744aad6216337e1a3e9fac2d4d9bbce7507d987ffb4d2af224a3e6b2fe645f73201d19c0354d4
7
+ data.tar.gz: 6e7743d42bd23c9b48024e6058f80c1725fddc2d0a5223bc9634215c1a2695c93668bcd25a7975b307dcc6e2d5bd922ae15fc8b7518e6051d57843d456351ff1
data/lib/tiny_chat_gpt.rb CHANGED
@@ -1,60 +1,71 @@
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.
7
8
  #
8
9
  # usage:
9
- # chatbot = ChatGPT.new("davinci", "your_api_key")
10
- # puts chatbot.ask("Hello, how are you today?")
11
10
  #
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.
11
+ # api_key = 'your_api_key'
12
+ # model = TinyChatGpt::MODEL_3_5_TURBO
13
+ # chat = TinyChatGpt.new(model, api_key)
14
+ # response = chat.prompt('Hello, how are you?')
15
+ # puts response
19
16
  class TinyChatGpt
20
- API_URL = "https://api.openai.com/v1/engines/davinci/jobs".freeze
17
+ URI = URI('https://api.openai.com/v1/chat/completions')
18
+ MODEL_3_5_TURBO = 'gpt-3.5-turbo'
19
+ MODEL_4 = 'gpt-4-0613'
21
20
 
22
21
  def initialize(model, api_key)
23
22
  @model = model
24
23
  @api_key = api_key
24
+ @msgs = []
25
25
  end
26
26
 
27
- def ask(prompt)
27
+ def prompt(prompt)
28
+ @msgs << {
29
+ role: 'user',
30
+ content: prompt
31
+ }
32
+
28
33
  request = {
29
- prompt: prompt,
30
- max_tokens: 100,
31
- n: 1,
32
- stop: "",
33
- temperature: 0.5,
34
- model: @model
34
+ model: @model,
35
+ messages: @msgs
35
36
  }
37
+
36
38
  response = _send_request(request)
37
39
 
38
- if response.code == 200
39
- _parse_response(response)
40
+ if response.code == '200'
41
+ TTY::Markdown.parse(_parse_ok(response))
40
42
  else
41
- raise TinyChatGpt::APIError.new("ERR: non-200 HTTP status to ChatGPT: #{response.code}")
43
+ "#{'ERR'.light_red}: HTTP status code #{response.code}, #{_parse_error(response)}"
42
44
  end
43
45
  end
44
46
 
45
47
  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)
48
+ Net::HTTP.post(
49
+ URI,
50
+ request.to_json,
51
+ {
52
+ 'Content-Type' => 'application/json',
53
+ 'Authorization' => "Bearer #{KEY}"
54
+ }
55
+ )
56
+ end
57
+
58
+ def _parse_ok(response)
59
+ JSON
60
+ .parse(response.body)["choices"]
61
+ .first
62
+ .dig('message', 'content')
53
63
  end
54
64
 
55
- def _parse_response(response)
56
- response = JSON.parse(response)
57
- response["choices"].first["text"]
65
+ def _parse_error(response)
66
+ JSON
67
+ .parse(response.body)
68
+ .dig('error', 'message')
58
69
  end
59
70
  end
60
71
 
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.1.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-28 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: []