monadic-chat 0.1.3 → 0.2.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: e590d4e06e923e510110868aaf97e6d00d94452de3711183a44453b5899af59f
4
- data.tar.gz: 67e2e4ed103bc0ca06842ad6cd544558a8e0332782b4c2f07468effb6435074f
3
+ metadata.gz: 5ee0dcb302392e3e503b2f26a777953956a998244634c5935fc5be3e2316dc28
4
+ data.tar.gz: 1a42d5a6c02552ee676f89e338a8f7ff550b1905497cce42d0ba55e54c28038d
5
5
  SHA512:
6
- metadata.gz: 39c32bbd0728520af652727f0c2d0ef8f0773bee409967bed7071e75dda4b8bff42c8d2eaf4e1ddb777374ef37ab1366e4584cda1ab56472144b6a2a48c7a874
7
- data.tar.gz: 7a577baef5a59d20c98ec7978cd7aeb7aed8947c2b3f1b9b8f6dc4b1b91d1acc246b17e13cb8008fc0122d1c29b8660825214642640c3470b2fa449bc3b33ccf
6
+ metadata.gz: 1bca191c52aad2d0b2fd0de6dad2a806046e57546e038b9905d19a78e58047c3cd2e44dbc9fed69b88e648e3970290130d4350ec3fb94aa59b28baf8b07fbe46
7
+ data.tar.gz: 7e9e8a1752c11ff3a50de51ab20ab7f77c4a85b46c15134e253b42f6522b30be15b4cbb8cb8bb9ed52c96588a36d9c409cfb75cc14f28f686279324f468d1ded
data/CHANGELOG.md CHANGED
@@ -8,3 +8,8 @@
8
8
 
9
9
  - Public release
10
10
  - Authentication problem fixed
11
+
12
+ ## [0.2.0] - 2023-03-13
13
+
14
+ - Research mode architecture changed
15
+ - Stability improvement
data/Gemfile.lock ADDED
@@ -0,0 +1,166 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ monadic-chat (0.2.0)
5
+ http
6
+ kramdown
7
+ launchy
8
+ oj
9
+ parallel
10
+ pastel
11
+ rouge
12
+ tty-box
13
+ tty-cursor
14
+ tty-markdown
15
+ tty-progressbar
16
+ tty-prompt
17
+ tty-screen
18
+
19
+ GEM
20
+ remote: https://rubygems.org/
21
+ specs:
22
+ addressable (2.8.1)
23
+ public_suffix (>= 2.0.2, < 6.0)
24
+ ast (2.4.2)
25
+ backport (1.2.0)
26
+ benchmark (0.2.1)
27
+ diff-lcs (1.5.0)
28
+ domain_name (0.5.20190701)
29
+ unf (>= 0.0.5, < 1.0.0)
30
+ e2mmap (0.1.0)
31
+ ffi (1.15.5)
32
+ ffi-compiler (1.0.1)
33
+ ffi (>= 1.0.0)
34
+ rake
35
+ http (5.1.1)
36
+ addressable (~> 2.8)
37
+ http-cookie (~> 1.0)
38
+ http-form_data (~> 2.2)
39
+ llhttp-ffi (~> 0.4.0)
40
+ http-cookie (1.0.5)
41
+ domain_name (~> 0.5)
42
+ http-form_data (2.3.0)
43
+ jaro_winkler (1.5.4)
44
+ json (2.6.3)
45
+ kramdown (2.4.0)
46
+ rexml
47
+ kramdown-parser-gfm (1.1.0)
48
+ kramdown (~> 2.0)
49
+ launchy (2.5.2)
50
+ addressable (~> 2.8)
51
+ llhttp-ffi (0.4.0)
52
+ ffi-compiler (~> 1.0)
53
+ rake (~> 13.0)
54
+ nokogiri (1.14.2-x86_64-darwin)
55
+ racc (~> 1.4)
56
+ oj (3.14.2)
57
+ parallel (1.22.1)
58
+ parser (3.2.1.1)
59
+ ast (~> 2.4.1)
60
+ pastel (0.8.0)
61
+ tty-color (~> 0.5)
62
+ public_suffix (5.0.1)
63
+ racc (1.6.2)
64
+ rainbow (3.1.1)
65
+ rake (13.0.6)
66
+ regexp_parser (2.7.0)
67
+ reverse_markdown (2.1.1)
68
+ nokogiri
69
+ rexml (3.2.5)
70
+ rouge (4.1.0)
71
+ rspec (3.12.0)
72
+ rspec-core (~> 3.12.0)
73
+ rspec-expectations (~> 3.12.0)
74
+ rspec-mocks (~> 3.12.0)
75
+ rspec-core (3.12.1)
76
+ rspec-support (~> 3.12.0)
77
+ rspec-expectations (3.12.2)
78
+ diff-lcs (>= 1.2.0, < 2.0)
79
+ rspec-support (~> 3.12.0)
80
+ rspec-mocks (3.12.4)
81
+ diff-lcs (>= 1.2.0, < 2.0)
82
+ rspec-support (~> 3.12.0)
83
+ rspec-support (3.12.0)
84
+ rubocop (1.48.0)
85
+ json (~> 2.3)
86
+ parallel (~> 1.10)
87
+ parser (>= 3.2.0.0)
88
+ rainbow (>= 2.2.2, < 4.0)
89
+ regexp_parser (>= 1.8, < 3.0)
90
+ rexml (>= 3.2.5, < 4.0)
91
+ rubocop-ast (>= 1.26.0, < 2.0)
92
+ ruby-progressbar (~> 1.7)
93
+ unicode-display_width (>= 2.4.0, < 3.0)
94
+ rubocop-ast (1.27.0)
95
+ parser (>= 3.2.1.0)
96
+ ruby-progressbar (1.13.0)
97
+ solargraph (0.48.0)
98
+ backport (~> 1.2)
99
+ benchmark
100
+ bundler (>= 1.17.2)
101
+ diff-lcs (~> 1.4)
102
+ e2mmap
103
+ jaro_winkler (~> 1.5)
104
+ kramdown (~> 2.3)
105
+ kramdown-parser-gfm (~> 1.1)
106
+ parser (~> 3.0)
107
+ reverse_markdown (>= 1.0.5, < 3)
108
+ rubocop (>= 0.52)
109
+ thor (~> 1.0)
110
+ tilt (~> 2.0)
111
+ yard (~> 0.9, >= 0.9.24)
112
+ strings (0.2.1)
113
+ strings-ansi (~> 0.2)
114
+ unicode-display_width (>= 1.5, < 3.0)
115
+ unicode_utils (~> 1.4)
116
+ strings-ansi (0.2.0)
117
+ thor (1.2.1)
118
+ tilt (2.1.0)
119
+ tty-box (0.7.0)
120
+ pastel (~> 0.8)
121
+ strings (~> 0.2.0)
122
+ tty-cursor (~> 0.7)
123
+ tty-color (0.6.0)
124
+ tty-cursor (0.7.1)
125
+ tty-markdown (0.7.2)
126
+ kramdown (>= 1.16.2, < 3.0)
127
+ pastel (~> 0.8)
128
+ rouge (>= 3.14, < 5.0)
129
+ strings (~> 0.2.0)
130
+ tty-color (~> 0.5)
131
+ tty-screen (~> 0.8)
132
+ tty-progressbar (0.18.2)
133
+ strings-ansi (~> 0.2)
134
+ tty-cursor (~> 0.7)
135
+ tty-screen (~> 0.8)
136
+ unicode-display_width (>= 1.6, < 3.0)
137
+ tty-prompt (0.23.1)
138
+ pastel (~> 0.8)
139
+ tty-reader (~> 0.8)
140
+ tty-reader (0.9.0)
141
+ tty-cursor (~> 0.7)
142
+ tty-screen (~> 0.8)
143
+ wisper (~> 2.0)
144
+ tty-screen (0.8.1)
145
+ unf (0.1.4)
146
+ unf_ext
147
+ unf_ext (0.0.8.2)
148
+ unicode-display_width (2.4.2)
149
+ unicode_utils (1.4.0)
150
+ webrick (1.7.0)
151
+ wisper (2.0.1)
152
+ yard (0.9.28)
153
+ webrick (~> 1.7.0)
154
+
155
+ PLATFORMS
156
+ x86_64-darwin-22
157
+
158
+ DEPENDENCIES
159
+ bundler
160
+ monadic-chat!
161
+ rake
162
+ rspec
163
+ solargraph
164
+
165
+ BUNDLED WITH
166
+ 2.4.1
data/README.md CHANGED
@@ -7,7 +7,11 @@
7
7
  </p>
8
8
 
9
9
  > **Warning**
10
- > This software is *under active development*. It may be unstable, and the latest version may behave slightly differently than this document. Also, specifications may change in the future.
10
+ > This software is ***under active development***. It may be unstable, and the latest version may behave slightly differently than this document. Also, specifications may change in the future.
11
+
12
+ ** Change Log**
13
+
14
+ - [March 13, 2023] Text on the architecture of the `research` mode updated in accordance with Version 0.2.0
11
15
 
12
16
  ## Table of Contents
13
17
 
@@ -80,7 +84,12 @@ Execute the following command in an environment where Ruby 2.6.10 or higher is i
80
84
  gem install monadic-chat
81
85
  ```
82
86
 
83
- To update the `monadic-chat` gem installed:
87
+ Then run the command to start the app:
88
+ ```text
89
+ monadic-chat
90
+ ```
91
+
92
+ To update:
84
93
 
85
94
  ```text
86
95
  gem update monadic-chat
@@ -190,13 +199,7 @@ In `research` mode, it may take a while (usually several seconds) after the `dat
190
199
 
191
200
  All the information retrievable by running the `data/context` function can be presented in HTML. The HTML file is automatically opened in the default web browser.
192
201
 
193
- <br />
194
-
195
- <kbd><img src="./doc/img/linguistic-html.png" width="700px" style="border: thin solid darkgray;"/></kbd>
196
-
197
- <br />
198
-
199
- The generated HTML is saved in the user's home directory (`$HOME`) with the file name `monadic_chat.html`. The file contents does not automatically updated. Run `html` command every time when you need it. HTML data is written to this file regardless of the app.
202
+ The generated HTML will be saved in the user’s home directory (`$HOME`) with the file `monadic_chat.html`. Once the `html` command is executed, the file contents will continue to be updated until you `reset` or quit the running app. Reload the browser tab or rerun the `html` command to show the latest data. HTML data is written to this file regardless of the app.
200
203
 
201
204
  In `research` mode, it may take several seconds to several minutes after the `html` command is executed before the acutual HTML is displayed. This is because in `research` mode, even after displaying a direct response to user input, there may be a process running in the background that retrieves and reconstructs the context data, requiring the system to wait for it to finish.
202
205
 
@@ -283,13 +286,13 @@ Sometimes, however, problematic translations are created. The user can "save" th
283
286
 
284
287
  ## Modes
285
288
 
286
- Monadic Chat has two modes. The `normal` mode utilizes OpenAI's chat API to achieve ChatGPT-like functionality. It is suitable for using a large language model as a competent companion for various pragmatic purposes. On the other hand, the `research` mode utilizes OpenAI's text-completion API. This mode allows for acquiring metadata in the background while receiving the primary response at each conversation turn. It may be especially useful for researchers exploring the possibilities of large-scale language models and their applications.
289
+ Monadic Chat has two modes. The `normal` mode utilizes OpenAI's chat API to achieve ChatGPT-like functionality. It is suitable for using a large language model as a competent companion for various pragmatic purposes. On the other hand, the `research` mode utilizes OpenAI's text-completion API. This mode allows for acquiring **metadata** in the background while receiving the primary response at each conversation turn. It may be especially useful for researchers exploring the possibilities of large-scale language models and their applications.
287
290
 
288
291
  ### Normal Mode
289
292
 
290
293
  The default language model for `normal` mode is `gpt-3.5-turbo`.
291
294
 
292
- 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 gave as instructions).
295
+ 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).
293
296
 
294
297
  ### Research Mode
295
298
 
@@ -353,7 +356,9 @@ Terms in bold in it may require more explanation.
353
356
  ]}
354
357
  ```
355
358
 
356
- The accumulator in `research` mode also looks like this.
359
+ The accumulator in `research` mode also looks like this.
360
+
361
+ The conversation history is kept entirely in memory until the running app is terminated or reset. The part of the conversation history sent through the API along with new input sentences is referred to here as the accumulator.
357
362
 
358
363
  ### Reducer
359
364
 
@@ -395,11 +400,20 @@ The specifications for Monadic Chat's command-line user interface for this app a
395
400
 
396
401
  > **Note**
397
402
  > The use of square brackets (instead of parentheses) in the notation of syntactic analysis here is to conform to the format of [RSyntaxTree](https://yohasebe.com/rsyntaxtree), a tree-drawing program for linguistic research developed by the author of Monadic Chat.
398
-
399
- <img src="./doc/img/syntree-sample.png" width="300px" />
403
+ > <img src="./doc/img/syntree-sample.png" width="300px" />
400
404
 
401
405
  The sample app we create in this section is stored in the [`sample_app`](https://github.com/yohasebe/monadic-chat/tree/main/sample_app) folder in the repository.
402
406
 
407
+ Below is a sample HTML displaying the conversation (sentence and its syntactic structure notation pairs) and metadata.
408
+
409
+ <br />
410
+
411
+ <kbd><img src="./doc/img/linguistic-html.png" width="700px" style="border: thin solid darkgray;"/></kbd>
412
+
413
+ <br />
414
+
415
+
416
+
403
417
  ### File Structure
404
418
 
405
419
  New Monadic Chat apps must be placed inside the `apps` folder. The folders and files for default apps `chat`, `code`, `novel`, and `translate` are also in this folder.
@@ -437,16 +451,14 @@ apps
437
451
  The purpose of each file is as follows.
438
452
 
439
453
  - `linguistic.rb`: Ruby code to define the "reducer"
440
- - `linguistic.json`: JSON template describing GPT behavior in `normal` mode
441
- - `linguistic.md`: Markdown template describing GPT behavior in `research` mode
442
-
443
- The `.rb` file is required, but you may create both `.json` and `.md` files, or only one of them.
454
+ - `linguistic.json`: JSON template describing GPT behavior in `normal` and `research` modes
455
+ - `linguistic.md`: Markdown template describing GPT behavior in addition to the `json` file above in `research` mode
444
456
 
445
457
  Template files with a name beginning with `_` are also ignored. If a folder has a name beginning with `_`, all its contents are ignored.
446
458
 
447
459
  ### Reducer Code
448
460
 
449
- We do not need to make the reducer do anything special for the current purposes. So, let's copy the code from the default `chat` app and make a minor modification, such as changing the class name so that it matches the app name. We save it as `apps/linguistic/linguistic.rb`.
461
+ We do not need to make the reducer do anything special for the current purposes. So, let's copy the code from the default `chat` app and make a minor modification, such as changing the class name and the app name so that it matches the app name. We save it as `apps/linguistic/linguistic.rb`.
450
462
 
451
463
  ### Template for `Normal` Mode
452
464
 
@@ -481,10 +493,13 @@ Below we will look at the `research` mode template for the `linguistic` app, sec
481
493
 
482
494
  **Main Section**
483
495
 
484
- <div style="highlight highlight-source-gfm"><pre style="white-space : pre-wrap !important;">You are a natural language syntactic/semantic/pragmatic analyzer. Analyze the new prompt from the user below and execute a syntactic parsing. Give your response in a variation of the penn treebank format, but use brackets [ ] instead of parentheses ( ). Also, give your response in a markdown code span. The sentence must always be parsed if the user's input sentence is enclosed in double quotes. Create a response to the following new prompt from the user and set your response to the "response" property of the JSON object below. All prompts by "user" in the "messages" property are continuous in content.
485
- </pre></div>
496
+ <div style="highlight highlight-source-gfm"><pre style="white-space : pre-wrap !important;">{{SYSTEM}
497
+
498
+ All prompts by "user" in the "messages" property are continuous in content.You are an English syntactic/semantic/pragmatic analyzer. Analyze the new prompt from the user below and execute a syntactic parsing. Give your response in a variation of the penn treebank format, but use brackets [ ] instead of parentheses ( ). Also, give your response in a markdown code span. The sentence must always be parsed if the user's input sentence is enclosed in double quotes. Let the user know if parsing the sentence is difficult or the input must be enclosed in double quotes.
486
499
 
487
- The text here is the same as the text in the template for the `normal` mode in an instruction message by the `system`. However, note that it contains an instruction that the response from GPT should be presented in the form of a JSON object, as shown in one of the following sections.
500
+ Create a response to "NEW PROMPT" from the user and set your response to the "response" property of the JSON object shown below. The preceding conversation is stored in "PAST MESSAGES". In "PAST MESSAGES", "assistant" refers to you.</pre></div>
501
+
502
+ Some of the text here is the same as the text of the directive message by `system` in the template in `normal` mode; Monadic Chat automatically replaces `{{SYSTEM}}} with the `system` directive text when the template is sent via API. However, the above text also includes a few additional paragpraphs, including the one instructing the response from GPT to be presented as a JSON object.
488
503
 
489
504
  **New Prompt**
490
505
 
@@ -492,7 +507,16 @@ The text here is the same as the text in the template for the `normal` mode in a
492
507
  NEW PROMPT: {{PROMPT}}
493
508
  ```
494
509
 
495
- Monadic Chat replaces `{{PROMPT}}` with input from the user when sending templates through the API.
510
+ Monadic Chat replaces `{{PROMPT}}` with input from the user when sending the template through the API.
511
+
512
+ **Past Messages**
513
+
514
+ ```markdown
515
+ PAST MESSAGES:
516
+ {{MESSAGES}}
517
+ ```
518
+
519
+ Monadic Chat replaces `{{MESSAGES}}` with messages from past conversations when sending the template. Note that not all the past messages have to be copied here: the reducer mechanism could select, modify, or even "generate" messages and include them instead.
496
520
 
497
521
  **JSON Object**
498
522
 
@@ -501,15 +525,11 @@ Monadic Chat replaces `{{PROMPT}}` with input from the user when sending templat
501
525
  "prompt": "\"We didn't have a camera.\"",
502
526
  "response": "`[S [NP We] [VP [V didn't] [VP [V have] [NP [Det a] [N camera] ] ] ] ] ]`\n\n###\n\n",
503
527
  "mode": "linguistic",
504
- "turns": 2,
528
+ "tokens": 351
529
+ "turns": 3,
505
530
  "sentence_type": ["declarative"],
506
531
  "sentiment": ["sad"],
507
532
  "summary": "The user saw a beautiful sunset, but did not take a picture because the user did not have a camera.",
508
- "tokens": 351,
509
- "messages": [{"user": "\"We saw a beautiful sunset.\"", "assistant": "`[S [NP He] [VP [V saw] [NP [det a] [N' [Adj beautiful] [N sunset] ] ] ] ]`\n\n###\n\n" },
510
- {"user": "\"We didn't take a picture.\"", "assistant": "`[S [NP We] [IP [I didn't] [VP [V take] [NP [Det a] [N picture] ] ] ] ] ]`\n\n###\n\n" },
511
- {"user": "\"We didn't have a camera.\"", "assistant": "`[S [NP We] [IP [I didn't] [VP [V have] [NP [Det a] [N camera] ] ] ] ] ]`\n\n###\n\n" }
512
- ]
513
533
  }
514
534
  ```
515
535
 
@@ -517,15 +537,20 @@ This is the core of the `research` mode template.
517
537
 
518
538
  Note that the entire `research` mode template is written in Markdown format, so the above JSON object is actually separated from the rest of the template by a code fence, as shown below.
519
539
 
520
- ```json
521
- {
522
- "prompt": ...
523
- ...
524
- "messages": ...
525
- }
526
- ```
540
+ ```json
541
+ ```json
542
+ {
543
+ "prompt": ...
544
+ "response": ...
545
+ "mode": ...
546
+ "tokens": ...
547
+ "turns": ...
548
+ ...
549
+ }
550
+ ```
551
+ ```
527
552
 
528
- The required properties of this JSON object are `prompt`, `response`, and `messages`. Other properties are optional. The format of the `messages` property is similar to that of the `normal` mode (i.e., OpenAI's chat API. The only difference is that it is structured as a list of objects whose keys are user and assistant to make it easier to describe.)
553
+ The required properties of this JSON object are `prompt`, `response`, `mode`, and `tokens`. Other properties are optional. The `mode` property is used to check the app name when saving the conversation data or loading from an external file. The `tokens` property is used in the reducer mechanism to check the approximate size of the current JSON object. The `turns` property is also used in the reducer mechanism.
529
554
 
530
555
  The JSON object in the `research` mode template is saved in the user’s home directory (`$HOME`) with the file `monadic_chat.json`. The content is overwritten every time the JSON object is updated. Note that this JSON file is created for logging purposes (so the data is not pretty printed). Modifying its content does not affect the processes carried out by the app.
531
556
 
@@ -536,13 +561,12 @@ Make sure the following content requirements are all fulfilled:
536
561
 
537
562
  - keep the value of the "mode" property at "linguistic"
538
563
  - set the new prompt to the "prompt" property
539
- - create your response to the new prompt in accordance with the "messages" and set it to "response"
540
- - insert both the new prompt and the response after all the existing items in the "messages"
564
+ - create your response to the new prompt based on "PAST MESSAGES" and set it to "response"
541
565
  - analyze the new prompt's sentence type and set a sentence type value such as "interrogative", "imperative", "exclamatory", or "declarative" to the "sentence_type" property
542
566
  - analyze the new prompt's sentiment and set one or more sentiment types such as "happy", "excited", "troubled", "upset", or "sad" to the "sentiment" property
543
- - summarize the user's messages so far and update the "summary" property with a text of fewer than 100 words.
567
+ - summarize the user's messages so far and update the "summary" property with a text of fewer than 100 words using as many discourse markers such as "because", "therefore", "but", and "so" to show the logical connection between the events.
544
568
  - update the value of "tokens" with the number of tokens of the resulting JSON object"
545
- - increment the value of "turns" by 1 and update the property so that the value of "turns" equals the number of the items in the "messages" of the resulting JSON object
569
+ - increment the value of "turns" by 1
546
570
  ```
547
571
 
548
572
  Note that all the properties of the JSON object above are mentioned so that GPT can update them accordingly.
@@ -554,6 +578,7 @@ Make sure the following formal requirements are all fulfilled:
554
578
 
555
579
  - do not use invalid characters in the JSON object
556
580
  - escape double quotes and other special characters in the text values in the resulting JSON object
581
+ - check the validity of the generated JSON object and correct any possible parsing problems before returning it
557
582
 
558
583
  Add "\n\n###\n\n" at the end of the "response" value.
559
584
 
data/apps/chat/chat.json CHANGED
@@ -1,4 +1,4 @@
1
1
  {"messages": [
2
2
  {"role": "system",
3
- "content": "You are a friendly but professional consultant who answers various questions, write computer program code, make decent suggestions, give helpful advice in response to a prompt from the user. If the prompt is not clear enough, ask the user to rephrase it. You are able to empathize with the user; insert an emoji (displayable on the terminal screen) that you deem appropriate for the user's input at the beginning of your response. If the user input is sentimentally neutral, pick up any emoji that matchs the topic."}
3
+ "content": "You are a friendly but professional consultant who answers various questions, writes computer program code, makes decent suggestions, and gives helpful advice in response to a prompt from the user. If the prompt is not clear enough, ask the user to rephrase it. You are able to empathize with the user; insert an emoji (displayable on the terminal screen) that you deem appropriate for the user's input at the beginning of your response. If the user input is sentimentally neutral, pick up any emoji that matches the topic."}
4
4
  ]}
data/apps/chat/chat.md CHANGED
@@ -1,19 +1,24 @@
1
- You are a friendly but professional AI assistant capable of answering various questions, writing computer program code, making decent suggestions, and giving helpful advice in response to a new prompt from the user. If the prompt is not clear enough, ask the user to rephrase it. You are able to empathize with the user; insert a unicode emoji (one that is displayable on the terminal screen) that you deem appropriate for the user's input at the beginning of your response. If the user input is sentimentally neutral, pick up any emoji that matchs the topic. Create a response to the following new prompt from the user and set your response to the "response" property of the JSON object shown below. The preceding conversation is stored in the value of the "conversation" property.
1
+ {{SYSTEM}}
2
2
 
3
- Make your response as detailed as possible.
3
+ Create a response to "NEW PROMPT" from the user and set your response to the "response" property of the JSON object below. The preceding conversation is stored in "PAST MESSAGES".
4
+ The preceding conversation is stored in "PAST MESSAGES". In "PAST MESSAGES", "assistant" refers to you. Make your response as detailed as possible.
4
5
 
5
6
  NEW PROMPT: {{PROMPT}}
6
7
 
8
+ PAST MESSAGES:
9
+ {{MESSAGES}}
10
+
11
+ JSON:
12
+
7
13
  ```json
8
14
  {
9
15
  "prompt": "Can I ask something?",
10
16
  "response": "Sure!\n\n###\n\n",
11
17
  "mode": "chat",
12
- "turns": 1,
18
+ "turns": 0,
13
19
  "language": "English",
14
20
  "topics": [],
15
- "tokens": 109,
16
- "messages": [{"user": "Can I ask something?", "assistant": "Sure!\n\n###\n\n"}]
21
+ "tokens": 109
17
22
  }
18
23
  ```
19
24
 
@@ -21,12 +26,11 @@ Make sure the following content requirements are all fulfilled:
21
26
 
22
27
  - keep the value of the "mode" property at "chat"
23
28
  - set the new prompt to the "prompt" property
24
- - create your response to the new prompt in accordance with the "messages" and set it to "response"
25
- - insert both the new prompt and the response after all the existing items in the "messages"
29
+ - create your response to the new prompt based on the PAST MESSAGES and set it to "response"
26
30
  - if the new prompt is in a language other than the current value of "language", set the name of the new prompt language to "language" and make sure that "response" is in that language
27
31
  - make your response in the same language as the new prompt
28
32
  - analyze the topic of the new prompt and insert it at the end of the value list of the "topics" property
29
- - avoid giving a response that is the same or similar to one of the previous responses in "messages"
33
+ - avoid giving a response that is the same or similar to one of the previous responses in PAST MESSAGES
30
34
  - program code in the response must be embedded in a code block in the markdown text
31
35
  - update the value of "tokens" with the number of tokens of the resulting JSON object"
32
36
 
@@ -34,9 +38,8 @@ Make sure the following formal requirements are all fulfilled:
34
38
 
35
39
  - do not use invalid characters in the JSON object
36
40
  - escape double quotes and other special characters in the text values in the resulting JSON object
37
- - increment the value of "turns" by 1 and update the property so that the value of "turns" equals the number of the items in the "messages" of the resulting JSON object
41
+ - increment the value of "turns" by 1
38
42
  - check the validity of the generated JSON object and correct any possible parsing problems before returning it
39
-
40
43
  Add "\n\n###\n\n" at the end of the "response" value.
41
44
 
42
45
  Wrap the JSON object with "<JSON>\n" and "\n</JSON>".
data/apps/chat/chat.rb CHANGED
@@ -21,56 +21,54 @@ class Chat < MonadicApp
21
21
  "stop" => nil
22
22
  }
23
23
  method = OpenAI.model_to_method(params["model"])
24
- template = case method
25
- when "completions"
26
- TEMPLATES["research/chat"]
27
- when "chat/completions"
28
- TEMPLATES["normal/chat"]
29
- end
30
- super(params,
31
- template,
32
- {},
33
- "messages",
34
- "response",
35
- proc do |res|
24
+ case method
25
+ when "completions"
26
+ tjson = TEMPLATES["normal/chat"]
27
+ tmarkdown = TEMPLATES["research/chat"]
28
+ when "chat/completions"
29
+ tjson = TEMPLATES["normal/chat"]
30
+ tmarkdown = nil
31
+ end
32
+ super(params: params,
33
+ tjson: tjson,
34
+ tmarkdown: tmarkdown,
35
+ placeholders: {},
36
+ prop_accumulator: "messages",
37
+ prop_newdata: "response",
38
+ update_proc: proc do
36
39
  case method
37
40
  when "completions"
38
- obj = objectify
39
41
  ############################################################
40
- # Research mode recuder defined here #
41
- # obj: old Hash object #
42
- # res: new response Hash object to be modified #
42
+ # Research mode reduder defined here #
43
+ # @messages: messages to this point #
44
+ # @metadata: currently available metdata sent from GPT #
43
45
  ############################################################
46
+
44
47
  conditions = [
45
- res["messages"].size > 1,
46
- res["tokens"].to_i > params["max_tokens"].to_i / 2,
47
- !obj["topics"].empty?,
48
- res["topics"] != obj["topics"]
48
+ @messages.size > 1,
49
+ @metadata["tokens"].to_i > params["max_tokens"].to_i / 2
49
50
  ]
50
- if conditions.all?
51
- res["messages"].shift(1)
52
- res["turns"] = res["turns"].to_i - 1
53
- end
54
- res
51
+
52
+ @metadata["turns"] = @metadata["turns"].to_i - 1 if conditions.all?
53
+
55
54
  when "chat/completions"
56
- # obj = objectify
57
55
  ############################################################
58
56
  # Normal mode recuder defined here #
59
- # obj: old Hash object (uncomment a line above before use) #
60
- # res: new response Hash object to be modified #
57
+ # @messages: messages to this point #
61
58
  ############################################################
59
+
62
60
  conditions = [
63
- res.size > @num_retained_turns * 2 + 1
61
+ @messages.size > @num_retained_turns * 2 + 1
64
62
  ]
63
+
65
64
  if conditions.all?
66
- res.each_with_index do |ele, i|
65
+ @messages.each_with_index do |ele, i|
67
66
  if ele["role"] != "system"
68
- res.delete_at i
67
+ @messages.delete_at i
69
68
  break
70
69
  end
71
70
  end
72
71
  end
73
- res
74
72
  end
75
73
  end
76
74
  )
data/apps/code/code.json CHANGED
@@ -1,4 +1,8 @@
1
1
  {"messages": [
2
2
  {"role": "system",
3
- "content": "You are a friendly but professional software engineer who answers various questions, write computer program code, make decent suggestions, give helpful advice in response to a prompt from the user."}
3
+ "content": "You are a friendly but professional software engineer who answers various questions, writes computer program code, makes decent suggestions, and gives helpful advice in response to a prompt from the user."},
4
+ {"role": "user",
5
+ "content": "Can I ask something?"},
6
+ {"role": "assistant",
7
+ "content": "Sure!"}
4
8
  ]}
data/apps/code/code.md CHANGED
@@ -1,8 +1,13 @@
1
- You are a friendly but professional computer software assistant capable of answering various questions, writing computer program code, making decent suggestions, and giving helpful advice in response to a new prompt from the user. Create a detailed response to the following new prompt from the user and set your response to the "response" property of the JSON object shown below. The preceding context is stored in the value of the "messages" property. Always try to make your response relavant to the preceding context.
1
+ {{SYSTEM}}
2
+
3
+ Create a response "NEW PROMPT" from the user and set your response to the "response" property of the JSON object shown below. In "PAST MESSAGES", "assistant" refers to you. Make your response as detailed as possible.
2
4
 
3
5
  NEW PROMPT: {{PROMPT}}
4
6
 
5
- Make your response as detailed as possible.
7
+ PAST MESSAGES:
8
+ {{MESSAGES}}
9
+
10
+ JSON:
6
11
 
7
12
  ```json
8
13
  {
@@ -12,8 +17,7 @@ Make your response as detailed as possible.
12
17
  "turns": 1,
13
18
  "language": "English",
14
19
  "topics": [],
15
- "tokens": 109,
16
- "messages": [{"user": "Can I ask something?", "assistant": "Sure!\n\n###\n\n"}]
20
+ "tokens": 109
17
21
  }
18
22
  ```
19
23
 
@@ -21,12 +25,11 @@ Make sure the following content requirements are all fulfilled:
21
25
 
22
26
  - keep the value of the "mode" property at "chat"
23
27
  - set the new prompt to the "prompt" property
24
- - create your response to the new prompt in accordance with the "messages" and set it to "response"
25
- - insert both the new prompt and the response after all the existing items in the "messages"
28
+ - create your response to the new prompt based on "PAST MESSAGES" and set it to "response"
26
29
  - if the prompt is in a language other than the current value of "language", set the name of the new prompt language to "language" and make sure that "response" is in that language
27
30
  - make your response in the same language as the new prompt
28
31
  - analyze the topic of the new prompt and insert it at the end of the value list of the "topics" property
29
- - avoid giving a response that is the same or similar to one of the previous responses in "messages"
32
+ - avoid giving a response that is the same or similar to one of the previous responses in "PAST MESSAGES"
30
33
  - program code in the response must be embedded in a code block in the markdown text
31
34
  - update the value of "tokens" with the number of tokens of the resulting JSON object"
32
35
 
@@ -34,7 +37,7 @@ Make sure the following formal requirements are all fulfilled:
34
37
 
35
38
  - do not use invalid characters in the JSON object
36
39
  - escape double quotes and other special characters in the text values in the resulting JSON object
37
- - increment the value of "turns" by 1 and update the property so that the value of "turns" equals the number of the items in the "messages" of the resulting JSON object
40
+ - increment the value of "turns" by 1
38
41
  - check the validity of the generated JSON object and correct any possible parsing problems before returning it
39
42
 
40
43
  Add "\n\n###\n\n" at the end of the "response" value.