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.
- checksums.yaml +4 -4
- data/lib/tiny_chat_gpt.rb +44 -33
- metadata +31 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13551022c05de5ff85240752f609c2a282c5bb5a10b283de0d4c63a6d89a7af6
|
4
|
+
data.tar.gz: 1fe62992425f0ea98de5930c62acf26df9ed602907b8e1fdaf0ab2d856eef8c8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
#
|
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
|
-
#
|
13
|
-
# TinyChatGpt::
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
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
|
-
|
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
|
27
|
+
def prompt(prompt)
|
28
|
+
@msgs << {
|
29
|
+
role: 'user',
|
30
|
+
content: prompt
|
31
|
+
}
|
32
|
+
|
28
33
|
request = {
|
29
|
-
|
30
|
-
|
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
|
-
|
40
|
+
if response.code == '200'
|
41
|
+
TTY::Markdown.parse(_parse_ok(response))
|
40
42
|
else
|
41
|
-
|
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
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
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
|
56
|
-
|
57
|
-
|
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.
|
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-
|
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: []
|