> ## Documentation Index
> Fetch the complete documentation index at: https://docs.getbindu.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 3.4 Kotlin OpenAI Agent

> Kotlin agent that uses OpenAI API with Bindu SDK

Kotlin agent that uses OpenAI API with Bindu SDK.

## Code

```kotlin theme={null}
/**
 * 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.ChatMessage
import com.getbindu.sdk.bindufy
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import com.google.gson.Gson
import com.google.gson.JsonObject

val 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)
    }
}
```

## Skill Configuration

Create `skills/kotlin-openai-skill/skill.yaml`:

```yaml theme={null}
# Kotlin OpenAI Agent Skill
# Kotlin-based AI agent using OpenAI API integration

id: kotlin-openai-skill
name: Kotlin OpenAI Agent Skill
version: 1.0.0
author: kotlin-developer@example.com

description: |
  Kotlin agent that demonstrates Bindu SDK integration with OpenAI API.
  Built with modern Kotlin practices and coroutines for async operations.

  Features:
  - Direct OpenAI API integration
  - Kotlin coroutines for async operations
  - Type-safe message handling
  - JSON-RPC 2.0 protocol compliance
  - CORS-enabled for frontend integration

tags:
  - kotlin
  - openai
  - api-integration
  - async
  - coroutines
  - bindu-sdk
  - microservice

input_modes:
  - application/json
output_modes:
  - application/json

examples:
  - "Explain the benefits of using Kotlin for backend development"
  - "What are the key features of Kotlin coroutines?"
  - "How do I integrate OpenAI API with Kotlin applications?"
  - "Create a Kotlin function that processes user input"
  - "What are the best practices for async programming in Kotlin?"

capabilities_detail:
  openai_integration:
    supported: true
    description: "Direct integration with OpenAI GPT models"
    models_supported: ["gpt-4o", "gpt-4o-mini"]
  
  async_operations:
    supported: true
    description: "Kotlin coroutines for non-blocking operations"
    features:
      - suspend_functions
      - structured_concurrency
      - exception_handling

  bindu_sdk:
    supported: true
    description: "Full Bindu SDK integration for protocol compliance"
    features:
      - did_support
      - x402_payments
      - a2a_protocol
      - cors_enabled

  type_safety:
    supported: true
    description: "Kotlin's type system for safe API interactions"
    features:
      - null_safety
      - type_inference
      - data_classes

requirements:
  packages:
    - "com.getbindu:sdk:kotlin"
    - "com.google.code.gson:gson:2.10.1"
  system:
    - java_17_or_higher
    - gradle_7_or_higher
  api_keys:
    - OPENAI_API_KEY

performance:
  avg_response_time_ms: 2000
  max_concurrent_requests: 10
  memory_usage_mb: 128
  scalability: vertical

assessment:
  keywords:
    - kotlin
    - openai
    - api
    - async
    - coroutines
    - bindu
    - microservice
    - gradle

  specializations:
    - domain: kotlin-development
      confidence_boost: 0.4
    - domain: openai-integration
      confidence_boost: 0.5
    - domain: async-programming
      confidence_boost: 0.3

  anti_patterns:
    - "blocking operations"
    - "callback hell"
    - "manual thread management"
    - "unsafe type casting"

  complexity_indicators:
    simple:
      - "explain"
      - "describe"
      - "what is"
    medium:
      - "how to integrate"
      - "best practices for"
    complex:
      - "create function that"
      - "implement system that"
      - "design architecture for"
```

## How It Works

**Kotlin SDK Integration**

* `bindufy()` function wraps Kotlin logic as Bindu agent
* Automatic microservice generation with DID and auth
* A2A protocol compliance handled by SDK
* Language-agnostic agent development

**OpenAI API Communication**

* `HttpClient` makes direct API calls to OpenAI
* JSON message formatting with Gson
* Bearer token authentication
* Response parsing and content extraction

**Message Processing**

* `ChatMessage` objects for conversation history
* Role-based message formatting (user/assistant)
* Suspended function for async API calls
* Direct content return to Bindu

## Dependencies

```bash theme={null}
# Create new Kotlin project
gradle init --type kotlin-application

# Add dependencies
echo 'dependencies {
    implementation("com.getbindu:sdk:kotlin")
    implementation("com.google.code.gson:gson:2.10.1")
}' >> build.gradle.kts

# Or use existing project with Gradle
```

## Environment Setup

Set your OpenAI API key:

```bash theme={null}
export OPENAI_API_KEY=your_openai_api_key_here
```

## Build Requirements

Add to `build.gradle.kts`:

```kotlin theme={null}
dependencies {
    implementation("com.getbindu:sdk:kotlin")
    implementation("com.google.code.gson:gson:2.10.1")
}
```

## Run

```bash theme={null}
./gradlew run
```

**Examples:**

* "Explain the benefits of using Kotlin for backend development"
* "What are the key features of Kotlin coroutines?"
* "How do I integrate OpenAI API with Kotlin applications?"

## Example API Calls

<AccordionGroup>
  <Accordion title="Message Send Request">
    ```json theme={null}
    {
      "jsonrpc": "2.0",
      "method": "message/send",
      "params": {
        "message": {
          "role": "user",
          "kind": "message",
          "messageId": "9f11c870-5616-49ad-b187-d93cbb100001",
          "contextId": "9f11c870-5616-49ad-b187-d93cbb100002",
          "taskId": "9f11c870-5616-49ad-b187-d93cbb100003",
          "parts": [
            {
              "kind": "text",
              "text": "Explain the benefits of using Kotlin for backend development"
            }
          ]
        },
         "skillId": "kotlin-openai-skill",
        "configuration": {
          "acceptedOutputModes": ["application/json"]
        }
      },
      "id": "9f11c870-5616-49ad-b187-d93cbb100003"
    }
    ```
  </Accordion>

  <Accordion title="Task get Request">
    ```json theme={null}
    {
      "jsonrpc": "2.0",
      "method": "tasks/get",
      "params": {
        "taskId": "9f11c870-5616-49ad-b187-d93cbb100003"
      },
      "id": "9f11c870-5616-49ad-b187-d93cbb100004"
    }
    ```
  </Accordion>
</AccordionGroup>

## Frontend Setup

```bash theme={null}
# Clone the Bindu repository
git clone https://github.com/GetBindu/Bindu

# Navigate to frontend directory
cd frontend

# Install dependencies
npm install

# Start frontend development server
npm run dev
```

Open [http://localhost:5173](http://localhost:5173) and try to chat with the Kotlin OpenAI agent
