From 45723956b5c5dca8b4a37f00978fe0f174174c10 Mon Sep 17 00:00:00 2001 From: Sheng Fan Date: Wed, 10 Apr 2024 14:02:59 +0800 Subject: [PATCH] update plugins and env template for better usages --- .env.template | 13 ++++++-- app/api/langchain-tools/bili_wbi_tools.ts | 13 +++++--- .../bilibili_music_recognition.ts | 31 ++++++++++++++----- .../bilibili_vid_conclusion.ts | 1 - app/api/langchain-tools/nodejs_tools.ts | 2 +- 5 files changed, 44 insertions(+), 16 deletions(-) diff --git a/.env.template b/.env.template index 7be2f86f3..3411db3d2 100644 --- a/.env.template +++ b/.env.template @@ -1,4 +1,3 @@ - # Your openai api key. (required) OPENAI_API_KEY=sk-xxxx @@ -68,4 +67,14 @@ QDRANT_URL= # Configuration is required when turning on RAG. # Default: Empty -QDRANT_API_KEY= \ No newline at end of file +QDRANT_API_KEY= + +# (optional) +# Default: Empty +# Put your cookies at bilibili.com here in the exact format as in the Cookie header in HTTP. Usually it works without cookies. Leaving this empty will disable searching and fetching videos' conclusion data from Bilibili. +BILIBILI_COOKIES= + +# (optional) +# Default: Empty +# Address of the metaprocess server for advanced video processing features (currently music recognition). Leaving this empty will disable them. +BILIVID_METAPROCESS_SERVER_ADDRESS= diff --git a/app/api/langchain-tools/bili_wbi_tools.ts b/app/api/langchain-tools/bili_wbi_tools.ts index b430422be..6f763e574 100644 --- a/app/api/langchain-tools/bili_wbi_tools.ts +++ b/app/api/langchain-tools/bili_wbi_tools.ts @@ -1,4 +1,5 @@ import md5 from "md5"; +import { getRandomUserAgent } from "./ua_tools"; const mixinKeyEncTab: number[] = [ 46, 47, 18, 2, 53, 8, 23, 32, 15, 50, 10, 31, 58, 3, 45, 35, 27, 43, 5, 49, @@ -45,14 +46,18 @@ export async function getWbiKeys(): Promise<{ img_key: string; sub_key: string; }> { + // check if process.env.BILIBILI_COOKIES is set + if (!process.env.BILIBILI_COOKIES) { + throw new Error( + "Cookie not found. Please set BILIBILI_COOKIES environment variable.", + ); + } const res: Response = await fetch( "https://api.bilibili.com/x/web-interface/nav", { headers: { - // SESSDATA 字段 - Cookie: "SESSDATA=xxxxxx", - "User-Agent": - "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3", + Cookie: process.env.BILIBILI_COOKIES, + "User-Agent": getRandomUserAgent(), Referer: "https://www.bilibili.com/", //对于直接浏览器调用可能不适用 }, }, diff --git a/app/api/langchain-tools/bilibili_music_recognition.ts b/app/api/langchain-tools/bilibili_music_recognition.ts index d4bf9d380..ec28fc27b 100644 --- a/app/api/langchain-tools/bilibili_music_recognition.ts +++ b/app/api/langchain-tools/bilibili_music_recognition.ts @@ -1,6 +1,5 @@ import { Tool } from "@langchain/core/tools"; import { getRandomUserAgent } from "./ua_tools"; -import { encWbi, getWbiKeys } from "./bili_wbi_tools"; export interface Headers { [key: string]: string; @@ -35,12 +34,24 @@ export class BilibiliMusicRecognitionTool extends Tool implements RequestTool { try { // let result = await this.doAcrcloudRecognitionUsingMetaprocAPI(searchQuery); // parse query - const [videoAid, pid, targetSec] = query.split(","); + var [videoAid, pid, targetSec] = query.split(","); // check if arguments are valid - // is videoAid a string of numbers? - if (!/^\d+$/.test(videoAid)) { + if (!(/^\d+$/.test(videoAid) || /^av\d+$/.test(videoAid))) { 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{BVid} 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 using av{Aid} format 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.", ); } @@ -62,9 +73,13 @@ export class BilibiliMusicRecognitionTool extends Tool implements RequestTool { pid: number, targetSec: number, ) { - // get http://10.0.1.3:32345/api/recog_music_in_bili_video?video_aid=170001&pid=1&target_sec=14 - // TODO open-source the forwarding server, and put the server address in an environment variable - const url = `http://10.0.1.3:32345/api/recog_music_in_bili_video?video_aid=${videoAid}&pid=${pid}&target_sec=${targetSec}`; + if (!process.env.BILIVID_METAPROCESS_SERVER_ADDRESS) { + throw new Error( + "BILIVID_METAPROCESS_SERVER_ADDRESS environment variable is not set. Please set it to the address of the BiliVid metaprocess server.", + ); + } + // for reference: https://github.com/fred913/bilivid-metaprocess-server + const url = `http://${process.env.BILIVID_METAPROCESS_SERVER_ADDRESS}/api/recog_music_in_bili_video?video_aid=${videoAid}&pid=${pid}&target_sec=${targetSec}`; const headers = { "User-Agent": getRandomUserAgent(), diff --git a/app/api/langchain-tools/bilibili_vid_conclusion.ts b/app/api/langchain-tools/bilibili_vid_conclusion.ts index 4bd6b1864..a41a2cae2 100644 --- a/app/api/langchain-tools/bilibili_vid_conclusion.ts +++ b/app/api/langchain-tools/bilibili_vid_conclusion.ts @@ -35,7 +35,6 @@ export class BilibiliVideoConclusionTool extends Tool implements RequestTool { try { var [videoAid, pid] = query.split(","); // check if arguments are valid - // is videoAid a string of numbers? if (!(/^\d+$/.test(videoAid) || /^av\d+$/.test(videoAid))) { 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.", diff --git a/app/api/langchain-tools/nodejs_tools.ts b/app/api/langchain-tools/nodejs_tools.ts index 4389b7c46..632971715 100644 --- a/app/api/langchain-tools/nodejs_tools.ts +++ b/app/api/langchain-tools/nodejs_tools.ts @@ -57,8 +57,8 @@ export class NodeJSTool { const pdfBrowserTool = new PDFBrowser(this.model, this.embeddings); const bilibiliVideoInfoTool = new BilibiliVideoInfoTool(); const bilibiliVideoSearchTool = new BilibiliVideoSearchTool(); - const bilibiliMusicRecognitionTool = new BilibiliMusicRecognitionTool(); const bilibiliVideoConclusionTool = new BilibiliVideoConclusionTool(); + const bilibiliMusicRecognitionTool = new BilibiliMusicRecognitionTool(); let tools = [ calculatorTool, webBrowserTool,