#!/usr/bin/env groovy
// PUBLIC DOMAIN - NO LICENSE, NO WARRANTY
// Copyright 2025 TimeHexOn & foxhop & russell@unturf
// https://www.permacomputer.com


// Qwen 3 Coder Streaming Example using OpenAI Java Client

@Grapes([
    @Grab(group='com.openai', module='openai-java', version='4.2.0')
])

import com.openai.client.OpenAIClient
import com.openai.client.okhttp.OpenAIOkHttpClient
import com.openai.models.ChatCompletionChunk
import com.openai.models.ChatCompletionCreateParams
import com.openai.models.ChatCompletionUserMessageParam

def baseUrl = "https://qwen.ai.unturf.com/v1"
def model = "hf.co/unsloth/Qwen3-Coder-30B-A3B-Instruct-GGUF:Q4_K_M"

try {
    println "Streaming from Qwen 3 Coder...\n"

    // Create OpenAI client with custom base URL
    def client = OpenAIOkHttpClient.builder()
        .baseUrl(baseUrl)
        .apiKey("dummy-api-key")
        .build()

    // Create streaming chat completion request
    def params = ChatCompletionCreateParams.builder()
        .model(model)
        .addMessage(ChatCompletionUserMessageParam.builder()
            .content("Give a Python Fizzbuzz solution in one line of code?")
            .build())
        .temperature(0.5)
        .maxTokens(150)
        .stream(true)
        .build()

    // Stream the response
    def stream = client.chat().completions().createStreaming(params)

    stream.forEach { chunk ->
        def delta = chunk.choices()[0].delta()
        if (delta.content().isPresent()) {
            print delta.content().get()
            System.out.flush()
        }
    }

    println "\n\nDone!"

} catch (Exception e) {
    println "\nError: ${e.message}"
    e.printStackTrace()
}
