What Does the TypeError in a Custom Code Generation Block Mean in a Node.js Script Using Function Calling?

This topic was automatically generated from Slack. You can find the original thread here.

What does this error code mean in the context of a custom code generation block?

TypeError: ""Write an openai function call based on the provided step by step instructions. Function calling enable models to generate function arguments which adhere to the provided specifications. Note that the API will not actually execute any function calls. It is up to developers to execute function calls using model outputs. In our experience, the real benefit of that feature is the ability for ChatGPT to support structured input and/or output. Here is an example of what a node.js script that uses function calling looks like."

" is not a function
    at eval (eval at _pd_deep_evaluate (/var/task/params_evaluator.js:68:16), <anonymous>:3:2)
    at _pd_deep_evaluate (/var/task/params_evaluator.js:68:16)
    at MessagePort.<anonymous> (/var/task/params_evaluator.js:11:9)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:737:20)
    at exports.emitMessage (node:internal/per_context/messageport:23:28)

It looks like you’re encountering a TypeError because you’re trying to call a string as a function. This error occurs when you attempt to invoke a string as if it were a function, but JavaScript does not recognize it as a valid function. To resolve this issue, please double-check your code and make sure you’re calling a valid function instead of a string. If you can provide more context or share the code you’re working with, I’d be happy to help you further.

This is the prompt to generate custom code for the pipedream api, is this input being interpreted as javascript?

Yes, in Pipedream, custom code is typically written in JavaScript (Node.js) and is executed within a code step in a workflow. If you provide a prompt for generating custom code, it should be written in JavaScript, following the Pipedream component structure. If you need help with generating custom code for a specific task, please provide more details about the task, and I’ll be happy to assist you.

I am referring to the generate_component_code block - are you sure?

Apologies for the confusion earlier. It seems I misunderstood your question. If you’re referring to the generate_component_code block in the context of OpenAI’s ChatGPT, it’s not related to Pipedream’s custom code generation. In this case, the error you encountered is not related to Pipedream’s custom code. Please provide more context or clarify your question, and I’ll do my best to help you.

I have written a prompt to generate custom code as step in my workflow, it is throwing an error when I attempt to run the block. Here is the prompt:

"Write an openai function call based on the provided step by step instructions. Function calling enable models to generate function arguments which adhere to the provided specifications. Note that the API will not actually execute any function calls. It is up to developers to execute function calls using model outputs. In our experience, the real benefit of that feature is the ability for ChatGPT to support structured input and/or output. Here is an example of what a node.js script that uses function calling looks like."


// Import necessary packages and modules
require(‘dotenv’).config(); // Load environment variables from a .env file
const { Configuration, OpenAIApi } = require(“openai”); // Import classes from the OpenAI API module
const axios = require(“axios”); // Import the axios library for making HTTP requests
const moment = require(“moment-timezone”); // Import the moment-timezone library for time formatting

//Create a new OpenAI API configuration object with an API key loaded from the environment variables
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});

const openai = new OpenAIApi(configuration); // Create an instance of the OpenAI API with the configuration object

/** Define an asynchronous function that takes a location string as a parameter,
retrieves the current time at that location using the World Time API,
then formats and logs the time to the console **/
async function lookupTime(location, name) {
try {
const response = await axios.get(http://worldtimeapi.org/api/timezone/${location});
// Make a GET request to the World Time API with the ‘location’ value as the timezone.
const { datetime } = response.data; // Destructure ‘datetime’ from the response to extract it.
const dateTime = moment.tz(datetime, location).format(“h:mmA”);
// Create a new date object using the specified timezone and format it in 12-hour time with AM/PM label.
console.log(The current time in ${name} is ${dateTime}.);
// Log the formatted time to the console.
} catch (error) {
console.error(error); //Log any errors that occur to the console.
}
}

/** Define an async function called ‘main’
which uses the OpenAI API to generate a chatbot-like response to a given input message
and includes the ability to call the ‘lookupTime’ function **/
async function main() {
const completion = await openai.createChatCompletion({
model: “gpt-3.5-turbo-0613”, // Choose the GPT model to use for generating responses
messages: [
{role: “system”, content: “You are a helpful assistant.”}, // Specify the role and content of the messages
{role: “user”, content: “What time is it in London, England?”} // sent to the GPT model for generating the response.
],
functions: [
{ // Define the ‘lookupTime’ function
name: “lookupTime”,
description: “get the current time in a given location”,
parameters: { // Define the input parameters for the function
type: “object”, // The parameter is an object
properties: {
location: { // The ‘location’ property of the object is a required string
type: “string”, // The ‘location’ property is a string value
description: “The location, e.g. Beijing, China. But it should be written in a timezone name like Asia/Shanghai”
},
name: {
type: “string”,
description: “The location mentioned in the prompt. Example: Beijing, China.”
}
},
required: [“location”, “name”] // The ‘location’ property is required
}
}
],
function_call: “auto” // Specify that the API should automatically detect and execute function calls within the response
})
console.log("completion.data: ", completion.data)

const completionResponse = completion.data.choices[0].message; // Extract the generated completion from the OpenAI API response

console.log("completionResponse: ", completionResponse); // Output the generated response to the console

if(!completionResponse.content) { // Check if the generated response includes a function call
    const functionCallName = completionResponse.function_call.name; 
    console.log("functionCallName: ", functionCallName);

    if(functionCallName === "lookupTime") { // If the function being called is 'lookupTime'
        const completionArguments = JSON.parse(completionResponse.function_call.arguments); // Extract the argument for the function call
        console.log("completionArguments: ", completionArguments);

        lookupTime(completionArguments.location, completionArguments.name) // Call the 'lookupTime' function with the specified location argument. 
    }
}

return completionResponse.content

}

main();
// Call the ‘main’ function to run the program and generate the chatbot response.



Here are your step by step instructions to write a custom function:

{{steps.task_classification_and_instruction_function.code_generation_output.job_to_be_done}}
{{steps.task_classification_and_instruction_function.code_generation_output.code_generation_instructions}}

Here are the related docs. 

{{steps.TESTING_source_doc}} 
{{steps.TESTING_context_doc}}
{{steps.TESTING_template_doc}}

Generate a script that sucesfully defines an output schema to achieve the job to be be done. 

I see that you have provided a prompt and a code snippet. However, it’s not clear what specific error you’re encountering when running the code. To better assist you, could you please provide more information about the error message or any issues you’re facing while running the code?

This is the error:

TypeError: ""Write an openai function call based on the provided step by step instructions. Function calling enable models to generate function arguments which adhere to the provided specifications. Note that the API will not actually execute any function calls. It is up to developers to execute function calls using model outputs. In our experience, the real benefit of that feature is the ability for ChatGPT to support structured input and/or output. Here is an example of what a node.js script that uses function calling looks like."

" is not a function
    at eval (eval at _pd_deep_evaluate (/var/task/params_evaluator.js:68:16), <anonymous>:3:2)
    at _pd_deep_evaluate (/var/task/params_evaluator.js:68:16)
    at MessagePort.<anonymous> (/var/task/params_evaluator.js:11:9)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:737:20)
    at exports.emitMessage (node:internal/per_context/messageport:23:28)

Could the problem be that the prompt incudes javascript in it?

It seems like the error is related to the evaluation of a string that was expected to be a function. The prompt you provided might be causing issues if it’s being treated as code instead of a string. To avoid this, make sure the prompt is enclosed within quotes or backticks to be treated as a string. If you can provide more context or the code snippet where this error is occurring, I can help you better understand and resolve the issue.

How could you include a node.js code example in the generate_component_code component prompt that would be a string and not complied as node.js code. I am trying to include a code example for generation reference but am running into errors, and I am not familiar with node.js

To include a Node.js code example as a string within the generate_component_code component prompt, you can use backticks () or single/double quotes to wrap the code example. This will ensure that the code is treated as a string and not executed as Node.js code. Here's an example: const codeExample = const exampleFunction = () => { console.log("This is a Node.js code example"); }; exampleFunction();; Or using single/double quotes: const codeExample = ‘const exampleFunction = () => { console.log(“This is a Node.js code example”); }; exampleFunction();’; ``` By wrapping the code example in quotes or backticks, it will be treated as a string and not executed as Node.js code.