Pypher icon indicating copy to clipboard operation
Pypher copied to clipboard

More elaborate function calls, e.g., batching openai embeddings

Open lvijnck opened this issue 7 months ago • 8 comments

Hello,

I'm trying to codify the following query in Pypher for readability, but I don't seem to get quite far:

CALL apoc.periodic.iterate(
    "MATCH (p) RETURN p",
    "
     CALL apoc.ml.openai.embedding([item in $_batch | labels(item.p)[1]], $apiKey, $configuration) 
    YIELD index, text, embedding
    CALL apoc.create.setProperty($_batch[index].p, 'rrrr', embedding) YIELD node
    RETURN count(*)
    ",
    {batchMode: "BATCH_SINGLE", batchSize: 2000, params: $ai_config}
)
YIELD batch, operations

I was trying to create custom classes to represent apoc.ml.openai.embedding and apoc.periodic.iterate, but when I do that the "CALL" keyword does not seem to show up in the query. Any recommendations?

from pypher import __,  create_function, Pypher

from pypher.builder import Func

class ApocIterate(Func):
  _CAPITALIZE = False
  _ALIASES = ['periodic_iterate', 'apoc_periodic_iterate']
  name = 'apoc.periodic.iterate'


class OpenAIEmbedding(Func):
  _CAPITALIZE = False
  _ALIASES = ['openai_embedding', 'apoc_ml_openai_embedding']
  name = 'apoc.ml.openai.embedding'

from pypher import Pypher
q = Pypher()
q.apoc_iterate(
  __.MATCH.node("n", labels="Entity").RETURN.n, 
  __.openai_embedding(__.n.property('category'))
)


print(str(q))

Returns

apoc_iterate MATCH (n:`Entity`) RETURN n, apoc.ml.openai.embedding(n.`category`)

Observations:

  • Call keyword not included
  • How to make the "subqueries" literals?

lvijnck avatar Jul 09 '24 21:07 lvijnck