tiny_chat_gpt 1.0.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 +7 -0
  2. data/lib/tiny_chat_gpt.rb +61 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 65e3af15974d0088f1cc53eacb0957e600a324ce7862d4e4656bb9022b097773
4
+ data.tar.gz: 5a01788191a33bccc7cdb77b2183f249ea3c576c7f2ecb11667ed636a9447d8a
5
+ SHA512:
6
+ metadata.gz: 1f4dbb55d0e27478b28619db2ec83f2a1bd801d3631686c2d11ac27ef14c5dd03fe8d88037da07988de1c8203fbaef20aaeb57b8b0401cec86ebd16bfd4f9efc
7
+ data.tar.gz: 12b646bc6db077e7407e7499bab7b6d0f33102a92b81f888d252a8e38a2b0effaf8123e09132e63167f786248cd07a4e78e18ef1d4ce92736967bb18e8016bee
@@ -0,0 +1,61 @@
1
+ require 'json'
2
+ require 'net/http'
3
+
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
+ #
8
+ # usage:
9
+ # chatbot = ChatGPT.new("davinci", "your_api_key")
10
+ # puts chatbot.ask("Hello, how are you today?")
11
+ #
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.
19
+ class TinyChatGpt
20
+ API_URL = "https://api.openai.com/v1/engines/davinci/jobs".freeze
21
+
22
+ def initialize(model, api_key)
23
+ @model = model
24
+ @api_key = api_key
25
+ end
26
+
27
+ def ask(prompt)
28
+ request = {
29
+ prompt: prompt,
30
+ max_tokens: 100,
31
+ n: 1,
32
+ stop: "",
33
+ temperature: 0.5,
34
+ model: @model
35
+ }
36
+ response = _send_request(request)
37
+
38
+ if response.code == 200
39
+ _parse_response(response)
40
+ else
41
+ raise TinyChatGpt::APIError.new("ERR: non-200 HTTP status to ChatGPT: #{response.code}")
42
+ end
43
+ end
44
+
45
+ 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)
53
+ end
54
+
55
+ def _parse_response(response)
56
+ response = JSON.parse(response)
57
+ response["choices"].first["text"]
58
+ end
59
+ end
60
+
61
+ class TinyChatGpt::APIError < RuntimeError; end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tiny_chat_gpt
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Lunt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-02-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: a tiny ChatGPT client
14
+ email: jefflunt@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/tiny_chat_gpt.rb
20
+ homepage: https://github.com/jefflunt/tiny_chat_gpt
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.4.1
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: this library provides a ChatGPT client in Ruby that can be used to interact
43
+ with ChatGPT programmatically
44
+ test_files: []