feat: rewrite WolframAlphaTool

This commit is contained in:
Hk-Gosuto 2023-12-31 14:32:47 +08:00
parent 98b5ace66e
commit cb096e5772
7 changed files with 67 additions and 41 deletions

View File

@ -10,7 +10,7 @@ import { GoogleSearch } from "@/app/api/langchain-tools/google_search";
import { Tool, DynamicTool } from "langchain/tools"; import { Tool, DynamicTool } from "langchain/tools";
import * as langchainTools from "langchain/tools"; import * as langchainTools from "langchain/tools";
import { Embeddings } from "langchain/dist/embeddings/base.js"; import { Embeddings } from "langchain/dist/embeddings/base.js";
import { promises } from "dns"; import { WolframAlphaTool } from "./wolframalpha";
export class EdgeTool { export class EdgeTool {
private apiKey: string | undefined; private apiKey: string | undefined;
@ -50,13 +50,16 @@ export class EdgeTool {
); );
const stableDiffusionTool = new StableDiffusionWrapper(); const stableDiffusionTool = new StableDiffusionWrapper();
const arxivAPITool = new ArxivAPIWrapper(); const arxivAPITool = new ArxivAPIWrapper();
return [ const wolframAlphaTool = new WolframAlphaTool();
let tools = [
// searchTool, // searchTool,
calculatorTool, calculatorTool,
webBrowserTool, webBrowserTool,
dallEAPITool, dallEAPITool,
stableDiffusionTool, stableDiffusionTool,
arxivAPITool, arxivAPITool,
wolframAlphaTool,
]; ];
return tools;
} }
} }

View File

@ -0,0 +1,33 @@
import { Tool } from "langchain/tools";
export class WolframAlphaTool extends Tool {
name = "wolfram_alpha_llm";
description = `A wrapper around Wolfram Alpha. Useful for when you need to answer questions about Math, Science, Technology, Culture, Society and Everyday Life. Input should be a search query. If the result contains an image link, use the markdown syntax to return the image.`;
constructor() {
super();
}
async _call(query: string) {
const appid = process.env.WOLFRAM_ALPHA_APP_ID;
if (!appid) {
return "`WOLFRAM_ALPHA_APP_ID` Not configured";
}
const url = `https://www.wolframalpha.com/api/v1/llm-api?appid=${appid}&input=${encodeURIComponent(
query,
)}`;
console.log("[WolframAlphaTool]", url);
try {
const res = await fetch(url, {
method: "GET",
});
const resText = await res.text();
console.log(resText);
return resText;
} catch (ex) {
console.error("[WolframAlphaTool]", ex);
return "query error";
}
}
}

View File

@ -16,7 +16,7 @@ import { ACCESS_CODE_PREFIX, ServiceProvider } from "@/app/constant";
import * as langchainTools from "langchain/tools"; import * as langchainTools from "langchain/tools";
import { HttpGetTool } from "@/app/api/langchain-tools/http_get"; import { HttpGetTool } from "@/app/api/langchain-tools/http_get";
import { DuckDuckGo } from "@/app/api/langchain-tools/duckduckgo_search"; import { DuckDuckGo } from "@/app/api/langchain-tools/duckduckgo_search";
import { DynamicTool, Tool, WolframAlphaTool } from "langchain/tools"; import { DynamicTool, Tool } from "langchain/tools";
import { BaiduSearch } from "@/app/api/langchain-tools/baidu_search"; import { BaiduSearch } from "@/app/api/langchain-tools/baidu_search";
import { GoogleSearch } from "@/app/api/langchain-tools/google_search"; import { GoogleSearch } from "@/app/api/langchain-tools/google_search";
import { useAccessStore } from "@/app/store"; import { useAccessStore } from "@/app/store";
@ -130,7 +130,7 @@ export class AgentApi {
}, },
async handleAgentAction(action) { async handleAgentAction(action) {
try { try {
// console.log("[handleAgentAction]", action.tool); // console.log("[handleAgentAction]", { action });
if (!reqBody.returnIntermediateSteps) return; if (!reqBody.returnIntermediateSteps) return;
var response = new ResponseBody(); var response = new ResponseBody();
response.isToolMessage = true; response.isToolMessage = true;
@ -153,10 +153,10 @@ export class AgentApi {
} }
}, },
async handleToolStart(tool, input) { async handleToolStart(tool, input) {
// console.log("[handleToolStart]", { tool }); console.log("[handleToolStart]", { tool, input });
}, },
async handleToolEnd(output, runId, parentRunId, tags) { async handleToolEnd(output, runId, parentRunId, tags) {
// console.log("[handleToolEnd]", { output, runId, parentRunId, tags }); console.log("[handleToolEnd]", { output, runId, parentRunId, tags });
}, },
async handleAgentEnd(action, runId, parentRunId, tags) { async handleAgentEnd(action, runId, parentRunId, tags) {
console.log("[handleAgentEnd]"); console.log("[handleAgentEnd]");
@ -282,16 +282,6 @@ export class AgentApi {
var tool = langchainTools[ var tool = langchainTools[
toolName as keyof typeof langchainTools toolName as keyof typeof langchainTools
] as any; ] as any;
if (
toolName === "wolfram_alpha" &&
process.env.WOLFRAM_ALPHA_APP_ID
) {
const tool = new WolframAlphaTool({
appid: process.env.WOLFRAM_ALPHA_APP_ID,
});
tools.push(tool);
return;
}
if (tool) { if (tool) {
tools.push(new tool()); tools.push(new tool());
} }

View File

@ -86,7 +86,7 @@ export const CN_PLUGINS: BuiltinPlugin[] = [
}, },
{ {
name: "WolframAlphaTool", name: "WolframAlphaTool",
toolName: "wolfram_alpha", toolName: "wolfram_alpha_llm",
lang: "cn", lang: "cn",
description: description:
"在需要回答有关数学、科学、技术、文化、社会和日常生活的问题时非常有用。", "在需要回答有关数学、科学、技术、文化、社会和日常生活的问题时非常有用。",

View File

@ -89,7 +89,7 @@ export const EN_PLUGINS: BuiltinPlugin[] = [
}, },
{ {
name: "WolframAlphaTool", name: "WolframAlphaTool",
toolName: "wolfram_alpha", toolName: "wolfram_alpha_llm",
lang: "en", lang: "en",
description: description:
"Useful for when you need to answer questions about Math, Science, Technology, Culture, Society and Everyday Life.", "Useful for when you need to answer questions about Math, Science, Technology, Culture, Society and Everyday Life.",

View File

@ -33,7 +33,7 @@
"html-to-image": "^1.11.11", "html-to-image": "^1.11.11",
"html-to-text": "^9.0.5", "html-to-text": "^9.0.5",
"https-proxy-agent": "^7.0.2", "https-proxy-agent": "^7.0.2",
"langchain": "^0.0.212", "langchain": "^0.0.213",
"mermaid": "^10.6.1", "mermaid": "^10.6.1",
"nanoid": "^5.0.3", "nanoid": "^5.0.3",
"next": "^13.4.9", "next": "^13.4.9",

View File

@ -1813,22 +1813,22 @@
"@jridgewell/resolve-uri" "3.1.0" "@jridgewell/resolve-uri" "3.1.0"
"@jridgewell/sourcemap-codec" "1.4.14" "@jridgewell/sourcemap-codec" "1.4.14"
"@langchain/community@~0.0.8": "@langchain/community@~0.0.12":
version "0.0.11" version "0.0.12"
resolved "https://registry.yarnpkg.com/@langchain/community/-/community-0.0.11.tgz#5e1dce3e1ec636e015c503593fdf7383d4478159" resolved "https://registry.yarnpkg.com/@langchain/community/-/community-0.0.12.tgz#95b5ddfaef47db6a1f665959e417a706cf56057f"
integrity sha512-8L19AyzBueHrRtawxU4JstRtmzqsHK2jDJtSg/bv9ivto0kDKlILnauIDijcb09V/4mql2XolaS9r7eGe99yGA== integrity sha512-mcm6FxxnLxSx9PiYvehGGwvcHjsVR5WXfYOwymojf/6d0apyewjOLzKsR3xx0HJVtCs8pff7NZSdDoE+jj8OcA==
dependencies: dependencies:
"@langchain/core" "~0.1.3" "@langchain/core" "~0.1.5"
"@langchain/openai" "~0.0.7" "@langchain/openai" "~0.0.9"
flat "^5.0.2" flat "^5.0.2"
langsmith "~0.0.48" langsmith "~0.0.48"
uuid "^9.0.0" uuid "^9.0.0"
zod "^3.22.3" zod "^3.22.3"
"@langchain/core@~0.1.3": "@langchain/core@~0.1.5":
version "0.1.4" version "0.1.6"
resolved "https://registry.yarnpkg.com/@langchain/core/-/core-0.1.4.tgz#f923c0d844faf6de2069d86d785e4ef3b1381257" resolved "https://registry.yarnpkg.com/@langchain/core/-/core-0.1.6.tgz#4b1dfc361379d6bd45c6652418a05f051488e6d9"
integrity sha512-Y3/mQLEiQ78ZbsTGYvPRj5bpvrhpTAcsbdyEtlYEvjMLbehEADfjQ41G9zc7U/emkrGtHRmxWO1ISmoeXeDmyQ== integrity sha512-mBXsHHAWgDYJPzPNwOLWXJob3DAlO1tXBybu9cJ+zcVzixiSh/oxQ5eonTK6nlFwO6kGpaIcCD9l9+yq/WCMBg==
dependencies: dependencies:
ansi-styles "^5.0.0" ansi-styles "^5.0.0"
camelcase "6" camelcase "6"
@ -1841,12 +1841,12 @@
uuid "^9.0.0" uuid "^9.0.0"
zod "^3.22.3" zod "^3.22.3"
"@langchain/openai@~0.0.7": "@langchain/openai@~0.0.9":
version "0.0.8" version "0.0.9"
resolved "https://registry.yarnpkg.com/@langchain/openai/-/openai-0.0.8.tgz#77e4a216f9ba5a347ee24b7f28a526fd57c24c8d" resolved "https://registry.yarnpkg.com/@langchain/openai/-/openai-0.0.9.tgz#e84b7db0554de75fdd4a8fe12914fdc2b2d2d1f6"
integrity sha512-F8DD6vs5BvMAHiDprINWCVf7jyNaYHp2zAqHwYij3+YAKsgIORm2EQvI6YtT3qPnGghpE3qz8H3DKjTNPywIzQ== integrity sha512-Py7rJijOjNtb9pj5He+E9uAj9d8PCX+8Ix4LvY7cUTMCDlfkhngw2DhJ8wlk23u1IvunakdnnN74pfbO2JJBrw==
dependencies: dependencies:
"@langchain/core" "~0.1.3" "@langchain/core" "~0.1.5"
js-tiktoken "^1.0.7" js-tiktoken "^1.0.7"
openai "^4.19.0" openai "^4.19.0"
zod "^3.22.3" zod "^3.22.3"
@ -5492,15 +5492,15 @@ kleur@^4.0.3:
resolved "https://registry.yarnpkg.com/kleur/-/kleur-4.1.5.tgz#95106101795f7050c6c650f350c683febddb1780" resolved "https://registry.yarnpkg.com/kleur/-/kleur-4.1.5.tgz#95106101795f7050c6c650f350c683febddb1780"
integrity sha512-o+NO+8WrRiQEE4/7nwRJhN1HWpVmJm511pBHUxPLtp0BUISzlBplORYSmTclCnJvQq2tKu/sgl3xVpkc7ZWuQQ== integrity sha512-o+NO+8WrRiQEE4/7nwRJhN1HWpVmJm511pBHUxPLtp0BUISzlBplORYSmTclCnJvQq2tKu/sgl3xVpkc7ZWuQQ==
langchain@^0.0.212: langchain@^0.0.213:
version "0.0.212" version "0.0.213"
resolved "https://registry.yarnpkg.com/langchain/-/langchain-0.0.212.tgz#68ddeb6bb83bc7714a6b8d97ace464951d10ea6d" resolved "https://registry.yarnpkg.com/langchain/-/langchain-0.0.213.tgz#52fd367d5f1a100a1cdb775ffbb98eaa5793307b"
integrity sha512-zPcnAX3t1RuyWkbEkyqlIdR29j51Eylz7rcDcqET1baquR/r9xuk7P/HAJ6TFowiXDUg10d5YBD6VBvjwF/WDA== integrity sha512-nQDOJXvtIAIuUzamCiF1AWyi2GH9FSDPR+3XulJUEpdU60aSFPZ9GBiWdu+dVHXeAmm8C0iCVi0+3GWLJrUoXA==
dependencies: dependencies:
"@anthropic-ai/sdk" "^0.9.1" "@anthropic-ai/sdk" "^0.9.1"
"@langchain/community" "~0.0.8" "@langchain/community" "~0.0.12"
"@langchain/core" "~0.1.3" "@langchain/core" "~0.1.5"
"@langchain/openai" "~0.0.7" "@langchain/openai" "~0.0.9"
binary-extensions "^2.2.0" binary-extensions "^2.2.0"
expr-eval "^2.0.2" expr-eval "^2.0.2"
js-tiktoken "^1.0.7" js-tiktoken "^1.0.7"