Skip to content

Commit

Permalink
feat: implement review api
Browse files Browse the repository at this point in the history
  • Loading branch information
richiemccoll committed Mar 13, 2023
1 parent 357b5f6 commit 97b5355
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 40 deletions.
50 changes: 26 additions & 24 deletions backend/routes/review.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,30 @@ import promptEngine from "../services/prompt-engine.js";
import suggestions from "../services/suggestions.js";

export default async function review(fastify) {
fastify.post(
"/review",
{
schema: {
response: {
200: {
description: "Success Response",
type: "object",
properties: {
message: { type: "string" },
},
},
},
},
},
async (request, reply) => {
const dynamicPrompt = promptEngine.build(JSON.parse(request.body));
const response = await suggestions.create({
transformerType: "chatGPT",
payload: dynamicPrompt,
});
return reply.status(200).send(response);
}
);
fastify.post("/review", {}, async (request, reply) => {
const prompts = promptEngine.build(request.body);
const response = await Promise.all(
prompts.map(async (file) => {
let suggestionsForFile = {};

const transformerResponse = await suggestions.create({
transformerType: "chatGPT",
payload: file.changes,
});

transformerResponse.forEach((suggestion, index) => {
suggestionsForFile = {
filename: file.fileName,
lineRange: file.changes[index].range,
diff: file.changes[index].diff,
suggestions: suggestion.choices.map((choice) => choice.text),
};
});

return suggestionsForFile;
})
);

return reply.status(200).send(response);
});
}
38 changes: 22 additions & 16 deletions backend/services/suggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,32 @@ import * as dotenv from "dotenv";
dotenv.config();

const suggestions = {
buildMessages(payload) {},
async callChatGPTService(payload) {
const apiUrl = "https://api.openai.com/v1/completions";
const response = await fetch(apiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.CHAT_GPT_API_KEY}`,
},
body: JSON.stringify({
model: "text-davinci-003",
prompt: payload,
}),
});
const body = await response.json();
return body;
},

async create({ transformerType, payload }) {
switch (transformerType) {
case "chatGPT": {
try {
const apiUrl = "https://api.openai.com/v1/completions";
const messages = this.buildMessages(payload);
const response = await fetch(apiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.CHAT_GPT_API_KEY}`,
},
body: JSON.stringify({
model: "text-davinci-003",
prompt: "Say this is a test",
}),
});
const body = await response.json();
console.log(body);
const prompts = payload.map((file) =>
this.callChatGPTService(file.prompt)
);
const suggestions = await Promise.all(prompts);
return suggestions;
} catch (error) {
throw new Error(`received error from chatGPT API + ${error.message}`);
}
Expand Down

0 comments on commit 97b5355

Please sign in to comment.