Android SDK
Streaming
Stream AI responses in real-time
Streaming
Basic Streaming
val stream = ProxyKit.openai.chat.completions.stream(
model = "gpt-4o",
messages = listOf(ChatMessage.user("Write a story"))
)
stream.collect { chunk ->
chunk.choices.firstOrNull()?.delta?.content?.let {
print(it) // Print each chunk
}
}
With SecureProxy
val assistant = SecureProxy(model = ChatModel.gpt4o)
val stream = assistant.streamResponse("Tell me a story")
stream.collect { chunk ->
print(chunk)
}
Compose with Streaming
@Composable
fun StreamingChat() {
var text by remember { mutableStateOf("") }
val scope = rememberCoroutineScope()
Button(onClick = {
text = ""
scope.launch {
val stream = ProxyKit.openai.chat.completions.stream(
model = "gpt-4o",
messages = listOf(ChatMessage.user("Write a poem"))
)
stream.collect { chunk ->
chunk.choices.firstOrNull()?.delta?.content?.let {
text += it
}
}
}
}) {
Text("Start Stream")
}
Text(text)
}