import * as fs from "fs"; import Anthropic from "@anthropic-ai/sdk"; function readApiKey(): string { return fs.readFileSync("secrets/api-key").toString().replace(/\n/,""); } const apiKey = readApiKey(); console.log(apiKey); const anthropic = new Anthropic({ apiKey: apiKey }); const prompt: string = fs.readFileSync("prompt.txt").toString(); export async function query(code: string) { if (false) { const demo: string = 'HIwhatExplain it!'; return demo; } // Replace placeholders like {{JULIA_CODE}} with real values, // because the SDK does not support variables. let myprompt: string = prompt.replace("{{JULIA_CODE}}", code); const msg = await anthropic.messages.create({ model: "claude-3-7-sonnet-20250219", max_tokens: 20000, temperature: 1, messages: [ { "role": "user", "content": [ { "type": "text", "text": "\n\n\na = rand(Float64, (100,))\na = a .+ 2\n\n\n\n\n\n\na = a .+ 2\n\n\na .+= 2\n\n\nInstead of creating a new array with `a .+ 2` and reassigning it to `a`, using the in-place broadcasting assignment operator `.+=` modifies the existing array without allocating a new one. This eliminates an unnecessary allocation.\n\n3\n\n\n\n\na = rand(Float64, (100,))\n\n\na = rand(Float64, 100)\n\n\nFor one-dimensional arrays, you can simply pass the length as an integer rather than using a tuple with one element. This makes the code more concise without changing functionality.\n\n1\n\n\n\n\n\n\n" }, { "type": "text", "text": myprompt } ] } ] }); let r: string = ''; for (let segment of msg.content) { if (segment.type == "text") { r = r + segment.text; } } console.log('====='); console.log(r); return r; }