Write logic to chunk content and generate embeddings using AI SDK.
Setting Up the AI Directory
embedding.ts
in the lib/ai
directoryGenerate Chunks
lib/ai/embedding.ts
:Install AI SDK
Generate Embeddings
lib/ai/embedding.ts
file:text-embedding-ada-002
embedding model.
Next, you create an asynchronous function called generateEmbeddings
. This function will take in the source material (value
) as an input and return a promise of an array of objects, each containing an embedding and content. Within the function, you first generate chunks for the input. Then, you pass those chunks to the embedMany
function imported from the AI SDK which will return embeddings of the chunks you passed in. Finally, you map over and return the embeddings in a format that is ready to save in the database.