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
54 changes: 46 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"build": "tsc"
},
"dependencies": {
"body-parser": "^1.20.2",
"connect-pg-simple": "^9.0.0",
"cookie-parser": "~1.4.4",
"cors": "^2.8.5",
Expand Down
51 changes: 28 additions & 23 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import pg from "pg";
import connectPg from "connect-pg-simple";
import path from "path";
import { simplifiedRepos } from "./utils";
import { promptGpt } from "./gpt";
import { createFileQuestionsPrompt, promptGpt } from "./gpt";
import bodyParser from "body-parser";

const ghClientId = process.env.GITHUB_CLIENT_ID;
const ghClientSecret = process.env.GITHUB_CLIENT_SECRET;
Expand Down Expand Up @@ -125,7 +126,9 @@ globalEm.then((em) => {
app.use(passport.initialize());
app.use(passport.session());

app.use(cors());
// app.use(cors());

app.use(bodyParser.json());

// Serve static react app
app.use(express.static(path.join(__dirname, "../public")));
Expand Down Expand Up @@ -192,7 +195,7 @@ globalEm.then((em) => {
}
);

app.get("/users/me", isAuthenticated, (req, res) => {
app.get("/api/users/me", isAuthenticated, (req, res) => {
const user = req.user as User;
const octokit = new Octokit({
auth: user.ghToken,
Expand Down Expand Up @@ -233,29 +236,30 @@ globalEm.then((em) => {
if (!repoTree.data.tree.length) {
throw new Error("Cannot get tree data");
}
let repoJson = {};
const files = repoTree.data.tree.filter((treeItem) => {
return (
treeItem.type === "blob" &&
treeItem.path?.endsWith(".js") &&
treeItem.url
);
});
const filesContent = await Promise.all(
repoTree.data.tree
.filter((treeItem) => {
return (
treeItem.type === "blob" &&
treeItem.path?.endsWith(".js") &&
treeItem.url
);
})
.map(async ({ url }) => {
// Download file content
const fileContent = await octokit.request(url as any);
// Parse file content
const conentDecoded = Buffer.from(
fileContent.data.content,
"base64"
).toString();
return conentDecoded;
})
files.map(async ({ url }) => {
// Download file content
const fileContent = await octokit.request(url as any);
// Parse file content
const conentDecoded = Buffer.from(
fileContent.data.content,
"base64"
).toString();
return conentDecoded;
})
);
console.log("JS files contents", filesContent);
return ["Some mock question"];
const prompt = createFileQuestionsPrompt(filesContent[0]);
const gptRes = await promptGpt(prompt);
console.log(gptRes);
return gptRes.content;
}

app.get("/quiz-questions/:repoId", isAuthenticated, (req, res) => {
Expand All @@ -271,6 +275,7 @@ globalEm.then((em) => {
});

app.post("/prompt-gpt", (req, res) => {
console.log(req.body);
const prompt = req.body.prompt;
if (!prompt) {
res.status(400).send({ error: "Missing prompt" });
Expand Down
22 changes: 11 additions & 11 deletions src/gpt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ const openai = new OpenAI({
apiKey: apiKey,
});

// function createFileQuestionsPrompt(fileContent) {
// return `
// I have a following JavaScript file:
export function createFileQuestionsPrompt(fileContent: string) {
return `
I have a following JavaScript file:

// ======== FILE STARTS HERE ========
// ${fileContent}
// ======== FILE ENDS HERE ==========
======== FILE STARTS HERE ========
${fileContent}
======== FILE ENDS HERE ==========

// Based on this file, please generate a question that can be asked
// in order to test whether I understand the contents of the file.
// `;
// }
Based on this file, please generate 4 questions (each in a new line)
that can be asked in order to test whether I understand the contents of the file.
`;
}

export async function promptGpt(prompt: string) {
const resp = await openai.chat.completions.create({
messages: [{ role: "user", content: "Say this is a test" }],
messages: [{ role: "user", content: prompt }],
model: "gpt-3.5-turbo",
});
return resp.choices[0].message;
Expand Down