LangChaingo API - Kotlin SDK Documentation

BosBase exposes the /api/langchaingo endpoints so you can run LangChainGo powered workflows without leaving the platform. The Kotlin SDK wraps these endpoints with the pb.langchaingo service.

The service exposes two high-level methods:

Method HTTP Endpoint Description
pb.langchaingo.completions() POST /api/langchaingo/completions Runs a chat/completion call using the configured LLM provider.
pb.langchaingo.rag() POST /api/langchaingo/rag Runs a retrieval-augmented generation pass over an llmDocuments collection.

📖 Reference: For detailed LangChaingo concepts, see the JavaScript SDK LangChaingo documentation.

Text + Chat Completions

import com.bosbase.sdk.BosBase

val pb = BosBase("http://localhost:8090")

val completion = pb.langchaingo.completions(
    payload = mapOf(
        "model" to mapOf(
            "provider" to "openai",
            "model" to "gpt-4o-mini"
        ),
        "messages" to listOf(
            mapOf("role" to "system", "content" to "Answer in one sentence."),
            mapOf("role" to "user", "content" to "Explain Rayleigh scattering.")
        ),
        "temperature" to 0.2
    )
)

val content = completion?.get("content")?.jsonPrimitive?.contentOrNull
println(content)

Retrieval-Augmented Generation (RAG)

Pair the LangChaingo endpoints with the /api/llm-documents store to build RAG workflows. The backend automatically uses the chromem-go collection configured for the target LLM collection.

val answer = pb.langchaingo.rag(
    payload = mapOf(
        "collection" to "knowledge-base",
        "question" to "Why is the sky blue?",
        "topK" to 4,
        "returnSources" to true,
        "filters" to mapOf(
            "where" to mapOf("topic" to "physics")
        )
    )
)

val answerText = answer?.get("answer")?.jsonPrimitive?.contentOrNull
println(answerText)

val sources = answer?.get("sources")?.jsonArray
sources?.forEach { source ->
    val score = source.jsonObject["score"]?.jsonPrimitive?.doubleOrNull
    val metadata = source.jsonObject["metadata"]?.jsonObject
    val title = metadata?.get("title")?.jsonPrimitive?.contentOrNull
    println("${String.format("%.3f", score)} $title")
}

Custom Prompt Template

Set promptTemplate when you want to control how the retrieved context is stuffed into the answer prompt:

pb.langchaingo.rag(
    payload = mapOf(
        "collection" to "knowledge-base",
        "question" to "Summarize the explanation below in 2 sentences.",
        "promptTemplate" to "Context:\n{{.context}}\n\nQuestion: {{.question}}\nSummary:"
    )
)

Complete Example

fun langchaingoExample(pb: BosBase) {
    // Authenticate as superuser
    pb.admins.authWithPassword("admin@example.com", "password")
    
    // Simple completion
    val completion = pb.langchaingo.completions(
        payload = mapOf(
            "messages" to listOf(
                mapOf("role" to "user", "content" to "Hello!")
            )
        )
    )
    
    println(completion?.get("content")?.jsonPrimitive?.contentOrNull)
    
    // RAG query
    val ragResult = pb.langchaingo.rag(
        payload = mapOf(
            "collection" to "knowledge-base",
            "question" to "What is Kotlin?",
            "topK" to 3
        )
    )
    
    println(ragResult?.get("answer")?.jsonPrimitive?.contentOrNull)
}