Skip to content

Commit

Permalink
Fix bedrock messages (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidSouther authored Apr 15, 2024
1 parent 5f4d1ee commit b5977ec
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 14 deletions.
23 changes: 14 additions & 9 deletions core/src/content/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,10 @@ export async function loadContent(
).filter(isDefined);

const isIsolated = Boolean(meta.isolated);
const context = meta.context ?? "content";
const context: NonNullable<ContentMeta["context"]> =
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--) {
Expand All @@ -287,12 +288,14 @@ export async function loadContent(
}

const folders: Record<string, Content> = {};
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<string, Content> = {
Expand All @@ -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;
}

Expand Down
46 changes: 44 additions & 2 deletions core/src/engine/bedrock/bedrock.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down Expand Up @@ -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" },
]);
});
});
});
3 changes: 1 addition & 2 deletions core/src/engine/bedrock/bedrock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,9 @@ async function addContentMeta(
context: Record<string, Content>
) {
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(
Expand Down
5 changes: 5 additions & 0 deletions integ/integ-bedrock.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

cd $(dirname $0)
AILLY_ENGINE=bedrock ./integ.sh
exit $?
5 changes: 5 additions & 0 deletions integ/integ-noop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

cd $(dirname $0)
AILLY_ENGINE=noop ./integ.sh
exit $?
4 changes: 3 additions & 1 deletion integ/integ.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit b5977ec

Please sign in to comment.