Kotlin implementation of creating OpenAI instance and Assistant

Hi,
I’ve been mainly working on the GUI side of things but now porting code into my software. I created an instance of creating and openAI instance and creating a assistant if its not created yet. I haven’t gotten much futher but looking for feedback and maybe it can benefit someone,

import com.aallam.openai.api.BetaOpenAI
import com.aallam.openai.api.assistant.Assistant
import com.aallam.openai.api.assistant.AssistantRequest
import com.aallam.openai.api.assistant.AssistantTool
import com.aallam.openai.api.http.Timeout
import com.aallam.openai.api.model.ModelId
import com.aallam.openai.client.OpenAI
import com.aallam.openai.client.OpenAIConfig
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import kotlin.time.Duration.Companion.seconds

@Configuration
@OptIn(BetaOpenAI::class)
class OpenAIConfig {

    @Value("\${openai.api.token}")
    private lateinit var openAiApiToken: String

    companion object {
        const val ASSISTANT_NAME = "Slappy"
    }

    /**
     * Creates our openAI client
     */
    @Bean
    fun openAIClient(): OpenAI {
        try {
            val config = OpenAIConfig(token = openAiApiToken, timeout = Timeout(socket = 60.seconds))
            return OpenAI(config)
        } catch (e: Exception) {
            e.printStackTrace()
            throw Exception("Error with OpenAI")
        }
    }

    /**
     * Finds the assistant or creates a new one
     */
    suspend fun getOrCreateAssistant(): Assistant {
        val openAi = openAIClient()

        // Placeholder for assistant check logic
        val existingAssistant = checkForExistingAssistant(ASSISTANT_NAME)
        if (existingAssistant != null) {
            return existingAssistant
        }

        val files = openAi.files(null) // Assuming this is a blocking I/O operation
        val eventsFile = files.find { it.filename == "events.jsonl" }

        // If no existing assistant found, create a new one
        return openAi.assistant(
            request = AssistantRequest(
                name = ASSISTANT_NAME,
                instructions = "You are a helpful social concierge suggesting plans for where when and with who to plan and do events",
                tools = listOf(AssistantTool.CodeInterpreter, AssistantTool.RetrievalTool),
                model = ModelId("gpt-4-0125-preview"),
                fileIds = listOf(eventsFile!!.id)
            )
        )
    }

    // Placeholder function for checking if an assistant already exists
    suspend fun checkForExistingAssistant(name: String): Assistant? {
        return openAIClient().assistants(null).find { assistant ->
            assistant.name.equals(name, ignoreCase = true)
        }
    }
}
I'm using this library as a helper. I could use some help on prompts as well giving examples. https://github.com/aallam/openai-kotlin/tree/main?tab=readme-ov-file
1 Like