/** * Kotlin OpenAI Agent — Bindufied * * Demonstrates using the Bindu Kotlin SDK with an OpenAI-compatible API. * The developer writes their agent logic in Kotlin — Bindu handles * the conversion to a microservice with DID, auth, x402, and A2A protocol. * * Usage: * 1. Set OPENAI_API_KEY in environment * 2. ./gradlew run */import com.getbindu.sdk.ChatMessageimport com.getbindu.sdk.bindufyimport java.net.URIimport java.net.http.HttpClientimport java.net.http.HttpRequestimport java.net.http.HttpResponseimport com.google.gson.Gsonimport com.google.gson.JsonObjectval httpClient: HttpClient = HttpClient.newHttpClient()val gson = Gson()suspend fun callOpenAI(messages: List<ChatMessage>): String { val apiKey = System.getenv("OPENAI_API_KEY") ?: throw RuntimeException("OPENAI_API_KEY not set") val messagesJson = messages.map { msg -> mapOf("role" to msg.role, "content" to msg.content) } val body = gson.toJson(mapOf( "model" to "gpt-4o", "messages" to messagesJson )) val request = HttpRequest.newBuilder() .uri(URI.create("https://api.openai.com/v1/chat/completions")) .header("Content-Type", "application/json") .header("Authorization", "Bearer $apiKey") .POST(HttpRequest.BodyPublishers.ofString(body)) .build() val response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()) val json = gson.fromJson(response.body(), JsonObject::class.java) return json .getAsJsonArray("choices") .get(0).asJsonObject .getAsJsonObject("message") .get("content").asString}fun main() { bindufy( config = mapOf( "author" to "dev@example.com", "name" to "kotlin-openai-agent", "description" to "An assistant built with Kotlin and Bindu", "version" to "1.0.0", "deployment" to mapOf( "url" to "http://localhost:3773", "expose" to true, "cors_origins" to listOf("http://localhost:5173") ), ), skills = listOf("skills/kotlin-openai-skill") ) { messages -> // Call OpenAI and return the response callOpenAI(messages) }}
# Clone the Bindu repositorygit clone https://github.com/GetBindu/Bindu# Navigate to frontend directorycd frontend# Install dependenciesnpm install# Start frontend development servernpm run dev