-
I am using an OpenAI functions agent When I create the function definition for my function the 'name' and 'description' look like this:
The problem is that when this function definition gets 'input' and tries to parse it with JSON.parse(input) it fails periodically. It doesn't fail all the time, but it does fail one out of five times. It fails because 'input' comes to the function as 'undefined'. When it works, 'input' comes in looking like this:
I know this because when I turn on verbose mode, I see this:
When it fails, 'input' looks like this:
I know this because when I turn on verbose mode, I see this:
what is the appropriate way to get 'input' into the right format? or try/catch it to reformat it? I can't try/catch it in the function definition because it's already 'undefined' by the time it gets there. I have read about handle_parsing_errors but I can't see how this would be a fix to this problem other than handling the issue more gracefully. What am i missing? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 10 replies
-
🤖 Hey @scottsuhy! Great to see you again in the LangChainJS repo. Hope all is well on your end. It seems like the issue you're encountering is due to the inconsistent format of the 'input' that is being passed to your function. Sometimes it's a stringified JSON object, and other times it's a plain JSON object. This inconsistency is likely causing the One way to handle this is to check if the input is a string before trying to parse it. If it's not a string, you can assume it's already a JSON object and use it directly. Here's how you can modify your function to handle this: const GETFROMVECTORDATABASE = new DynamicTool({
name: "GETFROMVECTORDATABASE",
description: "Retrieves the answer to a question from the users vector database. The user may mention their 'vector database' and if they do, then use " +
"this to query it. " +
"The input must be a JSON string using the same format as this example: 'input = {\"question\": \"This is my question\", \"user_email\": \"[email protected]\"}'. " +
"This input format includes an 'input' object containing two fields: 'question' (the users question for the vector database), 'user_email' (the users email). " +
"The function returns the answer.",
func: async (input) => {
const args = typeof input === 'string' ? JSON.parse(input) : input;
const question = args.question;
const user_email = args.user_email;
const result = await myFunctions.getFromVectorDatabase(question, user_email);
if(result.error){
capturedLogs.push('***GETFROMVECTORDATABASE ERROR***: ' + result.error);
}
else{
return result.answer;
}
},
}); This should prevent the As for the I hope this helps! If you have any other questions, feel free to ask. SourcesThis 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. If you want to continue the conversation, start your reply with @dosu-bot. |
Beta Was this translation helpful? Give feedback.
-
there is a bug in the langchain code related to JSON. The workaround in this bug report #1115 worked for me. |
Beta Was this translation helpful? Give feedback.
there is a bug in the langchain code related to JSON. The workaround in this bug report #1115 worked for me.