Skip to content

Commit

Permalink
Speed up webbrowser tool int test
Browse files Browse the repository at this point in the history
  • Loading branch information
nfcampos committed Apr 28, 2023
1 parent 844d821 commit 93dc8c3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 72 deletions.
64 changes: 1 addition & 63 deletions langchain/src/tools/tests/webbrowser.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("webbrowser Test suite", () => {
expect(result).toContain("Word of the Day:");
});

test("get word of the day with fetch adapter", async () => {
test("get a summary of the page when empty request with fetch adapter", async () => {
const model = new ChatOpenAI({ temperature: 0 });
const embeddings = new OpenAIEmbeddings();

Expand All @@ -28,18 +28,6 @@ describe("webbrowser Test suite", () => {
adapter: fetchAdapter,
},
});
const result = await browser.call(
`"https://www.merriam-webster.com/word-of-the-day","word of the day"`
);

expect(result).toContain("Word of the Day:");
});

test("get a summary of the page when empty request", async () => {
const model = new ChatOpenAI({ temperature: 0 });
const embeddings = new OpenAIEmbeddings();

const browser = new WebBrowser({ model, embeddings });
const result = await browser.call(
`"https://www.merriam-webster.com/word-of-the-day",""`
);
Expand All @@ -48,45 +36,6 @@ describe("webbrowser Test suite", () => {
expect(result).toMatch(/word of the day/i);
});

test("get a summary of the page if it drops second request quote", async () => {
const model = new ChatOpenAI({ temperature: 0 });
const embeddings = new OpenAIEmbeddings();

const browser = new WebBrowser({ model, embeddings });
const result = await browser.call(
`"https://www.merriam-webster.com/word-of-the-day","`
);

// fuzzy, sometimes its capped and others not
expect(result).toMatch(/word of the day/i);
});

test("get a summary of the page if it gives nothing after comma", async () => {
const model = new ChatOpenAI({ temperature: 0 });
const embeddings = new OpenAIEmbeddings();

const browser = new WebBrowser({ model, embeddings });
const result = await browser.call(
`"https://www.merriam-webster.com/word-of-the-day",`
);

// fuzzy, sometimes its capped and others not
expect(result).toMatch(/word of the day/i);
});

test("get a summary of the page if it gives no comma", async () => {
const model = new ChatOpenAI({ temperature: 0 });
const embeddings = new OpenAIEmbeddings();

const browser = new WebBrowser({ model, embeddings });
const result = await browser.call(
`"https://www.merriam-webster.com/word-of-the-day"`
);

// fuzzy, sometimes its capped and others not
expect(result).toMatch(/word of the day/i);
});

test("error no url", async () => {
const model = new ChatOpenAI({ temperature: 0 });
const embeddings = new OpenAIEmbeddings();
Expand Down Expand Up @@ -159,17 +108,6 @@ describe("webbrowser Test suite", () => {
expect(result).toContain("Esther Perel");
});

test("get a summary of a page that redirects", async () => {
const model = new ChatOpenAI({ temperature: 0 });
const embeddings = new OpenAIEmbeddings();

const browser = new WebBrowser({ model, embeddings });
const result = await browser.call(
`"https://www.themarginalian.org/2015/04/09/find-your-bliss-joseph-campbell-power-of-myth",""`
);
expect(result).toContain("The Marginalian");
});

// other urls that have done this too
// "https://wsimag.com/economy-and-politics/15473-power-and-money",
// "https://thriveglobal.com/stories/sleep-what-to-do-what-not-to-do",
Expand Down
25 changes: 24 additions & 1 deletion langchain/src/tools/tests/webbrowser.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test, expect, describe } from "@jest/globals";
import { readFileSync } from "fs";
import { getText } from "../webbrowser.js";
import { getText, parseInputs } from "../webbrowser.js";

describe("webbrowser Test suite", () => {
const html = readFileSync("./src/tools/fixtures/wordoftheday.html", "utf8");
Expand All @@ -10,4 +10,27 @@ describe("webbrowser Test suite", () => {
const text = getText(html, baseUrl, false);
expect(text).toContain("Word of the Day: Foible");
});

test("parseInputs", () => {
expect(
parseInputs(`"https://www.merriam-webster.com/word-of-the-day",""`)
).toEqual(["https://www.merriam-webster.com/word-of-the-day", ""]);
expect(
parseInputs(
`"https://www.merriam-webster.com/word-of-the-day","word of the day"`
)
).toEqual([
"https://www.merriam-webster.com/word-of-the-day",
"word of the day",
]);
expect(
parseInputs(`"https://www.merriam-webster.com/word-of-the-day","`)
).toEqual(["https://www.merriam-webster.com/word-of-the-day", ""]);
expect(
parseInputs(`"https://www.merriam-webster.com/word-of-the-day",`)
).toEqual(["https://www.merriam-webster.com/word-of-the-day", ""]);
expect(
parseInputs(`"https://www.merriam-webster.com/word-of-the-day"`)
).toEqual(["https://www.merriam-webster.com/word-of-the-day", undefined]);
});
});
22 changes: 14 additions & 8 deletions langchain/src/tools/webbrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ import {
import { Embeddings } from "../embeddings/base.js";
import fetchAdapter from "../util/axios-fetch-adapter.js";

export const parseInputs = (inputs: string): [string, string] => {
const [baseUrl, task] = inputs.split(",").map((input) => {
let t = input.trim();
t = t.startsWith('"') ? t.slice(1) : t;
t = t.endsWith('"') ? t.slice(0, -1) : t;
// it likes to put / at the end of urls, wont matter for task
t = t.endsWith("/") ? t.slice(0, -1) : t;
return t.trim();
});

return [baseUrl, task];
};

export const getText = (
html: string,
baseUrl: string,
Expand Down Expand Up @@ -174,14 +187,7 @@ export class WebBrowser extends Tool {

/** @ignore */
async _call(inputs: string, runManager?: CallbackManagerForToolRun) {
const [baseUrl, task] = inputs.split(",").map((input) => {
let t = input.trim();
t = t.startsWith('"') ? t.slice(1) : t;
t = t.endsWith('"') ? t.slice(0, -1) : t;
// it likes to put / at the end of urls, wont matter for task
t = t.endsWith("/") ? t.slice(0, -1) : t;
return t.trim();
});
const [baseUrl, task] = parseInputs(inputs);
const doSummary = !task;

let text;
Expand Down

0 comments on commit 93dc8c3

Please sign in to comment.