Skip to content

If I try to stop generate completion and cancel the coroutine job, CactusLM continues generating tokens until it's finished #16

@dmitriy-chernysh

Description

@dmitriy-chernysh

Am I doing something wrong? Is there any other way to stop the process of generation? Thanks

Library version: 1.0.2-beta
Android OS. No matter what Android version I use.

private suspend fun generateCompletionCancellable(
        messages: List<ChatMessage>,
        params: CactusCompletionParams,
        onToken: (String) -> Unit
    ): CactusCompletionResult? = suspendCancellableCoroutine { cont ->

        val workerJob = SupervisorJob()
        val workerScope = CoroutineScope(Dispatchers.IO + workerJob)

        workerScope.launch {
            try {
                cactusLM.generateCompletion(
                    messages = messages,
                    params = params,
                    onToken = { token, _ ->
                        Log.d("debug", "cactusLM token: $token")
                        if (!isActive) return@generateCompletion
                        onToken(token)
                    }
                )
                try {
                    cont.resume(result)
                } catch (_: Throwable) {
                }
            } catch (t: Throwable) {
                try {
                    cont.resumeWithException(t)
                } catch (_: Throwable) {
                }
            }
        }

        cont.invokeOnCancellation {
            workerJob.cancel()
            Log.d("debug", "cactusLM token: cancel the job")
        }
    }

From logs

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions