firebase / genkit

An open source framework for building AI-powered apps with familiar code-centric patterns. Genkit makes it easy to integrate, test, and deploy sophisticated AI features to Firebase or Google Cloud.
Apache License 2.0
370 stars 45 forks source link

Type error in retrievers [JS] #393

Open cabljac opened 3 weeks ago

cabljac commented 3 weeks ago

Describe the bug The types should complain in the options for the pinecone retriever. I get a runtime error:

Using DevFlowStateStore. Root: /var/folders/y9/0j765brj643gjz3ggwkpn2740000gn/T/.genkit/47a1bd8a68530c0ff153aeb3c40b4c32/flows
Error[/dev-run-action-wrapper/filmRetrieverFlow/filmRetrieverFlow, TypeError] {
  path: '/dev-run-action-wrapper/filmRetrieverFlow/filmRetrieverFlow',
  name: 'TypeError',
  message: "Cannot read properties of undefined (reading 'namespace')",
  stack: "TypeError: Cannot read properties of undefined (reading 'namespace')\n" +
    '    at /Users/jacob/invertase/genkit/genkit-samples/naive-rag/pinecone/node_modules/genkitx-pinecone/lib/index.js:156:37\n' +
    '    at Generator.next (<anonymous>)\n' +
    '    at fulfilled (/Users/jacob/invertase/genkit/genkit-samples/naive-rag/pinecone/node_modules/genkitx-pinecone/lib/index.js:47:24)\n' +
    '    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)',
  source: 'ts',
  sourceVersion: '0.5.0'
}

To Reproduce

The following code gives no type errors, but it should. The retriever won't work unless you specify k which is the number of results you wish to return.

import { config } from "dotenv";
config();
import { index, retrieve } from "@genkit-ai/ai/retriever";
import { configureGenkit } from "@genkit-ai/core";
import { defineFlow } from "@genkit-ai/flow";
import { textEmbeddingGecko, vertexAI } from "@genkit-ai/vertexai";
import { z } from "zod";
import { pineconeRetrieverRef } from "genkitx-pinecone";
import { pineconeIndexerRef } from "genkitx-pinecone";
import { pinecone } from "genkitx-pinecone";

if (!process.env.PINECONE_API_KEY) {
  throw new Error("PINECONE_API_KEY is required");
}

const fakeData = [
  {
    content: [
      {
        text: "A film about dodging bullets",
      },
    ],
    metadata: {
      name: "The Matrix",
    },
  },
  {
    content: [
      {
        text: "A film about a space opera",
      },
    ],
    metadata: {
      name: "Star Wars",
    },
  },
  {
    content: [
      {
        text: "A film about a young wizard",
      },
    ],
    metadata: {
      name: "Harry Potter",
    },
  },
];

export default configureGenkit({
  plugins: [
    vertexAI(),
    pinecone([
      {
        indexId: "films",
        embedder: textEmbeddingGecko,
        clientParams: {
          apiKey: process.env.PINECONE_API_KEY,
        },
      },
    ]),
  ],
});

export const filmRetriever = pineconeRetrieverRef({
  indexId: "films",
});

export const filmIndexer = pineconeIndexerRef({
  indexId: "films",
});

export const filmIndexerFlow = defineFlow(
  {
    name: "filmIndexerFlow",
  },
  async () => {
    const documents = fakeData;

    await index({ indexer: filmIndexer, documents });
  }
);

export const filmRetrieverFlow = defineFlow(
  {
    name: "filmRetrieverFlow",
    inputSchema: z.object({ query: z.string() }),
    // outputSchema: z.array(z.string()),
  },
  async ({ query, k }) => {
    const docs = await retrieve({ retriever: filmRetriever, query });

    console.log(docs);

    // return docs.map((d) => d.metadata!.name!);
  }
);

Expected behavior A type error about not having k in the options of retrieve.

Runtime (please complete the following information):

** Node version

Additional context genkit is on 0.5.2

I had some other issues with retrievers, will open new issues for them at a later point

cesarve77 commented 2 weeks ago

same here