genkit icon indicating copy to clipboard operation
genkit copied to clipboard

[JS] Genkit Vertex plugin not working in ESM mode

Open oerhahon opened this issue 1 year ago • 1 comments

Describe the bug node:internal/modules/esm/resolve:254 throw new ERR_UNSUPPORTED_DIR_IMPORT(path, fileURLToPath(base), String(resolved)); ^

Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import 'C:\sample\node_modules@genkit-ai\vertexai\lib\vector-search' is not supported resolving ES modules imported from C:\sample\node_modules@genkit-ai\vertexai\lib\index.mjs at finalizeResolution (node:internal/modules/esm/resolve:254:11) at moduleResolve (node:internal/modules/esm/resolve:921:10) at defaultResolve (node:internal/modules/esm/resolve:1124:11) at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:557:12) at ModuleLoader.resolve (node:internal/modules/esm/loader:526:25) at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:249:38) at ModuleJob._link (node:internal/modules/esm/module_job:126:49) { code: 'ERR_UNSUPPORTED_DIR_IMPORT', url: 'file:///C:/sample/node_modules/@genkit-ai/vertexai/lib/vector-search' }

Node.js v22.5.1

"@genkit-ai/vertexai": "^0.5.13",

To Reproduce use genkit init, rename src/index.ts to src/index.mjs, and add "type": "module" to package

oerhahon avatar Sep 14 '24 14:09 oerhahon

I have this same thing

seanaguinaga avatar Oct 11 '24 17:10 seanaguinaga

same here

ChristianFenkart avatar Nov 13 '24 19:11 ChristianFenkart

Upgrading to the latest version, resolve this issue for me:

"@genkit-ai/ai": "^0.9.6",
"@genkit-ai/core": "^0.9.6",
"@genkit-ai/firebase": "^0.9.6",
"@genkit-ai/vertexai": "^0.9.6",

file.mjs

import { textEmbedding004, gemini15Flash, vertexAI } from '@genkit-ai/vertexai'
import { defineFirestoreRetriever } from '@genkit-ai/firebase'
import { vertexAIEvaluation, VertexAIEvaluationMetricType } from '@genkit-ai/vertexai/evaluation'
import { genkit } from 'genkit'
import { chunk } from 'llm-chunk'
import { FieldValue } from 'firebase-admin/firestore'

// Initialize the Genkit AI instance
const ai = genkit({
  model: gemini15Flash,
  plugins: [
    vertexAI({ location: 'us-central1' }),
    vertexAIEvaluation({
      location: 'us-central1',
      metrics: [
        VertexAIEvaluationMetricType.SAFETY,
        {
          type: VertexAIEvaluationMetricType.ROUGE,
          metricSpec: {
            rougeType: 'rougeLsum'
          }
        }
      ]
    })
  ]
})

async function indexer(firestore, text, config) {
  if (!text) return

  const collectionRef = firestore.collection(config.collection)
  const snapshot = await collectionRef.get()

  if (!snapshot.empty) {
    const batch = firestore.batch()
    snapshot.forEach((doc) => {
      batch.delete(doc.ref)
    })
    await batch.commit()
  }

  const chunks = await chunk(text, {
    minLength: 50,
    maxLength: 600,
    splitter: 'paragraph',
    overlap: 50,
    delimiters: ['.', '\n']
  })

  for (const chunkText of chunks) {
    const embedding = await ai.embed({
      embedder: config.embedder || textEmbedding004,
      content: chunkText
    })
    await collectionRef.add({
      [config.vectorField]: FieldValue.vector(embedding),
      [config.contentField]: chunkText
    })
  }
}

async function retriever(firestore, query, config) {
  const retriever = defineFirestoreRetriever(ai, {
    name: 'customRetriever',
    firestore,
    collection: config.collection,
    contentField: config.contentField,
    vectorField: config.vectorField,
    embedder: config.embedder || textEmbedding004,
    distanceMeasure: 'COSINE'
  })

  const docs = await ai.retrieve({
    retriever,
    query: {
      content: [
        {
          text: query
        }
      ]
    },
    options: { k: 3, limit: 5 }
  })

  return docs
}

async function generator(query, context) {
  const { text } = await ai.generate({
    model: gemini15Flash,
    prompt: `
      Task: ${query}
      Context: ${context}
  `
  })
  return text
}

export { indexer, retriever, generator }

oerhahon avatar Dec 04 '24 12:12 oerhahon