Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #40 from hdresearch/memory/tweaks
Browse files Browse the repository at this point in the history
Memory/tweaks
  • Loading branch information
AlephNotation authored Apr 26, 2024
2 parents dd496fa + f40aef6 commit aa8d4ce
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 15 deletions.
7 changes: 7 additions & 0 deletions examples/shopping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ async function main() {
}
const logger = new Logger(["info"], (msg) => console.log(msg));

// You can pass in collective memory configuration to the agent browser
// const collectiveMemoryConfig = {
// apiKey: process.env.HDR_API_KEY!,
// endpoint: process.env.HDR_ENDPOINT!,
// };

const agentBrowser = new AgentBrowser({
agent: new Agent({ modelApi: chatApi }),
browser: await Browser.create(argv.headless),
Expand All @@ -44,6 +50,7 @@ async function main() {
{ value: "emma.lopez@gmail.com", name: "email", type: "string" },
{ value: "Password.123", name: "Password", type: "string" },
]),
// collectiveMemoryConfig,
});

const orderTotalAnswer = ObjectiveComplete.extend({
Expand Down
27 changes: 24 additions & 3 deletions src/agentBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {
ObjectiveState,
} from "./types/browser/browser.types";
import { Agent } from "./agent/agent";
import { fetchMemorySequence, remember } from "./collectiveMemory/remember";
import {
fetchMemorySequence,
findRoute,
remember,
} from "./collectiveMemory/remember";
import {
ModelResponseSchema,
ModelResponseType,
Expand Down Expand Up @@ -54,7 +58,9 @@ export class AgentBrowser {
{} as any // for zod optionals
);
this.inventory = agentBrowserArgs.inventory;
this.hdrConfig = CollectiveMemoryConfig.parse({});
this.hdrConfig =
agentBrowserArgs.collectiveMemoryConfig ??
CollectiveMemoryConfig.parse({});

this.objectiveProgress = [];
}
Expand Down Expand Up @@ -175,7 +181,8 @@ export class AgentBrowser {
currentObjective,
this.objectiveProgress
);
const memories = await remember(state);
const memories = await this.remember(state);
// console.log("Memories", memories);
let config = {};
if (this.inventory) {
config = { inventory: this.inventory };
Expand Down Expand Up @@ -208,6 +215,20 @@ export class AgentBrowser {
BrowserObjective.parse(browserObjective);

this.setMemorySequenceId();

const sequenceId = await findRoute(
{ url: startUrl, objective: objective[0] },
this.hdrConfig
);

if (sequenceId) {
return await this.followPath(
sequenceId,
browserObjective,
ObjectiveCompleteResponse<TObjectiveComplete>()
);
}

// goto the start url
await this.browser.goTo(startUrl, this.config.goToDelay);

Expand Down
5 changes: 2 additions & 3 deletions src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,13 @@ export class Browser {

async state(
objective: string,
objectiveProgress: string[],
limit = 4000
objectiveProgress: string[]
): Promise<ObjectiveState> {
let contentJSON = await this.parseContent();
let content: ObjectiveState = {
kind: "ObjectiveState",
url: this.url().replace(/[?].*/g, ""),
ariaTree: contentJSON.substring(0, limit),
ariaTree: contentJSON,
progress: objectiveProgress,
objective: objective,
};
Expand Down
61 changes: 56 additions & 5 deletions src/collectiveMemory/remember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@ export async function fetchStateActionPairs(
state: ObjectiveState,
hdrConfig: HDRConfig,
limit: number = 2
) {
): Promise<Memory[]> {
const { apiKey, endpoint } = hdrConfig;

const body = JSON.stringify({
state: ObjectiveState.parse(state),
limit,
});

const response = await fetch(`${endpoint}/memories/remember`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify({
state: ObjectiveState.parse(state),
limit,
}),
body,
});

if (!response.ok) {
Expand All @@ -36,6 +38,10 @@ export async function fetchStateActionPairs(
}
const data = await response.json();

if (data.length === 0) {
return DEFAULT_STATE_ACTION_PAIRS;
}

return data.map((m: any) =>
Memory.parse({
actionStep: m.action,
Expand Down Expand Up @@ -89,3 +95,48 @@ export async function fetchMemorySequence(
})
) as Memory[];
}

export async function fetchRoute(
routeParams: { url: string; objective: string },
hdrConfig: HDRConfig
) {
const { apiKey, endpoint } = hdrConfig;

const body = JSON.stringify({
state: ObjectiveState.parse(routeParams),
});

const response = await fetch(`${endpoint}/memories/findpath`, {
headers: {
Authorization: `Bearer ${apiKey}`,
},
body,
});

if (!response.ok) {
throw new Error(
`HDR API request failed with status ${response.status} to url ${response.url}`
);
}

const data = await response.json();

return { sequenceId: data.sequenceId };
}

export async function findRoute(
routeParams: { url: string; objective: string },
hdrConfig: HDRConfig
): Promise<string | undefined> {
const apiKey = hdrConfig?.apiKey || process.env.HDR_API_KEY;
if (!apiKey) {
return undefined;
}

try {
const config = hdrConfig || { apiKey, endpoint: "https://api.hdr.is" };
return (await fetchRoute(routeParams, config)).sequenceId;
} catch (error) {
return undefined;
}
}
14 changes: 10 additions & 4 deletions tests/collectiveMemory/remember.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import { describe, expect, it } from "@jest/globals";
import { remember, fetchMemorySequence } from "../../src/collectiveMemory";
import { objectiveStateExample1 } from "../../src/collectiveMemory/examples";
import {
objectiveStateExample1,
objectiveStateExample2,
} from "../../src/collectiveMemory/examples";

describe("Remember", () => {
it("should remember", async () => {
const memories = await remember(objectiveStateExample1, {
apiKey: process.env.HDR_API_KEY!,
console.log("Endpoint", process.env.HDR_ENDPOINT);
const memories = await remember(objectiveStateExample2, {
apiKey: "hdr-24aceaa62a28cabf96afbd720816484",
endpoint: process.env.HDR_ENDPOINT!,
});

console.log(JSON.stringify(memories, null, 2));

const memory = memories[0].actionStep.command![0];
expect(memory.kind).toBe("Type");
});
}, 10000);
});

describe("Fetch state actions sequences", () => {
Expand Down

0 comments on commit aa8d4ce

Please sign in to comment.