This topic was automatically generated from Slack. You can find the original thread here.
Hey all, I need some help sending chunks of data to GPT.
This is my code:
const openaiEndpoint = "https://api.openai.com/v1/engines/davinci/completions";
const apiKey = process.env.OPENAI_API_KEY;
// Get the chunks from the previous step
const chunks = steps.chunking_script.$return_value;
async function getGPTAnalysis(chunk) {
const requestBody = {
prompt: `Analyze the following business data: ${chunk}`,
max_tokens: 500,
temperature: 0.7
};
const response = await fetch(openaiEndpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${apiKey}`
},
body: JSON.stringify(requestBody)
});
const responseData = await response.json();
if (response.ok) {
return responseData.choices[0].text.trim();
} else {
throw new Error(`OpenAI returned an error: ${responseData.error.message}`);
}
}
// Handle each chunk with GPT analysis
// (For simplicity, only the first chunk is processed here. You can loop through all chunks if needed.)
getGPTAnalysis(chunks[0]).then(result => {
this.analysis = result;
}).catch(error => {
console.error("Failed to get GPT analysis:", error);
});
And I keep getting this error:
ReferenceError
steps is not defined DETAILS
ReferenceError: steps is not defined
at Object.<anonymous> (/tmp/__pdg__/dist/code/3642e31336643cd6301dc50d6d9fe6ccbd503d2aa96a842533aff3779da94be7/code.js:5:16)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29)
at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
I know the previous step “chunking_script” is properly named.
Any help?
Sure, I’ve integrated your code snippet into a Pipedream Node.js code step. Here’s the updated code: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { chunks: { type: "object", label: "Chunks from previous step", }, }, async run({ steps, $ }) { const openaiEndpoint = "https://api.openai.com/v1/engines/davinci/completions"; const apiKey = process.env.OPENAI_API_KEY; async function getGPTAnalysis(chunk) { const requestBody = { prompt: `Analyze the following business data: ${chunk}`, max_tokens: 500, temperature: 0.7, }; const response = await axios($, { method: "POST", url: openaiEndpoint, headers: { "Content-Type": "application/json", "Authorization": `Bearer ${apiKey}`, }, data: requestBody, }); const responseData = response.data; if (response.status === 200) { return responseData.choices[0].text.trim(); } else { throw new Error(`OpenAI returned an error: ${responseData.error.message}`); } } try { const result = await getGPTAnalysis(this.chunks[0]); this.analysis = result; } catch (error) { console.error("Failed to get GPT analysis:", error); } }, }); Make sure to replace process.env.OPENAI_API_KEY with your actual API key or use a Pipedream secret to store it securely. Also, update the chunks prop to receive the data from the previous step correctly.
Thanks very much! That made the step successful.
But now I’m running into this error again:
This model's maximum context length is 2049 tokens, however you requested 4572 tokens (572 in your prompt; 4000 for the completion). Please reduce your prompt; or completion length.
Copy Path
•
Copy Value
type:
invalid_request_error
Even though I can see the chunks in the previous step (there are 17 in total)
I really appreciate you taking the time to answer my dumb questions.
In hopes that you haven’t reached your quota of dumb questions for the day, I’ve got a couple more:
The purpose of the chunking script is to turn it into chunks that GPT can look at and get around the token limit- so why isn’t that working?
I thought the token limit on GPT4 was somewhere closer to 4000, no?