APOC GenAI Procedures

The APOC Extended procedure library integrates with various AI/ML platforms to provide a set of useful utilities for quickly demonstrate GenAI features. The library includes procedures for generating texts, images, and vector embedding. It also allows the generation and execution of Cypher queries from natural language as well as schema explanation.

APOC Extended integrates with

Installation

APOC extended is available as a plugin for Neo4j. To install APOC, follow the instructions in the APOC Manual. In most installation you can download the appropriate release and place the JAR file in the plugins directory of your Neo4j installation.

In Neo4j Sandbox, APOC is already installed and you can start using the procedures right away.

APOC Extended is not available in the Neo4j Aura Cloud Service.

Usage

Here is a quick example:

// generate embedding
CALL apoc.ml.openai.embedding(['Knowledge Graphs work well with LLMs'], $apiKey, {}) yield index, text, embedding;

// generate text
CALL apoc.ml.openai.completion('What color is the sky? Answer in one word: ', $apiKey, {config}) yield value;

/*
{ created=1684248202, model="text-davinci-003", id="cmpl-7GqBWwX49yMJljdmnLkWxYettZoOy",
  usage={completion_tokens=2, prompt_tokens=12, total_tokens=14},
  choices=[{finish_reason="stop", index=0, text="Blue", logprobs=null}], object="text_completion"}
*/

CALL apoc.ml.query("What movies did Tom Hanks play in?", {apiKey: $apiKey}) yield value, query
RETURN *;


CALL apoc.ml.schema({apiKey: $apiKey});
/*
The graph database schema represents a system where users can follow other users and review movies.
Users (:Person) can either follow other users (:Person) or review movies (:Movie).
The relationships allow users to express their preferences and opinions about movies.
This schema can be compared to social media platforms where users can follow each other and leave reviews or ratings for movies they have watched.
It can also be related to movie recommendation systems where user preferences and reviews play a crucial role in generating personalized recommendations.
*/

Most of the procedures take besides your input the configuration for API keys or tokens as well as the model name.

The API keys can also be configured in apoc.conf to make the functionality available to all your users, make sure to protect they keys with rate and budget limits.

Functionality Includes

  • Generate vector embeddings

  • Text completion

  • Chat completion with multiple messages having roles for system, assistant and user

  • Image generation

  • Cypher query generation from natural language

  • Schema explanation

Documentation