-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
20 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
@@ -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. | ||
|
@@ -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, | ||
|