completeStream method
Complete text with streaming support
Implementation
Stream<String> completeStream(CompletionRequest request) async* {
final messages = [ChatMessage.user(request.prompt)];
final requestBody = <String, dynamic>{
'model': config.model,
'messages': client.buildApiMessages(messages),
'stream': true,
if (request.maxTokens != null) 'max_tokens': request.maxTokens,
if (request.temperature != null) 'temperature': request.temperature,
if (request.topP != null) 'top_p': request.topP,
if (request.stop != null) 'stop': request.stop,
};
final stream = client.postStreamRaw('chat/completions', requestBody);
await for (final chunk in stream) {
final jsonList = client.parseSSEChunk(chunk);
if (jsonList.isEmpty) continue;
// Process each JSON object in the chunk
for (final json in jsonList) {
final choices = json['choices'] as List?;
if (choices == null || choices.isEmpty) continue;
final choice = choices.first as Map<String, dynamic>;
final delta = choice['delta'] as Map<String, dynamic>?;
if (delta == null) continue;
final content = delta['content'] as String?;
if (content != null && content.isNotEmpty) {
yield content;
}
}
}
}