mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-16 16:30:15 +01:00
* 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
122 lines
3.3 KiB
JavaScript
122 lines
3.3 KiB
JavaScript
const { Agent } = require('langchain/agents');
|
|
const { LLMChain } = require('langchain/chains');
|
|
const { FunctionChatMessage, AIChatMessage } = require('langchain/schema');
|
|
const {
|
|
ChatPromptTemplate,
|
|
MessagesPlaceholder,
|
|
SystemMessagePromptTemplate,
|
|
HumanMessagePromptTemplate,
|
|
} = require('langchain/prompts');
|
|
const { logger } = require('~/config');
|
|
|
|
const PREFIX = 'You are a helpful AI assistant.';
|
|
|
|
function parseOutput(message) {
|
|
if (message.additional_kwargs.function_call) {
|
|
const function_call = message.additional_kwargs.function_call;
|
|
return {
|
|
tool: function_call.name,
|
|
toolInput: function_call.arguments ? JSON.parse(function_call.arguments) : {},
|
|
log: message.text,
|
|
};
|
|
} else {
|
|
return { returnValues: { output: message.text }, log: message.text };
|
|
}
|
|
}
|
|
|
|
class FunctionsAgent extends Agent {
|
|
constructor(input) {
|
|
super({ ...input, outputParser: undefined });
|
|
this.tools = input.tools;
|
|
}
|
|
|
|
lc_namespace = ['langchain', 'agents', 'openai'];
|
|
|
|
_agentType() {
|
|
return 'openai-functions';
|
|
}
|
|
|
|
observationPrefix() {
|
|
return 'Observation: ';
|
|
}
|
|
|
|
llmPrefix() {
|
|
return 'Thought:';
|
|
}
|
|
|
|
_stop() {
|
|
return ['Observation:'];
|
|
}
|
|
|
|
static createPrompt(_tools, fields) {
|
|
const { prefix = PREFIX, currentDateString } = fields || {};
|
|
|
|
return ChatPromptTemplate.fromMessages([
|
|
SystemMessagePromptTemplate.fromTemplate(`Date: ${currentDateString}\n${prefix}`),
|
|
new MessagesPlaceholder('chat_history'),
|
|
HumanMessagePromptTemplate.fromTemplate('Query: {input}'),
|
|
new MessagesPlaceholder('agent_scratchpad'),
|
|
]);
|
|
}
|
|
|
|
static fromLLMAndTools(llm, tools, args) {
|
|
FunctionsAgent.validateTools(tools);
|
|
const prompt = FunctionsAgent.createPrompt(tools, args);
|
|
const chain = new LLMChain({
|
|
prompt,
|
|
llm,
|
|
callbacks: args?.callbacks,
|
|
});
|
|
return new FunctionsAgent({
|
|
llmChain: chain,
|
|
allowedTools: tools.map((t) => t.name),
|
|
tools,
|
|
});
|
|
}
|
|
|
|
async constructScratchPad(steps) {
|
|
return steps.flatMap(({ action, observation }) => [
|
|
new AIChatMessage('', {
|
|
function_call: {
|
|
name: action.tool,
|
|
arguments: JSON.stringify(action.toolInput),
|
|
},
|
|
}),
|
|
new FunctionChatMessage(observation, action.tool),
|
|
]);
|
|
}
|
|
|
|
async plan(steps, inputs, callbackManager) {
|
|
// Add scratchpad and stop to inputs
|
|
const thoughts = await this.constructScratchPad(steps);
|
|
const newInputs = Object.assign({}, inputs, { agent_scratchpad: thoughts });
|
|
if (this._stop().length !== 0) {
|
|
newInputs.stop = this._stop();
|
|
}
|
|
|
|
// Split inputs between prompt and llm
|
|
const llm = this.llmChain.llm;
|
|
const valuesForPrompt = Object.assign({}, newInputs);
|
|
const valuesForLLM = {
|
|
tools: this.tools,
|
|
};
|
|
for (let i = 0; i < this.llmChain.llm.callKeys.length; i++) {
|
|
const key = this.llmChain.llm.callKeys[i];
|
|
if (key in inputs) {
|
|
valuesForLLM[key] = inputs[key];
|
|
delete valuesForPrompt[key];
|
|
}
|
|
}
|
|
|
|
const promptValue = await this.llmChain.prompt.formatPromptValue(valuesForPrompt);
|
|
const message = await llm.predictMessages(
|
|
promptValue.toChatMessages(),
|
|
valuesForLLM,
|
|
callbackManager,
|
|
);
|
|
logger.debug('[FunctionsAgent] plan message', message);
|
|
return parseOutput(message);
|
|
}
|
|
}
|
|
|
|
module.exports = FunctionsAgent;
|