Skip to content

Commit

Permalink
🔄 refactor: Consolidate Ask/Edit Controllers (danny-avila#1365)
Browse files Browse the repository at this point in the history
* refactor(Ask/Edit): consolidate ask/edit controllers between the main modules and openAI controllers to reduce repetition of code and increase reusability

* fix(winston/logger): circular dependency issue

* fix(config/scripts): fix script imports

* refactor(indexSync): make not configured message an info log message

* chore: create a rollup script for api/server/index.js to check circular dependencies

* chore: bump @keyv/redis
  • Loading branch information
danny-avila authored Dec 15, 2023
1 parent 0c5a5e7 commit 6d4a8c8
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 339 deletions.
2 changes: 1 addition & 1 deletion app/clients/AnthropicClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ class AnthropicClient extends BaseClient {
top_p,
top_k,
};
logger.debug('[AnthropicClient]', { requestOptions });
logger.debug('[AnthropicClient]', { ...requestOptions });
const response = await client.completions.create(requestOptions);

signal.addEventListener('abort', () => {
Expand Down
11 changes: 8 additions & 3 deletions config/winston.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const path = require('path');
const winston = require('winston');
require('winston-daily-rotate-file');
const { redact, deepObjectFormat } = require('./parsers');
const { isEnabled } = require('~/server/utils/handleText');

const logDir = path.join(__dirname, '..', 'logs');

Expand Down Expand Up @@ -67,7 +66,10 @@ const transports = [
// );
// }

if (isEnabled && isEnabled(DEBUG_LOGGING)) {
if (
(typeof DEBUG_LOGGING === 'string' && DEBUG_LOGGING?.toLowerCase() === 'true') ||
DEBUG_LOGGING === true
) {
transports.push(
new winston.transports.DailyRotateFile({
level: 'debug',
Expand All @@ -88,7 +90,10 @@ const consoleFormat = winston.format.combine(
winston.format.printf((info) => `${info.timestamp} ${info.level}: ${info.message}`),
);

if (isEnabled && isEnabled(DEBUG_CONSOLE)) {
if (
(typeof DEBUG_CONSOLE === 'string' && DEBUG_CONSOLE?.toLowerCase() === 'true') ||
DEBUG_CONSOLE === true
) {
transports.push(
new winston.transports.Console({
level: 'debug',
Expand Down
2 changes: 2 additions & 0 deletions lib/db/indexSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ async function indexSync(req, res, next) {
logger.error('[indexSync] Trouble creating indices, try restarting the server.', err);
}
}, 750);
} else if (err.message.includes('Meilisearch not configured')) {
logger.info('[indexSync] Meilisearch not configured, search will be disabled.');
} else {
logger.error('[indexSync] error', err);
// res.status(500).json({ error: 'Server error' });
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@anthropic-ai/sdk": "^0.5.4",
"@azure/search-documents": "^12.0.0",
"@keyv/mongo": "^2.1.8",
"@keyv/redis": "^2.8.0",
"@keyv/redis": "^2.8.1",
"@langchain/google-genai": "^0.0.2",
"axios": "^1.3.4",
"bcryptjs": "^2.4.3",
Expand Down
70 changes: 43 additions & 27 deletions server/controllers/AskController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ const { saveMessage, getConvoTitle, getConvo } = require('~/models');
const { createAbortController, handleAbortError } = require('~/server/middleware');
const { logger } = require('~/config');

const AskController = async (req, res, next, initializeClient) => {
const AskController = async (req, res, next, initializeClient, addTitle) => {
let {
text,
endpointOption,
conversationId,
parentMessageId = null,
overrideParentMessageId = null,
} = req.body;

logger.debug('[AskController]', { text, conversationId, ...endpointOption });

let metadata;
let userMessage;
let promptTokens;
Expand All @@ -21,8 +23,11 @@ const AskController = async (req, res, next, initializeClient) => {
let lastSavedTimestamp = 0;
let saveDelay = 100;
const sender = getResponseSender({ ...endpointOption, model: endpointOption.modelOptions.model });
const newConvo = !conversationId;
const user = req.user.id;

const addMetadata = (data) => (metadata = data);

const getReqData = (data = {}) => {
for (let key in data) {
if (key === 'userMessage') {
Expand Down Expand Up @@ -50,6 +55,7 @@ const AskController = async (req, res, next, initializeClient) => {
conversationId,
parentMessageId: overrideParentMessageId ?? userMessageId,
text: partialText,
model: endpointOption.modelOptions.model,
unfinished: true,
cancelled: false,
error: false,
Expand All @@ -62,46 +68,50 @@ const AskController = async (req, res, next, initializeClient) => {
}
},
});
try {
const addMetadata = (data) => (metadata = data);
const getAbortData = () => ({
conversationId,
messageId: responseMessageId,
sender,
parentMessageId: overrideParentMessageId ?? userMessageId,
text: getPartialText(),
userMessage,
promptTokens,
});

const { abortController, onStart } = createAbortController(req, res, getAbortData);
const getAbortData = () => ({
sender,
conversationId,
messageId: responseMessageId,
parentMessageId: overrideParentMessageId ?? userMessageId,
text: getPartialText(),
userMessage,
promptTokens,
});

const { client } = await initializeClient({ req, res, endpointOption });
const { abortController, onStart } = createAbortController(req, res, getAbortData);

let response = await client.sendMessage(text, {
// debug: true,
try {
const { client } = await initializeClient({ req, res, endpointOption });
const messageOptions = {
user,
conversationId,
parentMessageId,
conversationId,
overrideParentMessageId,
...endpointOption,
getReqData,
onStart,
addMetadata,
abortController,
onProgress: progressCallback.call(null, {
res,
text,
parentMessageId: overrideParentMessageId ?? userMessageId,
parentMessageId: overrideParentMessageId || userMessageId,
}),
onStart,
getReqData,
addMetadata,
abortController,
});
};

let response = await client.sendMessage(text, messageOptions);

if (overrideParentMessageId) {
response.parentMessageId = overrideParentMessageId;
}

if (metadata) {
response = { ...response, ...metadata };
}

if (overrideParentMessageId) {
response.parentMessageId = overrideParentMessageId;
if (client.options.attachments) {
userMessage.files = client.options.attachments;
delete userMessage.image_urls;
}

sendMessage(res, {
Expand All @@ -116,7 +126,13 @@ const AskController = async (req, res, next, initializeClient) => {
await saveMessage({ ...response, user });
await saveMessage(userMessage);

// TODO: add title service
if (addTitle && parentMessageId === '00000000-0000-0000-0000-000000000000' && newConvo) {
addTitle(req, {
text,
response,
client,
});
}
} catch (error) {
const partialText = getPartialText();
handleAbortError(res, req, error, {
Expand Down
47 changes: 22 additions & 25 deletions server/controllers/EditController.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ const EditController = async (req, res, next, initializeClient) => {
parentMessageId = null,
overrideParentMessageId = null,
} = req.body;

logger.debug('[EditController]', {
text,
generation,
isContinued,
conversationId,
...endpointOption,
});

let metadata;
let userMessage;
let promptTokens;
Expand All @@ -48,6 +50,7 @@ const EditController = async (req, res, next, initializeClient) => {
generation,
onProgress: ({ text: partialText }) => {
const currentTimestamp = Date.now();

if (currentTimestamp - lastSavedTimestamp > saveDelay) {
lastSavedTimestamp = currentTimestamp;
saveMessage({
Expand All @@ -56,6 +59,7 @@ const EditController = async (req, res, next, initializeClient) => {
conversationId,
parentMessageId: overrideParentMessageId ?? userMessageId,
text: partialText,
model: endpointOption.modelOptions.model,
unfinished: true,
cancelled: false,
isEdited: true,
Expand All @@ -69,19 +73,20 @@ const EditController = async (req, res, next, initializeClient) => {
}
},
});
try {
const getAbortData = () => ({
conversationId,
messageId: responseMessageId,
sender,
parentMessageId: overrideParentMessageId ?? userMessageId,
text: getPartialText(),
userMessage,
promptTokens,
});

const { abortController, onStart } = createAbortController(req, res, getAbortData);
const getAbortData = () => ({
conversationId,
messageId: responseMessageId,
sender,
parentMessageId: overrideParentMessageId ?? userMessageId,
text: getPartialText(),
userMessage,
promptTokens,
});

const { abortController, onStart } = createAbortController(req, res, getAbortData);

try {
const { client } = await initializeClient({ req, res, endpointOption });

let response = await client.sendMessage(text, {
Expand All @@ -93,25 +98,22 @@ const EditController = async (req, res, next, initializeClient) => {
parentMessageId,
responseMessageId,
overrideParentMessageId,
...endpointOption,
onProgress: progressCallback.call(null, {
res,
text,
parentMessageId: overrideParentMessageId ?? userMessageId,
}),
getReqData,
onStart,
addMetadata,
abortController,
onProgress: progressCallback.call(null, {
res,
text,
parentMessageId: overrideParentMessageId || userMessageId,
}),
});

if (metadata) {
response = { ...response, ...metadata };
}

if (overrideParentMessageId) {
response.parentMessageId = overrideParentMessageId;
}
await saveMessage({ ...response, user });

sendMessage(res, {
title: await getConvoTitle(user, conversationId),
Expand All @@ -121,11 +123,6 @@ const EditController = async (req, res, next, initializeClient) => {
responseMessage: response,
});
res.end();

await saveMessage({ ...response, user });
await saveMessage(userMessage);

// TODO: add title service
} catch (error) {
const partialText = getPartialText();
handleAbortError(res, req, error, {
Expand Down
Loading

0 comments on commit 6d4a8c8

Please sign in to comment.