Skip to content

Commit

Permalink
fix: query parameters are not passed correctly to WolframAlpha API (#…
Browse files Browse the repository at this point in the history
…3502)

* fix: query parameters are not passed correctly to WolframAlpha API

* Lint + format

* Fix test

---------

Co-authored-by: jacoblee93 <[email protected]>
  • Loading branch information
amellouki and jacoblee93 authored Dec 4, 2023
1 parent f289f3d commit 71a70b1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
47 changes: 47 additions & 0 deletions langchain/src/tools/tests/wolframalpha.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { jest, 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", () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let fetchMock: any;

beforeEach(() => {
fetchMock = jest.spyOn(global, "fetch").mockImplementation(
async () =>
({
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);
});
});
4 changes: 3 additions & 1 deletion langchain/src/tools/wolframalpha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ export class WolframAlphaTool extends Tool {
}

async _call(query: string): Promise<string> {
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();
Expand Down

1 comment on commit 71a70b1

@vercel
Copy link

@vercel vercel bot commented on 71a70b1 Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

langchainjs-docs – ./docs/core_docs/

langchainjs-docs-langchain.vercel.app
langchainjs-docs-ruddy.vercel.app
langchainjs-docs-git-main-langchain.vercel.app
js.langchain.com

Please sign in to comment.