
GQLINSERT (p1:Paper {_id:'P1', title:'Efficient Graph Search', score:6, author:'Alex'}), (p2:Paper {_id:'P2', title:'Optimizing Queries', score:9, author:'Alex'}), (p3:Paper {_id:'P3', title:'Path Patterns', score:7, author:'Zack'}), (p1)-[:Cites {weight:2}]->(p2), (p2)-[:Cites {weight:1}]->(p3)
AI completion functions use a large language model to generate or execute GQL queries from natural language. Using ai.setapikey() to configure both the embedding and completion provider at once, no extra setup is needed. To use a different provider for completion (e.g., Anthropic for completion, OpenAI for embeddings), use ai.setapikey() with false to set the key without activating, then ai.setCompletionProvider() to switch.
Converts a natural language question into a GQL query using the configured completion provider. The function automatically includes the current graph's schema (labels, properties, edge patterns) as context for the LLM.
| Syntax | ai.gql(<question>) | ||
| Arguments | Name | Type | Description |
<question> | STRING | A natural language question about the graph data | |
| Return Type | STRING | ||
GQLRETURN ai.gql("Find all papers written by Alex")
Result:
| ai.gql |
|---|
| MATCH (p:Paper) WHERE p.author = 'Alex' RETURN p |
Converts a natural language question into a read-only GQL query, executes it, and returns the results. Only read operations are allowed, any generated query containing write operations (INSERT, DELETE, SET, etc.) is rejected.
| Syntax | ai.read(<question>) | ||
| Arguments | Name | Type | Description |
<question> | STRING | A natural language question about the graph data | |
| Return Type | RECORD | ||
The returned record contains:
| Field | Type | Description |
|---|---|---|
query | STRING | The generated GQL query |
results | LIST | The query results |
count | INT | Number of result rows |
GQLRETURN ai.read("How many papers did Alex write?")
Result:
JSON{ "query": "MATCH (p:Paper) WHERE p.author = 'Alex' RETURN COUNT(p) AS count", "results": [ { "count": 2 } ], "count": 1 }
Sets the active completion provider. The provider's API key must have been set first via ai.setapikey().
| Syntax | ai.setCompletionProvider(<provider>) | ||
| Arguments | Name | Type | Description |
<provider> | STRING | Provider name: "openai", "gemini", "xai", or "anthropic" | |
| Return Type | BOOL | ||
GQLRETURN ai.setCompletionProvider("openai")
Returns the name of the current completion provider.
| Syntax | ai.completionProvider() | ||
| Arguments | None | ||
| Return Type | STRING | ||
GQLRETURN ai.completionProvider()
Converts a list of numbers to a VECTOR type. Values are stored as 32-bit floats, so minor precision differences may occur (e.g., 0.1 becomes 0.10000000149011612). This is useful when you need to explicitly create a VECTOR value for storing in a VECTOR property or passing to similarity functions.
| Syntax | ai.vector(<list>) | ||
| Arguments | Name | Type | Description |
<list> | LIST | A list of numeric values | |
| Return Type | VECTOR | ||
GQLRETURN ai.vector([0.1, 0.2, 0.3])
Result:
JSON{ "values": [ 0.10000000149011612, 0.20000000298023224, 0.30000001192092896 ] }
Generates an embedding vector from text using the configured AI provider.
| Syntax | ai.embed(<text>) | ||
| Arguments | Name | Type | Description |
<text> | STRING | The text to generate an embedding for | |
| Return Type | VECTOR | ||
An AI provider must be configured with ai.setapikey() before using this function.
GQLLET embedding = ai.embed("Introduction to graph databases") RETURN embedding, ai.dimension(embedding) AS dimensions
Result:
JSON{ "embedding": { "values": [ -0.0258026123046875, -0.0126800537109375, … 0.0162200927734375, -0.017486572265625 ] }, "dimensions": 1536 }
Computes cosine similarity between two vectors. Returns a value between -1 and 1, where 1 means identical direction.
| Syntax | ai.cosine(<vector1>, <vector2>) | ||
| Arguments | Name | Type | Description |
<vector1> | VECTOR | The first vector | |
<vector2> | VECTOR | The second vector; must have the same dimension as <vector1> | |
| Return Type | FLOAT | ||
GQLLET v1 = ai.vector([1.0, 0.0, 0.0]) LET v2 = ai.vector([1.0, 1.0, 0.0]) RETURN ai.cosine(v1, v2)
Result: 0.7071067690849304
Computes Euclidean distance between two vectors. Lower values indicate more similarity.
| Syntax | ai.euclidean(<vector1>, <vector2>) | ||
| Arguments | Name | Type | Description |
<vector1> | VECTOR | The first vector | |
<vector2> | VECTOR | The second vector; must have the same dimension as <vector1> | |
| Return Type | FLOAT | ||
GQLLET v1 = ai.vector([1.0, 0.0]) LET v2 = ai.vector([0.0, 1.0]) RETURN ai.euclidean(v1, v2)
Result: 1.4142135381698608
Computes the dot product of two vectors.
| Syntax | ai.dot(<vector1>, <vector2>) | ||
| Arguments | Name | Type | Description |
<vector1> | VECTOR | The first vector | |
<vector2> | VECTOR | The second vector; must have the same dimension as <vector1> | |
| Return Type | FLOAT | ||
GQLLET v1 = ai.vector([1.0, 2.0, 3.0]) LET v2 = ai.vector([4.0, 5.0, 6.0]) RETURN ai.dot(v1, v2)
Result: 32
Computes the cosine distance between two vectors (1 - cosine similarity). Lower values indicate more similarity.
| Syntax | ai.distance(<vector1>, <vector2>) | ||
| Arguments | Name | Type | Description |
<vector1> | VECTOR | The first vector | |
<vector2> | VECTOR | The second vector; must have the same dimension as <vector1> | |
| Return Type | FLOAT | ||
GQLLET v1 = ai.vector([1.0, 0.0, 0.0]) LET v2 = ai.vector([1.0, 1.0, 0.0]) RETURN ai.distance(v1, v2)
Result: 0.2928932309150696
Returns the number of dimensions in a vector.
| Syntax | ai.dimension(<vector>) | ||
| Arguments | Name | Type | Description |
<vector> | VECTOR | A vector value | |
| Return Type | INT | ||
GQLLET v = ai.vector([3.0, 4.0]) RETURN ai.dimension(v)
Result: 2
Returns the magnitude (L2 norm) of a vector.
| Syntax | ai.magnitude(<vector>) | ||
| Arguments | Name | Type | Description |
<vector> | VECTOR | A vector value | |
| Return Type | FLOAT | ||
GQLLET v = ai.vector([3.0, 4.0]) RETURN ai.magnitude(v)
Result: 5
Normalizes a vector to a unit vector (magnitude of 1).
| Syntax | ai.normalize(<vector>) | ||
| Arguments | Name | Type | Description |
<vector> | VECTOR | A vector value | |
| Return Type | VECTOR | ||
GQLLET v = ai.vector([3.0, 4.0]) RETURN ai.normalize(v)
Result:
JSON{ "values": [ 0.6000000238418579, 0.800000011920929 ] }
Converts a vector to a list of numbers.
| Syntax | ai.toList(<vector>) | ||
| Arguments | Name | Type | Description |
<vector> | VECTOR | A vector value | |
| Return Type | LIST | ||
GQLLET embedding = ai.embed("Introduction to graph databases") RETURN ai.toList(embedding)
Result: [-0.0258026123046875, -0.0126800537109375, …, 0.0162200927734375, -0.017486572265625]
Adds two vectors element-wise.
| Syntax | ai.add(<vector1>, <vector2>) | ||
| Arguments | Name | Type | Description |
<vector1> | VECTOR | The first vector | |
<vector2> | VECTOR | The second vector; must have the same dimension as <vector1> | |
| Return Type | VECTOR | ||
GQLLET v1 = ai.vector([1.0, 2.0]) LET v2 = ai.vector([3.0, 4.0]) RETURN ai.toList(ai.add(v1, v2))
Result: [4, 6]
Subtracts the second vector from the first element-wise.
| Syntax | ai.subtract(<vector1>, <vector2>) | ||
| Arguments | Name | Type | Description |
<vector1> | VECTOR | The first vector | |
<vector2> | VECTOR | The second vector; must have the same dimension as <vector1> | |
| Return Type | VECTOR | ||
GQLLET v1 = ai.vector([5.0, 3.0]) LET v2 = ai.vector([1.0, 2.0]) RETURN ai.toList(ai.subtract(v1, v2))
Result: [4, 1]
Multiplies a vector by a scalar value.
| Syntax | ai.scale(<vector>, <scalar>) | ||
| Arguments | Name | Type | Description |
<vector> | VECTOR | A vector value | |
<scalar> | Numeric | The scalar multiplier | |
| Return Type | VECTOR | ||
GQLLET v = ai.vector([1.0, 2.0, 3.0]) RETURN ai.toList(ai.scale(v, 2))
Result: [2, 4, 6]
Sets the API key for an AI provider. Optionally activates it as the current provider.
| Syntax | ai.setapikey(<provider>, <apiKey> [, <activate>]) | ||
| Arguments | Name | Type | Description |
<provider> | STRING | Provider name: "openai", "gemini", "xai", or "anthropic" (completion only) | |
<apiKey> | STRING | The API key | |
<activate> | BOOL | Optional. Whether to set this as the active provider (default: true) | |
| Return Type | BOOL | ||
By default, calling ai.setapikey() both sets the key and activates the provider:
GQLRETURN ai.setapikey("openai", "sk-...")
Each provider stores one API key. Calling ai.setapikey() again for the same provider overwrites the previous key. To set keys for multiple providers without activating them, pass false as the third argument, then use ai.setprovider() to switch:
GQLRETURN ai.setapikey("gemini", "AQ.za...", false)
NOTE
"anthropic"supports completion only (ai.gql(),ai.read()), it has no embedding model. Useai.setapikey("anthropic", "sk-ant-...", false)followed byai.setCompletionProvider("anthropic")to configure it for completion.
Switches the active embedding provider. The provider's API key must have been set first via ai.setapikey().
| Syntax | ai.setprovider(<provider>) | ||
| Arguments | Name | Type | Description |
<provider> | STRING | Provider name: "openai", "gemini", or "xai" | |
| Return Type | BOOL | ||
GQLRETURN ai.setprovider("openai")
Returns the name of the current AI provider.
| Syntax | ai.provider() | ||
| Arguments | None | ||
| Return Type | STRING | ||
GQLRETURN ai.provider()
Returns the embedding dimension of the current provider.
| Syntax | ai.embeddim() | ||
| Arguments | None | ||
| Return Type | INT | ||
GQLRETURN ai.embeddim()