openaivec.util
TextChunker
dataclass
Utility for splitting text into token‑bounded chunks.
Source code in src/openaivec/util.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
|
split(original, max_tokens, sep)
Token‑aware sentence segmentation.
The text is first split by the given separators, then greedily packed
into chunks whose token counts do not exceed max_tokens
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
original
|
str
|
Original text to split. |
required |
max_tokens
|
int
|
Maximum number of tokens allowed per chunk. |
required |
sep
|
List[str]
|
List of separator patterns used by
:pyfunc: |
required |
Returns:
Type | Description |
---|---|
List[str]
|
List[str]: List of text chunks respecting the |
Source code in src/openaivec/util.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
|
backoff(exception, scale=None, max_retries=None)
Decorator implementing exponential back‑off retry logic.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
exception
|
Exception
|
Exception type that triggers a retry. |
required |
scale
|
int | None
|
Initial scale parameter for the exponential jitter.
This scale is used as the mean for the first delay's exponential
distribution and doubles with each subsequent retry. If |
None
|
max_retries
|
Optional[int]
|
Maximum number of retries. |
None
|
Returns:
Type | Description |
---|---|
Callable[..., V]
|
Callable[..., V]: A decorated function that retries on the specified exception with exponential back‑off. |
Raises:
Type | Description |
---|---|
exception
|
Re‑raised when the maximum number of retries is exceeded. |
Source code in src/openaivec/util.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
|
backoff_async(exception, scale=None, max_retries=None)
Asynchronous version of the backoff decorator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
exception
|
Exception
|
Exception type that triggers a retry. |
required |
scale
|
int | None
|
Initial scale parameter for the exponential jitter.
This scale is used as the mean for the first delay's exponential
distribution and doubles with each subsequent retry. If |
None
|
max_retries
|
int | None
|
Maximum number of retries. |
None
|
Returns:
Type | Description |
---|---|
Callable[..., Awaitable[V]]
|
Callable[..., Awaitable[V]]: A decorated asynchronous function that retries on the specified exception with exponential back‑off. |
Raises:
Type | Description |
---|---|
exception
|
Re‑raised when the maximum number of retries is exceeded. |
Source code in src/openaivec/util.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
|
get_exponential_with_cutoff(scale)
Sample an exponential random variable with an upper cutoff.
A value is repeatedly drawn from an exponential distribution with rate
1/scale
until it is smaller than 3 * scale
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scale
|
float
|
Scale parameter of the exponential distribution. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
Sampled value bounded by |
Source code in src/openaivec/util.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
map(inputs, f, batch_size=128)
Map a function f
over a list of inputs in batches.
This function divides the input list into smaller batches and applies the
function f
to each batch. It gathers the results and returns them in the
same order as the original inputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs
|
List[T]
|
List of inputs to be processed. |
required |
f
|
Callable[[List[T]], List[U]]
|
Function to apply. It takes a batch of inputs (List[T]) and must return a list of corresponding outputs (List[U]) of the same size. |
required |
batch_size
|
int
|
Size of each batch for processing. |
128
|
Returns:
Type | Description |
---|---|
List[U]
|
List[U]: List of outputs corresponding to the original inputs, in order. |
Source code in src/openaivec/util.py
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
|
map_async(inputs, f, batch_size=128)
async
Asynchronously map a function f
over a list of inputs in batches.
This function divides the input list into smaller batches and applies the
asynchronous function f
to each batch concurrently. It gathers the results
and returns them in the same order as the original inputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs
|
List[T]
|
List of inputs to be processed. |
required |
f
|
Callable[[List[T]], Awaitable[List[U]]]
|
Asynchronous function to apply. It takes a batch of inputs (List[T]) and must return a list of corresponding outputs (List[U]) of the same size. |
required |
batch_size
|
int
|
Size of each batch for processing. |
128
|
Returns:
Type | Description |
---|---|
List[U]
|
List[U]: List of outputs corresponding to the original inputs, in order. |
Source code in src/openaivec/util.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
|