upsertFromYouTube
Inserts or updates vectors from a YouTube video.
upsertFromYouTube(
videoId: string,
options?: {
language?: string
addVideoInfo?: boolean
metadata?: Record<string, any>
textSplitter?: SplitterParams
}
): Promise<string[]>
Reference
import { myVectorStore } from "#elements";
export default async function () {
const count1 = await myVectorStore.upsertFromYouTube(
"https://www.youtube.com/watch?v=bZQun8Y4L2A",
{ language: "en", addVideoInfo: false }
);
console.log(`${count1} vectors upserted`);
const count2 = await myVectorStore.upsertFromYouTube("bZQun8Y4L2A", {
language: "zh-Hans",
addVideoInfo: false,
});
console.log(`${count2} vectors upserted`);
}
Parameters
videoId
: The ID of the YouTube video to extract content from.options
: Optional configuration parameters, including:language
: (optional) The language of the subtitles to extract, defaults to 'en'.addVideoInfo
: (optional) Whether to add video information (title, description, etc.) to the metadata, defaults to false.metadata
: (optional) The metadata to associate with the vectors.textSplitter
: (optional) The text splitter employed to divide the content into multiple vectors. In the absence of a provided splitter, the token splitter is used by default.
Returns
Promise of an array of IDs of the upserted vectors.
Caveats
- This method will insert a new vector if the
videoId
does not exist, or update the existing vector if thevideoId
exists. - When
addVideoInfo
is set to true, there may be instances where the video metadata is not available or accessible. - You can query all the results by filtering the metadata field
source-by-babel
tovideoId
.
Examples
In the HTTP Element, load YouTube video transcript data:
import * as Koa from "koa";
import { myVectorStore } from "#elements";
export default async function (
request: Koa.Request,
response: Koa.Response,
ctx: Koa.Context
) {
await myVectorStore.upsertFromYouTube("dVtrJYSdVo0");
const result = await myVectorStore.search("How to cook Steak", {
topK: 3,
});
return result[0].metadata!["transcript"];
}