Skip to content

Commit 472f676

Browse files
committed
Custom function documentation: enable cache, links to examples.
1 parent 8fc3c7b commit 472f676

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

docs/docs/core/custom_function.mdx

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ A function executor is defined as a class annotated by `@cocoindex.op.executor_c
5656

5757

5858
```python
59-
@cocoindex.op.executor_class()
59+
@cocoindex.op.executor_class(...)
6060
class DemoFunctionExecutor:
6161
spec: DemoFunctionSpec
6262
...
@@ -70,12 +70,39 @@ class DemoFunctionExecutor:
7070

7171
Notes:
7272

73-
* The `cocoindex.op.executor_class()` class also takes the following input:
73+
* The `cocoindex.op.executor_class()` class decorator also takes the following optional arguments:
74+
7475
* `gpu: bool`: Whether the executor will use GPU. It will affect the way the function is scheduled.
76+
77+
* `cache: bool`: Whether the executor will enable cache for this function.
78+
When `True`, the executor will cache the result of the function for reuse during reprocessing.
79+
We recommend to set this to `True` for any function that is computationally intensive.
80+
81+
* `behavior_version: int`: The version of the behavior of the function.
82+
When the version is changed, the function will be re-executed even if cache is enabled.
83+
It's required to be set if `cache` is `True`.
84+
85+
For example, this enables cache for the function:
86+
87+
```python
88+
@cocoindex.op.executor_class(cache=True, behavior_version=1)
89+
class DemoFunctionExecutor:
90+
...
91+
```
92+
7593
* A `spec` field must be present in the class, and must be annoated with the spec class name.
7694
* The `prepare()` method is optional. It's executed once and only once before any `__call__` execution, to prepare the function execution.
7795
* The `__call__()` method is required. It's executed for each specific rows of data.
7896
Types of arugments and the return value must be annotated, so that CocoIndex will have information about data types of the operation's output fields. See [Data Types](/docs/core/data_types) for supported types.
7997

8098
</TabItem>
81-
</Tabs>
99+
</Tabs>
100+
101+
## Examples
102+
103+
The cocoindex repository contains the following examples of custom functions:
104+
105+
* In the [pdf_embedding](https://github.com/cocoindex-io/cocoindex/blob/main/examples/pdf_embedding/pdf_embedding.py) example, we define a custom function `PdfToMarkdown`
106+
* The `SentenceTransformerEmbed` function shipped with the CocoIndex Python package is defined by Python SDK.
107+
Search for [`SentenceTransformerEmbedExecutor`](https://github.com/search?q=repo%3Acocoindex-io%2Fcocoindex+SentenceTransformerEmbedExecutor&type=code) to see the code.
108+

0 commit comments

Comments
 (0)