monadic-chat 0.3.6 → 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: 3e2cd88bc8efbeb6fcc338449160fd6d100c42b44fa8d178816023fac853926c
4
- data.tar.gz: e8ebe92f6b1ba1a4d583ce1d58469bcac453b0633ca5303a0f98cab53bc679ee
3
+ metadata.gz: 73e7b781492fab243c740ffe311a90291b2c5197e90b41b0a2972e7339aeba0d
4
+ data.tar.gz: b2abac3f0925214f36d7eb64d19796bfb923b910e32d29031751822855fc629d
5
5
  SHA512:
6
- metadata.gz: 401e24445a672a6fd451a80a225235d36cd520186918bc34dc9b7c86d9753e1ea0dddab5ecec319c7f1bca99c52024c95052264715715ab7bf54b3c36c9426ac
7
- data.tar.gz: cb9e6c76f4086e72f58fbcec7e3c28b5106053c64659e3f7fe6997ceec44e6dfc7f8f07ef5dd5f092580822e84987389f75294f27704815cd48d70cbba87aefb
6
+ metadata.gz: 107f07c41b2a40905ee32319f455b1da03a65d7270831f5810be75dd9800dbe905ed61aba0aac00b9018f54d5d978e90f2b01a6e9d6b1bfeff00c35286a7d6e5
7
+ data.tar.gz: 48098f9dac1512feea8fc8d23bfaa15ac33e669e3989465751e7676e2924630f34fe11db6806d8723cf70ecd8e4132342335d34df6469bd7c21f6d07e6204067
data/CHANGELOG.md CHANGED
@@ -32,5 +32,17 @@
32
32
 
33
33
  ## [0.3.5] - 2023-04-05
34
34
 
35
- - `Wikipedia` app added (experimental, requires GPT-4)
35
+ - `Wikipedia` app added (experimental)
36
36
  - `monadic-chat new/del app_name` command added
37
+
38
+ ## [0.3.7] - 2023-10-08
39
+
40
+ - Default model changed to `gpt-3.5-turbo-0613`
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.5)
4
+ monadic-chat (0.4.0)
5
5
  blingfire
6
6
  http
7
7
  kramdown
@@ -20,13 +20,13 @@ PATH
20
20
  GEM
21
21
  remote: https://rubygems.org/
22
22
  specs:
23
- addressable (2.8.4)
23
+ addressable (2.8.5)
24
24
  public_suffix (>= 2.0.2, < 6.0)
25
- blingfire (0.1.8)
25
+ blingfire (0.2.0)
26
26
  diff-lcs (1.5.0)
27
27
  domain_name (0.5.20190701)
28
28
  unf (>= 0.0.5, < 1.0.0)
29
- ffi (1.15.5)
29
+ ffi (1.16.3)
30
30
  ffi-compiler (1.0.1)
31
31
  ffi (>= 1.0.0)
32
32
  rake
@@ -45,26 +45,26 @@ GEM
45
45
  llhttp-ffi (0.4.0)
46
46
  ffi-compiler (~> 1.0)
47
47
  rake (~> 13.0)
48
- oj (3.14.3)
48
+ oj (3.16.1)
49
49
  pastel (0.8.0)
50
50
  tty-color (~> 0.5)
51
- public_suffix (5.0.1)
51
+ public_suffix (5.0.3)
52
52
  rake (13.0.6)
53
- rexml (3.2.5)
54
- rouge (4.1.0)
53
+ rexml (3.2.6)
54
+ rouge (4.1.3)
55
55
  rspec (3.12.0)
56
56
  rspec-core (~> 3.12.0)
57
57
  rspec-expectations (~> 3.12.0)
58
58
  rspec-mocks (~> 3.12.0)
59
- rspec-core (3.12.1)
59
+ rspec-core (3.12.2)
60
60
  rspec-support (~> 3.12.0)
61
- rspec-expectations (3.12.2)
61
+ rspec-expectations (3.12.3)
62
62
  diff-lcs (>= 1.2.0, < 2.0)
63
63
  rspec-support (~> 3.12.0)
64
- rspec-mocks (3.12.5)
64
+ rspec-mocks (3.12.6)
65
65
  diff-lcs (>= 1.2.0, < 2.0)
66
66
  rspec-support (~> 3.12.0)
67
- rspec-support (3.12.0)
67
+ rspec-support (3.12.1)
68
68
  strings (0.2.1)
69
69
  strings-ansi (~> 0.2)
70
70
  unicode-display_width (>= 1.5, < 3.0)
@@ -101,7 +101,7 @@ GEM
101
101
  unf (0.1.4)
102
102
  unf_ext
103
103
  unf_ext (0.0.8.2)
104
- unicode-display_width (2.4.2)
104
+ unicode-display_width (2.5.0)
105
105
  unicode_utils (1.4.0)
106
106
  wisper (2.0.1)
107
107
 
@@ -116,4 +116,4 @@ DEPENDENCIES
116
116
  rspec
117
117
 
118
118
  BUNDLED WITH
119
- 2.4.10
119
+ 2.4.17
data/README.md CHANGED
@@ -27,8 +27,10 @@
27
27
 
28
28
  **Change Log**
29
29
 
30
+ - [November 10, 2023] Stability improvement; default model changed to `gpt-3.5-turbo`
31
+ - [October 07, 2023] Stability improvement; default model changed to `gpt-3.5-turbo-0613`
30
32
  - [June 11, 2023] The repository renamed to `monadic-chat-cli`
31
- - [April 05, 2023] `Wikipedia` app added (experimental, requires GPT-4)
33
+ - [April 05, 2023] `Wikipedia` app added (experimental)
32
34
  - [April 05, 2023] `monadic-chat new/del app_name` command
33
35
  - [April 02, 2023] Architecture refined here and there
34
36
  - [March 26, 2023] Command line options to directly run individual apps
data/apps/chat/chat.md CHANGED
@@ -12,9 +12,9 @@ JSON:
12
12
 
13
13
  ```json
14
14
  {
15
+ "mode": "chat",
15
16
  "response": "Sure!",
16
17
  "summary": "",
17
- "mode": "chat",
18
18
  "language": "English",
19
19
  "topics": [],
20
20
  "confidence": 1.00,
@@ -33,6 +33,7 @@ Make sure the following content requirements are all fulfilled: ###
33
33
  - update the value of the "ambiguity" property based on the clarity of the user input, ranging from 0.00 (not at all ambiguous, clearly stated) to 1.00 (fully ambiguous, nonsensical)
34
34
  - avoid giving a response that is the same or similar to one of the previous responses in MESSAGES
35
35
  - program code in the response must be embedded in a code block in the markdown text
36
+ - the output JSON object must contain "mode", "response", "summary", "language", "topics", "confidence", and "ambiguity"
36
37
  ###
37
38
 
38
39
  Make sure the following formal requirements are all fulfilled: ###
data/apps/code/code.md CHANGED
@@ -10,9 +10,9 @@ JSON:
10
10
 
11
11
  ```json
12
12
  {
13
+ "mode": "chat",
13
14
  "response": "Sure!",
14
15
  "summary": "",
15
- "mode": "chat",
16
16
  "language": "English",
17
17
  "topics": []
18
18
  }
@@ -27,6 +27,7 @@ Make sure the following content requirements are all fulfilled: ###
27
27
  - summarize the user's messages so far and update the "summary" property with a text of fewer than 100 words
28
28
  - avoid giving a response that is the same or similar to one of the previous responses in "MESSAGES"
29
29
  - program code in the response must be embedded in a code block in the markdown text
30
+ - the output JSON object must contain "mode", "response", "summary", "language", and "topics"
30
31
  ###
31
32
 
32
33
  Make sure the following formal requirements are all fulfilled: ###
data/apps/novel/novel.md CHANGED
@@ -10,9 +10,9 @@ JSON:
10
10
 
11
11
  ```json
12
12
  {
13
+ "mode": "novel",
13
14
  "response": "What follows is a story that an AI assistant tells. It is guaranteed that this will be an incredibly realistic and interesting novel.",
14
- "summary": "",
15
- "mode": "novel"
15
+ "summary": ""
16
16
  }
17
17
  ```
18
18
 
@@ -22,6 +22,7 @@ Make sure the following content requirements are all fulfilled: ###
22
22
  - do not repeat in your response what is already told in "MESSAGES"
23
23
  - make your response as detailed as possible within the maximum limit of 200 words
24
24
  - summarize the user's messages so far and update the "summary" property with a text of fewer than 100 words
25
+ - the output JSON object must contain "mode", "response", and "summary"
25
26
  ###
26
27
 
27
28
  Make sure the following formal requirements are all fulfilled: ###
@@ -11,9 +11,9 @@ JSON:
11
11
  ```json
12
12
  {
13
13
  "mode": "translate",
14
+ "target_lang": "English",
14
15
  "response": "This is a sentence in Japanese.",
15
- "dictioanry": {"日本語": "Japanese", "文": "sentence"},
16
- "target_lang": "English"
16
+ "dictioanry": {"日本語": "Japanese", "文": "sentence"}
17
17
  }
18
18
  ```
19
19
 
@@ -22,6 +22,7 @@ Make sure the following requirements are all fulfilled: ###
22
22
  - translate the new prompt text to the language specified in the "target_lang" set it to "response" and set the translation to the "response" property
23
23
  - update the "dictionary" property with translation suggested by the user (using parentheses) for specific expressions
24
24
  - add user-suggested translations (translations in parentheses) to the "dictionary" property
25
+ - the output JSON object must contain "mode", "target_lang", "response", and "dictionary"
25
26
  ###
26
27
 
27
28
  Make sure the following formal requirements are all fulfilled: ###
data/bin/monadic-chat CHANGED
@@ -159,7 +159,7 @@ else
159
159
  next unless app == ARGV[0]
160
160
 
161
161
  openai_completion ||= MonadicChat.authenticate(message: false)
162
- app_obj = eval(app.capitalize, binding, __FILE__, __LINE__).new(openai_completion, research_mode: false, params: { "model" => "gpt-4" })
162
+ app_obj = eval(app.capitalize, binding, __FILE__, __LINE__).new(openai_completion, research_mode: false, params: {})
163
163
  app_obj.bind(ARGV[1..].join(" "), num_retrials: 2)
164
164
  exit
165
165
  end
@@ -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,6 +9,10 @@ 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
@@ -19,17 +23,29 @@ module OpenAI
19
23
  end
20
24
 
21
25
  def self.model_to_method(model)
22
- {
23
- "text-davinci-003" => "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,44 +57,82 @@ 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.split("\n\n").each do |data|
55
- content = data.strip[6..]
56
- break if content == "[DONE]"
57
-
58
- stream = JSON.parse(content)
59
- fragment = case method
60
- when "completions"
61
- stream["choices"][0]["text"]
62
- when "chat/completions"
63
- stream["choices"][0]["delta"]["content"] || ""
64
- end
65
- block&.call fragment
66
- if !json
67
- json = stream
68
- else
69
- case method
70
- when "completions"
71
- json["choices"][0]["text"] << fragment
72
- when "chat/completions"
73
- json["choices"][0]["text"] ||= +""
74
- 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
75
108
  end
109
+ else
110
+ buffer = scanner.rest
111
+ break
76
112
  end
77
113
  end
78
114
  end
79
115
  json
80
116
  else
81
- JSON.parse res.body
117
+ begin
118
+ JSON.parse res.body
119
+ rescue JSON::ParserError
120
+ res = { "type" => "error", "content" => "Error: JSON Parsing" }
121
+ pp res
122
+ block&.call res
123
+ res
124
+ end
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
82
136
  end
83
137
  end
84
138
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MonadicChat
4
- VERSION = "0.3.6"
4
+ VERSION = "0.4.0"
5
5
  end
@@ -3,7 +3,7 @@
3
3
  require_relative "../../lib/monadic_app"
4
4
 
5
5
  class Wikipedia < MonadicApp
6
- DESC = "Searches Wikipedia for you (experimental, requires GPT-4)"
6
+ DESC = "Searches Wikipedia for you (experimental)"
7
7
  COLOR = "white"
8
8
 
9
9
  attr_accessor :template, :config, :params, :completion
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.6
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-09-04 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