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 * 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;
}
}

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

View File

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

View File

@@ -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.",