Created
October 21, 2024 18:51
-
-
Save zackangelo/d0dcd7c1bb8a77a8f11ce2a455e58ba0 to your computer and use it in GitHub Desktop.
Generate and run a script in-context
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
// Script Inception | |
// Ask the model to generate code and execute it in-context | |
// Generate some javascript code to compute the fibonacci sequence | |
prompt( | |
`Can you write a non-recursive javascript function named \"fib\" ` + | |
`to compute the fibonacci sequence? (Please just define the function and nothing else.) \n`, | |
{ role: 'user' } | |
); | |
// pre-fill response with code fence | |
prompt("```javascript", { role: 'assistant' }) | |
let script = gen({ limit: 1000, stop_at: "```", role: 'assistant' }); | |
// Now run the code the model generated to compute | |
// the 10th element of the fibonacci sequence. | |
let fib10 = eval(`${script}\nfib(10)`); | |
// Ask the model if the result is correct | |
// (We use "11th element" to because we don't use a zero-based index here) | |
prompt( | |
`\nThe 11th element of ` + | |
`the fibonacci sequence is ${fib10}.`, | |
{ role: 'user' } | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment