monadic-chat 0.4.0 → 0.4.1

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: 73e7b781492fab243c740ffe311a90291b2c5197e90b41b0a2972e7339aeba0d
4
- data.tar.gz: b2abac3f0925214f36d7eb64d19796bfb923b910e32d29031751822855fc629d
3
+ metadata.gz: 9d44ae83c28ccafde3e3b2ea1d486730c2dbf5ff3cc7193dc4c25c97f6a46fbb
4
+ data.tar.gz: f69a30f93b4777b70d078b007c3342e212d3deb4bf2cf92d782fb6af6b825f07
5
5
  SHA512:
6
- metadata.gz: 107f07c41b2a40905ee32319f455b1da03a65d7270831f5810be75dd9800dbe905ed61aba0aac00b9018f54d5d978e90f2b01a6e9d6b1bfeff00c35286a7d6e5
7
- data.tar.gz: 48098f9dac1512feea8fc8d23bfaa15ac33e669e3989465751e7676e2924630f34fe11db6806d8723cf70ecd8e4132342335d34df6469bd7c21f6d07e6204067
6
+ metadata.gz: 31a241cff282c2e98eed745e4e32612dc018793832c38dddd79614479fd8b816a1c94be7ff16dc16b73858eb883d2da42b81109ed3d94e253a7e473526533386
7
+ data.tar.gz: eddd1596523564ac1f066b93c762d13b44515990ba1e479b74f6b51cf94e8fc840d45d45763757264a0c1dde21beac1b68c3652a2baf8a08ab6f306e64a818e9
data/CHANGELOG.md CHANGED
@@ -46,3 +46,8 @@
46
46
  - Support for OpenAI's latest models
47
47
  - Missing character issue addressed
48
48
  - API access timeout/retry mechanism improved
49
+
50
+ ## [0.4.1] - 2024-01-27
51
+
52
+ - New models of January 2024 supported
53
+ - Default model changed to `gpt-4`
@@ -152,29 +152,31 @@ class MonadicApp
152
152
  research_mode = @mode == :research
153
153
 
154
154
  escaping = +""
155
- last_chunk = +""
155
+ finished = false
156
156
 
157
157
  res = @completion.run(params,
158
158
  research_mode: research_mode,
159
159
  timeout_sec: SETTINGS["timeout_sec"],
160
160
  num_retrials: num_retrials) do |chunk|
161
- if escaping
162
- chunk = escaping + chunk
163
- escaping = ""
164
- end
161
+ if chunk.instance_of?(Hash) && chunk["content"] == "DONE"
162
+ finished = true
163
+ elsif chunk.instance_of?(String) && !finished
164
+ if escaping
165
+ chunk = escaping + chunk
166
+ escaping = ""
167
+ end
165
168
 
166
- if /(?:\\\z)/ =~ chunk
167
- escaping += chunk
168
- next
169
- else
170
- chunk = chunk.gsub('\\n') { "\n" }
171
- end
169
+ if /(?:\\\z)/ =~ chunk
170
+ escaping += chunk
171
+ next
172
+ else
173
+ chunk = chunk.gsub('\\n') { "\n" }
174
+ end
172
175
 
173
- print last_chunk
174
- last_chunk = chunk
176
+ print chunk
177
+ end
175
178
  end
176
179
 
177
- print last_chunk
178
180
  print "\n"
179
181
 
180
182
  message = case role
@@ -16,9 +16,9 @@ RETRY_WAIT_TIME_SEC = 1
16
16
  module OpenAI
17
17
  def self.default_model(research_mode: false)
18
18
  if research_mode
19
- "gpt-3.5-turbo"
19
+ "gpt-4-turbo-preview"
20
20
  else
21
- "gpt-3.5-turbo"
21
+ "gpt-4"
22
22
  end
23
23
  end
24
24
 
@@ -27,13 +27,12 @@ module OpenAI
27
27
  "gpt-3.5-turbo-1106" => "chat/completions",
28
28
  "gpt-3.5-turbo" => "chat/completions",
29
29
  "gpt-3.5-turbo-16k" => "chat/completions",
30
+ "gpt-4-0125-preview" => "chat/completions",
31
+ "gpt-4-turbo-preview" => "chat/completions",
30
32
  "gpt-4-1106-preview" => "chat/completions",
31
33
  "gpt-4" => "chat/completions",
32
34
  "gpt-4-0613" => "chat/completions",
33
- "gpt-4-32K" => "chat/completions",
34
- "gpt-4-32k-0613" => "chat/completions",
35
- "gpt-3.5-turbo-0613" => "chat/completions",
36
- "gpt-3.5-turbo-16k-0613" => "chat/completions"
35
+ "gpt-4-32K" => "chat/completions"
37
36
  }[model]
38
37
  if res.nil?
39
38
  puts ""
@@ -73,12 +72,16 @@ module OpenAI
73
72
  if query["stream"]
74
73
  json = nil
75
74
  buffer = ""
76
- break_flag = false
75
+ # break_flag = false
76
+
77
77
  res.body.each do |chunk|
78
- break if break_flag
78
+ break if /\Rdata: [DONE]\R/ =~ chunk
79
+
80
+ # break if break_flag
79
81
 
80
82
  buffer << chunk
81
- break_flag = true if /\Rdata: [DONE]\R/ =~ buffer
83
+ # break_flag = true if /\Rdata: [DONE]\R/ =~ buffer
84
+
82
85
  scanner = StringScanner.new(buffer)
83
86
  pattern = /data: (\{.*?\})(?=\n|\z)/m
84
87
  until scanner.eos?
@@ -92,6 +95,7 @@ module OpenAI
92
95
  fragment = choice.dig("delta", "content").to_s
93
96
 
94
97
  block&.call fragment
98
+
95
99
  if !json
96
100
  json = res
97
101
  else
@@ -99,7 +103,11 @@ module OpenAI
99
103
  json["choices"][0]["text"] << fragment
100
104
  end
101
105
 
102
- break if choice["finish_reason"] == "length" || choice["finish_reason"] == "stop"
106
+ if choice["finish_reason"] == "length" || choice["finish_reason"] == "stop"
107
+ finish = { "type" => "message", "content" => "DONE" }
108
+ block&.call finish
109
+ break
110
+ end
103
111
  rescue JSON::ParserError
104
112
  res = { "type" => "error", "content" => "Error: JSON Parsing" }
105
113
  pp res
@@ -156,6 +164,7 @@ module OpenAI
156
164
  method = OpenAI.model_to_method(params["model"])
157
165
 
158
166
  response = OpenAI.query(@access_token, "post", method, timeout_sec, params, &block)
167
+
159
168
  if response["error"]
160
169
  raise response["error"]["message"]
161
170
  elsif response["choices"][0]["finish_reason"] == "length"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MonadicChat
4
- VERSION = "0.4.0"
4
+ VERSION = "0.4.1"
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",
31
- "research_model" => "gpt-3.5-turbo",
30
+ "normal_model" => "gpt-4",
31
+ "research_model" => "gpt-4-turbo-preview",
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.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - yohasebe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-10 00:00:00.000000000 Z
11
+ date: 2024-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler