Skip to content

Commit

Permalink
feat: add nextjs plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 committed May 9, 2024
1 parent f1906bf commit e61e843
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 16 deletions.
4 changes: 3 additions & 1 deletion packages/core/e2e/examples/nextjs-agent/next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/** @type {import('next').NextConfig} */
const nextConfig = {};

export default nextConfig;
import withLlamaIndex from "llamaindex/next";

export default withLlamaIndex(nextConfig);
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/** @type {import('next').NextConfig} */
const nextConfig = {};

export default nextConfig;
import withLlamaIndex from "llamaindex/next";

export default withLlamaIndex(nextConfig);
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use server";
// test runtime
import "llamaindex";
import "llamaindex/embeddings/ClipEmbedding";
import "llamaindex/readers/SimpleDirectoryReader";

// @ts-expect-error
Expand Down
17 changes: 3 additions & 14 deletions packages/core/src/embeddings/ClipEmbedding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import type { ImageType } from "../Node.js";
import { MultiModalEmbedding } from "./MultiModalEmbedding.js";

async function readImage(input: ImageType) {
const { RawImage } = await import(
/* webpackIgnore: true */
"@xenova/transformers"
);
const { RawImage } = await import("@xenova/transformers");
if (input instanceof Blob) {
return await RawImage.fromBlob(input);
} else if (_.isString(input) || input instanceof URL) {
Expand All @@ -32,21 +29,15 @@ export class ClipEmbedding extends MultiModalEmbedding {

async getTokenizer() {
if (!this.tokenizer) {
const { AutoTokenizer } = await import(
/* webpackIgnore: true */
"@xenova/transformers"
);
const { AutoTokenizer } = await import("@xenova/transformers");
this.tokenizer = await AutoTokenizer.from_pretrained(this.modelType);
}
return this.tokenizer;
}

async getProcessor() {
if (!this.processor) {
const { AutoProcessor } = await import(
/* webpackIgnore: true */
"@xenova/transformers"
);
const { AutoProcessor } = await import("@xenova/transformers");
this.processor = await AutoProcessor.from_pretrained(this.modelType);
}
return this.processor;
Expand All @@ -55,7 +46,6 @@ export class ClipEmbedding extends MultiModalEmbedding {
async getVisionModel() {
if (!this.visionModel) {
const { CLIPVisionModelWithProjection } = await import(
/* webpackIgnore: true */
"@xenova/transformers"
);
this.visionModel = await CLIPVisionModelWithProjection.from_pretrained(
Expand All @@ -69,7 +59,6 @@ export class ClipEmbedding extends MultiModalEmbedding {
async getTextModel() {
if (!this.textModel) {
const { CLIPTextModelWithProjection } = await import(
/* webpackIgnore: true */
"@xenova/transformers"
);
this.textModel = await CLIPTextModelWithProjection.from_pretrained(
Expand Down
36 changes: 36 additions & 0 deletions packages/core/src/next.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* This is a Next.js configuration file that is used to customize the build process.
*
* @example
* ```js
* // next.config.js
* const withLlamaIndex = require("llamaindex/next")
*
* module.exports = withLlamaIndex({
* // Your Next.js configuration
* })
* ```
*
* This is only for Next.js projects, do not export this function on top-level.
*
* @module
*/
export default function withLlamaIndex(config: any) {
const userWebpack = config.webpack;
//#region hack for `@xenova/transformers`
// Ignore node-specific modules when bundling for the browser
// See https://webpack.js.org/configuration/resolve/#resolvealias
config.webpack = function (webpackConfig: any) {
if (userWebpack) {
webpackConfig = userWebpack(webpackConfig);
}
webpackConfig.resolve.alias = {
...webpackConfig.resolve.alias,
sharp$: false,
"onnxruntime-node$": false,
};
return webpackConfig;
};
//#endregion
return config;
}

0 comments on commit e61e843

Please sign in to comment.