tiny_chat_gpt 1.0.0 → 1.2.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 +63 -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: 76e6d7ac36335701339ae739c5f4b4e01359f2e50d613acc9e2575bfcd406a8a
|
4
|
+
data.tar.gz: c28f7e4e0ad73791bc343fc0baa1f0aa986b09d1a211113e8203f7cf16d68265
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
#
|
5
|
-
#
|
6
|
-
#
|
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
|
-
#
|
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.
|
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
|
-
|
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
|
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
|
-
|
30
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
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
|
56
|
-
|
57
|
-
|
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.
|
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-
|
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: []
|