Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,21 @@
"license": "MIT",
"dependencies": {
"@babel/core": "^7.25.2",
"@babel/traverse": "^7.25.9",
"@babel/types": "^7.25.2",
"@google/generative-ai": "^0.20.0",
"@instructor-ai/instructor": "^1.5.0",
"@types/babel__core": "^7.20.5",
"babel-plugin-transform-beautifier": "^0.1.0",
"commander": "^12.1.0",
"dotenv": "^16.4.5",
"ipull": "^3.9.0",
"node-llama-cpp": "^3.0.0-beta.40",
"openai": "^4.55.1",
"openai": "^4.74.0",
"tsx": "^4.16.2",
"typescript": "^5.5.4",
"webcrack": "^2.13.0"
"webcrack": "^2.13.0",
"zod": "^3.23.8"
},
"devDependencies": {
"@eslint/js": "^9.8.0",
Expand Down
44 changes: 20 additions & 24 deletions src/plugins/openai/openai-rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import OpenAI from "openai";
import { visitAllIdentifiers } from "../local-llm-rename/visit-all-identifiers.js";
import { showPercentage } from "../../progress.js";
import { verbose } from "../../verbose.js";
import Instructor from "@instructor-ai/instructor";
import { z } from 'zod';

export function openaiRename({
apiKey,
Expand All @@ -14,23 +16,26 @@ export function openaiRename({
model: string;
contextWindowSize: number;
}) {
const client = new OpenAI({ apiKey, baseURL });

const oai = new OpenAI({ apiKey, baseURL });
const instructor = Instructor({
client: oai,
mode: "JSON",
})
return async (code: string): Promise<string> => {
return await visitAllIdentifiers(
code,
async (name, surroundingCode) => {
verbose.log(`Renaming ${name}`);
verbose.log("Context: ", surroundingCode);

const response = await client.chat.completions.create(
const result = await instructor.chat.completions.create(
toRenamePrompt(name, surroundingCode, model)
);
const result = response.choices[0].message?.content;

if (!result) {
throw new Error("Failed to rename", { cause: response });
throw new Error("Failed to rename", { cause: result });
}
const renamed = JSON.parse(result).newName;
const renamed = result.newName;

verbose.log(`Renamed to ${renamed}`);

Expand All @@ -46,7 +51,12 @@ function toRenamePrompt(
name: string,
surroundingCode: string,
model: string
): OpenAI.Chat.Completions.ChatCompletionCreateParamsNonStreaming {
) {
const schema = z.object({
newName: z.string({
description: `The new name for the variable/function called \`${name}\``
})
})
return {
model,
messages: [
Expand All @@ -59,23 +69,9 @@ function toRenamePrompt(
content: surroundingCode
}
],
response_format: {
type: "json_schema",
json_schema: {
strict: true,
name: "rename",
schema: {
type: "object",
properties: {
newName: {
type: "string",
description: `The new name for the variable/function called \`${name}\``
}
},
required: ["newName"],
additionalProperties: false
}
}
response_model: {
name: "Result",
schema: schema
}
};
}