Skip to content

Commit

Permalink
refactor(api): Central Logging 📜 (#1348)
Browse files Browse the repository at this point in the history
* WIP: initial logging changes
add several transports in ~/config/winston
omit messages in logs, truncate long strings
add short blurb in dotenv for debug logging
GoogleClient: using logger
OpenAIClient: using logger, handleOpenAIErrors
Adding typedef for payload message
bumped winston and using winston-daily-rotate-file
moved config for server paths to ~/config dir
Added `DEBUG_LOGGING=true` to .env.example

* WIP: Refactor logging statements in code

* WIP: Refactor logging statements and import configurations

* WIP: Refactor logging statements and import configurations

* refactor: broadcast Redis initialization message with `info` not `debug`

* refactor: complete Refactor logging statements and import configurations

* chore: delete unused tools

* fix: circular dependencies due to accessing logger

* refactor(handleText): handle booleans and write tests

* refactor: redact sensitive values, better formatting

* chore: improve log formatting, avoid passing strings to 2nd arg

* fix(ci): fix jest tests due to logger changes

* refactor(getAvailablePluginsController): cache plugins as they are static and avoids async addOpenAPISpecs call every time

* chore: update docs

* chore: update docs

* chore: create separate meiliSync logger, clean up logs to avoid being unnecessarily verbose

* chore: spread objects where they are commonly logged to allow string truncation

* chore: improve error log formatting
  • Loading branch information
danny-avila authored Dec 14, 2023
1 parent 49571ac commit ea1dd59
Show file tree
Hide file tree
Showing 115 changed files with 1,271 additions and 1,001 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
APP_TITLE=LibreChat
# CUSTOM_FOOTER="My custom footer"

DEBUG_LOGGING=true

HOST=localhost
PORT=3080

Expand Down
5 changes: 3 additions & 2 deletions api/app/bingai.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require('dotenv').config();
const { KeyvFile } = require('keyv-file');
const { getUserKey, checkUserKeyExpiry } = require('../server/services/UserService');
const { getUserKey, checkUserKeyExpiry } = require('~/server/services/UserService');
const { logger } = require('~/config');

const askBing = async ({
text,
Expand Down Expand Up @@ -100,7 +101,7 @@ const askBing = async ({
}
}

console.log('bing options', options);
logger.debug('bing options', options);

const res = await bingAIClient.sendMessage(text, options);

Expand Down
38 changes: 11 additions & 27 deletions api/app/clients/AnthropicClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { encoding_for_model: encodingForModel, get_encoding: getEncoding } = requ
const { getResponseSender, EModelEndpoint } = require('librechat-data-provider');
const { getModelMaxTokens } = require('~/utils');
const BaseClient = require('./BaseClient');
const { logger } = require('~/config');

const HUMAN_PROMPT = '\n\nHuman:';
const AI_PROMPT = '\n\nAssistant:';
Expand Down Expand Up @@ -103,9 +104,8 @@ class AnthropicClient extends BaseClient {
messages,
parentMessageId,
});
if (this.options.debug) {
console.debug('AnthropicClient: orderedMessages', orderedMessages, parentMessageId);
}

logger.debug('[AnthropicClient] orderedMessages', { orderedMessages, parentMessageId });

const formattedMessages = orderedMessages.map((message) => ({
author: message.isCreatedByUser ? this.userLabel : this.assistantLabel,
Expand Down Expand Up @@ -247,7 +247,7 @@ class AnthropicClient extends BaseClient {
}

getCompletion() {
console.log('AnthropicClient doesn\'t use getCompletion (all handled in sendCompletion)');
logger.debug('AnthropicClient doesn\'t use getCompletion (all handled in sendCompletion)');
}

async sendCompletion(payload, { onProgress, abortController }) {
Expand All @@ -262,12 +262,7 @@ class AnthropicClient extends BaseClient {
modelOptions.stream = true;
}

const { debug } = this.options;
if (debug) {
console.debug();
console.debug(modelOptions);
console.debug();
}
logger.debug('modelOptions', { modelOptions });

const client = this.getClient();
const metadata = {
Expand Down Expand Up @@ -295,32 +290,23 @@ class AnthropicClient extends BaseClient {
top_p,
top_k,
};
if (this.options.debug) {
console.log('AnthropicClient: requestOptions');
console.dir(requestOptions, { depth: null });
}
logger.debug('[AnthropicClient]', { requestOptions });
const response = await client.completions.create(requestOptions);

signal.addEventListener('abort', () => {
if (this.options.debug) {
console.log('AnthropicClient: message aborted!');
}
logger.debug('[AnthropicClient] message aborted!');
response.controller.abort();
});

for await (const completion of response) {
if (this.options.debug) {
// Uncomment to debug message stream
// console.debug(completion);
}
// Uncomment to debug message stream
// logger.debug(completion);
text += completion.completion;
onProgress(completion.completion);
}

signal.removeEventListener('abort', () => {
if (this.options.debug) {
console.log('AnthropicClient: message aborted!');
}
logger.debug('[AnthropicClient] message aborted!');
response.controller.abort();
});

Expand All @@ -336,9 +322,7 @@ class AnthropicClient extends BaseClient {
}

getBuildMessagesOptions() {
if (this.options.debug) {
console.log('AnthropicClient doesn\'t use getBuildMessagesOptions');
}
logger.debug('AnthropicClient doesn\'t use getBuildMessagesOptions');
}

static getTokenizer(encoding, isModelName = false, extendSpecialTokens = {}) {
Expand Down
85 changes: 38 additions & 47 deletions api/app/clients/BaseClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const TextStream = require('./TextStream');
const { getConvo, getMessages, saveMessage, updateMessage, saveConvo } = require('~/models');
const { addSpaceIfNeeded, isEnabled } = require('~/server/utils');
const checkBalance = require('~/models/checkBalance');
const { logger } = require('~/config');

class BaseClient {
constructor(apiKey, options = {}) {
Expand Down Expand Up @@ -41,15 +42,14 @@ class BaseClient {
}

async getTokenCountForResponse(response) {
if (this.options.debug) {
console.debug('`recordTokenUsage` not implemented.', response);
}
logger.debug('`[BaseClient] recordTokenUsage` not implemented.', response);
}

async recordTokenUsage({ promptTokens, completionTokens }) {
if (this.options.debug) {
console.debug('`recordTokenUsage` not implemented.', { promptTokens, completionTokens });
}
logger.debug('`[BaseClient] recordTokenUsage` not implemented.', {
promptTokens,
completionTokens,
});
}

getBuildMessagesOptions() {
Expand Down Expand Up @@ -194,14 +194,14 @@ class BaseClient {
const update = {};

if (messageId === tokenCountMap.summaryMessage?.messageId) {
this.options.debug && console.debug(`Adding summary props to ${messageId}.`);
logger.debug(`[BaseClient] Adding summary props to ${messageId}.`);

update.summary = tokenCountMap.summaryMessage.content;
update.summaryTokenCount = tokenCountMap.summaryMessage.tokenCount;
}

if (message.tokenCount && !update.summaryTokenCount) {
this.options.debug && console.debug(`Skipping ${messageId}: already had a token count.`);
logger.debug(`[BaseClient] Skipping ${messageId}: already had a token count.`);
continue;
}

Expand Down Expand Up @@ -278,19 +278,17 @@ class BaseClient {
if (instructions) {
({ tokenCount, ..._instructions } = instructions);
}
this.options.debug && _instructions && console.debug('instructions tokenCount', tokenCount);
_instructions && logger.debug('[BaseClient] instructions tokenCount: ' + tokenCount);
let payload = this.addInstructions(formattedMessages, _instructions);
let orderedWithInstructions = this.addInstructions(orderedMessages, instructions);

let { context, remainingContextTokens, messagesToRefine, summaryIndex } =
await this.getMessagesWithinTokenLimit(orderedWithInstructions);

this.options.debug &&
console.debug(
'remainingContextTokens, this.maxContextTokens (1/2)',
remainingContextTokens,
this.maxContextTokens,
);
logger.debug('[BaseClient] Context Count (1/2)', {
remainingContextTokens,
maxContextTokens: this.maxContextTokens,
});

let summaryMessage;
let summaryTokenCount;
Expand All @@ -308,10 +306,9 @@ class BaseClient {

if (diff > 0) {
payload = payload.slice(diff);
this.options.debug &&
console.debug(
`Difference between original payload (${length}) and context (${context.length}): ${diff}`,
);
logger.debug(
`[BaseClient] Difference between original payload (${length}) and context (${context.length}): ${diff}`,
);
}

const latestMessage = orderedWithInstructions[orderedWithInstructions.length - 1];
Expand All @@ -338,12 +335,10 @@ class BaseClient {
// Make sure to only continue summarization logic if the summary message was generated
shouldSummarize = summaryMessage && shouldSummarize;

this.options.debug &&
console.debug(
'remainingContextTokens, this.maxContextTokens (2/2)',
remainingContextTokens,
this.maxContextTokens,
);
logger.debug('[BaseClient] Context Count (2/2)', {
remainingContextTokens,
maxContextTokens: this.maxContextTokens,
});

let tokenCountMap = orderedWithInstructions.reduce((map, message, index) => {
const { messageId } = message;
Expand All @@ -361,19 +356,13 @@ class BaseClient {

const promptTokens = this.maxContextTokens - remainingContextTokens;

if (this.options.debug) {
console.debug('<-------------------------PAYLOAD/TOKEN COUNT MAP------------------------->');
console.debug('Payload:', payload);
console.debug('Token Count Map:', tokenCountMap);
console.debug(
'Prompt Tokens',
promptTokens,
'remainingContextTokens',
remainingContextTokens,
'this.maxContextTokens',
this.maxContextTokens,
);
}
logger.debug('[BaseClient] Payload size:', payload.length);
logger.debug('[BaseClient] tokenCountMap:', tokenCountMap);
logger.debug('[BaseClient]', {
promptTokens,
remainingContextTokens,
maxContextTokens: this.maxContextTokens,
});

return { payload, tokenCountMap, promptTokens, messages: orderedWithInstructions };
}
Expand Down Expand Up @@ -421,11 +410,11 @@ class BaseClient {
);

if (tokenCountMap) {
console.dir(tokenCountMap, { depth: null });
logger.debug('[BaseClient] tokenCountMap', tokenCountMap);
if (tokenCountMap[userMessage.messageId]) {
userMessage.tokenCount = tokenCountMap[userMessage.messageId];
console.log('userMessage.tokenCount', userMessage.tokenCount);
console.log('userMessage', userMessage);
logger.debug('[BaseClient] userMessage.tokenCount', userMessage.tokenCount);
logger.debug('[BaseClient] userMessage', userMessage);
}

this.handleTokenCountMap(tokenCountMap);
Expand All @@ -443,7 +432,6 @@ class BaseClient {
user: this.user,
tokenType: 'prompt',
amount: promptTokens,
debug: this.options.debug,
model: this.modelOptions.model,
endpoint: this.options.endpoint,
},
Expand Down Expand Up @@ -483,9 +471,7 @@ class BaseClient {
}

async loadHistory(conversationId, parentMessageId = null) {
if (this.options.debug) {
console.debug('Loading history for conversation', conversationId, parentMessageId);
}
logger.debug('[BaseClient] Loading history:', { conversationId, parentMessageId });

const messages = (await getMessages({ conversationId })) ?? [];

Expand Down Expand Up @@ -516,9 +502,14 @@ class BaseClient {
}
}

if (this.options.debug && this.previous_summary) {
if (this.previous_summary) {
const { messageId, summary, tokenCount, summaryTokenCount } = this.previous_summary;
console.debug('Previous summary:', { messageId, summary, tokenCount, summaryTokenCount });
logger.debug('[BaseClient] Previous summary:', {
messageId,
summary,
tokenCount,
summaryTokenCount,
});
}

return orderedMessages;
Expand Down
32 changes: 13 additions & 19 deletions api/app/clients/GoogleClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { getResponseSender, EModelEndpoint, endpointSettings } = require('librech
const { getModelMaxTokens } = require('~/utils');
const { formatMessage } = require('./prompts');
const BaseClient = require('./BaseClient');
const { logger } = require('~/config');

const loc = 'us-central1';
const publisher = 'google';
Expand Down Expand Up @@ -42,8 +43,7 @@ class GoogleClient extends BaseClient {

jwtClient.authorize((err) => {
if (err) {
console.error('Error: jwtClient failed to authorize');
console.error(err.message);
logger.error('jwtClient failed to authorize', err);
throw err;
}
});
Expand All @@ -58,11 +58,9 @@ class GoogleClient extends BaseClient {
return new Promise((resolve, reject) => {
jwtClient.authorize((err, tokens) => {
if (err) {
console.error('Error: jwtClient failed to authorize');
console.error(err.message);
logger.error('jwtClient failed to authorize', err);
reject(err);
} else {
console.log('Access Token:', tokens.access_token);
resolve(tokens.access_token);
}
});
Expand Down Expand Up @@ -213,8 +211,7 @@ class GoogleClient extends BaseClient {
}

if (this.options.debug) {
console.debug('GoogleClient buildMessages');
console.dir(payload, { depth: null });
logger.debug('GoogleClient buildMessages', payload);
}

return { prompt: payload };
Expand All @@ -226,7 +223,10 @@ class GoogleClient extends BaseClient {
parentMessageId,
});
if (this.options.debug) {
console.debug('GoogleClient: orderedMessages', orderedMessages, parentMessageId);
logger.debug('GoogleClient: orderedMessages, parentMessageId', {
orderedMessages,
parentMessageId,
});
}

const formattedMessages = orderedMessages.map((message) => ({
Expand Down Expand Up @@ -377,10 +377,7 @@ class GoogleClient extends BaseClient {
const { debug } = this.options;
const url = this.completionsUrl;
if (debug) {
console.debug();
console.debug(url);
console.debug(this.modelOptions);
console.debug();
logger.debug('GoogleClient _getCompletion', { url, payload });
}
const opts = {
method: 'POST',
Expand All @@ -397,7 +394,7 @@ class GoogleClient extends BaseClient {

const client = await this.getClient();
const res = await client.request({ url, method: 'POST', data: payload });
console.dir(res.data, { depth: null });
logger.debug('GoogleClient _getCompletion', { res });
return res.data;
}

Expand Down Expand Up @@ -476,21 +473,18 @@ class GoogleClient extends BaseClient {
}

getBuildMessagesOptions() {
// console.log('GoogleClient doesn\'t use getBuildMessagesOptions');
// logger.debug('GoogleClient doesn\'t use getBuildMessagesOptions');
}

async sendCompletion(payload, opts = {}) {
let reply = '';
try {
reply = await this.getCompletion(payload, opts);
if (this.options.debug) {
console.debug('result');
console.debug(reply);
logger.debug('GoogleClient sendCompletion', { reply });
}
} catch (err) {
console.error('Error: failed to send completion to Google');
console.error(err);
console.error(err.message);
logger.error('failed to send completion to Google', err);
}
return reply.trim();
}
Expand Down
Loading

0 comments on commit ea1dd59

Please sign in to comment.