1.1.0 • Published 1 year ago

payload-plugin-ai v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Payload AI plugin

npm version

ATTENTION: This plugin currently requires a custom fork of payload to work (replace payload with yarn add alessiogr/payload#production-with-custom). This is because this plugins depends on two PRs which haven't been merged into payload yet:

In this repository I will add a bunch of AI stuff! Currently, it can just generate embeddings for fields using the OpenAI API. Expect things to break with updates without notice.

Installation

yarn add payload-plugin-ai

Add this to your payload.config.ts:

import { ai } from 'payload-plugin-ai';

...

plugins: [
    ai({
      OPENAI_SECRET: process.env.OPENAI_SECRET,
      NLPCLOUD_API_KEY: process.env.NLP_CLOUD_API_KEY,
      embeddings: {
        provider: "nlpcloud", // Nlpcloud is recommended
      },
    }),
]

Embeddings

Embeddings generated in the Admin UI

To enable embeddings on a field, simply add this hook to the field:

import { genEmbeddings } from 'payload-plugin-ai';

...

yourField: {
  ...
  custom: {
        ...genEmbeddings({ visible: true }),
  },
}

Now, every time you save the collection, embeddings for that field will be generated.

Search through embeddings

You can use MongoDB atlas search to search through embeddings. In MongoDB Atlas, create a search index which looks like this:

{
  "mappings": {
    "fields": {
      "textArea_embeddings": [
        {
          "dimensions": 768,
          "similarity": "euclidean",
          "type": "knnVector"
        }
      ]
    }
  }
}

Replace textArea_embeddings with the name of the embeddings field generated by this plugins.

Once the index is created, you can search through your documents using something like mongoose. There's an example in the payload.config.ts in the demo folder (make sure you adjust the index name and the path).

Other

Project structure inspired by payload-plugin-cloud-storage and pcms-backpop