From 95c957597ec919eb13bd5179946c9a972ce6de96 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Fri, 6 Dec 2024 16:29:42 +0000 Subject: [PATCH] =?UTF-8?q?refactor:=20update=20file=20handling=20and=20im?= =?UTF-8?q?prove=20scripts=20structure=20=F0=9F=94=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/cli/src/run.ts | 29 ++++++++++--------- .../sample/genaisrc/llm-as-expert.genai.mts | 1 - packages/sample/genaisrc/prediction.genai.mjs | 1 + .../sample/genaisrc/summarize-link.genai.js | 1 + .../genaisrc/summarize-ollama-gemma2.genai.js | 22 -------------- .../summary-of-summary-gpt35.genai.js | 24 --------------- ...3.genai.js => summary-of-summary.genai.js} | 8 ++--- packages/sample/genaisrc/todo.genai.js | 18 +++++++----- 8 files changed, 31 insertions(+), 73 deletions(-) delete mode 100644 packages/sample/genaisrc/summarize-ollama-gemma2.genai.js delete mode 100644 packages/sample/genaisrc/summary-of-summary-gpt35.genai.js rename packages/sample/genaisrc/{summary-of-summary-phi3.genai.js => summary-of-summary.genai.js} (73%) diff --git a/packages/cli/src/run.ts b/packages/cli/src/run.ts index 69524253fb..269197abb0 100644 --- a/packages/cli/src/run.ts +++ b/packages/cli/src/run.ts @@ -250,24 +250,25 @@ export async function runScript( if (GENAI_ANY_REGEX.test(scriptId)) toolFiles.push(scriptId) for (const arg of files) { + if (HTTPS_REGEX.test(arg)) { + resolvedFiles.add(arg) + continue + } const stats = await host.statFile(arg) if (!stats) return fail(`file not found: ${arg}`, FILES_NOT_FOUND_ERROR_CODE) if (stats.type !== "file") continue - if (HTTPS_REGEX.test(arg)) resolvedFiles.add(arg) - else { - const ffs = await host.findFiles(arg, { - applyGitIgnore: excludeGitIgnore, - }) - if (!ffs?.length) { - return fail( - `no files matching ${arg} under ${process.cwd()}`, - FILES_NOT_FOUND_ERROR_CODE - ) - } - for (const file of ffs) { - resolvedFiles.add(filePathOrUrlToWorkspaceFile(file)) - } + const ffs = await host.findFiles(arg, { + applyGitIgnore: excludeGitIgnore, + }) + if (!ffs?.length) { + return fail( + `no files matching ${arg} under ${process.cwd()}`, + FILES_NOT_FOUND_ERROR_CODE + ) + } + for (const file of ffs) { + resolvedFiles.add(filePathOrUrlToWorkspaceFile(file)) } } diff --git a/packages/sample/genaisrc/llm-as-expert.genai.mts b/packages/sample/genaisrc/llm-as-expert.genai.mts index aac24e1695..679971422f 100644 --- a/packages/sample/genaisrc/llm-as-expert.genai.mts +++ b/packages/sample/genaisrc/llm-as-expert.genai.mts @@ -3,7 +3,6 @@ script({ system: ["system", "system.assistant", "system.tools"], tests: { keywords: [ - "Permanent Waves", "Moving Pictures", "Signals", "Power Windows", diff --git a/packages/sample/genaisrc/prediction.genai.mjs b/packages/sample/genaisrc/prediction.genai.mjs index c653514763..dd7b444b3a 100644 --- a/packages/sample/genaisrc/prediction.genai.mjs +++ b/packages/sample/genaisrc/prediction.genai.mjs @@ -1,5 +1,6 @@ script({ model: "openai:gpt-4o", + group: "openai", files: "src/greeter.ts", tests: { files: "src/greeter.ts", diff --git a/packages/sample/genaisrc/summarize-link.genai.js b/packages/sample/genaisrc/summarize-link.genai.js index 720d79ca2c..b1ddeb1855 100644 --- a/packages/sample/genaisrc/summarize-link.genai.js +++ b/packages/sample/genaisrc/summarize-link.genai.js @@ -5,6 +5,7 @@ script({ group: "hello world", system: ["system", "system.files"], temperature: 0, + files: "https://raw.githubusercontent.com/microsoft/genaiscript/main/packages/sample/src/rag/markdown.md", tests: { files: "https://raw.githubusercontent.com/microsoft/genaiscript/main/packages/sample/src/rag/markdown.md", keywords: "markdown", diff --git a/packages/sample/genaisrc/summarize-ollama-gemma2.genai.js b/packages/sample/genaisrc/summarize-ollama-gemma2.genai.js deleted file mode 100644 index 61a0c8443c..0000000000 --- a/packages/sample/genaisrc/summarize-ollama-gemma2.genai.js +++ /dev/null @@ -1,22 +0,0 @@ -script({ - title: "summarize with ollama gemma 2 2b", - system: [], - files: "src/rag/markdown.md", - tests: { - files: "src/rag/markdown.md", - keywords: "markdown", - }, -}) - -const res = await runPrompt( - (ctx) => { - const file = ctx.def("FILE", env.files) - ctx.$`Summarize ${file} in a sentence. Make it short.` - }, - { - model: "ollama:gemma2:2b", - label: "ollama gemmaa2", - throwOnError: true, - } -) -console.log(res.text) diff --git a/packages/sample/genaisrc/summary-of-summary-gpt35.genai.js b/packages/sample/genaisrc/summary-of-summary-gpt35.genai.js deleted file mode 100644 index 2229840392..0000000000 --- a/packages/sample/genaisrc/summary-of-summary-gpt35.genai.js +++ /dev/null @@ -1,24 +0,0 @@ -script({ - title: "summary of summary - gp35", - model: "small", - files: ["src/rag/*"], - tests: { - files: ["src/rag/*"], - keywords: ["markdown", "lorem", "microsoft"], - }, -}) - -// map each file to its summary -for (const file of env.files) { - const { text } = await runPrompt( - (_) => { - _.def("FILE", file) - _.$`Summarize FILE. Be concise.` - }, - { model: "gpt-3.5-turbo", cache: "summary_gpt35" } - ) - // save the summary in the main prompt - def("FILE", { filename: file.filename, content: text }) -} -// reduce all summaries to a single summary -$`Summarize all the FILE.` diff --git a/packages/sample/genaisrc/summary-of-summary-phi3.genai.js b/packages/sample/genaisrc/summary-of-summary.genai.js similarity index 73% rename from packages/sample/genaisrc/summary-of-summary-phi3.genai.js rename to packages/sample/genaisrc/summary-of-summary.genai.js index 42f466e74f..0d4a448755 100644 --- a/packages/sample/genaisrc/summary-of-summary-phi3.genai.js +++ b/packages/sample/genaisrc/summary-of-summary.genai.js @@ -1,13 +1,13 @@ script({ model: "small", - title: "summary of summary - phi3", - files: ["src/rag/*.md"], + files: "src/rag/*", tests: { - files: ["src/rag/*.md"], + files: ["src/rag/*"], keywords: ["markdown", "lorem", "microsoft"], }, }) +if (!env.files.length) throw new Error("No files found") // summarize each files individually for (const file of env.files) { const { text } = await runPrompt( @@ -15,7 +15,7 @@ for (const file of env.files) { _.def("FILE", file) _.$`Extract keywords for the contents of FILE.` }, - { model: "ollama:phi3", cache: "summary_phi3" } + { model: "small", cache: "summary_summary" } ) def("FILE", { ...file, content: text }) } diff --git a/packages/sample/genaisrc/todo.genai.js b/packages/sample/genaisrc/todo.genai.js index 4eb354d632..3b9e960a8e 100644 --- a/packages/sample/genaisrc/todo.genai.js +++ b/packages/sample/genaisrc/todo.genai.js @@ -3,16 +3,18 @@ script({ description: "Try to implement TODOs found in source code.", group: "samples", system: ["system", "system.files"], - temperature: 0, - model: "small", + model: "large", + files: "src/fib.ts", tests: { files: "src/fib.ts", - asserts: [{ - type: "javascript", - transform: "output", - value: "Object.keys(output.fileEdits).length > 0" - }] - } + asserts: [ + { + type: "javascript", + transform: "output", + value: "Object.keys(output.fileEdits).length > 0", + }, + ], + }, }) def("CODE", env.files, { lineNumbers: true })