generateContent method

  1. @override
Stream<String> generateContent(
  1. String sessionId,
  2. String prompt, {
  3. EdgeGenAIGenerationOptions? options,
  4. bool useMemory = false,
  5. Uint8List? image,
  6. List<EdgeGenAITool> tools = const [],
})
override

Sends prompt (and, optionally, image) to the on-device model and streams its generated text.

Each event is the full response text generated so far, not just the newly-added chunk.

sessionId identifies the calling EdgeGenAIPrompt instance so each instance keeps its own isolated conversation. By default, this is a stateless one-off call. Pass useMemory as true to remember (and build on) prior useMemory calls from the same sessionId; use resetConversation to start that remembered conversation over.

tools are functions the model may call while generating; their Dart implementations run in this isolate when it does.

Implementation

@override
Stream<String> generateContent(
  String sessionId,
  String prompt, {
  EdgeGenAIGenerationOptions? options,
  bool useMemory = false,
  Uint8List? image,
  List<EdgeGenAITool> tools = const [],
}) async* {
  if (tools.isEmpty) {
    _toolsBySession.remove(sessionId);
  } else {
    _toolsBySession[sessionId] = {for (final tool in tools) tool.name: tool};
    if (!_toolExecutorRegistered) {
      _toolExecutorRegistered = true;
      EdgeGenAIToolExecutorApi.setUp(_ToolExecutor(_toolsBySession));
    }
  }
  await hostApi.startGenerateContent(
    sessionId,
    prompt,
    options,
    useMemory,
    image,
    [for (final tool in tools) _toDefinition(tool)],
  );
  yield* generateContentChunk();
}