refactor(api): Central Logging 📜 (#1348)

* 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
This commit is contained in:
Danny Avila 2023-12-14 07:49:27 -05:00 committed by GitHub
parent 49571ac635
commit ea1dd59ef4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
115 changed files with 1271 additions and 1001 deletions

View file

@ -1,4 +1,5 @@
const OpenAI = require('openai');
const { logger } = require('~/config');
/**
* Handles errors that may occur when making requests to OpenAI's API.
@ -12,14 +13,14 @@ const OpenAI = require('openai');
*/
async function handleOpenAIErrors(err, errorCallback, context = 'stream') {
if (err instanceof OpenAI.APIError && err?.message?.includes('abort')) {
console.warn(`[OpenAIClient.chatCompletion][${context}] Aborted Message`);
logger.warn(`[OpenAIClient.chatCompletion][${context}] Aborted Message`);
}
if (err instanceof OpenAI.OpenAIError && err?.message?.includes('missing finish_reason')) {
console.warn(`[OpenAIClient.chatCompletion][${context}] Missing finish_reason`);
logger.warn(`[OpenAIClient.chatCompletion][${context}] Missing finish_reason`);
} else if (err instanceof OpenAI.APIError) {
console.warn(`[OpenAIClient.chatCompletion][${context}] API Error`);
logger.warn(`[OpenAIClient.chatCompletion][${context}] API error`);
} else {
console.warn(`[OpenAIClient.chatCompletion][${context}] Unhandled error type`);
logger.warn(`[OpenAIClient.chatCompletion][${context}] Unhandled error type`);
}
if (errorCallback) {

View file

@ -1,17 +1,14 @@
const { getUserPluginAuthValue } = require('../../../../server/services/PluginService');
const { OpenAIEmbeddings } = require('langchain/embeddings/openai');
const { ZapierToolKit } = require('langchain/agents');
const { SerpAPI, ZapierNLAWrapper } = require('langchain/tools');
const { ChatOpenAI } = require('langchain/chat_models/openai');
const { Calculator } = require('langchain/tools/calculator');
const { WebBrowser } = require('langchain/tools/webbrowser');
const { SerpAPI, ZapierNLAWrapper } = require('langchain/tools');
const { OpenAIEmbeddings } = require('langchain/embeddings/openai');
const { getUserPluginAuthValue } = require('~/server/services/PluginService');
const {
availableTools,
AIPluginTool,
GoogleSearchAPI,
WolframAlphaAPI,
StructuredWolfram,
HttpRequestTool,
OpenAICreateImage,
StableDiffusionAPI,
DALLE3,
@ -23,8 +20,9 @@ const {
CodeSherpaTools,
CodeBrew,
} = require('../');
const { loadSpecs } = require('./loadSpecs');
const { loadToolSuite } = require('./loadToolSuite');
const { loadSpecs } = require('./loadSpecs');
const { logger } = require('~/config');
const getOpenAIKey = async (options, user) => {
let openAIApiKey = options.openAIApiKey ?? process.env.OPENAI_API_KEY;
@ -64,7 +62,7 @@ const validateTools = async (user, tools = []) => {
return Array.from(validToolsSet.values());
} catch (err) {
console.log('There was a problem validating tools', err);
logger.error('[validateTools] There was a problem validating tools', err);
throw new Error(err);
}
};
@ -161,15 +159,6 @@ const loadTools = async ({
const zapier = new ZapierNLAWrapper({ apiKey });
return ZapierToolKit.fromZapierNLAWrapper(zapier);
},
plugins: async () => {
return [
new HttpRequestTool(),
await AIPluginTool.fromPluginUrl(
'https://www.klarna.com/.well-known/ai-plugin.json',
new ChatOpenAI({ openAIApiKey: options.openAIApiKey, temperature: 0 }),
),
];
},
};
const requestedTools = {};

View file

@ -1,7 +1,8 @@
const fs = require('fs');
const path = require('path');
const { z } = require('zod');
const { createOpenAPIPlugin } = require('../dynamic/OpenAPIPlugin');
const { logger } = require('~/config');
const { createOpenAPIPlugin } = require('~/app/clients/tools/dynamic/OpenAPIPlugin');
// The minimum Manifest definition
const ManifestDefinition = z.object({
@ -26,28 +27,17 @@ const ManifestDefinition = z.object({
legal_info_url: z.string().optional(),
});
function validateJson(json, verbose = true) {
function validateJson(json) {
try {
return ManifestDefinition.parse(json);
} catch (error) {
if (verbose) {
console.debug('validateJson error', error);
}
logger.debug('[validateJson] manifest parsing error', error);
return false;
}
}
// omit the LLM to return the well known jsons as objects
async function loadSpecs({
llm,
user,
message,
tools = [],
map = false,
memory,
signal,
verbose = false,
}) {
async function loadSpecs({ llm, user, message, tools = [], map = false, memory, signal }) {
const directoryPath = path.join(__dirname, '..', '.well-known');
let files = [];
@ -60,7 +50,7 @@ async function loadSpecs({
await fs.promises.access(filePath, fs.constants.F_OK);
files.push(tools[i] + '.json');
} catch (err) {
console.error(`File ${tools[i] + '.json'} does not exist`);
logger.error(`[loadSpecs] File ${tools[i] + '.json'} does not exist`, err);
}
}
@ -73,9 +63,7 @@ async function loadSpecs({
const validJsons = [];
const constructorMap = {};
if (verbose) {
console.debug('files', files);
}
logger.debug('[validateJson] files', files);
for (const file of files) {
if (path.extname(file) === '.json') {
@ -84,7 +72,7 @@ async function loadSpecs({
const json = JSON.parse(fileContent);
if (!validateJson(json)) {
verbose && console.debug('Invalid json', json);
logger.debug('[validateJson] Invalid json', json);
continue;
}
@ -97,13 +85,12 @@ async function loadSpecs({
memory,
signal,
user,
verbose,
});
continue;
}
if (llm) {
validJsons.push(createOpenAPIPlugin({ data: json, llm, verbose }));
validJsons.push(createOpenAPIPlugin({ data: json, llm }));
continue;
}
@ -117,10 +104,8 @@ async function loadSpecs({
const plugins = (await Promise.all(validJsons)).filter((plugin) => plugin);
// if (verbose) {
// console.debug('plugins', plugins);
// console.debug(plugins[0].name);
// }
// logger.debug('[validateJson] plugins', plugins);
// logger.debug(plugins[0].name);
return plugins;
}