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

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
AlephNotation committed Aug 19, 2024
1 parent 1049956 commit 6769092
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 40 deletions.
14 changes: 3 additions & 11 deletions examples/login.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import yargs from "yargs/yargs";

import { Browser } from "../src/browser";
import { Agent } from "../src/agent/agent";
import { Inventory } from "../src/inventory";
import { completionApiBuilder } from "../src/agent";
import { makeAgent } from "../src/agent";
import { Logger } from "../src/utils";
import { nolitarc } from "../src/utils/config";

Expand All @@ -29,16 +28,10 @@ async function main() {

// We can create a chat api using the completionApiBuilder.
// These can be swapped out for other providers like OpenAI
const chatApi = completionApiBuilder(providerOptions, {
const agent = makeAgent(providerOptions, {
model: agentModel || "claude-3-5-sonnet-20240620",
});

if (!chatApi) {
throw new Error(
`Failed to create chat api for ${providerOptions.provider}`
);
}

// You can define a custom callback function to handle logs
// this callback will print logs to the console
// but you can further extend this to send logs to a logging service
Expand All @@ -54,15 +47,14 @@ async function main() {
{ value: "Password123", name: "Password", type: "string" },
]);

const agent = new Agent({ modelApi: chatApi });
const browser = await Browser.launch(argv.headless, agent, logger, {
inventory,
});

const page = await browser.newPage();
await page.goto(startUrl);
const answer = await page.browse(objective, {
maxTurns: maxIterations
maxTurns: maxIterations,
});
// @ts-expect-error - we are not using the full response schema
console.log("Answer:", answer?.objectiveComplete?.result);
Expand Down
25 changes: 12 additions & 13 deletions examples/shopping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import yargs from "yargs/yargs";
import { z } from "zod";

import { Browser } from "../src/browser";
import { Agent } from "../src/agent/agent";
import { Inventory } from "../src/inventory";
import { completionApiBuilder } from "../src/agent";
import { makeAgent } from "../src/agent";
import { Logger } from "../src/utils";
import { nolitarc } from "../src/utils/config";

Expand All @@ -30,16 +29,11 @@ async function main() {
apiKey: agentApiKey || process.env.OPENAI_API_KEY!,
provider: agentProvider || "openai",
};
const chatApi = completionApiBuilder(providerOptions, { model: agentModel || "gpt-4" });

if (!chatApi) {
throw new Error(
`Failed to create chat api for ${providerOptions.provider}`
);
}
const agent = makeAgent(providerOptions, {
model: agentModel || "gpt-4",
});
const logger = new Logger(["info"], (msg) => console.log(msg));

const agent = new Agent({ modelApi: chatApi });
const browser = await Browser.launch(argv.headless, agent, logger, {
apiKey: hdrApiKey || process.env.HDR_API_KEY!,
});
Expand All @@ -48,15 +42,20 @@ async function main() {
const answer = await page.browse(objective, {
maxTurns: maxIterations,
schema: z.object({
orderTotals: z.array(z.number()).describe("The order total in number format"),
orderTotals: z
.array(z.number())
.describe("The order total in number format"),
}),
inventory: new Inventory([
{ value: "emma.lopez@gmail.com", name: "email", type: "string" },
{ value: "Password.123", name: "Password", type: "string" },
]),
});
// @ts-expect-error - we are not using the full response schema
console.log("\x1b[32m Answer:", JSON.stringify(answer?.objectiveComplete?.orderTotals));
console.log(
"\x1b[32m Answer:",
// @ts-expect-error - we are not using the full response schema
JSON.stringify(answer?.objectiveComplete?.orderTotals)
);
await browser.close();
}

Expand Down
32 changes: 16 additions & 16 deletions examples/wikipedia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import yargs from "yargs/yargs";
import { z } from "zod";

import { Browser } from "../src/browser";
import { Agent } from "../src/agent/agent";
import { completionApiBuilder } from "../src/agent";
import { makeAgent } from "../src/agent";
import { Logger } from "../src/utils";
import { nolitarc } from "../src/utils/config";

Expand All @@ -26,25 +25,26 @@ async function main() {
apiKey: agentApiKey || process.env.OPENAI_API_KEY!,
provider: agentProvider || "openai",
};
const chatApi = completionApiBuilder(providerOptions, { model: agentModel || "gpt-4" });

if (!chatApi) {
throw new Error(
`Failed to create chat api for ${providerOptions.provider}`
);
}
const agent = makeAgent(providerOptions, {
model: agentModel || "gpt-4",
});
const logger = new Logger(["info"], (msg) => console.log(msg));
const agent = new Agent({ modelApi: chatApi });

const browser = await Browser.launch(argv.headless, agent, logger);
const page = await browser.newPage();
await page.goto(argv.startUrl || "https://google.com");
const answer = await page.browse(argv.objective || "How many accounts are on Wikipedia?", {
maxTurns: argv.maxIterations || 10,
schema: z.object({
numberOfEditors: z.number().int().describe("The number of accounts in int format"),
}),
});
const answer = await page.browse(
argv.objective || "How many accounts are on Wikipedia?",
{
maxTurns: argv.maxIterations || 10,
schema: z.object({
numberOfEditors: z
.number()
.int()
.describe("The number of accounts in int format"),
}),
}
);

// @ts-expect-error - we are not using the full response schema
console.log("Answer:", answer?.objectiveComplete?.numberOfEditors);
Expand Down

0 comments on commit 6769092

Please sign in to comment.