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" ;
7
2
import { cache } from "https://deno.land/x/rmmbr@0.0.19/client/src/index.ts" ;
8
3
import { equal } from "https://deno.land/std@0.174.0/testing/asserts.ts" ;
9
4
import { isPureFunction } from "./purity.ts" ;
10
5
11
- const openai = new OpenAI ( Deno . env . get ( "openai_key" ) ! ) ;
6
+ const openai = new OpenAI ( { apiKey : Deno . env . get ( "openai_key" ) ! } ) ;
12
7
13
8
const cachedOpenAI = cache ( { cacheId : "createChatCompletion" } ) (
14
- ( x : ChatCompletionOptions ) => openai . createChatCompletion ( x ) ,
9
+ ( x ) => openai . chat . completions . create ( x ) ,
15
10
) ;
16
11
17
12
const doPrompt = ( prompt : string ) =>
18
13
cachedOpenAI ( {
19
14
model : "gpt-4" ,
20
15
messages : [ { role : "user" , content : prompt } ] ,
21
- } ) . then ( ( { choices } : ChatCompletion ) => choices [ 0 ] . message ?. content || "" ) ;
16
+ } ) . then ( ( { choices } ) => choices [ 0 ] . message ?. content || "" ) ;
22
17
23
18
const maxPromptLength = 2400 ;
24
19
@@ -30,11 +25,13 @@ const testCaseToString = <Input, Output>([input, output]: TestCase<
30
25
output: ${ JSON . stringify ( output ) } ` ;
31
26
32
27
const prefix = `Write a javascript function as dscribed below.
28
+
33
29
It must be called \`f\`, it must be unary and the variable should be called \`x\`.
30
+
34
31
No side effects or dependencies are allowed, so no \`console.log\` for example.
35
32
Your answer must start with \`function f(x){\` and must end with \`}\`, bceause it's a single function.
36
33
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.
38
35
39
36
Please make the code as concise and as readable as you can, no repetitions.
40
37
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> = {
93
90
testCases : TestCase < Input , Output > [ ] ;
94
91
} ;
95
92
93
+ const cleanSurroundingQuotes = ( code : string ) =>
94
+ code . trim ( ) . startsWith ( "```" )
95
+ ? code . trim ( ) . replace ( / ^ ` ` ` j a v a s c r i p t / , "" ) . replace ( / ` ` ` $ / , "" )
96
+ : code ;
97
+
96
98
export const makeFunction = async < Input , Output > ( {
97
99
description,
98
100
testCases,
99
101
} : 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
+ ) ;
101
105
if ( ! isPureFunction ( code ) ) throw new Error ( `impure code detected: ${ code } ` ) ;
102
106
const f = Function ( "x" , code . slice ( 14 , code . length - 1 ) ) as (
103
107
input : Input ,
0 commit comments