The example CLI now has improved token counting UI. The tiktoken wasm processing is moved to the main package instead.master
@@ -1,15 +1,3 @@ | |||||
const { wasmLoader } = require('esbuild-plugin-wasm'); | |||||
module.exports = { | module.exports = { | ||||
target: 'esnext', | target: 'esnext', | ||||
plugins: ({ isCJS }) => { | |||||
if (isCJS) { | |||||
return [ | |||||
wasmLoader({ | |||||
mode: 'deferred', | |||||
}), | |||||
]; | |||||
} | |||||
return []; | |||||
}, | |||||
} | } |
@@ -7,7 +7,7 @@ export interface CreateOpenAiClientParams { | |||||
} | } | ||||
export const createOpenAiClient = (params: CreateOpenAiClientParams) => ( | export const createOpenAiClient = (params: CreateOpenAiClientParams) => ( | ||||
createAiClient({ | |||||
createAiClient<OpenAi.PlatformEventEmitter>({ | |||||
platform: OpenAi.PLATFORM_ID, | platform: OpenAi.PLATFORM_ID, | ||||
platformConfiguration: { | platformConfiguration: { | ||||
apiKey: params.apiKey, | apiKey: params.apiKey, | ||||
@@ -1,13 +1,13 @@ | |||||
import yargs from 'yargs'; | import yargs from 'yargs'; | ||||
import { hideBin } from 'yargs/helpers'; | import { hideBin } from 'yargs/helpers'; | ||||
import { OpenAi, PlatformEventEmitter } from '@modal-sh/mio-ai'; | |||||
import { OpenAi } from '@modal-sh/mio-ai'; | |||||
import { TextPrompt, isCancel } from '@clack/core'; | import { TextPrompt, isCancel } from '@clack/core'; | ||||
import { createOpenAiClient } from './app'; | import { createOpenAiClient } from './app'; | ||||
export type Argv = Record<string, unknown>; | export type Argv = Record<string, unknown>; | ||||
const receiveData = ( | const receiveData = ( | ||||
aiClient: PlatformEventEmitter, | |||||
aiClient: OpenAi.PlatformEventEmitter, | |||||
theContent: string, | theContent: string, | ||||
argv: Record<string, unknown>, | argv: Record<string, unknown>, | ||||
memory: OpenAi.MessageObject[], | memory: OpenAi.MessageObject[], | ||||
@@ -15,11 +15,20 @@ const receiveData = ( | |||||
let completionTokens: number; | let completionTokens: number; | ||||
const normalizedChatMessage = OpenAi.normalizeChatMessage(theContent); | const normalizedChatMessage = OpenAi.normalizeChatMessage(theContent); | ||||
const model = ( | const model = ( | ||||
argv.model as OpenAi.ChatCompletionModel | argv.model as OpenAi.ChatCompletionModel | ||||
?? OpenAi.ChatCompletionModel.GPT_3_5_TURBO | ?? OpenAi.ChatCompletionModel.GPT_3_5_TURBO | ||||
); | ); | ||||
const { length: promptTokens } = OpenAi.getPromptTokens( | |||||
normalizedChatMessage, | |||||
model, | |||||
); | |||||
process.stdout.write(`(${promptTokens} prompt tokens)`); | |||||
if (argv.memory) { | |||||
process.stdout.write(`\n${memory.length} memory items`); | |||||
} | |||||
process.stdout.write('\n\n'); | |||||
let assistantMessage: Partial<OpenAi.MessageObject>; | let assistantMessage: Partial<OpenAi.MessageObject>; | ||||
aiClient.on<OpenAi.ChatCompletionChunkDataEvent>('data', (d) => { | aiClient.on<OpenAi.ChatCompletionChunkDataEvent>('data', (d) => { | ||||
@@ -41,12 +50,7 @@ const receiveData = ( | |||||
memory.push(m); | memory.push(m); | ||||
}); | }); | ||||
memory.push(assistantMessage as OpenAi.MessageObject); | memory.push(assistantMessage as OpenAi.MessageObject); | ||||
process.stdout.write('\n\n'); | |||||
const { length: promptTokens } = OpenAi.getPromptTokens( | |||||
normalizedChatMessage, | |||||
model, | |||||
); | |||||
process.stdout.write(`info:\n${promptTokens} prompt tokens\n${completionTokens} completion tokens`); | |||||
process.stdout.write(`\n(${completionTokens} completion tokens)`); | |||||
if (argv.memory) { | if (argv.memory) { | ||||
process.stdout.write(`\n${memory.length} memory items`); | process.stdout.write(`\n${memory.length} memory items`); | ||||
} | } | ||||
@@ -120,7 +124,6 @@ const main = async (argv: Argv) => { | |||||
done = true; | done = true; | ||||
break; | break; | ||||
} | } | ||||
process.stdout.write('\n'); | |||||
// eslint-disable-next-line no-await-in-loop | // eslint-disable-next-line no-await-in-loop | ||||
await receiveData(aiClient, content, argv, memory); | await receiveData(aiClient, content, argv, memory); | ||||
} catch { | } catch { | ||||
@@ -129,7 +132,7 @@ const main = async (argv: Argv) => { | |||||
} | } | ||||
} while (!done); | } while (!done); | ||||
process.stdout.write('\ninfo:\nUser exited loop\n\n'); | |||||
process.stdout.write('(User exited loop)\n\n'); | |||||
return resolveResult; | return resolveResult; | ||||
}; | }; | ||||
@@ -15,6 +15,7 @@ | |||||
"devDependencies": { | "devDependencies": { | ||||
"@types/node": "^18.14.1", | "@types/node": "^18.14.1", | ||||
"dotenv": "^16.0.3", | "dotenv": "^16.0.3", | ||||
"esbuild-plugin-wasm": "^1.0.0", | |||||
"eslint": "^8.35.0", | "eslint": "^8.35.0", | ||||
"eslint-config-lxsmnsyc": "^0.5.0", | "eslint-config-lxsmnsyc": "^0.5.0", | ||||
"pridepack": "2.4.4", | "pridepack": "2.4.4", | ||||
@@ -0,0 +1,15 @@ | |||||
const { wasmLoader } = require('esbuild-plugin-wasm'); | |||||
module.exports = { | |||||
target: 'esnext', | |||||
plugins: ({ isCJS }) => { | |||||
if (isCJS) { | |||||
return [ | |||||
wasmLoader({ | |||||
mode: 'deferred', | |||||
}), | |||||
]; | |||||
} | |||||
return []; | |||||
}, | |||||
} |
@@ -1,3 +0,0 @@ | |||||
{ | |||||
"target": "es2018" | |||||
} |
@@ -18,7 +18,7 @@ export type PlatformEventEmitter = ( | |||||
| ElevenLabsImpl.PlatformEventEmitter | | ElevenLabsImpl.PlatformEventEmitter | ||||
); | ); | ||||
export const createAiClient = (configParams: PlatformConfig): PlatformEventEmitter => { | |||||
export const createAiClient = <T extends PlatformEventEmitter = PlatformEventEmitter> (configParams: PlatformConfig): T => { | |||||
const { | const { | ||||
platform, | platform, | ||||
platformConfiguration, | platformConfiguration, | ||||
@@ -32,5 +32,5 @@ export const createAiClient = (configParams: PlatformConfig): PlatformEventEmitt | |||||
throw new Error(`Unsupported platform: ${platform}. Supported platforms are: ${supportedPlatforms}`); | throw new Error(`Unsupported platform: ${platform}. Supported platforms are: ${supportedPlatforms}`); | ||||
} | } | ||||
return new platformModule.PlatformEventEmitterImpl(platformConfiguration); | |||||
return new platformModule.PlatformEventEmitterImpl(platformConfiguration) as T; | |||||
}; | }; |
@@ -1295,6 +1295,11 @@ es-to-primitive@^1.2.1: | |||||
is-date-object "^1.0.1" | is-date-object "^1.0.1" | ||||
is-symbol "^1.0.2" | is-symbol "^1.0.2" | ||||
esbuild-plugin-wasm@^1.0.0: | |||||
version "1.0.0" | |||||
resolved "https://registry.yarnpkg.com/esbuild-plugin-wasm/-/esbuild-plugin-wasm-1.0.0.tgz#4ca95ac4ae553331d2b0099223f8713a49b6cff2" | |||||
integrity sha512-iXIf3hwfqorExG66/eNr3U8JakIZuge70nMNQtinvxbzdljQ/RjvwaBiGPqF/DvuIumUApbe3zj2kqHLVyc7uQ== | |||||
esbuild@^0.17.4, esbuild@^0.17.5: | esbuild@^0.17.4, esbuild@^0.17.5: | ||||
version "0.17.16" | version "0.17.16" | ||||
resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.17.16.tgz#5efec24a8ff29e0c157359f27e1b5532a728b720" | resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.17.16.tgz#5efec24a8ff29e0c157359f27e1b5532a728b720" | ||||