langchainjs
langchainjs copied to clipboard
Cannot read properties of undefined (reading 'log') - AgentExecutor
Checked other resources
- [X] I added a very descriptive title to this issue.
- [X] I searched the LangChain.js documentation with the integrated search.
- [X] I used the GitHub search to find a similar question and didn't find it.
- [X] I am sure that this is a bug in LangChain.js rather than my code.
- [X] The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
Example Code
The following code
const llm = new ChatAnthropic({
temperature: 0,
model: MODEL_ID,
apiKey: await getAnthropicKey(),
maxTokens: 1024,
});
const tools = createTools();
const prompt = createPrompt();
const chat_history = createChatHistory(conversation);
const agent = await createToolCallingAgent({
llm,
tools,
prompt,
});
const agentExecutor = new AgentExecutor({
agent,
verbose: true,
tools,
});
const {text} = userMessage.payload;
console.log('Invoking the agent');
const result: ChainValues = await agentExecutor.invoke({
input: text,
// chat_history,
});
result.console.log('Result: ', result);
const answer: string = result.output;
where
import {DynamicStructuredTool} from '@langchain/core/tools';
import {z} from 'zod';
export const createTools = () => {
// Define the tools the agent will have access to.
console.log('Creating Tools');
const toolTest = new DynamicStructuredTool({
name: 'buyer-assistant',
description: 'Helps the user to buy items - by returning a website',
schema: z.object({
item: z
.string()
.describe('The keyword of the item the user wants to search'),
}),
func: async ({item}) => {
return `Tell the user that Facebook Marketplace is the best place to find ${item}`; // Outputs still must be strings
},
});
const tools = [];
return tools;
};
and
import {ChatPromptTemplate} from '@langchain/core/prompts';
import {interfaces, prompts} from '../../imported-modules';
import {AIMessage, HumanMessage} from '@langchain/core/messages';
export const createChatHistory = (
conversation: interfaces.Conversation,
): (HumanMessage | AIMessage)[] => {
console.log('Creating history');
// https://js.langchain.com/docs/integrations/chat/anthropic#tools
const history = [
new HumanMessage('hi!'),
new AIMessage('Hello! How can I assist you today?'),
];
return history;
};
export const createPrompt = (): ChatPromptTemplate => {
console.log('Creating prompt');
const prompt = ChatPromptTemplate.fromMessages([
['system', prompts.SYSTEM_PROMPT],
['placeholder', '{chat_history}'],
['human', '{input}'],
['placeholder', '{agent_scratchpad}'],
]);
return prompt;
};
'log' is present in the agent chain, as you can see in the logs. Why is the agentExecutor invoke failing?
Error Message and Stack Trace (if applicable)
2024-04-25T16:11:46.577Z 3a2cea18-d484-412f-aa49-42f8bdc98574 INFO [36m[chain/end][39m [[90m1:chain:AgentExecutor > 2:chain:ToolCallingAgent > [1m8:parser:ToolCallingAgentOutputParser[22m[39m] [1ms] Exiting Chain run with output: {
"returnValues": {
"output": "I apologize, but I am not able to browse the internet or provide specific website recommendations. As an AI language model, my knowledge is based on the text data I was trained on, which has a knowledge cutoff in 2021. However, I can provide general information about chairs and suggest that you can find a wide variety of chairs for purchase on online marketplaces like Amazon, Wayfair, or IKEA, as well as at furniture retailers and department stores."
},
"log": "I apologize, but I am not able to browse the internet or provide specific website recommendations. As an AI language model, my knowledge is based on the text data I was trained on, which has a knowledge cutoff in 2021. However, I can provide general information about chairs and suggest that you can find a wide variety of chairs for purchase on online marketplaces like Amazon, Wayfair, or IKEA, as well as at furniture retailers and department stores."
}
2024-04-25T16:11:46.578Z 3a2cea18-d484-412f-aa49-42f8bdc98574 INFO [36m[chain/end][39m [[90m1:chain:AgentExecutor > [1m2:chain:ToolCallingAgent[22m[39m] [6.33s] Exiting Chain run with output: {
"returnValues": {
"output": "I apologize, but I am not able to browse the internet or provide specific website recommendations. As an AI language model, my knowledge is based on the text data I was trained on, which has a knowledge cutoff in 2021. However, I can provide general information about chairs and suggest that you can find a wide variety of chairs for purchase on online marketplaces like Amazon, Wayfair, or IKEA, as well as at furniture retailers and department stores."
},
"log": "I apologize, but I am not able to browse the internet or provide specific website recommendations. As an AI language model, my knowledge is based on the text data I was trained on, which has a knowledge cutoff in 2021. However, I can provide general information about chairs and suggest that you can find a wide variety of chairs for purchase on online marketplaces like Amazon, Wayfair, or IKEA, as well as at furniture retailers and department stores."
}
2024-04-25T16:11:46.581Z 3a2cea18-d484-412f-aa49-42f8bdc98574 INFO [36m[chain/end][39m [[90m[1m1:chain:AgentExecutor[22m[39m] [6.34s] Exiting Chain run with output: {
"input": "Describe in 1 sentence and tell me a site where I can buy a chair",
"output": "I apologize, but I am not able to browse the internet or provide specific website recommendations. As an AI language model, my knowledge is based on the text data I was trained on, which has a knowledge cutoff in 2021. However, I can provide general information about chairs and suggest that you can find a wide variety of chairs for purchase on online marketplaces like Amazon, Wayfair, or IKEA, as well as at furniture retailers and department stores."
}
2024-04-25T16:11:46.582Z 3a2cea18-d484-412f-aa49-42f8bdc98574 ERROR Invoke Error {"errorType":"TypeError","errorMessage":"Cannot read properties of undefined (reading 'log')","stack":["TypeError: Cannot read properties of undefined (reading 'log')"," at createAgent (/var/task/index.js:887502:18)"," at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"," at async invokeLangchain (/var/task/index.js:887513:18)"," at async index (/var/task/index.js:887561:50)"," at async Runtime.main [as handler] (/var/task/index.js:887576:19)"]}
Description
- I am trying to use the AgentExecutor with Langchain Anthropic tools
System Info
"@langchain/anthropic": "^0.1.16",
"aws-sdk": "^2.1606.0",
"langchain": "^0.1.35",
"zod": "^3.23.4"
Node version 18 Running on a AWS Lambda
The error you're encountering, "Cannot read properties of undefined (reading 'log')", suggests that there's an attempt to access a log
property on an object that hasn't been properly initialized. This issue often arises when dealing with asynchronous operations or when an object expected to have a certain structure is undefined at the time of access.
To address this, you should ensure that all objects, especially those related to logging or involved in the execution flow of AgentExecutor.invoke
, are correctly initialized before they're used. This includes verifying the initialization of any logging utilities or callback managers that the AgentExecutor
or related components might rely on.
Since the error could stem from a part of the codebase not shown in your snippets, such as the internal workings of AgentExecutor
or its interactions with other components like CallbackManagerForChainRun
or BasePlanner
, reviewing the full implementation for proper object initialization patterns is crucial. Specifically, look for any logging functionality that might be accessed before being fully initialized and ensure that asynchronous operations are properly managed to prevent premature access to uninitialized objects.
Without direct access to the full codebase or the specific implementation details of the invoke
method and related logging functionality, this guidance is based on common patterns and issues related to the error message you're seeing. Ensuring proper initialization and handling of all objects and their properties related to the execution flow and logging should help resolve the issue.
Details
Sources
- langchain/src/experimental/plan_and_execute/agent_executor.ts
- langchain/src/agents/executor.ts
- examples/src/agents/openai_runnable_stream_log.ts
- examples/src/agents/mrkl_with_tracing.ts
- langchain/src/agents/initialize.ts
- examples/src/agents/aiplugin-tool.ts
- langchain/src/agents/tool_calling/index.ts
- langchain-core/src/tools.ts
- examples/src/use_cases/tool_use/agents.ts
- examples/src/use_cases/tool_use/quickstart_agents.ts
About Dosu This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.
Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.
To continue the conversation, mention @dosu.