Skip to content

Commit

Permalink
Merge pull request aidar-freeed#5 from delmarcode/json-mode
Browse files Browse the repository at this point in the history
upgrade openai package
  • Loading branch information
jgautsch authored Sep 7, 2024
2 parents 64e5f38 + d340263 commit 05ee559
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cSpell.words": [
"openai"
]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@actions/core": "^1.10.0",
"@octokit/rest": "^19.0.7",
"minimatch": "^7.4.2",
"openai": "^3.2.1",
"openai": "^4.58.1",
"parse-diff": "^0.11.1",
"ts-node": "^10.9.1"
},
Expand Down
17 changes: 8 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { readFileSync } from "fs";
import * as core from "@actions/core";
import { Configuration, OpenAIApi } from "openai";
import OpenAI from "openai";
import { Octokit } from "@octokit/rest";
import parseDiff, { Chunk, File } from "parse-diff";
import minimatch from "minimatch";
Expand All @@ -16,12 +17,10 @@ const RESPONSE_TOKENS = 1024;

const octokit = new Octokit({ auth: GITHUB_TOKEN });

const configuration = new Configuration({
const openai = new OpenAI({
apiKey: OPENAI_API_KEY,
});

const openai = new OpenAIApi(configuration);

interface PRDetails {
owner: string;
repo: string;
Expand Down Expand Up @@ -168,11 +167,11 @@ async function getAIResponse(
presence_penalty: 0,
response_format: {
type: "json_object",
},
} as const,
};

try {
const response = await openai.createChatCompletion({
const response = await openai.chat.completions.create({
...queryConfig,
messages: [
{
Expand All @@ -182,12 +181,12 @@ async function getAIResponse(
],
});

if (response.status !== 200) {
throw new Error(`OpenAI API returned non-200 status: ${response.status}`);
if (!response.choices || response.choices.length === 0) {
throw new Error("OpenAI API returned an invalid response");
}

core.info("Received response from OpenAI API.");
const res = response.data.choices[0].message?.content?.trim() || "[]";
const res = response.choices[0].message?.content?.trim() || "[]";
// Remove any markdown formatting before parsing JSON
const jsonString = res.replace(/```json\n|\n```/g, "").trim();
return JSON.parse(jsonString);
Expand Down Expand Up @@ -301,7 +300,7 @@ async function main() {

diff = String(response.data);
} else {
core.info("Unsupported event:", process.env.GITHUB_EVENT_NAME);
core.info(`Unsupported event: ${process.env.GITHUB_EVENT_NAME}`);
return;
}

Expand Down
Loading

0 comments on commit 05ee559

Please sign in to comment.