iTranslated by AI
Langchain.js Tool Recipes: Page Fetching, YouTube Transcription, and More
In this article, I will quickly introduce some tools that can be used immediately with Langchain.js, along with their code. I'll be going through them quickly, so let's dive right in.
1. ReadPageTool
Required packages: @mozilla/readability, jsdom
Recommended package: deepl
This tool fetches a page from a URL, extracts the main content, and returns it. It uses @mozilla/readability for content extraction. When token limits are exceeded on Japanese pages, integrating a translation service like deepl can help mitigate the issue.
import { JSDOM } from "jsdom";
import { Readability } from "@mozilla/readability";
class ReadPageTool extends Tool {
name = "read-page-tool";
description = `Reads the content of the entered URL and returns the content`;
call(input) {
console.log("Fetch:", input);
const nhm = new NodeHtmlMarkdown();
return fetch(input, FETCH_OPTION)
.then((res) => res.text())
.then((html) => new JSDOM(html))
.then((dom) => new Readability(dom.window.document).parse())
.then((html) => html.textContent)
.catch((e) => "Sorry, I couldn't fetch the content.");
}
}
2. GPTTool
This tool attempts to improve processing performance by acting as a secretary for the main agent. To be honest, I'm not sure of its full effectiveness, but you can see conversations between GPT instances. I feel it has some effect on tasks that tend to result in token overflow.
import { ChatOpenAI } from "langchain/chat_models";
import { initializeAgentExecutor } from "langchain/agents";
import { Tool } from "langchain/tools";
export class GPTTool extends Tool {
name = "secretary-tool";
description = `I am your secretary. Convenient but not complicated.`;
async call(input) {
const llms = new ChatOpenAI({ temperature: 0.2 });
const tools = [YOUR_TOOLS];
const executor = await initializeAgentExecutor(tools, llms, "zero-shot-react-description");
const result = await executor.call({ input });
return result.output;
}
}
3. YoutubeTranscriptionTool
Required packages: ytdl-core, axios
This tool downloads videos from YouTube and performs transcription using Whisper. In my environment, I couldn't POST FormData using fetch, so I'm using axios. It becomes more manageable if you summarize the content before returning it when dealing with token overflows.
import ytdl from "ytdl-core";
import axios from "axios";
const streamToBlob = (readable, type) =>
new Promise((resolve, reject) => {
const data = [];
readable.on("data", (chunk) => {
data.push(chunk);
});
readable.on("end", () => {
const blob = new Blob(data, { type });
resolve(blob);
});
readable.on("error", reject);
});
class YoutubeTranscriptionTool extends Tool {
name = "youtube-transcription-tool";
description = `Give me the Youtube link and we will transcribe it into text and return it to you. Search is not available.`;
constructor(opt) {
super();
this.opt = opt;
}
async call(input) {
try {
const audioStream = await ytdl(input, { quality: this.opt || "lowestaudio" });
const audio = await streamToBlob(audioStream, "audio/mpeg");
const formData = new FormData();
formData.append("file", audio, "audio.mp3");
formData.append("model", "whisper-1");
const endpoint =
this.opt?.translate == false ? "/audio/transcriptions" : "/audio/translations";
const res = await axios({
method: "POST",
url: `https://api.openai.com/v1${endpoint}`,
headers: { Authorization: `Bearer ${process.env.OPENAI_API_KEY}` },
data: formData,
});
return res.data.text;
} catch (e) {
return "Video not found";
}
}
}
Conclusion
I hope these tools are useful in some way. It might be good to have a site where these kinds of tools can be shared (I might build one). Enjoy your ChatGPT life!!!
Discussion