You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*The `cocoindex.op.function()` function decorator also takes optional parameters.
34
+
* The `cocoindex.op.function()` function decorator also takes optional parameters.
35
35
See [Parameters for custom functions](#parameters-for-custom-functions) for details.
36
-
*Types of arguments and the return value must be annotated, so that CocoIndex will have information about data types of the operation's output fields.
36
+
* Types of arguments and the return value must be annotated, so that CocoIndex will have information about data types of the operation's output fields.
37
37
See [Data Types](/docs/core/data_types) for supported types.
38
38
39
39
</TabItem>
@@ -43,12 +43,11 @@ Notes:
43
43
44
44
The cocoindex repository contains the following examples of custom functions defined in this way:
45
45
46
-
*In the [code_embedding](https://github.com/cocoindex-io/cocoindex/blob/main/examples/code_embedding/main.py) example,
46
+
* In the [code_embedding](https://github.com/cocoindex-io/cocoindex/blob/main/examples/code_embedding/main.py) example,
47
47
`extract_extension` is a custom function to extract the extension of a file name.
48
-
*In the [manuals_llm_extraction](https://github.com/cocoindex-io/cocoindex/blob/main/examples/manuals_llm_extraction/main.py) example,
48
+
* In the [manuals_llm_extraction](https://github.com/cocoindex-io/cocoindex/blob/main/examples/manuals_llm_extraction/main.py) example,
49
49
`summarize_manuals` is a custom function to summarize structured information of a manual page.
50
50
51
-
52
51
## Option 2: By a function spec and an executor
53
52
54
53
This is more advanced and flexible way to define a custom function.
@@ -75,30 +74,29 @@ class ComputeSomething(cocoindex.op.FunctionSpec):
75
74
```
76
75
77
76
Notes:
78
-
* All fields of the spec must have a type serializable / deserializable by the `json` module.
79
-
* All subclasses of `FunctionSpec` can be instantiated similar to a dataclass, i.e. `ClassName(param1=value1, param2=value2, ...)`.
77
+
78
+
* All fields of the spec must have a type serializable / deserializable by the `json` module.
79
+
* All subclasses of `FunctionSpec` can be instantiated similar to a dataclass, i.e. `ClassName(param1=value1, param2=value2, ...)`.
80
80
81
81
</TabItem>
82
82
</Tabs>
83
83
84
-
85
84
### Function Executor
86
85
87
86
A function executor defines behavior of a function. It's instantiated for each operation that uses this function.
88
87
89
88
The function executor is responsible for:
90
89
91
-
**Prepare* for the function execution, based on the spec.
90
+
**Prepare* for the function execution, based on the spec.
92
91
It happens once and only once before execution.
93
92
e.g. if the function calls a machine learning model, the model name can be a parameter as a field of the spec, and we may load the model in this phase.
94
-
**Run* the function, for each specific input arguments. This happens multiple times, for each specific row of data.
93
+
**Run* the function, for each specific input arguments. This happens multiple times, for each specific row of data.
95
94
96
95
<Tabs>
97
96
<TabItemvalue="python"label="Python"default>
98
97
99
98
A function executor is defined as a class decorated by `@cocoindex.op.executor_class()`.
100
99
101
-
102
100
```python
103
101
@cocoindex.op.executor_class(...)
104
102
classComputeSomethingExecutor:
@@ -114,12 +112,12 @@ class ComputeSomethingExecutor:
114
112
115
113
Notes:
116
114
117
-
*The `cocoindex.op.executor_class()` class decorator also takes optional parameters.
115
+
* The `cocoindex.op.executor_class()` class decorator also takes optional parameters.
118
116
See [Parameters for custom functions](#parameters-for-custom-functions) for details.
119
117
120
-
*A `spec` field must be present in the class, and must be annotated with the spec class name.
121
-
*The `prepare()` method is optional. It's executed once and only once before any `__call__` execution, to prepare the function execution.
122
-
*The `__call__()` method is required. It's executed for each specific rows of data.
118
+
* A `spec` field must be present in the class, and must be annotated with the spec class name.
119
+
* The `prepare()` method is optional. It's executed once and only once before any `__call__` execution, to prepare the function execution.
120
+
* The `__call__()` method is required. It's executed for each specific rows of data.
123
121
Types of arugments and the return value must be decorated, so that CocoIndex will have information about data types of the operation's output fields.
124
122
See [Data Types](/docs/core/data_types) for supported types.
125
123
@@ -130,34 +128,37 @@ Notes:
130
128
131
129
The cocoindex repository contains the following examples of custom functions defined in this way:
132
130
133
-
*In the [pdf_embedding](https://github.com/cocoindex-io/cocoindex/blob/main/examples/pdf_embedding/main.py) example, we define a custom function `PdfToMarkdown`
134
-
*The `SentenceTransformerEmbed` function shipped with the CocoIndex Python package is defined by Python SDK.
131
+
* In the [pdf_embedding](https://github.com/cocoindex-io/cocoindex/blob/main/examples/pdf_embedding/main.py) example, we define a custom function `PdfToMarkdown`
132
+
* The `SentenceTransformerEmbed` function shipped with the CocoIndex Python package is defined by Python SDK.
135
133
Search for [`SentenceTransformerEmbedExecutor`](https://github.com/search?q=repo%3Acocoindex-io%2Fcocoindex+lang%3Apython+SentenceTransformerEmbedExecutor&type=code) to see the code.
136
134
137
135
## Parameters for custom functions
138
136
139
137
Custom functions take the following additional parameters:
140
138
141
-
*`gpu: bool`: Whether the executor will use GPU. It will affect the way the function is scheduled.
139
+
*`gpu: bool`: Whether the executor will use GPU. It will affect the way the function is scheduled.
142
140
143
-
*`cache: bool`: Whether the executor will enable cache for this function.
141
+
*`cache: bool`: Whether the executor will enable cache for this function.
144
142
When `True`, the executor will cache the result of the function for reuse during reprocessing.
145
143
We recommend to set this to `True` for any function that is computationally intensive.
146
144
147
-
*`behavior_version: int`: The version of the behavior of the function.
145
+
*`batching: bool`: Whether the executor will consume requests in batch.
146
+
See the [Batching](#batching) section below for details.
147
+
148
+
*`behavior_version: int`: The version of the behavior of the function.
148
149
When the version is changed, the function will be re-executed even if cache is enabled.
149
150
It's required to be set if `cache` is `True`.
150
151
151
-
*`arg_relationship: tuple[ArgRelationship, str]`: It specifies the relationship between an input argument and the output,
152
+
*`arg_relationship: tuple[ArgRelationship, str]`: It specifies the relationship between an input argument and the output,
152
153
e.g. `(ArgRelationship.CHUNKS_BASE_TEXT, "content")` means the output is chunks for the text represented by the
153
154
input argument with name `content`.
154
155
This provides metadata for tools, e.g. CocoInsight.
155
156
Currently the following attributes are supported:
156
157
157
-
*`ArgRelationship.CHUNKS_BASE_TEXT`:
158
+
*`ArgRelationship.CHUNKS_BASE_TEXT`:
158
159
The output is chunks for the text represented by the input argument. In this case, the output is expected to be a *Table*, whose each row represents a text chunk, and the first column has type *Range*, representing the range of the text chunk.
159
-
*`ArgRelationship.EMBEDDING_ORIGIN_TEXT`: The output is embedding vector for the text represented by the input argument. The output is expected to be a *Vector*.
160
-
*`ArgRelationship.RECTS_BASE_IMAGE`: The output is rectangles for the image represented by the input argument. The output is expected to be a *Table*, whose each row represents a rectangle, and the first column has type *Struct*, with fields `min_x`, `min_y`, `max_x`, `max_y` to represent the coordinates of the rectangle.
160
+
*`ArgRelationship.EMBEDDING_ORIGIN_TEXT`: The output is embedding vector for the text represented by the input argument. The output is expected to be a *Vector*.
161
+
*`ArgRelationship.RECTS_BASE_IMAGE`: The output is rectangles for the image represented by the input argument. The output is expected to be a *Table*, whose each row represents a rectangle, and the first column has type *Struct*, with fields `min_x`, `min_y`, `max_x`, `max_y` to represent the coordinates of the rectangle.
161
162
162
163
For example:
163
164
@@ -187,3 +188,38 @@ class ComputeSomethingExecutor:
187
188
188
189
</TabItem>
189
190
</Tabs>
191
+
192
+
## Batching
193
+
194
+
Batching allows a function executor to process multiple function calls in batch.
195
+
Sometimes batching is more efficient than processing them one by one, e.g. running inference on GPU, calling remote APIs with quota limits, etc.
196
+
197
+
Batching can be enabled by setting the `batching` parameter to `True` in custom function parameters.
198
+
Once it's set to `True`, type of the argument and return value must be a `list`.
199
+
Currently we only support batching functions taking a single argument.
200
+
201
+
<Tabs>
202
+
<TabItemvalue="python"label="Python"default>
203
+
204
+
For example, for a CocoIndex custom function taking `str` as argument and returning `str` as result, it can be defined as:
0 commit comments