Skip to content

Commit

Permalink
updated alan
Browse files Browse the repository at this point in the history
  • Loading branch information
Leask committed Jul 26, 2024
1 parent a623c4b commit 1437c4e
Showing 1 changed file with 65 additions and 7 deletions.
72 changes: 65 additions & 7 deletions lib/alan.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -675,10 +675,7 @@ const checkEmbeddingInput = async (input, model) => {
const getInput = () => arrInput.join(' ');
const _model = MODELS[model];
assert(_model, `Invalid model: '${model}'.`);
while ((await countTokens(getInput(), { fast: true }) > _model.contextWindow)
|| (await countTokens(getInput()) > _model.contextWindow)) {
arrInput.pop();
}
await trimPrompt(getInput, arrInput.pop, _model.contextWindow);
return getInput();
};

Expand Down Expand Up @@ -925,16 +922,16 @@ const talk = async (input, options) => {
default:
throwError(`Invalid AI engine: '${engine}'.`);
}
while (await countTokens([
await trimPrompt(() => [
...sys, ...messages, buildVertexMessage(input, { role: user })
]) >= _MODEL.maxInputTokens) {
], () => {
if (messages.length) {
session.messages.shift();
msgBuilder && msgBuilder();
} else {
input = trimTailing(trimTailing(input).slice(0, -1)) + '...';
}
}
}, _MODEL.maxInputTokens);
const chat = { request: input };
const attachments = [];
(options?.attachments || []).filter(
Expand Down Expand Up @@ -1030,6 +1027,63 @@ const distillFile = async (attachments, o) => {
return packResult(resp);
};

const prompt = async (input, options) => {
const preferredEngines = [
{ client: OPENAI, func: promptChatGPT },
{ client: VERTEX, func: promptGemini },
{ client: GEMINI, func: promptVertex },
];
for (const engine of preferredEngines) {
if (clients[engine.client]) {
const extra = {};
if (engine.client === OPENAI && options?.fast) {
extra.model = GPT_4O_MINI;
}
return await engine.func(input, { ...extra, ...options || {} });
}
}
throwError('No AI provider is available.');
};

const trimPrompt = async (getPrompt, trimFunc, contextWindow, options) => {
let i = 0;
while ((await countTokens(await getPrompt(), { fast: true }) > contextWindow)
|| (await countTokens(await getPrompt()) > contextWindow)) {
await trimFunc();
if (~~options?.maxTry && ++i >= ~~options?.maxTry) { break; }
};
};

const analyzeSessions = async (sessionIds, options) => {
chatConfig.sessions = options?.session;
const ids = ensureArray(sessionIds);
const mdl = MODELS[DEFAULT_MODELS[DEFAULT_CHAT_ENGINE]];
const [sses, resp] = [{}, {}];
for (const i in ids) { sses[ids[i]] = await getSession(ids[i], options); }
const pmt = options?.prompt || (
'Help me organize the dialogues in the following JSON into a title '
+ 'dictionary and return it in JSON format. The returned JSON uses the '
+ 'key from the original JSON session data as the key and the title as '
+ 'the value. The following is a conversation data that needs to be '
+ 'organized: ');
const getInput = () => `${pmt}\n\`\`\`JSON\n${JSON.stringify(sses, null, 2)}\n\`\`\``;
mdl.contextWindow = 2000
await trimPrompt(getInput, () => {
if (!Object.values(sses).sort((x, y) =>
y.messages.length - x.messages.length)[0].messages.shift()) {
delete sses[Object.keys(sses).map(x => [
x, JSON.stringify(sses[x]).length,
]).sort((x, y) => y[1] - x[1])[0][0]];
}
}, mdl.contextWindow);
const aiResp = (await prompt(getInput(), {
jsonMode: true, fast: true, ...options || {}
}))?.response[0].json;
assert(aiResp, 'Unable to analyze sessions.');
ids.map(x => resp[x] = aiResp[x] || null)
return Array.isArray(sessionIds) ? resp : resp[sessionIds[0]];
};

export default init;
export {
_NEED,
Expand All @@ -1050,6 +1104,7 @@ export {
MODELS,
RETRIEVAL,
TEXT_EMBEDDING_3_SMALL,
analyzeSessions,
buildGptTrainingCase,
buildGptTrainingCases,
cancelGptFineTuningJob,
Expand All @@ -1074,6 +1129,7 @@ export {
getLatestMessage,
getMaxChatPromptLimit,
getRun,
getSession,
getThread,
init,
initChat,
Expand All @@ -1085,6 +1141,7 @@ export {
listMessages,
listOpenAIModels,
modifyAssistant,
prompt,
promptAssistant,
promptChatGPT,
promptGemini,
Expand All @@ -1094,6 +1151,7 @@ export {
run,
tailGptFineTuningEvents,
talk,
trimPrompt,
uploadFile,
uploadFileForAssistants,
uploadFileForFineTuning,
Expand Down

0 comments on commit 1437c4e

Please sign in to comment.