Skip to content

Commit 5fbbebb

Browse files
committed
Add chat api
1 parent 1fffc6f commit 5fbbebb

28 files changed

+106
-78
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ APP_WEBHOOK_PATH=
66
APP_API_TIMEOUT=
77
APP_MAX_GROUPS=
88
APP_MAX_USERS=
9-
APP_MAX_PROMPT_SENTENCES=
9+
APP_MAX_PROMPT_MESSAGES=
1010
APP_MAX_PROMPT_TOKENS=
1111

1212
HUMAN_NAME=

app/handlers/continue.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ const exec = (context) => check(context) && (
1818
async () => {
1919
updateHistory(context.id, (history) => history.erase());
2020
const prompt = getPrompt(context.userId);
21-
const { lastSentence } = prompt;
22-
if (lastSentence.isEnquiring) prompt.erase();
21+
const { lastMessage } = prompt;
22+
if (lastMessage.isEnquiring) prompt.erase();
2323
try {
24-
const { text, isFinishReasonStop } = await generateCompletion({ prompt: prompt.toString() });
24+
const { text, isFinishReasonStop } = await generateCompletion({ prompt });
2525
prompt.patch(text);
26-
if (lastSentence.isEnquiring && !isFinishReasonStop) prompt.write('', lastSentence.text);
26+
if (lastMessage.isEnquiring && !isFinishReasonStop) prompt.write('', lastMessage.text);
2727
setPrompt(context.userId, prompt);
28-
if (!lastSentence.isEnquiring) updateHistory(context.id, (history) => history.patch(text));
29-
const defaultActions = ALL_COMMANDS.filter(({ type }) => type === lastSentence.text);
28+
if (!lastMessage.isEnquiring) updateHistory(context.id, (history) => history.patch(text));
29+
const defaultActions = ALL_COMMANDS.filter(({ type }) => type === lastMessage.text);
3030
const actions = isFinishReasonStop ? defaultActions : [COMMAND_BOT_CONTINUE];
3131
context.pushText(text, actions);
3232
} catch (err) {

app/handlers/enquire.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { generateCompletion, getCommand } from '../../utils/index.js';
55
import { ALL_COMMANDS, COMMAND_BOT_CONTINUE, ENQUIRE_COMMANDS } from '../commands/index.js';
66
import Context from '../context.js';
77
import { getHistory, updateHistory } from '../history/index.js';
8-
import { getPrompt, setPrompt } from '../prompt/index.js';
8+
import { getPrompt, setPrompt, Prompt } from '../prompt/index.js';
99

1010
/**
1111
* @param {Context} context
@@ -26,13 +26,14 @@ const exec = (context) => check(context) && (
2626
updateHistory(context.id, (history) => history.erase());
2727
const command = getCommand(context.trimmedText);
2828
const history = getHistory(context.id);
29-
if (!history.lastRecord) return context;
30-
const reference = command.type === TYPE_TRANSLATE ? history.lastRecord.text : history.toString();
29+
if (!history.lastMessage) return context;
30+
const reference = command.type === TYPE_TRANSLATE ? history.lastMessage.text : history.toString();
3131
const content = `${command.prompt}\n${t('__COMPLETION_QUOTATION_MARK_OPENING')}\n${reference}\n${t('__COMPLETION_QUOTATION_MARK_CLOSING')}`;
32+
const partial = (new Prompt()).write(ROLE_HUMAN, content);
3233
const prompt = getPrompt(context.userId);
3334
prompt.write(ROLE_HUMAN, content).write(ROLE_AI);
3435
try {
35-
const { text, isFinishReasonStop } = await generateCompletion({ prompt: content });
36+
const { text, isFinishReasonStop } = await generateCompletion({ prompt: partial });
3637
prompt.patch(text);
3738
if (!isFinishReasonStop) prompt.write('', command.type);
3839
setPrompt(context.userId, prompt);

app/handlers/retry.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const exec = (context) => check(context) && (
2222
const prompt = getPrompt(context.userId);
2323
prompt.erase().write(ROLE_AI);
2424
try {
25-
const { text, isFinishReasonStop } = await generateCompletion({ prompt: prompt.toString() });
25+
const { text, isFinishReasonStop } = await generateCompletion({ prompt });
2626
prompt.patch(text);
2727
setPrompt(context.userId, prompt);
2828
updateHistory(context.id, (history) => history.write(config.BOT_NAME, text));

app/handlers/search.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const exec = (context) => check(context) && (
3030
}
3131
prompt.write(ROLE_HUMAN, `${trimmedText}。`).write(ROLE_AI);
3232
try {
33-
const { text, isFinishReasonStop } = await generateCompletion({ prompt: prompt.toString() });
33+
const { text, isFinishReasonStop } = await generateCompletion({ prompt });
3434
prompt.patch(text);
3535
setPrompt(context.userId, prompt);
3636
updateHistory(context.id, (history) => history.write(config.BOT_NAME, text));

app/handlers/talk.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const exec = (context) => check(context) && (
2626
const prompt = getPrompt(context.userId);
2727
prompt.write(ROLE_HUMAN, `${t('__COMPLETION_DEFAULT_AI_TONE')(config.BOT_TONE)}${context.trimmedText}。`).write(ROLE_AI);
2828
try {
29-
const { text, isFinishReasonStop } = await generateCompletion({ prompt: prompt.toString() });
29+
const { text, isFinishReasonStop } = await generateCompletion({ prompt });
3030
prompt.patch(text);
3131
setPrompt(context.userId, prompt);
3232
updateHistory(context.id, (history) => history.write(config.BOT_NAME, text));

app/history/history.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { encode } from 'gpt-3-encoder';
22
import config from '../../config/index.js';
3-
import Record from './record.js';
3+
import Message from './message.js';
44

5-
const MAX_RECORDS = config.APP_MAX_PROMPT_SENTENCES / 2;
5+
const MAX_MESSAGES = config.APP_MAX_PROMPT_MESSAGES / 2;
66
const MAX_TOKENS = config.APP_MAX_PROMPT_TOKENS / 2;
77

88
class History {
9-
records = [];
9+
messages = [];
1010

1111
/**
12-
* @returns {Record}
12+
* @returns {Message}
1313
*/
14-
get lastRecord() {
15-
return this.records.length > 0 ? this.records[this.records.length - 1] : null;
14+
get lastMessage() {
15+
return this.messages.length > 0 ? this.messages[this.messages.length - 1] : null;
1616
}
1717

1818
get tokenCount() {
@@ -21,8 +21,8 @@ class History {
2121
}
2222

2323
erase() {
24-
if (this.records.length > 0) {
25-
this.records.pop();
24+
if (this.messages.length > 0) {
25+
this.messages.pop();
2626
}
2727
return this;
2828
}
@@ -32,23 +32,23 @@ class History {
3232
* @param {string} content
3333
*/
3434
write(role, content) {
35-
if (this.records.length >= MAX_RECORDS || this.tokenCount >= MAX_TOKENS) {
36-
this.records.shift();
35+
if (this.messages.length >= MAX_MESSAGES || this.tokenCount >= MAX_TOKENS) {
36+
this.messages.shift();
3737
}
38-
this.records.push(new Record({ role, content }));
38+
this.messages.push(new Message({ role, content }));
3939
return this;
4040
}
4141

4242
/**
4343
* @param {string} content
4444
*/
4545
patch(content) {
46-
if (this.records.length < 1) return;
47-
this.records[this.records.length - 1].content += content;
46+
if (this.messages.length < 1) return;
47+
this.messages[this.messages.length - 1].content += content;
4848
}
4949

5050
toString() {
51-
return this.records.map((record) => record.toString()).join('\n');
51+
return this.messages.map((record) => record.toString()).join('\n');
5252
}
5353
}
5454

app/history/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ const removeHistory = (userId) => {
3333
};
3434

3535
const printHistories = () => {
36-
const records = Array.from(histories.keys())
37-
.filter((contextId) => getHistory(contextId).records.length > 0)
36+
const messages = Array.from(histories.keys())
37+
.filter((contextId) => getHistory(contextId).messages.length > 0)
3838
.map((contextId) => `\n=== ${contextId.slice(0, 6)} ===\n\n${getHistory(contextId).toString()}`);
39-
if (records.length < 1) return;
40-
console.info(records.join('\n'));
39+
if (messages.length < 1) return;
40+
console.info(messages.join('\n'));
4141
};
4242

4343
export {

app/history/record.js renamed to app/history/message.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class Record {
1+
class Message {
22
role;
33

44
content;
@@ -16,4 +16,4 @@ class Record {
1616
}
1717
}
1818

19-
export default Record;
19+
export default Message;

app/prompt/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const printPrompts = () => {
3030
};
3131

3232
export {
33+
Prompt,
3334
getPrompt,
3435
setPrompt,
3536
removePrompt,

0 commit comments

Comments
 (0)