Skip to content

Commit

Permalink
add api caller
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuyz0112 committed Dec 21, 2023
1 parent 979f936 commit 7324489
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
53 changes: 53 additions & 0 deletions prompts/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { retryAsync } from "https://deno.land/x/[email protected]/mod.ts";
import OpenAI from "https://deno.land/x/[email protected]/mod.ts";
import {
ChatCompletionMessageParam,
CompletionUsage,
} from "https://deno.land/x/[email protected]/resources/mod.ts";
import { assert } from "https://deno.land/[email protected]/assert/assert.ts";
import { ChatCompletionCreateParamsBase } from "https://deno.land/x/[email protected]/resources/chat/completions.ts";
import { fromMarkdown } from "https://esm.sh/[email protected]";
import { visitParents } from "https://esm.sh/[email protected]";

const apiKey = Deno.env.get("OPENAI_API_KEY");
assert(apiKey, "failed to get openAI API key");

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

export async function getCode(
messages: ChatCompletionMessageParam[] = [],
model: ChatCompletionCreateParamsBase["model"]
): Promise<{ code: string; usage?: CompletionUsage | undefined }> {
return await retryAsync<{
code: string;
usage?: CompletionUsage | undefined;
}>(
async () => {
const chatCompletion = await openai.chat.completions.create({
messages,
model,
max_tokens: 3000,
temperature: 1,
});

const codeBlocks: string[] = [];
const tree = fromMarkdown(chatCompletion.choices[0].message.content);
// deno-lint-ignore no-explicit-any
visitParents(tree, "code", (node: any) => {
codeBlocks.push(node.value.trim());
});

if (codeBlocks.length !== 1) {
throw new Error(`invalid code blocks ${JSON.stringify(codeBlocks)}`);
}

return {
code: codeBlocks[0],
usage: chatCompletion.usage,
};
},
{ delay: 100, maxTry: 3 }
);
}
6 changes: 5 additions & 1 deletion prompts/ui-gen.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Octokit } from "npm:octokit";
import commitPlugin from "npm:octokit-commit-multiple-files";
import { assert } from "https://deno.land/[email protected]/assert/assert.ts";
import { getCode } from "./common.ts";

type IssueEvent = {
issue: {
Expand All @@ -25,6 +26,9 @@ const whitelist = ["Yuyz0112"];
const vxDevPrefix = `[vx.dev]`;
const uiGenLabel = `ui-gen`;

const systemPrompt = await Deno.readTextFile("./ui-gen.md");
console.log(systemPrompt);

async function getConnectedPr(
owner: string,
repo: string,
Expand Down Expand Up @@ -213,7 +217,7 @@ async function main() {
})
).default;

console.log(githubEvent);
console.log(githubEvent.issue);

if (githubEvent.issue.labels.every((l) => l.name !== uiGenLabel)) {
return;
Expand Down

0 comments on commit 7324489

Please sign in to comment.