mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-05-23 14:10:18 +09:00
feat: rewrite WolframAlphaTool
This commit is contained in:
parent
98b5ace66e
commit
cb096e5772
@ -10,7 +10,7 @@ import { GoogleSearch } from "@/app/api/langchain-tools/google_search";
|
||||
import { Tool, DynamicTool } from "langchain/tools";
|
||||
import * as langchainTools from "langchain/tools";
|
||||
import { Embeddings } from "langchain/dist/embeddings/base.js";
|
||||
import { promises } from "dns";
|
||||
import { WolframAlphaTool } from "./wolframalpha";
|
||||
|
||||
export class EdgeTool {
|
||||
private apiKey: string | undefined;
|
||||
@ -50,13 +50,16 @@ export class EdgeTool {
|
||||
);
|
||||
const stableDiffusionTool = new StableDiffusionWrapper();
|
||||
const arxivAPITool = new ArxivAPIWrapper();
|
||||
return [
|
||||
const wolframAlphaTool = new WolframAlphaTool();
|
||||
let tools = [
|
||||
// searchTool,
|
||||
calculatorTool,
|
||||
webBrowserTool,
|
||||
dallEAPITool,
|
||||
stableDiffusionTool,
|
||||
arxivAPITool,
|
||||
wolframAlphaTool,
|
||||
];
|
||||
return tools;
|
||||
}
|
||||
}
|
||||
|
33
app/api/langchain-tools/wolframalpha.ts
Normal file
33
app/api/langchain-tools/wolframalpha.ts
Normal 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";
|
||||
}
|
||||
}
|
||||
}
|
@ -16,7 +16,7 @@ import { ACCESS_CODE_PREFIX, ServiceProvider } from "@/app/constant";
|
||||
import * as langchainTools from "langchain/tools";
|
||||
import { HttpGetTool } from "@/app/api/langchain-tools/http_get";
|
||||
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 { GoogleSearch } from "@/app/api/langchain-tools/google_search";
|
||||
import { useAccessStore } from "@/app/store";
|
||||
@ -130,7 +130,7 @@ export class AgentApi {
|
||||
},
|
||||
async handleAgentAction(action) {
|
||||
try {
|
||||
// console.log("[handleAgentAction]", action.tool);
|
||||
// console.log("[handleAgentAction]", { action });
|
||||
if (!reqBody.returnIntermediateSteps) return;
|
||||
var response = new ResponseBody();
|
||||
response.isToolMessage = true;
|
||||
@ -153,10 +153,10 @@ export class AgentApi {
|
||||
}
|
||||
},
|
||||
async handleToolStart(tool, input) {
|
||||
// console.log("[handleToolStart]", { tool });
|
||||
console.log("[handleToolStart]", { tool, input });
|
||||
},
|
||||
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) {
|
||||
console.log("[handleAgentEnd]");
|
||||
@ -282,16 +282,6 @@ export class AgentApi {
|
||||
var tool = langchainTools[
|
||||
toolName as keyof typeof langchainTools
|
||||
] 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) {
|
||||
tools.push(new tool());
|
||||
}
|
||||
|
@ -86,7 +86,7 @@ export const CN_PLUGINS: BuiltinPlugin[] = [
|
||||
},
|
||||
{
|
||||
name: "WolframAlphaTool",
|
||||
toolName: "wolfram_alpha",
|
||||
toolName: "wolfram_alpha_llm",
|
||||
lang: "cn",
|
||||
description:
|
||||
"在需要回答有关数学、科学、技术、文化、社会和日常生活的问题时非常有用。",
|
||||
|
@ -89,7 +89,7 @@ export const EN_PLUGINS: BuiltinPlugin[] = [
|
||||
},
|
||||
{
|
||||
name: "WolframAlphaTool",
|
||||
toolName: "wolfram_alpha",
|
||||
toolName: "wolfram_alpha_llm",
|
||||
lang: "en",
|
||||
description:
|
||||
"Useful for when you need to answer questions about Math, Science, Technology, Culture, Society and Everyday Life.",
|
||||
|
@ -33,7 +33,7 @@
|
||||
"html-to-image": "^1.11.11",
|
||||
"html-to-text": "^9.0.5",
|
||||
"https-proxy-agent": "^7.0.2",
|
||||
"langchain": "^0.0.212",
|
||||
"langchain": "^0.0.213",
|
||||
"mermaid": "^10.6.1",
|
||||
"nanoid": "^5.0.3",
|
||||
"next": "^13.4.9",
|
||||
|
44
yarn.lock
44
yarn.lock
@ -1813,22 +1813,22 @@
|
||||
"@jridgewell/resolve-uri" "3.1.0"
|
||||
"@jridgewell/sourcemap-codec" "1.4.14"
|
||||
|
||||
"@langchain/community@~0.0.8":
|
||||
version "0.0.11"
|
||||
resolved "https://registry.yarnpkg.com/@langchain/community/-/community-0.0.11.tgz#5e1dce3e1ec636e015c503593fdf7383d4478159"
|
||||
integrity sha512-8L19AyzBueHrRtawxU4JstRtmzqsHK2jDJtSg/bv9ivto0kDKlILnauIDijcb09V/4mql2XolaS9r7eGe99yGA==
|
||||
"@langchain/community@~0.0.12":
|
||||
version "0.0.12"
|
||||
resolved "https://registry.yarnpkg.com/@langchain/community/-/community-0.0.12.tgz#95b5ddfaef47db6a1f665959e417a706cf56057f"
|
||||
integrity sha512-mcm6FxxnLxSx9PiYvehGGwvcHjsVR5WXfYOwymojf/6d0apyewjOLzKsR3xx0HJVtCs8pff7NZSdDoE+jj8OcA==
|
||||
dependencies:
|
||||
"@langchain/core" "~0.1.3"
|
||||
"@langchain/openai" "~0.0.7"
|
||||
"@langchain/core" "~0.1.5"
|
||||
"@langchain/openai" "~0.0.9"
|
||||
flat "^5.0.2"
|
||||
langsmith "~0.0.48"
|
||||
uuid "^9.0.0"
|
||||
zod "^3.22.3"
|
||||
|
||||
"@langchain/core@~0.1.3":
|
||||
version "0.1.4"
|
||||
resolved "https://registry.yarnpkg.com/@langchain/core/-/core-0.1.4.tgz#f923c0d844faf6de2069d86d785e4ef3b1381257"
|
||||
integrity sha512-Y3/mQLEiQ78ZbsTGYvPRj5bpvrhpTAcsbdyEtlYEvjMLbehEADfjQ41G9zc7U/emkrGtHRmxWO1ISmoeXeDmyQ==
|
||||
"@langchain/core@~0.1.5":
|
||||
version "0.1.6"
|
||||
resolved "https://registry.yarnpkg.com/@langchain/core/-/core-0.1.6.tgz#4b1dfc361379d6bd45c6652418a05f051488e6d9"
|
||||
integrity sha512-mBXsHHAWgDYJPzPNwOLWXJob3DAlO1tXBybu9cJ+zcVzixiSh/oxQ5eonTK6nlFwO6kGpaIcCD9l9+yq/WCMBg==
|
||||
dependencies:
|
||||
ansi-styles "^5.0.0"
|
||||
camelcase "6"
|
||||
@ -1841,12 +1841,12 @@
|
||||
uuid "^9.0.0"
|
||||
zod "^3.22.3"
|
||||
|
||||
"@langchain/openai@~0.0.7":
|
||||
version "0.0.8"
|
||||
resolved "https://registry.yarnpkg.com/@langchain/openai/-/openai-0.0.8.tgz#77e4a216f9ba5a347ee24b7f28a526fd57c24c8d"
|
||||
integrity sha512-F8DD6vs5BvMAHiDprINWCVf7jyNaYHp2zAqHwYij3+YAKsgIORm2EQvI6YtT3qPnGghpE3qz8H3DKjTNPywIzQ==
|
||||
"@langchain/openai@~0.0.9":
|
||||
version "0.0.9"
|
||||
resolved "https://registry.yarnpkg.com/@langchain/openai/-/openai-0.0.9.tgz#e84b7db0554de75fdd4a8fe12914fdc2b2d2d1f6"
|
||||
integrity sha512-Py7rJijOjNtb9pj5He+E9uAj9d8PCX+8Ix4LvY7cUTMCDlfkhngw2DhJ8wlk23u1IvunakdnnN74pfbO2JJBrw==
|
||||
dependencies:
|
||||
"@langchain/core" "~0.1.3"
|
||||
"@langchain/core" "~0.1.5"
|
||||
js-tiktoken "^1.0.7"
|
||||
openai "^4.19.0"
|
||||
zod "^3.22.3"
|
||||
@ -5492,15 +5492,15 @@ kleur@^4.0.3:
|
||||
resolved "https://registry.yarnpkg.com/kleur/-/kleur-4.1.5.tgz#95106101795f7050c6c650f350c683febddb1780"
|
||||
integrity sha512-o+NO+8WrRiQEE4/7nwRJhN1HWpVmJm511pBHUxPLtp0BUISzlBplORYSmTclCnJvQq2tKu/sgl3xVpkc7ZWuQQ==
|
||||
|
||||
langchain@^0.0.212:
|
||||
version "0.0.212"
|
||||
resolved "https://registry.yarnpkg.com/langchain/-/langchain-0.0.212.tgz#68ddeb6bb83bc7714a6b8d97ace464951d10ea6d"
|
||||
integrity sha512-zPcnAX3t1RuyWkbEkyqlIdR29j51Eylz7rcDcqET1baquR/r9xuk7P/HAJ6TFowiXDUg10d5YBD6VBvjwF/WDA==
|
||||
langchain@^0.0.213:
|
||||
version "0.0.213"
|
||||
resolved "https://registry.yarnpkg.com/langchain/-/langchain-0.0.213.tgz#52fd367d5f1a100a1cdb775ffbb98eaa5793307b"
|
||||
integrity sha512-nQDOJXvtIAIuUzamCiF1AWyi2GH9FSDPR+3XulJUEpdU60aSFPZ9GBiWdu+dVHXeAmm8C0iCVi0+3GWLJrUoXA==
|
||||
dependencies:
|
||||
"@anthropic-ai/sdk" "^0.9.1"
|
||||
"@langchain/community" "~0.0.8"
|
||||
"@langchain/core" "~0.1.3"
|
||||
"@langchain/openai" "~0.0.7"
|
||||
"@langchain/community" "~0.0.12"
|
||||
"@langchain/core" "~0.1.5"
|
||||
"@langchain/openai" "~0.0.9"
|
||||
binary-extensions "^2.2.0"
|
||||
expr-eval "^2.0.2"
|
||||
js-tiktoken "^1.0.7"
|
||||
|
Loading…
Reference in New Issue
Block a user