monadic-chat 0.3.7 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2256497e5cedc7850024f7ed9a8b8ec7d9b6cef546d2d20fea7feac024689012
4
- data.tar.gz: 288b0fa1ceb0367d8c0758b98d1397f307a4926add65ab532867b131968f89d1
3
+ metadata.gz: 73e7b781492fab243c740ffe311a90291b2c5197e90b41b0a2972e7339aeba0d
4
+ data.tar.gz: b2abac3f0925214f36d7eb64d19796bfb923b910e32d29031751822855fc629d
5
5
  SHA512:
6
- metadata.gz: cfae3022ccca777f643a122d73e14044963392044bbf26f4833010296fb73d12f86d960d349e480089aa3c734d91ef1ee2415be592a2aa3fa11360e5c90ade23
7
- data.tar.gz: 58347893d5ae84aa9374acef3e52421e0f6abf06e9fde78ed9ebbbd8ef57dfb5d6da705b3348a56fc90d4903dbc33c50e3e5330c2c275e5da083bf61705cb982
6
+ metadata.gz: 107f07c41b2a40905ee32319f455b1da03a65d7270831f5810be75dd9800dbe905ed61aba0aac00b9018f54d5d978e90f2b01a6e9d6b1bfeff00c35286a7d6e5
7
+ data.tar.gz: 48098f9dac1512feea8fc8d23bfaa15ac33e669e3989465751e7676e2924630f34fe11db6806d8723cf70ecd8e4132342335d34df6469bd7c21f6d07e6204067
data/CHANGELOG.md CHANGED
@@ -39,3 +39,10 @@
39
39
 
40
40
  - Default model changed to `gpt-3.5-turbo-0613`
41
41
  - Stability improvement
42
+
43
+ ## [0.4.0] - 2023-11-10
44
+
45
+ - Default model changed to `gpt-3.5-turbo`
46
+ - Support for OpenAI's latest models
47
+ - Missing character issue addressed
48
+ - API access timeout/retry mechanism improved
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- monadic-chat (0.3.7)
4
+ monadic-chat (0.4.0)
5
5
  blingfire
6
6
  http
7
7
  kramdown
data/README.md CHANGED
@@ -27,6 +27,7 @@
27
27
 
28
28
  **Change Log**
29
29
 
30
+ - [November 10, 2023] Stability improvement; default model changed to `gpt-3.5-turbo`
30
31
  - [October 07, 2023] Stability improvement; default model changed to `gpt-3.5-turbo-0613`
31
32
  - [June 11, 2023] The repository renamed to `monadic-chat-cli`
32
33
  - [April 05, 2023] `Wikipedia` app added (experimental)
@@ -303,13 +304,13 @@ Monadic Chat has two modes. The `normal` mode utilizes OpenAI's chat API to achi
303
304
 
304
305
  ### Normal Mode
305
306
 
306
- The current default language model for `normal` mode is `gpt-3.5-turbo-613`.
307
+ The current default language model for `normal` mode is `gpt-3.5-turbo`.
307
308
 
308
309
  In the default configuration, the dialogue messages are reduced after ten turns by deleting the oldest ones (but not the messages that the `system` role has given as instructions).
309
310
 
310
311
  ### Research Mode
311
312
 
312
- The current default language model for `research` mode is `gpt-3.5-turbo-0613`.
313
+ The current default language model for `research` mode is `gpt-3.5-turbo`.
313
314
 
314
315
  In `research` mode, the conversation between the user and the large-scale language model is accomplished with a mechanism that tracks the conversation history in a monadic structure. In the default configuration, the dialogue messages are reduced after ten turns by deleting the oldest ones (but not the messages that the `system` role has given as instructions).
315
316
 
@@ -1,4 +1,4 @@
1
- # frozen_string_literal: true
1
+ # frozen_string_literal: false
2
2
 
3
3
  require "http"
4
4
  require "oj"
@@ -9,27 +9,43 @@ require "tty-progressbar"
9
9
 
10
10
  Oj.mimic_JSON
11
11
 
12
+ OPEN_TIMEOUT = 10
13
+ RETRY_MAX_COUNT = 10
14
+ RETRY_WAIT_TIME_SEC = 1
15
+
12
16
  module OpenAI
13
17
  def self.default_model(research_mode: false)
14
18
  if research_mode
15
- "gpt-3.5-turbo-0613"
19
+ "gpt-3.5-turbo"
16
20
  else
17
- "gpt-3.5-turbo-0613"
21
+ "gpt-3.5-turbo"
18
22
  end
19
23
  end
20
24
 
21
25
  def self.model_to_method(model)
22
- {
23
- "gpt-3.5-turbo-instruct" => "completions",
26
+ res = {
27
+ "gpt-3.5-turbo-1106" => "chat/completions",
28
+ "gpt-3.5-turbo" => "chat/completions",
29
+ "gpt-3.5-turbo-16k" => "chat/completions",
30
+ "gpt-4-1106-preview" => "chat/completions",
24
31
  "gpt-4" => "chat/completions",
25
32
  "gpt-4-0613" => "chat/completions",
26
33
  "gpt-4-32K" => "chat/completions",
27
34
  "gpt-4-32k-0613" => "chat/completions",
28
- "gpt-3.5-turbo" => "chat/completions",
29
35
  "gpt-3.5-turbo-0613" => "chat/completions",
30
- "gpt-3.5-turbo-16k" => "chat/completions",
31
36
  "gpt-3.5-turbo-16k-0613" => "chat/completions"
32
37
  }[model]
38
+ if res.nil?
39
+ puts ""
40
+ puts "============================================================="
41
+ puts "Model #{model} not found."
42
+ puts "Maybe you are trying to use a model not available any more."
43
+ puts "Check your monadic_chat.conf and try again."
44
+ puts "============================================================="
45
+ puts ""
46
+ exit 1
47
+ end
48
+ res
33
49
  end
34
50
 
35
51
  def self.query(access_token, mode, method, timeout_sec = 60, query = {}, &block)
@@ -41,49 +57,59 @@ module OpenAI
41
57
  headers["Accept"] = "text/event-stream" if query["stream"]
42
58
  http = HTTP.headers(headers)
43
59
 
60
+ timeout_settings = {
61
+ connect: OPEN_TIMEOUT,
62
+ write: timeout_sec,
63
+ read: timeout_sec
64
+ }
65
+
44
66
  case mode
45
67
  when "post"
46
- res = http.timeout(timeout_sec).post(target_uri, json: query)
68
+ res = http.timeout(timeout_settings).post(target_uri, json: query)
47
69
  when "get"
48
- res = http.timeout(timeout_sec).get(target_uri)
70
+ res = http.timeout(timeout_settings).get(target_uri)
49
71
  end
50
72
 
51
73
  if query["stream"]
52
74
  json = nil
75
+ buffer = ""
76
+ break_flag = false
53
77
  res.body.each do |chunk|
54
- chunk.scan(/data: (\{.*\})/i).flatten.each do |data|
55
- content = data.strip
56
- break if content == "[DONE]"
57
-
58
- begin
59
- stream = JSON.parse(content)
60
- rescue JSON::ParserError
61
- next
62
- end
63
-
64
- fragment = case method
65
- when "completions"
66
- stream["choices"][0]["text"]
67
- when "chat/completions"
68
- stream["choices"][0]["delta"]["content"] || ""
69
- end
70
- block&.call fragment
71
- if !json
72
- json = stream
73
- else
74
- case method
75
- when "completions"
76
- json["choices"][0]["text"] << fragment
77
- when "chat/completions"
78
- json["choices"][0]["text"] ||= +""
79
- json["choices"][0]["text"] << fragment
78
+ break if break_flag
79
+
80
+ buffer << chunk
81
+ break_flag = true if /\Rdata: [DONE]\R/ =~ buffer
82
+ scanner = StringScanner.new(buffer)
83
+ pattern = /data: (\{.*?\})(?=\n|\z)/m
84
+ until scanner.eos?
85
+ matched = scanner.scan_until(pattern)
86
+ if matched
87
+ json_data = matched.match(pattern)[1]
88
+
89
+ begin
90
+ res = JSON.parse(json_data)
91
+ choice = res.dig("choices", 0) || {}
92
+ fragment = choice.dig("delta", "content").to_s
93
+
94
+ block&.call fragment
95
+ if !json
96
+ json = res
97
+ else
98
+ json["choices"][0]["text"] ||= +""
99
+ json["choices"][0]["text"] << fragment
100
+ end
101
+
102
+ break if choice["finish_reason"] == "length" || choice["finish_reason"] == "stop"
103
+ rescue JSON::ParserError
104
+ res = { "type" => "error", "content" => "Error: JSON Parsing" }
105
+ pp res
106
+ block&.call res
107
+ res
80
108
  end
109
+ else
110
+ buffer = scanner.rest
111
+ break
81
112
  end
82
- rescue JSON::ParserError
83
- res = { "type" => "error", "content" => "Error: JSON Parsing" }
84
- pp res
85
- block&.call res
86
- res
87
113
  end
88
114
  end
89
115
  json
@@ -97,6 +123,17 @@ module OpenAI
97
123
  res
98
124
  end
99
125
  end
126
+ rescue HTTP::Error, HTTP::TimeoutError
127
+ if num_retrial < MAX_RETRIES
128
+ num_retrial += 1
129
+ sleep RETRY_DELAY
130
+ retry
131
+ else
132
+ res = { "type" => "error", "content" => "Error: Timeout" }
133
+ pp res
134
+ block&.call res
135
+ res
136
+ end
100
137
  end
101
138
 
102
139
  def self.models(access_token)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MonadicChat
4
- VERSION = "0.3.7"
4
+ VERSION = "0.4.0"
5
5
  end
data/lib/monadic_chat.rb CHANGED
@@ -27,8 +27,8 @@ Oj.mimic_JSON
27
27
 
28
28
  module MonadicChat
29
29
  SETTINGS = {
30
- "normal_model" => "gpt-3.5-turbo-0613",
31
- "research_model" => "gpt-3.5-turbo-0613",
30
+ "normal_model" => "gpt-3.5-turbo",
31
+ "research_model" => "gpt-3.5-turbo",
32
32
  "max_tokens_wiki" => 1000,
33
33
  "num_retrials" => 2,
34
34
  "min_query_size" => 5,
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: monadic-chat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.7
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - yohasebe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-07 00:00:00.000000000 Z
11
+ date: 2023-11-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler