(options: {
/**
* A method which processes a batch of text inputs and returns embedding
* results.
*/
readonly embedMany: (input: ReadonlyArray<string>) => Effect.Effect<Array<Result>, AiError.AiError>
/**
* Optional maximum number of text inputs to process in one batch.
*/
readonly maxBatchSize?: number
/**
* Optional configuration to control how batch request results are cached.
*/
readonly cache?: {
/**
* The capacity of the cache.
*/
readonly capacity: number
/**
* The time-to-live for items in the cache.
*/
readonly timeToLive: Duration.DurationInput
}
})
| 197 | * @category Constructors |
| 198 | */ |
| 199 | export const make = (options: { |
| 200 | /** |
| 201 | * A method which processes a batch of text inputs and returns embedding |
| 202 | * results. |
| 203 | */ |
| 204 | readonly embedMany: (input: ReadonlyArray<string>) => Effect.Effect<Array<Result>, AiError.AiError> |
| 205 | /** |
| 206 | * Optional maximum number of text inputs to process in one batch. |
| 207 | */ |
| 208 | readonly maxBatchSize?: number |
| 209 | /** |
| 210 | * Optional configuration to control how batch request results are cached. |
| 211 | */ |
| 212 | readonly cache?: { |
| 213 | /** |
| 214 | * The capacity of the cache. |
| 215 | */ |
| 216 | readonly capacity: number |
| 217 | /** |
| 218 | * The time-to-live for items in the cache. |
| 219 | */ |
| 220 | readonly timeToLive: Duration.DurationInput |
| 221 | } |
| 222 | }) => |
| 223 | Effect.gen(function*() { |
| 224 | const cache = yield* Option.fromNullable(options.cache).pipe( |
| 225 | Effect.flatMap((config) => Request.makeCache(config)), |
| 226 | Effect.optionFromOptional |
| 227 | ) |
| 228 | |
| 229 | const resolver = makeBatchedResolver(options.embedMany).pipe( |
| 230 | options.maxBatchSize ? RequestResolver.batchN(options.maxBatchSize) : identity |
| 231 | ) |
| 232 | |
| 233 | const embed = (input: string) => { |
| 234 | const request = Effect.request(new EmbeddingRequest({ input }), resolver) |
| 235 | return Option.match(cache, { |
| 236 | onNone: () => request, |
| 237 | onSome: (cache) => |
| 238 | request.pipe( |
| 239 | Effect.withRequestCaching(true), |
| 240 | Effect.withRequestCache(cache) |
| 241 | ) |
| 242 | }) |
| 243 | } |
| 244 | |
| 245 | const embedMany = (inputs: ReadonlyArray<string>, options?: { |
| 246 | readonly concurrency?: Types.Concurrency | undefined |
| 247 | }) => |
| 248 | Effect.forEach(inputs, embed, { |
| 249 | batching: true, |
| 250 | concurrency: options?.concurrency |
| 251 | }) |
| 252 | |
| 253 | return EmbeddingModel.of({ |
| 254 | embed: (input) => |
| 255 | embed(input).pipe( |
| 256 | Effect.withSpan("EmbeddingModel.embed", { captureStackTrace: false }) |
nothing calls this directly
no test coverage detected