From bab7929215b3d5a1d66b43ee5b03ad767aa32374 Mon Sep 17 00:00:00 2001 From: Scott Lawrence Date: Wed, 19 Mar 2025 21:23:32 -0600 Subject: Initial commit --- claude-julia/query.ts | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 claude-julia/query.ts (limited to 'claude-julia/query.ts') diff --git a/claude-julia/query.ts b/claude-julia/query.ts new file mode 100644 index 0000000..50bd775 --- /dev/null +++ b/claude-julia/query.ts @@ -0,0 +1,56 @@ +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; +} + -- cgit v1.2.3-54-g00ecf