EmbeddingFunction class abstract interface

Interface for custom embedding generation.

Implement this interface to provide your own embedding generation logic, for example using OpenAI, Cohere, or local models.

Example:

class OpenAIEmbeddingFunction implements EmbeddingFunction {
  final OpenAIClient client;
  final String model;

  OpenAIEmbeddingFunction(this.client, {this.model = 'text-embedding-3-small'});

  @override
  Future<List<List<double>>> generate(List<Embeddable> inputs) async {
    final texts = inputs.map((e) => switch (e) {
      EmbeddableDocument(:final document) => document,
      EmbeddableImage(:final image) => image,
    }).toList();

    final response = await client.embeddings.create(
      model: model,
      input: texts,
    );
    return response.data.map((e) => e.embedding).toList();
  }
}

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

generate(List<Embeddable> inputs) Future<List<List<double>>>
Generates embeddings for the given inputs.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited