Skip to content

Commit f66204e

Browse files
committed
improve
1 parent e767ccd commit f66204e

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

src/index.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Deno.test("basic", async () => {
1111
[4, false],
1212
],
1313
});
14-
await waitAllWrites()
14+
await waitAllWrites();
1515
assertEquals([53, 44].map(f), [true, false]);
1616
});
1717

@@ -22,10 +22,11 @@ Deno.test("complex", async () => {
2222
["building", false],
2323
["is", true],
2424
["growth", false],
25+
["that", true],
2526
],
2627
});
27-
await waitAllWrites()
28-
assertEquals(["that", "tree"].map(f), [true, false]);
28+
await waitAllWrites();
29+
assertEquals(["this", "tree"].map(f), [true, false]);
2930
});
3031

3132
Deno.test("composite output / input", async () => {
@@ -46,7 +47,7 @@ Deno.test("composite output / input", async () => {
4647
],
4748
],
4849
});
49-
await waitAllWrites()
50+
await waitAllWrites();
5051
assertEquals(
5152
f([
5253
{ name: "john", age: 99 },

src/index.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
1-
import {
2-
ChatCompletion,
3-
ChatCompletionOptions,
4-
OpenAI,
5-
} from "https://deno.land/x/openai@1.4.2/mod.ts";
6-
1+
import { OpenAI } from "npm:openai@4.17.5";
72
import { cache } from "https://deno.land/x/rmmbr@0.0.19/client/src/index.ts";
83
import { equal } from "https://deno.land/std@0.174.0/testing/asserts.ts";
94
import { isPureFunction } from "./purity.ts";
105

11-
const openai = new OpenAI(Deno.env.get("openai_key")!);
6+
const openai = new OpenAI({ apiKey: Deno.env.get("openai_key")! });
127

138
const cachedOpenAI = cache({ cacheId: "createChatCompletion" })(
14-
(x: ChatCompletionOptions) => openai.createChatCompletion(x),
9+
(x) => openai.chat.completions.create(x),
1510
);
1611

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

2318
const maxPromptLength = 2400;
2419

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

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

93+
const cleanSurroundingQuotes = (code: string) =>
94+
code.trim().startsWith("```")
95+
? code.trim().replace(/^```javascript/, "").replace(/```$/, "")
96+
: code;
97+
9698
export const makeFunction = async <Input, Output>({
9799
description,
98100
testCases,
99101
}: Options<Input, Output>): Promise<(input: Input) => Output> => {
100-
const code = await doPrompt(getPrompt(description, testCases));
102+
const code = cleanSurroundingQuotes(
103+
await doPrompt(getPrompt(description, testCases)),
104+
);
101105
if (!isPureFunction(code)) throw new Error(`impure code detected: ${code}`);
102106
const f = Function("x", code.slice(14, code.length - 1)) as (
103107
input: Input,

0 commit comments

Comments
 (0)