Skip to content

Commit 80ca662

Browse files
committed
support the latest gpt 4 and json mode
1 parent bc0dab4 commit 80ca662

File tree

3 files changed

+147
-27
lines changed

3 files changed

+147
-27
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"@actions/core": "^1.10.0",
1616
"@octokit/rest": "^19.0.7",
1717
"minimatch": "^7.4.2",
18-
"openai": "^3.2.1",
18+
"openai": "^4.20.1",
1919
"parse-diff": "^0.11.1",
2020
"ts-node": "^10.9.1"
2121
},

src/main.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { readFileSync } from "fs";
22
import * as core from "@actions/core";
3-
import { Configuration, OpenAIApi } from "openai";
3+
import OpenAI from "openai";
44
import { Octokit } from "@octokit/rest";
55
import parseDiff, { Chunk, File } from "parse-diff";
66
import minimatch from "minimatch";
@@ -11,12 +11,10 @@ const OPENAI_API_MODEL: string = core.getInput("OPENAI_API_MODEL");
1111

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

14-
const configuration = new Configuration({
14+
const openai = new OpenAI({
1515
apiKey: OPENAI_API_KEY,
1616
});
1717

18-
const openai = new OpenAIApi(configuration);
19-
2018
interface PRDetails {
2119
owner: string;
2220
repo: string;
@@ -98,9 +96,9 @@ async function getBaseAndHeadShas(
9896

9997
function createPrompt(file: File, chunk: Chunk, prDetails: PRDetails): string {
10098
return `Your task is to review pull requests. Instructions:
101-
- Provide the response in following JSON format: [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}]
99+
- Provide the response in following JSON format: {"reviews": [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}]}
102100
- Do not give positive comments or compliments.
103-
- Provide comments and suggestions ONLY if there is something to improve, otherwise return an empty array.
101+
- Provide comments and suggestions ONLY if there is something to improve, otherwise "reviews" should be an empty array.
104102
- Write the comment in GitHub Markdown format.
105103
- Use the given description only for the overall context and only comment the code.
106104
- IMPORTANT: NEVER suggest adding comments to the code.
@@ -142,8 +140,12 @@ async function getAIResponse(prompt: string): Promise<Array<{
142140
};
143141

144142
try {
145-
const response = await openai.createChatCompletion({
143+
const response = await openai.chat.completions.create({
146144
...queryConfig,
145+
// return JSON if the model supports it:
146+
...(OPENAI_API_MODEL === "gpt-4-1106-preview"
147+
? { response_format: { type: "json_object" } }
148+
: {}),
147149
messages: [
148150
{
149151
role: "system",
@@ -152,8 +154,8 @@ async function getAIResponse(prompt: string): Promise<Array<{
152154
],
153155
});
154156

155-
const res = response.data.choices[0].message?.content?.trim() || "[]";
156-
return JSON.parse(res);
157+
const res = response.choices[0].message?.content?.trim() || "{}";
158+
return JSON.parse(res).reviews;
157159
} catch (error) {
158160
console.error("Error:", error);
159161
return null;

yarn.lock

+135-17
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,28 @@
163163
resolved "https://registry.yarnpkg.com/@tsconfig/node16/-/node16-1.0.3.tgz#472eaab5f15c1ffdd7f8628bd4c4f753995ec79e"
164164
integrity sha512-yOlFc+7UtL/89t2ZhjPvvB/DeAr3r+Dq58IgzsFkOAvVC6NMJXmCGjbptdXdR9qsX7pKcTL+s87FtYREi2dEEQ==
165165

166+
"@types/node-fetch@^2.6.4":
167+
version "2.6.9"
168+
resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.6.9.tgz#15f529d247f1ede1824f7e7acdaa192d5f28071e"
169+
integrity sha512-bQVlnMLFJ2d35DkPNjEPmd9ueO/rh5EiaZt2bhqiSarPjZIuIV6bPQVqcrEyvNo+AfTrRGVazle1tl597w3gfA==
170+
dependencies:
171+
"@types/node" "*"
172+
form-data "^4.0.0"
173+
174+
"@types/node@*":
175+
version "20.10.2"
176+
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.10.2.tgz#32a5e8228357f57714ad28d52229ab04880c2814"
177+
integrity sha512-37MXfxkb0vuIlRKHNxwCkb60PNBpR94u4efQuN4JgIAm66zfCDXGSAFCef9XUWFovX2R1ok6Z7MHhtdVXXkkIw==
178+
dependencies:
179+
undici-types "~5.26.4"
180+
181+
"@types/node@^18.11.18":
182+
version "18.19.1"
183+
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.19.1.tgz#e3ed7d5ab5ea21f33a4503decb2171e0d8f53070"
184+
integrity sha512-mZJ9V11gG5Vp0Ox2oERpeFDl+JvCwK24PGy76vVY/UgBtjwJWc5rYBThFxmbnYOm9UPZNm6wEl/sxHt2SU7x9A==
185+
dependencies:
186+
undici-types "~5.26.4"
187+
166188
"@types/node@^18.15.5":
167189
version "18.15.5"
168190
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.15.5.tgz#3af577099a99c61479149b716183e70b5239324a"
@@ -173,6 +195,13 @@
173195
resolved "https://registry.yarnpkg.com/@vercel/ncc/-/ncc-0.36.1.tgz#d4c01fdbbe909d128d1bf11c7f8b5431654c5b95"
174196
integrity sha512-S4cL7Taa9yb5qbv+6wLgiKVZ03Qfkc4jGRuiUQMQ8HGBD5pcNRnHeYM33zBvJE4/zJGjJJ8GScB+WmTsn9mORw==
175197

198+
abort-controller@^3.0.0:
199+
version "3.0.0"
200+
resolved "https://registry.yarnpkg.com/abort-controller/-/abort-controller-3.0.0.tgz#eaf54d53b62bae4138e809ca225c8439a6efb392"
201+
integrity sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==
202+
dependencies:
203+
event-target-shim "^5.0.0"
204+
176205
acorn-walk@^8.1.1:
177206
version "8.2.0"
178207
resolved "https://registry.yarnpkg.com/acorn-walk/-/acorn-walk-8.2.0.tgz#741210f2e2426454508853a2f44d0ab83b7f69c1"
@@ -183,6 +212,13 @@ acorn@^8.4.1:
183212
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.8.2.tgz#1b2f25db02af965399b9776b0c2c391276d37c4a"
184213
integrity sha512-xjIYgE8HBrkpd/sJqOGNspf8uHG+NOHGOw6a/Urj8taM2EXfdNAH2oFcPeIFfsv3+kz/mJrS5VuMqbNLjCa2vw==
185214

215+
agentkeepalive@^4.2.1:
216+
version "4.5.0"
217+
resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-4.5.0.tgz#2673ad1389b3c418c5a20c5d7364f93ca04be923"
218+
integrity sha512-5GG/5IbQQpC9FpkRGsSvZI5QYeSCzlJHdpBQntCsuTOxhKD8lqKhrleg2Yi7yvMIf82Ycmmqln9U8V9qwEiJew==
219+
dependencies:
220+
humanize-ms "^1.2.1"
221+
186222
arg@^4.1.0:
187223
version "4.1.3"
188224
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
@@ -193,18 +229,16 @@ asynckit@^0.4.0:
193229
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
194230
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==
195231

196-
axios@^0.26.0:
197-
version "0.26.1"
198-
resolved "https://registry.yarnpkg.com/axios/-/axios-0.26.1.tgz#1ede41c51fcf51bbbd6fd43669caaa4f0495aaa9"
199-
integrity sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==
200-
dependencies:
201-
follow-redirects "^1.14.8"
202-
203232
balanced-match@^1.0.0:
204233
version "1.0.2"
205234
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
206235
integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==
207236

237+
base-64@^0.1.0:
238+
version "0.1.0"
239+
resolved "https://registry.yarnpkg.com/base-64/-/base-64-0.1.0.tgz#780a99c84e7d600260361511c4877613bf24f6bb"
240+
integrity sha512-Y5gU45svrR5tI2Vt/X9GPd3L0HNIKzGu202EjxrXMpuc2V2CiKgemAbUUsqYmZJvPtCXoUKjNZwBJzsNScUbXA==
241+
208242
before-after-hook@^2.2.0:
209243
version "2.2.3"
210244
resolved "https://registry.yarnpkg.com/before-after-hook/-/before-after-hook-2.2.3.tgz#c51e809c81a4e354084422b9b26bad88249c517c"
@@ -217,6 +251,11 @@ brace-expansion@^2.0.1:
217251
dependencies:
218252
balanced-match "^1.0.0"
219253

254+
255+
version "0.0.2"
256+
resolved "https://registry.yarnpkg.com/charenc/-/charenc-0.0.2.tgz#c0a1d2f3a7092e03774bfa83f14c0fc5790a8667"
257+
integrity sha512-yrLQ/yVUFXkzg7EDQsPieE/53+0RlaWTs+wBrvW36cyilJ2SaDWfl4Yj7MtLTXleV9uEKefbAGUPv2/iWSooRA==
258+
220259
combined-stream@^1.0.8:
221260
version "1.0.8"
222261
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
@@ -229,6 +268,11 @@ create-require@^1.1.0:
229268
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
230269
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==
231270

271+
272+
version "0.0.2"
273+
resolved "https://registry.yarnpkg.com/crypt/-/crypt-0.0.2.tgz#88d7ff7ec0dfb86f713dc87bbb42d044d3e6c41b"
274+
integrity sha512-mCxBlsHFYh9C+HVpiEacem8FEBnMXgU9gy4zmNC+SXAZNB/1idgp/aulFJ4FgCi7GPEVbfyng092GqL2k2rmow==
275+
232276
delayed-stream@~1.0.0:
233277
version "1.0.0"
234278
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
@@ -244,10 +288,23 @@ diff@^4.0.1:
244288
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
245289
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
246290

247-
follow-redirects@^1.14.8:
248-
version "1.15.2"
249-
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13"
250-
integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==
291+
digest-fetch@^1.3.0:
292+
version "1.3.0"
293+
resolved "https://registry.yarnpkg.com/digest-fetch/-/digest-fetch-1.3.0.tgz#898e69264d00012a23cf26e8a3e40320143fc661"
294+
integrity sha512-CGJuv6iKNM7QyZlM2T3sPAdZWd/p9zQiRNS9G+9COUCwzWFTs0Xp8NF5iePx7wtvhDykReiRRrSeNb4oMmB8lA==
295+
dependencies:
296+
base-64 "^0.1.0"
297+
md5 "^2.3.0"
298+
299+
event-target-shim@^5.0.0:
300+
version "5.0.1"
301+
resolved "https://registry.yarnpkg.com/event-target-shim/-/event-target-shim-5.0.1.tgz#5d4d3ebdf9583d63a5333ce2deb7480ab2b05789"
302+
integrity sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==
303+
304+
305+
version "1.7.2"
306+
resolved "https://registry.yarnpkg.com/form-data-encoder/-/form-data-encoder-1.7.2.tgz#1f1ae3dccf58ed4690b86d87e4f57c654fbab040"
307+
integrity sha512-qfqtYan3rxrnCk1VYaA4H+Ms9xdpPqvLZa6xmMgFvhO32x7/3J/ExcTd6qpxM0vH2GdMI+poehyBZvqfMTto8A==
251308

252309
form-data@^4.0.0:
253310
version "4.0.0"
@@ -258,6 +315,26 @@ form-data@^4.0.0:
258315
combined-stream "^1.0.8"
259316
mime-types "^2.1.12"
260317

318+
formdata-node@^4.3.2:
319+
version "4.4.1"
320+
resolved "https://registry.yarnpkg.com/formdata-node/-/formdata-node-4.4.1.tgz#23f6a5cb9cb55315912cbec4ff7b0f59bbd191e2"
321+
integrity sha512-0iirZp3uVDjVGt9p49aTaqjk84TrglENEDuqfdlZQ1roC9CWlPk6Avf8EEnZNcAqPonwkG35x4n3ww/1THYAeQ==
322+
dependencies:
323+
node-domexception "1.0.0"
324+
web-streams-polyfill "4.0.0-beta.3"
325+
326+
humanize-ms@^1.2.1:
327+
version "1.2.1"
328+
resolved "https://registry.yarnpkg.com/humanize-ms/-/humanize-ms-1.2.1.tgz#c46e3159a293f6b896da29316d8b6fe8bb79bbed"
329+
integrity sha512-Fl70vYtsAFb/C06PTS9dZBo7ihau+Tu/DNCk/OyHhea07S+aeMWpFFkUaXRa8fI+ScZbEI8dfSxwY7gxZ9SAVQ==
330+
dependencies:
331+
ms "^2.0.0"
332+
333+
is-buffer@~1.1.6:
334+
version "1.1.6"
335+
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be"
336+
integrity sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==
337+
261338
is-plain-object@^5.0.0:
262339
version "5.0.0"
263340
resolved "https://registry.yarnpkg.com/is-plain-object/-/is-plain-object-5.0.0.tgz#4427f50ab3429e9025ea7d52e9043a9ef4159344"
@@ -268,6 +345,15 @@ make-error@^1.1.1:
268345
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
269346
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==
270347

348+
md5@^2.3.0:
349+
version "2.3.0"
350+
resolved "https://registry.yarnpkg.com/md5/-/md5-2.3.0.tgz#c3da9a6aae3a30b46b7b0c349b87b110dc3bda4f"
351+
integrity sha512-T1GITYmFaKuO91vxyoQMFETst+O71VUPEU3ze5GNzDm0OWdP8v1ziTaAEPUr/3kLsY3Sftgz242A1SetQiDL7g==
352+
dependencies:
353+
charenc "0.0.2"
354+
crypt "0.0.2"
355+
is-buffer "~1.1.6"
356+
271357
272358
version "1.52.0"
273359
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70"
@@ -287,6 +373,16 @@ minimatch@^7.4.2:
287373
dependencies:
288374
brace-expansion "^2.0.1"
289375

376+
ms@^2.0.0:
377+
version "2.1.3"
378+
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2"
379+
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==
380+
381+
382+
version "1.0.0"
383+
resolved "https://registry.yarnpkg.com/node-domexception/-/node-domexception-1.0.0.tgz#6888db46a1f71c0b76b3f7555016b63fe64766e5"
384+
integrity sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==
385+
290386
node-fetch@^2.6.7:
291387
version "2.6.9"
292388
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.9.tgz#7c7f744b5cc6eb5fd404e0c7a9fec630a55657e6"
@@ -301,13 +397,20 @@ once@^1.4.0:
301397
dependencies:
302398
wrappy "1"
303399

304-
openai@^3.2.1:
305-
version "3.2.1"
306-
resolved "https://registry.yarnpkg.com/openai/-/openai-3.2.1.tgz#1fa35bdf979cbde8453b43f2dd3a7d401ee40866"
307-
integrity sha512-762C9BNlJPbjjlWZi4WYK9iM2tAVAv0uUp1UmI34vb0CN5T2mjB/qM6RYBmNKMh/dN9fC+bxqPwWJZUTWW052A==
400+
openai@^4.20.1:
401+
version "4.20.1"
402+
resolved "https://registry.yarnpkg.com/openai/-/openai-4.20.1.tgz#afa0d496d125b5a0f6cebcb4b9aeabf71e00214e"
403+
integrity sha512-Dd3q8EvINfganZFtg6V36HjrMaihqRgIcKiHua4Nq9aw/PxOP48dhbsk8x5klrxajt5Lpnc1KTOG5i1S6BKAJA==
308404
dependencies:
309-
axios "^0.26.0"
310-
form-data "^4.0.0"
405+
"@types/node" "^18.11.18"
406+
"@types/node-fetch" "^2.6.4"
407+
abort-controller "^3.0.0"
408+
agentkeepalive "^4.2.1"
409+
digest-fetch "^1.3.0"
410+
form-data-encoder "1.7.2"
411+
formdata-node "^4.3.2"
412+
node-fetch "^2.6.7"
413+
web-streams-polyfill "^3.2.1"
311414

312415
parse-diff@^0.11.1:
313416
version "0.11.1"
@@ -353,6 +456,11 @@ typescript@^5.0.2:
353456
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.0.2.tgz#891e1a90c5189d8506af64b9ef929fca99ba1ee5"
354457
integrity sha512-wVORMBGO/FAs/++blGNeAVdbNKtIh1rbBL2EyQ1+J9lClJ93KiiKe8PmFIVdXhHcyv44SL9oglmfeSsndo0jRw==
355458

459+
undici-types@~5.26.4:
460+
version "5.26.5"
461+
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-5.26.5.tgz#bcd539893d00b56e964fd2657a4866b221a65617"
462+
integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==
463+
356464
universal-user-agent@^6.0.0:
357465
version "6.0.0"
358466
resolved "https://registry.yarnpkg.com/universal-user-agent/-/universal-user-agent-6.0.0.tgz#3381f8503b251c0d9cd21bc1de939ec9df5480ee"
@@ -368,6 +476,16 @@ v8-compile-cache-lib@^3.0.1:
368476
resolved "https://registry.yarnpkg.com/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz#6336e8d71965cb3d35a1bbb7868445a7c05264bf"
369477
integrity sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==
370478

479+
480+
version "4.0.0-beta.3"
481+
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-4.0.0-beta.3.tgz#2898486b74f5156095e473efe989dcf185047a38"
482+
integrity sha512-QW95TCTaHmsYfHDybGMwO5IJIM93I/6vTRk+daHTWFPhwh+C8Cg7j7XyKrwrj8Ib6vYXe0ocYNrmzY4xAAN6ug==
483+
484+
web-streams-polyfill@^3.2.1:
485+
version "3.2.1"
486+
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-3.2.1.tgz#71c2718c52b45fd49dbeee88634b3a60ceab42a6"
487+
integrity sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q==
488+
371489
webidl-conversions@^3.0.0:
372490
version "3.0.1"
373491
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871"

0 commit comments

Comments
 (0)