Public
Edited
Jan 22, 2024
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
prompt1 = `Consider the task of annotation updating. Look at the following old annotated file, then copy the annotations to the updated version of the file below it. In this case, annotations are represented by numbered opening and closing tags, like <1> </1>. Your response should be only the updated file with annotations transferred to analogous positions.

OLD ANNOTATED FILE:
${old_annotated_file}
END OLD ANNOTATED FILE

UPDATED FILE:
${updated_file}
END UPDATED FILE

Write your answer below. Again, your response should be only the updated file with annotations transferred to analogous positions.
`
Insert cell
output = askChatGPT(prompt1)
Insert cell
fpcore = askChatGPT(fpcore_prompt(output))
Insert cell
import {secret} from "@tmcw/secret"
Insert cell
openai = new OpenAI({
apiKey: OPENAI_API_KEY,
dangerouslyAllowBrowser: true
})
Insert cell
OpenAI = (await import("https://esm.sh/openai@4.19.0")).OpenAI
Insert cell
test = {
//return 42 // comment to run test on completion endpoint
let completion = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [
//{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: fpcore_prompt },
// {
// role: "assistant",
// content: "4"
// },
// { role: "user", content: "What is 3 times 3?" }
]
});
console.log(completion.choices[0]);
return completion;
}
Insert cell
test.choices[0].message.content
Insert cell
messages = [
//{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: prompt },
// {
// role: "assistant",
// content: "4"
// },
// { role: "user", content: "What is 3 times 3?" }
]
Insert cell
function getFirstChoice(completion) {
return completion.choices[0].message.content
}
Insert cell
async function getChatCompletion(model, messages) {
let completion = await openai.chat.completions.create({
model, //: "gpt-3.5-turbo",
messages
})
return completion;
}
Insert cell
getChatCompletion([{role: 'user', content: prompt1}])
Insert cell
async function askChatGPT(prompt, model='gpt-3.5-turbo') {
return getFirstChoice(await getChatCompletion(model, [{role: 'user', content: prompt}]))
}
Insert cell
output1 = `static Py_complex
cmath_asinh_impl(PyObject *module, Py_complex z)
/*[clinic end generated code: output=733d8107841a7599 input=5c09448fcfc89a79]*/
/*last modified Jan 2024*/
{
Py_complex s1, s2, r;

SPECIAL_VALUE(z, asinh_special_values);

if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) {
if (z.imag >= 0.) {
r.real = copysign(<1>log(hypot(z.real/2., z.imag/2.)) +
M_LN2*2.</1>, z.real);
} else {
r.real = -copysign(log(hypot(z.real/2., z.imag/2.)) +
M_LN2*2., -z.real);
}
r.imag = atan2(z.imag, fabs(z.real));
} else {
s1.real = 1.+z.imag;
s1.imag = -z.real;
s1 = cmath_sqrt_impl(module, s1);
s2.real = 1.-z.imag;
s2.imag = z.real;
s2 = cmath_sqrt_impl(module, s2);
r.real = asinh(s1.real*s2.imag-s2.real*s1.imag);
r.imag = atan2(z.imag, s1.real*s2.real-s1.imag*s2.imag);
}
errno = 0;
return r;
}
END UPDATED FILE`
Insert cell
function extractFirstMatch(str) {
const regex = /<1>(.*?)<\/1>/s;
const match = str.match(regex);
return match ? match[1] : null;
}
Insert cell
fpcore_prompt = output => `Consider the following Python code.

\`\`\`python
import math

def compute_expression(N):
if N > 1 and N < 1e+40:
if N >= 1e+3:
return 1/N - 1/(2*N**2) + 1/(3*N**3) - 1/(4*N**4)
else:
return math.log(1 + 1/N)
else:
return "N is out of the valid range"
\`\`\`

We can represent this expression in FPCore, a format for floating-point analysis, as follows:
\`\`\`fpcore
(FPCore (N)
:pre (and (> N 1) (< N 1e+40))
(if (>= N 1e+3)
(+ (/ 1 N) (/ -1 (* 2 (pow N 2))) (/ 1 (* 3 (pow N 3))) (/ -1 (* 4 (pow N 4))))
(log (+ 1 (/ 1 N)))))
\`\`\`

Now, represent the following expression (which may or may not be Python!) as an FPCore expression:
${extractFirstMatch(output)}


Write the FPCore expression only below.
`
Insert cell
/*
TODO

Model may need fine-tune or introductory examples of how this process works, especially for covering corner cases.

* Test effect of structure of language on success
* Maybe in some places error is tolerated?
* Could it learn through examples?
* Probably should let the user help the model fit the domain

Things to do with the snippet
* Translate into English
* Execute
* Visualize
* Structured editing/Livelits
* Using AST/types would let you attach visualizations to particular branches

* Test different models
* would want to know what users "mean" by section
* what about one-to-many mappings (code duplication)
* Git 3-way-merge as baseline -- good as anecdote
*


*/
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more