From cc015dcff0297efaa2bf007c3461ca9887dbfc28 Mon Sep 17 00:00:00 2001 From: AMellouki Date: Sat, 2 Dec 2023 21:42:38 +0100 Subject: [PATCH 1/3] fix: query parameters are not passed correctly to WolframAlpha API --- .../src/tools/tests/wolframalpha.test.ts | 46 +++++++++++++++++++ langchain/src/tools/wolframalpha.ts | 4 +- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 langchain/src/tools/tests/wolframalpha.test.ts diff --git a/langchain/src/tools/tests/wolframalpha.test.ts b/langchain/src/tools/tests/wolframalpha.test.ts new file mode 100644 index 000000000000..a4be244e1698 --- /dev/null +++ b/langchain/src/tools/tests/wolframalpha.test.ts @@ -0,0 +1,46 @@ +import {afterEach, beforeEach, describe, expect} from "@jest/globals"; +import {WolframAlphaTool} from "../wolframalpha.js"; + +const MOCK_APP_ID = '[MOCK_APP_ID]'; +const QUERY_1 = 'What is 2 + 2?'; +const MOCK_ANSWER = '[MOCK_ANSWER]'; + +describe("wolfram alpha test suite", () => { + + let fetchMock: jest.SpyInstance; + + beforeEach(() => { + fetchMock = jest.spyOn(global, "fetch").mockImplementation(() => { + return Promise.resolve({ + text: () => Promise.resolve(MOCK_ANSWER), + } as Response); + }); + }); + + afterEach(() => { + fetchMock.mockRestore(); + }); + + test("test query parameters passed correctly", async () => { + const wolframAlpha = new WolframAlphaTool({ + appid: MOCK_APP_ID, + }); + await wolframAlpha._call(QUERY_1); + const [url] = fetchMock.mock.calls[0]; + const parsedUrl = new URL(url); + const params = new URLSearchParams(parsedUrl.search); + + expect(fetchMock).toBeCalledTimes(1); + expect(params.get("appid")).toBe(MOCK_APP_ID); + expect(params.get("input")).toBe(QUERY_1); + }); + + test("test answer retrieved", async () => { + const wolframAlpha = new WolframAlphaTool({ + appid: MOCK_APP_ID, + }); + + const answer = await wolframAlpha._call(QUERY_1); + expect(answer).toBe(MOCK_ANSWER); + }); +}); diff --git a/langchain/src/tools/wolframalpha.ts b/langchain/src/tools/wolframalpha.ts index b486d95e1db2..1899ed74d9af 100644 --- a/langchain/src/tools/wolframalpha.ts +++ b/langchain/src/tools/wolframalpha.ts @@ -31,7 +31,9 @@ export class WolframAlphaTool extends Tool { } async _call(query: string): Promise { - const url = `https://www.wolframalpha.com/api/v1/llm-api?appid=${this.appid}&input=${query}`; + const url = `https://www.wolframalpha.com/api/v1/llm-api?appid=${this.appid}&input=${ + encodeURIComponent(query) + }`; const res = await fetch(url); return res.text(); From 3b35ab0f4debc6786106b0481faefebf24f9ab12 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Sun, 3 Dec 2023 16:40:21 -0800 Subject: [PATCH 2/3] Lint + format --- .../src/tools/tests/wolframalpha.test.ts | 22 +++++++++---------- langchain/src/tools/wolframalpha.ts | 6 ++--- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/langchain/src/tools/tests/wolframalpha.test.ts b/langchain/src/tools/tests/wolframalpha.test.ts index a4be244e1698..47175d8c6eaa 100644 --- a/langchain/src/tools/tests/wolframalpha.test.ts +++ b/langchain/src/tools/tests/wolframalpha.test.ts @@ -1,20 +1,20 @@ -import {afterEach, beforeEach, describe, expect} from "@jest/globals"; -import {WolframAlphaTool} from "../wolframalpha.js"; +import { afterEach, beforeEach, describe, expect } from "@jest/globals"; +import { WolframAlphaTool } from "../wolframalpha.js"; -const MOCK_APP_ID = '[MOCK_APP_ID]'; -const QUERY_1 = 'What is 2 + 2?'; -const MOCK_ANSWER = '[MOCK_ANSWER]'; +const MOCK_APP_ID = "[MOCK_APP_ID]"; +const QUERY_1 = "What is 2 + 2?"; +const MOCK_ANSWER = "[MOCK_ANSWER]"; describe("wolfram alpha test suite", () => { - let fetchMock: jest.SpyInstance; beforeEach(() => { - fetchMock = jest.spyOn(global, "fetch").mockImplementation(() => { - return Promise.resolve({ - text: () => Promise.resolve(MOCK_ANSWER), - } as Response); - }); + fetchMock = jest.spyOn(global, "fetch").mockImplementation( + async () => + ({ + text: () => Promise.resolve(MOCK_ANSWER), + } as Response) + ); }); afterEach(() => { diff --git a/langchain/src/tools/wolframalpha.ts b/langchain/src/tools/wolframalpha.ts index 1899ed74d9af..76658428a734 100644 --- a/langchain/src/tools/wolframalpha.ts +++ b/langchain/src/tools/wolframalpha.ts @@ -31,9 +31,9 @@ export class WolframAlphaTool extends Tool { } async _call(query: string): Promise { - const url = `https://www.wolframalpha.com/api/v1/llm-api?appid=${this.appid}&input=${ - encodeURIComponent(query) - }`; + const url = `https://www.wolframalpha.com/api/v1/llm-api?appid=${ + this.appid + }&input=${encodeURIComponent(query)}`; const res = await fetch(url); return res.text(); From 617cbd1a7e08dfd8b71986f39ec09622c59ae39f Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Mon, 4 Dec 2023 11:45:07 -0800 Subject: [PATCH 3/3] Fix test --- langchain/src/tools/tests/wolframalpha.test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/langchain/src/tools/tests/wolframalpha.test.ts b/langchain/src/tools/tests/wolframalpha.test.ts index 47175d8c6eaa..2c2b7f7fd297 100644 --- a/langchain/src/tools/tests/wolframalpha.test.ts +++ b/langchain/src/tools/tests/wolframalpha.test.ts @@ -1,4 +1,4 @@ -import { afterEach, beforeEach, describe, expect } from "@jest/globals"; +import { jest, afterEach, beforeEach, describe, expect } from "@jest/globals"; import { WolframAlphaTool } from "../wolframalpha.js"; const MOCK_APP_ID = "[MOCK_APP_ID]"; @@ -6,7 +6,8 @@ const QUERY_1 = "What is 2 + 2?"; const MOCK_ANSWER = "[MOCK_ANSWER]"; describe("wolfram alpha test suite", () => { - let fetchMock: jest.SpyInstance; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + let fetchMock: any; beforeEach(() => { fetchMock = jest.spyOn(global, "fetch").mockImplementation(