diff --git a/app/api/langchain-tools/bilibili_music_recognition.ts b/app/api/langchain-tools/bilibili_music_recognition.ts index ec28fc27b..429d3ba5f 100644 --- a/app/api/langchain-tools/bilibili_music_recognition.ts +++ b/app/api/langchain-tools/bilibili_music_recognition.ts @@ -1,5 +1,6 @@ -import { Tool } from "@langchain/core/tools"; +import { StructuredTool } from "@langchain/core/tools"; import { getRandomUserAgent } from "./ua_tools"; +import { z } from "zod"; export interface Headers { [key: string]: string; @@ -11,7 +12,10 @@ export interface RequestTool { timeout: number; } -export class BilibiliMusicRecognitionTool extends Tool implements RequestTool { +export class BilibiliMusicRecognitionTool + extends StructuredTool + implements RequestTool +{ name = "bilibili_music_recognition"; maxOutputLength = Infinity; @@ -29,36 +33,30 @@ export class BilibiliMusicRecognitionTool extends Tool implements RequestTool { this.timeout = timeout ?? this.timeout; } + schema = z.object({ + videoAid: z.string().describe("The AID of the video to be recognized"), + pid: z.number().describe("The page ID of the video, starting from 1"), + targetSec: z + .number() + .describe( + "The time in seconds where the recognition is expected to start from", + ), + }); + /** @ignore */ - async _call(query: string) { + async _call({ videoAid, pid, targetSec }: z.infer) { try { - // let result = await this.doAcrcloudRecognitionUsingMetaprocAPI(searchQuery); - // parse query - var [videoAid, pid, targetSec] = query.split(","); - // check if arguments are valid - if (!(/^\d+$/.test(videoAid) || /^av\d+$/.test(videoAid))) { + var newVideoAid = videoAid.toString(); + if (!(/^\d+$/.test(newVideoAid) || /^av\d+$/.test(newVideoAid))) { throw new Error( - "Invalid videoAid: It should be a string of numbers. If a BVid or a short link is given, please convert it to Aid using av{Aid} format using BiliVideoInfo tool.", + "Invalid videoAid: It should be a string of numbers. If a BVid or a short link is given, please convert it to Aid number using BiliVideoInfo tool.", ); } - if (videoAid.startsWith("av")) videoAid = videoAid.slice(2); - - if (!/^\d+$/.test(pid)) { - throw new Error( - "Invalid pid: it should be a number representing the page number of the video, starting from 1.", - ); - } - - if (!/^\d+$/.test(targetSec)) { - throw new Error( - "Invalid targetSec: it should be a number representing the time in seconds where the recognition is expected to start from.", - ); - } - + if (newVideoAid.startsWith("av")) newVideoAid = newVideoAid.slice(2); const result = await this.doAcrcloudRecognitionUsingMetaprocAPI( - videoAid, - parseInt(pid), - parseInt(targetSec), + newVideoAid, + typeof pid === "string" ? parseInt(pid) : pid, + typeof targetSec === "string" ? parseInt(targetSec) : targetSec, ); // console.log(result) return JSON.stringify(result); @@ -116,5 +114,5 @@ export class BilibiliMusicRecognitionTool extends Tool implements RequestTool { return response; } - description = `A tool that recognizes music in Bilibili videos using ACRCloud API. Input string is in this format: video_aid,pid,target_sec. video_aid is extracted from av{video_aid}, e.g. av170001 means video_aid=170001. pid is the page ID of the video(note: pid starts from 1, not 0). and target_sec is the time in seconds where the recognition is expected to start from. To recognize music that begins with video, use timestamp 0 or 1, which is the most useful case.`; + description = `A tool that recognizes music in Bilibili videos using ACRCloud API. As for input parameters, videoAid is the AID of the video to be recognized, pid is the page ID of the video(note: pid starts from 1, not 0). and target_sec is the time in seconds where the recognition is expected to start from. To recognize music that begins with video, use timestamp 0 or 1, which is the most useful case.`; } diff --git a/app/api/langchain-tools/bilibili_vid_conclusion.ts b/app/api/langchain-tools/bilibili_vid_conclusion.ts index a41a2cae2..99f70f512 100644 --- a/app/api/langchain-tools/bilibili_vid_conclusion.ts +++ b/app/api/langchain-tools/bilibili_vid_conclusion.ts @@ -1,6 +1,7 @@ -import { Tool } from "@langchain/core/tools"; +import { StructuredTool } from "@langchain/core/tools"; import { getRandomUserAgent } from "./ua_tools"; import { encWbi, getWbiKeys } from "./bili_wbi_tools"; +import { z } from "zod"; export interface Headers { [key: string]: string; @@ -12,7 +13,10 @@ export interface RequestTool { timeout: number; } -export class BilibiliVideoConclusionTool extends Tool implements RequestTool { +export class BilibiliVideoConclusionTool + extends StructuredTool + implements RequestTool +{ name = "bilibili_video_conclusion"; maxOutputLength = Infinity; @@ -30,25 +34,25 @@ export class BilibiliVideoConclusionTool extends Tool implements RequestTool { this.timeout = timeout ?? this.timeout; } + schema = z.object({ + videoAid: z.string().describe("The AID of the video to be recognized"), + pid: z.number().describe("The page ID of the video, starting from 1"), + }); + /** @ignore */ - async _call(query: string) { + async _call({ videoAid, pid }: z.infer) { try { - var [videoAid, pid] = query.split(","); + var newVideoAid = videoAid.toString(); // check if arguments are valid - if (!(/^\d+$/.test(videoAid) || /^av\d+$/.test(videoAid))) { + if (!(/^\d+$/.test(newVideoAid) || /^av\d+$/.test(newVideoAid))) { throw new Error( - "Invalid videoAid: It should be a string of numbers. If a BVid or a short link is given, please convert it to Aid using av{Aid} format using BiliVideoInfo tool.", - ); - } - if (!/^\d+$/.test(pid)) { - throw new Error( - "Invalid pid: it should be a number representing the page number of the video, starting from 1.", + "Invalid videoAid: It should be a string of numbers. If a BVid or a short link is given, please convert it to Aid number using BiliVideoInfo tool.", ); } - if (videoAid.startsWith("av")) videoAid = videoAid.slice(2); + if (newVideoAid.startsWith("av")) newVideoAid = newVideoAid.slice(2); - let result = await this.fetchVideoInfo(+videoAid, +pid); + let result = await this.fetchVideoInfo(+newVideoAid, +pid); // console.log(result) return result; } catch (error) { @@ -98,7 +102,7 @@ export class BilibiliVideoConclusionTool extends Tool implements RequestTool { const model_result = data.data.model_result; switch (model_result.result_type) { case 0: - return "BiliAPI returned result: Unable to provide a conclusion for this video, or the conclusion cannot be generated for this video due to the video quality or the video content."; + return "BiliAPI returned result: Unable to provide a conclusion for this video, or the conclusion cannot be generated for this video due to the content (news, ad, ). Remember, this is a restriction by the Bilibili Services, not from the tool."; case 1: return model_result.summary; case 2: @@ -133,5 +137,5 @@ export class BilibiliVideoConclusionTool extends Tool implements RequestTool { return response; } - description = `A tool to fetch the conclusion of a Bilibili video. It uses the BiliAPI to fetch the conclusion of the video. Input string should be in the format of "videoAid,pid" where videoAid is the Aid of the video and pid is the page number of the video, starting from 1.`; + description = `A tool to fetch the conclusion of a Bilibili video. It uses the BiliAPI to fetch the conclusion of the video. As for input parameters, videoAid is the Aid of the video and pid is the page number of the video, starting from 1.`; }