Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update node openai lib from 3.3.0 to latest (4.52.3+) #19

Closed
0xdevalias opened this issue Jul 3, 2024 · 2 comments
Closed

Update node openai lib from 3.3.0 to latest (4.52.3+) #19

0xdevalias opened this issue Jul 3, 2024 · 2 comments

Comments

@0xdevalias
Copy link

Some notes I made in another issue, but felt like it deserved it's own more specific issue to track it.

I also noticed that this project is using the node openai 3.3.0 package, whereas it's up to 4.52.3 now (that might not make a difference at all, but you never know):

"openai": "^3.3.0",

The changelog entries only seem to start from 4.2.0:

There are a few bugfix entries related to handling errors while streaming.. I wonder if that might be helpful?

Also some related to tools/functions, eg.

And new model:


  • https://github.com/openai/openai-node#automated-function-calls
    • We provide the openai.beta.chat.completions.runTools({…}) convenience helper for using function tool calls with the /chat/completions endpoint which automatically call the JavaScript functions you provide and sends their results back to the /chat/completions endpoint, looping as long as the model requests tool calls.

    • If you pass a parse function, it will automatically parse the arguments for you and returns any parsing errors to the model to attempt auto-recovery. Otherwise, the args will be passed to the function you provide as a string.

    • If you pass tool_choice: {function: {name: …}} instead of auto, it returns immediately after calling that function (and only loops to auto-recover parsing errors).

    • Note that runFunctions was previously available as well, but has been deprecated in favor of runTools.


We can see that the openai stuff is initially called here, which generates a 'plugin' that is then applied to the code of each of the files extracted with webcrack:

humanify/src/index.ts

Lines 56 to 71 in 002fd68

const PLUGINS = [
humanify,
argv.local
? localReanme()
: openai({ apiKey: argv.key ?? env("OPENAI_TOKEN"), use4k: argv["4k"] }),
prettier,
];
const extractedFiles = await webcrack(bundledCode, argv.output);
for (const file of extractedFiles) {
const code = await fs.readFile(file.path, "utf-8");
const formattedCode = await PLUGINS.reduce(
(p, next) => p.then(next),
Promise.resolve(code)
);

With the main logic being implemented here, and the actual SDK call within codeToVariableRenames:

export default ({ apiKey, use4k }: Options) => {
const client = new OpenAIApi(new Configuration({ apiKey }));
return async (code: string): Promise<string> => {
const codeBlocks = await splitCode(code, use4k);
let variablesAndFunctionsToRename: Rename[] = [];
await mapPromisesParallel(10, codeBlocks, async (codeBlock) => {
const renames = await codeToVariableRenames(codeBlock);
variablesAndFunctionsToRename =
variablesAndFunctionsToRename.concat(renames);
});
return renameVariablesAndFunctions(code, variablesAndFunctionsToRename);
};
async function codeToVariableRenames(code: string) {
const chatCompletion = await client.createChatCompletion({
model: use4k ? "gpt-3.5-turbo" : "gpt-3.5-turbo-16k",
functions: [
{
name: "rename_variables_and_functions",
description: "Rename variables and function names in Javascript code",
parameters: {
type: "object",
properties: {
variablesAndFunctionsToRename: {
type: "array",
items: {
type: "object",
properties: {
name: {
type: "string",
description:
"The name of the variable or function name to rename",
},
newName: {
type: "string",
description:
"The new name of the variable or function name",
},
},
required: ["name", "newName"],
},
},
},
required: ["variablesToRename"],
},
},
],
messages: [
{
role: "assistant",
content:
"Rename all Javascript variables and functions to have descriptive names based on their usage in the code.",
},
{ role: "user", content: code },
],
});
const data = chatCompletion.data.choices[0];
if (data.finish_reason !== "function_call") return [];
const {
variablesAndFunctionsToRename,
}: { variablesAndFunctionsToRename: Rename[] } = JSON.parse(
fixPerhapsBrokenResponse(data.message?.function_call?.arguments!)
);
return variablesAndFunctionsToRename;
}
};

We can also see that it's using the functions config, which is deprecated now, and replaced by tools:

Originally posted by @0xdevalias in #18 (comment)

@Acters
Copy link

Acters commented Jul 15, 2024

Hello, I have updated the OpenAI package to 4.52.7 and switched the models to use GPT4o. On top of that I had also switched gpt3-encoder to the newer gpt-tokenizer package. I kept almost all the processes the same as to help this project to stay up to date on the available OpenAI models and its API changes. I didnt make a pull request as I didn't test my code extensively. I also see that the owner is quite busy and don't wish to bother him. So I will be keeping my own version for now.

If you wish to look at the changes I am making to the humanify project, then the link to it is here: https://github.com/Acters/humanify-ng/tree/GPT4o

@0xdevalias
Copy link
Author

0xdevalias commented Aug 12, 2024

This should now be fixed in v2 since there's the long awaited JSON mode with the new structured outputs. Please take a look and repoen if anything comes up

Originally posted by @jehna in #22 (comment)

Since the v2 release is using the 4.55.1 version of the openai lib, I consider this issue implemented now:

"openai": "^4.55.1",

See also:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants