Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: query parameters are not passed correctly to WolframAlpha API #3502

Merged
merged 3 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions langchain/src/tools/tests/wolframalpha.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {afterEach, beforeEach, describe, expect} from "@jest/globals";
Copy link

Choose a reason for hiding this comment

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

Hey team, just wanted to flag that this PR adds a new external HTTP request using the fetch API for testing the Wolfram Alpha tool. This comment is to ensure the maintainers review and approve the addition. Great work on the testing implementation!

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);
});
});
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 {
}
Copy link

Choose a reason for hiding this comment

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

Hey team, just a heads up that I've flagged a new external HTTP request using the fetch function in the wolframalpha.ts file. The change includes properly encoding the query parameter before making the request, so it's important for maintainers to review. Keep up the great work!


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