Skip to content

Commit

Permalink
add typescript tokenizer and better chat trimmer
Browse files Browse the repository at this point in the history
  • Loading branch information
cogentapps committed Mar 20, 2023
1 parent 216ecd5 commit b6881c6
Show file tree
Hide file tree
Showing 24 changed files with 477 additions and 1,362 deletions.
61 changes: 31 additions & 30 deletions app/craco.config.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
const cracoWasm = require("craco-wasm");

/*
{
"plugins": [
[
"formatjs",
{
"idInterpolationPattern": "[sha512:contenthash:base64:6]",
"ast": true
}
]
]
}
*/
const webpack = require("webpack");

module.exports = {
plugins: [
cracoWasm(),
],
eslint: {
enable: false
},
babel: {
plugins: [
cracoWasm(),
],
eslint: {
enable: false
[
'formatjs',
{
removeDefaultMessage: false,
idInterpolationPattern: '[sha512:contenthash:base64:6]',
ast: true
}
]
]
},
webpack: {
configure: {
resolve: {
fallback: {
buffer: require.resolve("buffer"),
},
},
plugins: [
new webpack.ProvidePlugin({
Buffer: ["buffer", "Buffer"],
}),
],
},
babel: {
plugins: [
[
'formatjs',
{
removeDefaultMessage: false,
idInterpolationPattern: '[sha512:contenthash:base64:6]',
ast: true
}
]
]
}
},
}
1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@reduxjs/toolkit": "^1.9.3",
"@svgr/webpack": "^6.5.1",
"broadcast-channel": "^4.20.2",
"buffer": "^6.0.3",
"csv": "^6.2.8",
"expiry-set": "^1.0.0",
"idb-keyval": "^6.2.0",
Expand Down
3 changes: 1 addition & 2 deletions app/src/chat-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { createStreamingChatCompletion } from './openai';
import { createTitle } from './titles';
import { ellipsize, sleep } from './utils';
import * as idb from './idb';
import { selectMessagesToSendSafely } from './tokenizer';

export const channel = new BroadcastChannel('chats');

Expand Down Expand Up @@ -145,7 +144,7 @@ export class ChatManager extends EventEmitter {
this.emit(chat.id);
channel.postMessage({ type: 'chat-update', data: serializeChat(chat) });

const messagesToSend = selectMessagesToSendSafely(messages.map(getOpenAIMessageFromMessage));
const messagesToSend = messages.map(getOpenAIMessageFromMessage)

const { emitter, cancel } = await createStreamingChatCompletion(messagesToSend, requestedParameters);

Expand Down
2 changes: 1 addition & 1 deletion app/src/components/pages/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useAppContext } from '../../context';
import { backend } from '../../backend';
import { Page } from '../page';

const Message = React.lazy(() => import(/* webpackChunkName: "message" */ '../message'));
const Message = React.lazy(() => import(/* webpackPreload: true */ '../message'));

const Messages = styled.div`
max-height: 100%;
Expand Down
14 changes: 13 additions & 1 deletion app/src/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export async function createStreamingChatCompletion(messages: OpenAIMessage[], p

const emitter = new EventEmitter();

const messagesToSend = [...messages].filter(m => m.role !== 'app');
let messagesToSend = [...messages].filter(m => m.role !== 'app');

for (let i = messagesToSend.length - 1; i >= 0; i--) {
const m = messagesToSend[i];
Expand All @@ -87,6 +87,8 @@ export async function createStreamingChatCompletion(messages: OpenAIMessage[], p
content: (parameters.initialSystemPrompt || defaultSystemPrompt).replace('{{ datetime }}', new Date().toLocaleString()),
});

messagesToSend = await selectMessagesToSendSafely(messagesToSend, 2048);

const eventSource = new SSE('https://api.openai.com/v1/chat/completions', {
method: "POST",
headers: {
Expand Down Expand Up @@ -151,4 +153,14 @@ export async function createStreamingChatCompletion(messages: OpenAIMessage[], p
emitter,
cancel: () => eventSource.close(),
};
}

async function selectMessagesToSendSafely(messages: OpenAIMessage[], maxTokens: number) {
const { ChatHistoryTrimmer } = await import(/* webpackPreload: true */ './tokenizer/chat-history-trimmer');
const compressor = new ChatHistoryTrimmer(messages, {
maxTokens,
preserveFirstUserMessage: true,
preserveSystemPrompt: true,
});
return compressor.process();
}
134 changes: 0 additions & 134 deletions app/src/tiktoken/dist/README.md

This file was deleted.

1 change: 0 additions & 1 deletion app/src/tiktoken/dist/bundler.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion app/src/tiktoken/dist/bundler.js

This file was deleted.

8 changes: 0 additions & 8 deletions app/src/tiktoken/dist/init.d.ts

This file was deleted.

20 changes: 0 additions & 20 deletions app/src/tiktoken/dist/init.js

This file was deleted.

37 changes: 0 additions & 37 deletions app/src/tiktoken/dist/package.json

This file was deleted.

Loading

0 comments on commit b6881c6

Please sign in to comment.