|
|
@@ -1,13 +1,13 @@ |
|
|
|
import yargs from 'yargs'; |
|
|
|
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 { createOpenAiClient } from './app'; |
|
|
|
|
|
|
|
export type Argv = Record<string, unknown>; |
|
|
|
|
|
|
|
const receiveData = ( |
|
|
|
aiClient: PlatformEventEmitter, |
|
|
|
aiClient: OpenAi.PlatformEventEmitter, |
|
|
|
theContent: string, |
|
|
|
argv: Record<string, unknown>, |
|
|
|
memory: OpenAi.MessageObject[], |
|
|
@@ -15,11 +15,20 @@ const receiveData = ( |
|
|
|
let completionTokens: number; |
|
|
|
|
|
|
|
const normalizedChatMessage = OpenAi.normalizeChatMessage(theContent); |
|
|
|
|
|
|
|
const model = ( |
|
|
|
argv.model as OpenAi.ChatCompletionModel |
|
|
|
?? 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>; |
|
|
|
aiClient.on<OpenAi.ChatCompletionChunkDataEvent>('data', (d) => { |
|
|
@@ -41,12 +50,7 @@ const receiveData = ( |
|
|
|
memory.push(m); |
|
|
|
}); |
|
|
|
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) { |
|
|
|
process.stdout.write(`\n${memory.length} memory items`); |
|
|
|
} |
|
|
@@ -120,7 +124,6 @@ const main = async (argv: Argv) => { |
|
|
|
done = true; |
|
|
|
break; |
|
|
|
} |
|
|
|
process.stdout.write('\n'); |
|
|
|
// eslint-disable-next-line no-await-in-loop |
|
|
|
await receiveData(aiClient, content, argv, memory); |
|
|
|
} catch { |
|
|
@@ -129,7 +132,7 @@ const main = async (argv: Argv) => { |
|
|
|
} |
|
|
|
} while (!done); |
|
|
|
|
|
|
|
process.stdout.write('\ninfo:\nUser exited loop\n\n'); |
|
|
|
process.stdout.write('(User exited loop)\n\n'); |
|
|
|
return resolveResult; |
|
|
|
}; |
|
|
|
|
|
|
|