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


// Hermes AI Streaming Example using OpenAI Java Client
// Demonstrates real-time streaming responses

@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://hermes.ai.unturf.com/v1"
def model = "adamo1139/Hermes-3-Llama-3.1-8B-FP8-Dynamic"

try {
    println "Streaming from Hermes AI...\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()
}
