Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
uriva committed Nov 14, 2023
1 parent e767ccd commit f66204e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
9 changes: 5 additions & 4 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Deno.test("basic", async () => {
[4, false],
],
});
await waitAllWrites()
await waitAllWrites();
assertEquals([53, 44].map(f), [true, false]);
});

Expand All @@ -22,10 +22,11 @@ Deno.test("complex", async () => {
["building", false],
["is", true],
["growth", false],
["that", true],
],
});
await waitAllWrites()
assertEquals(["that", "tree"].map(f), [true, false]);
await waitAllWrites();
assertEquals(["this", "tree"].map(f), [true, false]);
});

Deno.test("composite output / input", async () => {
Expand All @@ -46,7 +47,7 @@ Deno.test("composite output / input", async () => {
],
],
});
await waitAllWrites()
await waitAllWrites();
assertEquals(
f([
{ name: "john", age: 99 },
Expand Down
26 changes: 15 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
import {
ChatCompletion,
ChatCompletionOptions,
OpenAI,
} from "https://deno.land/x/[email protected]/mod.ts";

import { OpenAI } from "npm:[email protected]";
import { cache } from "https://deno.land/x/[email protected]/client/src/index.ts";
import { equal } from "https://deno.land/[email protected]/testing/asserts.ts";
import { isPureFunction } from "./purity.ts";

const openai = new OpenAI(Deno.env.get("openai_key")!);
const openai = new OpenAI({ apiKey: Deno.env.get("openai_key")! });

const cachedOpenAI = cache({ cacheId: "createChatCompletion" })(
(x: ChatCompletionOptions) => openai.createChatCompletion(x),
(x) => openai.chat.completions.create(x),
);

const doPrompt = (prompt: string) =>
cachedOpenAI({
model: "gpt-4",
messages: [{ role: "user", content: prompt }],
}).then(({ choices }: ChatCompletion) => choices[0].message?.content || "");
}).then(({ choices }) => choices[0].message?.content || "");

const maxPromptLength = 2400;

Expand All @@ -30,11 +25,13 @@ const testCaseToString = <Input, Output>([input, output]: TestCase<
output: ${JSON.stringify(output)}`;

const prefix = `Write a javascript function as dscribed below.
It must be called \`f\`, it must be unary and the variable should be called \`x\`.
No side effects or dependencies are allowed, so no \`console.log\` for example.
Your answer must start with \`function f(x){\` and must end with \`}\`, bceause it's a single function.
Your answer must be code that compiles only, no other text is allowed. No need to list the test cases.
Your answer must javascript code that compiles, no other text is allowed. No need to list the test cases.
Please make the code as concise and as readable as you can, no repetitions.
After the description there are test cases, go over each one and make sure your code works for them.
Expand Down Expand Up @@ -93,11 +90,18 @@ type Options<Input, Output> = {
testCases: TestCase<Input, Output>[];
};

const cleanSurroundingQuotes = (code: string) =>
code.trim().startsWith("```")
? code.trim().replace(/^```javascript/, "").replace(/```$/, "")
: code;

export const makeFunction = async <Input, Output>({
description,
testCases,
}: Options<Input, Output>): Promise<(input: Input) => Output> => {
const code = await doPrompt(getPrompt(description, testCases));
const code = cleanSurroundingQuotes(
await doPrompt(getPrompt(description, testCases)),
);
if (!isPureFunction(code)) throw new Error(`impure code detected: ${code}`);
const f = Function("x", code.slice(14, code.length - 1)) as (
input: Input,
Expand Down

0 comments on commit f66204e

Please sign in to comment.