djmuted commited on
Commit
93326ac
1 Parent(s): e6f4b4a

8k context fix

Browse files
Files changed (4) hide show
  1. src/config.json.example +1 -0
  2. src/openai.js +2 -1
  3. src/slack.js +39 -0
  4. src/utils.js +37 -9
src/config.json.example CHANGED
@@ -7,5 +7,6 @@
7
  "claudeId": ""
8
  }
9
  ],
 
10
  "apiKey": ""
11
  }
 
7
  "claudeId": ""
8
  }
9
  ],
10
+ "hfToken": "",
11
  "apiKey": ""
12
  }
src/openai.js CHANGED
@@ -3,7 +3,7 @@ const bodyParser = require('body-parser');
3
  const config = require('./config.json');
4
  const slack = require('./slack');
5
  const yup = require('yup');
6
- const { splitJsonArray, dataToResponse, buildPrompt } = require("./utils");
7
  const { Queue } = require('async-await-queue');
8
 
9
  const messageArraySchema = yup.array().of(
@@ -84,6 +84,7 @@ openaiRouter.post("/chat/completions", jsonParser, async (req, res) => {
84
  slackConfig.locked = true;
85
  try {
86
  await slack.sendChatReset(slackConfig);
 
87
  const response = await slack.waitForWebSocketResponse(slackConfig, messagesSplit, onData);
88
  slackConfig.locked = false;
89
  return response;
 
3
  const config = require('./config.json');
4
  const slack = require('./slack');
5
  const yup = require('yup');
6
+ const { splitJsonArray, dataToResponse, buildPrompt, wait } = require("./utils");
7
  const { Queue } = require('async-await-queue');
8
 
9
  const messageArraySchema = yup.array().of(
 
84
  slackConfig.locked = true;
85
  try {
86
  await slack.sendChatReset(slackConfig);
87
+ await wait(500);
88
  const response = await slack.waitForWebSocketResponse(slackConfig, messagesSplit, onData);
89
  slackConfig.locked = false;
90
  return response;
src/slack.js CHANGED
@@ -140,9 +140,48 @@ async function waitForWebSocketResponse(config, messages, onData) {
140
  });
141
  }
142
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
 
144
  module.exports = {
145
  sendPromptMessage,
146
  sendChatReset,
147
  waitForWebSocketResponse,
 
148
  };
 
140
  });
141
  }
142
 
143
+ function deleteAllMessages(config) {
144
+ const form = createBaseForm(config);
145
+ const headers = genHeaders(config);
146
+
147
+ const requestOptions = {
148
+ method: 'POST',
149
+ path: `/api/conversations.history?channel=${config.claudeId}`,
150
+ headers: {
151
+ ...headers,
152
+ ...form.getHeaders(),
153
+ },
154
+ };
155
+
156
+ const req = https.request(requestOptions, (res) => {
157
+ let data = '';
158
+ res.on('data', (chunk) => {
159
+ data += chunk;
160
+ });
161
+ res.on('end', () => {
162
+ const messages = JSON.parse(data).messages;
163
+ messages.forEach((message) => {
164
+ const deleteOptions = {
165
+ method: 'POST',
166
+ path: '/api/chat.delete',
167
+ headers: {
168
+ 'Content-Type': 'application/json',
169
+ },
170
+ };
171
+ const deleteReq = https.request(deleteOptions, (deleteRes) => { });
172
+ deleteReq.write(JSON.stringify({ channel: channelId, ts: message.ts }));
173
+ deleteReq.end();
174
+ });
175
+ });
176
+ });
177
+
178
+ req.end();
179
+ }
180
+
181
 
182
  module.exports = {
183
  sendPromptMessage,
184
  sendChatReset,
185
  waitForWebSocketResponse,
186
+ deleteAllMessages,
187
  };
src/utils.js CHANGED
@@ -45,7 +45,7 @@ function preparePrompt(messages) {
45
  }
46
 
47
  return `${author}: ${m.content.trim()}`;
48
- }).join('\n\n') + `\nAssistant: `;
49
  }
50
 
51
  const currentTime = () => {
@@ -73,25 +73,53 @@ function splitJsonArray(jsonArray, maxLength) {
73
  let currentChunk = [];
74
  let currentLength = 2; // Accounts for the opening and closing square brackets in the JSON array
75
 
76
- jsonArray.forEach((obj) => {
77
- const jsonString = JSON.stringify(obj);
78
- const objLength = jsonString.length + 1; // +1 for the comma between objects
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
  if (currentLength + objLength <= maxLength) {
81
- currentChunk.push(obj);
82
- currentLength += objLength;
83
  } else {
84
- // Store the current chunk and start a new one
 
 
 
 
 
 
 
85
  result.push(currentChunk);
86
  currentChunk = [obj];
87
  currentLength = 2 + objLength;
88
  }
89
- });
90
 
91
  if (currentChunk.length > 0) {
92
  result.push(currentChunk);
93
  }
94
 
 
 
 
 
 
 
 
 
 
95
  return result;
96
  }
97
 
@@ -121,7 +149,7 @@ const dataToResponse = (
121
  reason = null
122
  ) => {
123
  const currDate = new Date();
124
- const contentData = { content: data, role: 'assistant' };
125
  const contentName = stream ? 'delta' : 'message';
126
 
127
  return {
 
45
  }
46
 
47
  return `${author}: ${m.content.trim()}`;
48
+ }).join('\n\n');
49
  }
50
 
51
  const currentTime = () => {
 
73
  let currentChunk = [];
74
  let currentLength = 2; // Accounts for the opening and closing square brackets in the JSON array
75
 
76
+ const jail = '(OOC: This is only a part of the context, reply only "understood")';
77
+ const assistant = "\n\nAssistant: ";
78
+
79
+ const addObjectToChunk = (object, chunk) => {
80
+ chunk.push(object);
81
+ return currentLength + JSON.stringify(object).length + 1;
82
+ };
83
+
84
+ const appendTextToContent = (object, text) => {
85
+ const newObj = JSON.parse(JSON.stringify(object));
86
+ newObj.content += text;
87
+ return newObj;
88
+ };
89
+
90
+ for (const obj of jsonArray) {
91
+ const objLength = JSON.stringify(obj).length + 1;
92
 
93
  if (currentLength + objLength <= maxLength) {
94
+ currentLength = addObjectToChunk(obj, currentChunk);
 
95
  } else {
96
+ const lastObjectInChunk = currentChunk[currentChunk.length - 1];
97
+ const lastObjectWithJail = appendTextToContent(lastObjectInChunk, ` ${jail}`);
98
+ const lastObjectWithJailLength = JSON.stringify(lastObjectWithJail).length + 1;
99
+
100
+ if (currentLength - JSON.stringify(lastObjectInChunk).length - 1 + lastObjectWithJailLength <= maxLength) {
101
+ currentChunk[currentChunk.length - 1] = lastObjectWithJail;
102
+ }
103
+
104
  result.push(currentChunk);
105
  currentChunk = [obj];
106
  currentLength = 2 + objLength;
107
  }
108
+ }
109
 
110
  if (currentChunk.length > 0) {
111
  result.push(currentChunk);
112
  }
113
 
114
+ const lastChunk = result[result.length - 1];
115
+ const lastObjectInLastChunk = lastChunk[lastChunk.length - 1];
116
+ const lastObjectWithAssistant = appendTextToContent(lastObjectInLastChunk, assistant);
117
+ const lastObjectWithAssistantLength = JSON.stringify(lastObjectWithAssistant).length + 1;
118
+
119
+ if (currentLength - JSON.stringify(lastObjectInLastChunk).length - 1 + lastObjectWithAssistantLength <= maxLength) {
120
+ lastChunk[lastChunk.length - 1] = lastObjectWithAssistant;
121
+ }
122
+
123
  return result;
124
  }
125
 
 
149
  reason = null
150
  ) => {
151
  const currDate = new Date();
152
+ const contentData = { content: data?.toString().trim(), role: 'assistant' };
153
  const contentName = stream ? 'delta' : 'message';
154
 
155
  return {