From b5977ec46c0b885ba22726c42f809035c8e21baa Mon Sep 17 00:00:00 2001 From: David Souther Date: Mon, 15 Apr 2024 17:22:59 -0400 Subject: [PATCH] Fix bedrock messages (#49) --- core/src/content/content.ts | 23 ++++++++----- core/src/engine/bedrock/bedrock.test.ts | 46 +++++++++++++++++++++++-- core/src/engine/bedrock/bedrock.ts | 3 +- integ/integ-bedrock.sh | 5 +++ integ/integ-noop.sh | 5 +++ integ/integ.sh | 4 ++- 6 files changed, 72 insertions(+), 14 deletions(-) create mode 100755 integ/integ-bedrock.sh create mode 100755 integ/integ-noop.sh diff --git a/core/src/content/content.ts b/core/src/content/content.ts index 4bee699..6ea2d37 100644 --- a/core/src/content/content.ts +++ b/core/src/content/content.ts @@ -264,9 +264,10 @@ export async function loadContent( ).filter(isDefined); const isIsolated = Boolean(meta.isolated); - const context = meta.context ?? "content"; + const context: NonNullable = + meta.context ?? "conversation"; switch (context) { - case "content": + case "conversation": if (isIsolated) break; files.sort((a, b) => a.name.localeCompare(b.name)); for (let i = files.length - 1; i > 0; i--) { @@ -287,12 +288,14 @@ export async function loadContent( } const folders: Record = {}; - for (const folder of dir.folders) { - if (folder.name == ".vectors") continue; - fs.pushd(folder.name); - let contents = await loadContent(fs, system, meta); - Object.assign(folders, contents); - fs.popd(); + if (context != "none") { + for (const folder of dir.folders) { + if (folder.name == ".vectors") continue; + fs.pushd(folder.name); + let contents = await loadContent(fs, system, meta); + Object.assign(folders, contents); + fs.popd(); + } } const content: Record = { @@ -302,7 +305,9 @@ export async function loadContent( ), ...folders, }; - DEFAULT_LOGGER.debug(`Found ${content.length} at or below ${fs.cwd()}`); + DEFAULT_LOGGER.debug( + `Found ${Object.keys(content).length} at or below ${fs.cwd()}` + ); return content; } diff --git a/core/src/engine/bedrock/bedrock.test.ts b/core/src/engine/bedrock/bedrock.test.ts index 97fd424..7615e61 100644 --- a/core/src/engine/bedrock/bedrock.test.ts +++ b/core/src/engine/bedrock/bedrock.test.ts @@ -1,5 +1,13 @@ -import { describe, expect, it } from "vitest"; -import { claude3 } from "./prompt-builder"; +import { describe, expect, it, beforeEach } from "vitest"; +import { cleanState } from "@davidsouther/jiffies/lib/esm/scope/state.js"; +import { + FileSystem, + RecordFileSystemAdapter, +} from "@davidsouther/jiffies/lib/esm/fs.js"; +import { claude3 } from "./prompt-builder.js"; +import { makePipelineSettings } from "../../ailly"; +import { loadContent } from "../../content/content"; +import { format } from "./bedrock"; describe("bedrock claude3", () => { describe("prompt builder", () => { @@ -31,4 +39,38 @@ describe("bedrock claude3", () => { ]); }); }); + + describe("format", () => { + const state = cleanState(async () => { + const root = "/root"; + const settings = await makePipelineSettings({ root }); + const fs = new FileSystem( + new RecordFileSystemAdapter({ + "/root/a": "prompt a", + "/root/a.ailly.md": "response a", + "/root/b": "prompt b", + }) + ); + const context = await loadContent(fs, [], settings); + return { root, settings, context }; + }, beforeEach); + + it("formats contents into messages", async () => { + const contents = Object.values(state.context); + await format(contents, state.context); + + const system = { role: "system", content: "" }; + expect(contents[0].meta?.messages).toEqual([ + system, + { role: "user", content: "prompt a" }, + { role: "assistant", content: "response a" }, + ]); + expect(contents[1].meta?.messages).toEqual([ + system, + { role: "user", content: "prompt a" }, + { role: "assistant", content: "response a" }, + { role: "user", content: "prompt b" }, + ]); + }); + }); }); diff --git a/core/src/engine/bedrock/bedrock.ts b/core/src/engine/bedrock/bedrock.ts index 6752eb1..ac1fd61 100644 --- a/core/src/engine/bedrock/bedrock.ts +++ b/core/src/engine/bedrock/bedrock.ts @@ -119,10 +119,9 @@ async function addContentMeta( context: Record ) { content.meta ??= {}; - if (content.context.predecessor) - content.meta.messages = getMessagesPredecessor(content, context); if (content.context.folder) content.meta.messages = getMessagesFolder(content, context); + else content.meta.messages = getMessagesPredecessor(content, context); } export function getMessagesPredecessor( diff --git a/integ/integ-bedrock.sh b/integ/integ-bedrock.sh new file mode 100755 index 0000000..af69562 --- /dev/null +++ b/integ/integ-bedrock.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +cd $(dirname $0) +AILLY_ENGINE=bedrock ./integ.sh +exit $? diff --git a/integ/integ-noop.sh b/integ/integ-noop.sh new file mode 100755 index 0000000..a34a448 --- /dev/null +++ b/integ/integ-noop.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +cd $(dirname $0) +AILLY_ENGINE=noop ./integ.sh +exit $? diff --git a/integ/integ.sh b/integ/integ.sh index 9a6d554..cf96f4b 100755 --- a/integ/integ.sh +++ b/integ/integ.sh @@ -10,7 +10,7 @@ npm link ../core ../cli set -x -export AILLY_ENGINE=noop +export AILLY_ENGINE=${AILLY_ENGINE:-noop} echo "basic" npx ailly --root 01_basic @@ -20,7 +20,9 @@ rm 01_basic/basic.ailly.md echo "combined" npx ailly --root 02_combined --combined [ ! -f 02_combined/combined.ailly.md ] +git restore 02_combined/combined echo "edit" AILLY_NOOP_RESPONSE="Edited" npx ailly --root 04_edit --edit file --lines 2:4 --prompt "edit" --yes grep -q 'Edited' 04_edit/file +git restore 04_edit/file