Skip to content

Commit

Permalink
feat(BE-174): integrate anthropic api
Browse files Browse the repository at this point in the history
 - code refactor by using Koin
  • Loading branch information
hanrw committed Mar 16, 2024
1 parent 96928a4 commit eb470f7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package com.tddworks.anthropic.api

import com.tddworks.anthropic.api.internal.AnthropicApi
import com.tddworks.anthropic.api.messages.api.AnthropicConfig
import com.tddworks.anthropic.api.messages.api.Messages
import com.tddworks.anthropic.api.messages.api.internal.DefaultMessagesApi
import com.tddworks.anthropic.di.initKoin
import com.tddworks.common.network.api.ktor.api.HttpRequester
import com.tddworks.common.network.api.ktor.internal.createHttpClient
import com.tddworks.common.network.api.ktor.internal.default
import org.koin.dsl.module

interface Anthropic : Messages {
companion object {
Expand All @@ -14,11 +21,37 @@ interface Anthropic : Messages {
}

fun Anthropic(
apiKey: String = "CONFIGURE_ME",
baseUrl: String = Anthropic.BASE_URL,
anthropicVersion: String = "2023-06-01",
): Anthropic = AnthropicApi(
apiKey = apiKey,
apiURL = baseUrl,
anthropicVersion = anthropicVersion
)
apiKey: () -> String = { "CONFIGURE_ME" },
baseUrl: () -> String = { Anthropic.BASE_URL },
anthropicVersion: () -> String = { "2023-06-01" },
): Anthropic {

initKoin(
module = module {
single<HttpRequester> {
HttpRequester.default(
createHttpClient(
url = baseUrl()
)
)
}
single<Messages> {
DefaultMessagesApi(
anthropicConfig = AnthropicConfig(
apiKey = apiKey,
anthropicVersion = anthropicVersion

),
jsonLenient = get(),
requester = get()
)
}
}
)

return AnthropicApi(
apiKey = apiKey(),
apiURL = baseUrl(),
anthropicVersion = anthropicVersion()
)
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
package com.tddworks.anthropic.api.internal

import com.tddworks.anthropic.api.Anthropic
import com.tddworks.anthropic.api.messages.api.AnthropicConfig
import com.tddworks.anthropic.api.messages.api.Messages
import com.tddworks.anthropic.api.messages.api.internal.DefaultMessagesApi
import com.tddworks.common.network.api.ktor.api.HttpRequester
import com.tddworks.common.network.api.ktor.internal.createHttpClient
import com.tddworks.common.network.api.ktor.internal.default
import com.tddworks.di.getInstance


class AnthropicApi(
private val apiKey: String,
private val apiURL: String,
private val anthropicVersion: String,
) : Anthropic, Messages by DefaultMessagesApi(
AnthropicConfig(
apiKey = { apiKey },
anthropicVersion = { anthropicVersion }
),
HttpRequester.default(
createHttpClient(
url = apiURL
)
)
) {
) : Anthropic, Messages by getInstance() {
override fun apiKey(): String {
return apiKey
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import org.koin.dsl.module

expect fun platformModule(): Module

fun initKoin(enableNetworkLogs: Boolean = false, appDeclaration: KoinAppDeclaration = {}) =
fun initKoin(module: Module, appDeclaration: KoinAppDeclaration = {}) =
startKoin {
appDeclaration()
modules(commonModule(enableNetworkLogs = enableNetworkLogs))
modules(commonModule() + module)
}

fun commonModule(enableNetworkLogs: Boolean) = module {
fun commonModule() = module {
singleOf(::createJson)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.tddworks.anthropic.api

import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.koin.test.junit5.AutoCloseKoinTest

class AnthropicTest {
class AnthropicTest : AutoCloseKoinTest() {

@Test
fun `should return overridden settings`() {
val target = Anthropic(
baseUrl = "http://localhost:8080",
apiKey = "1234",
anthropicVersion = "2024-03-01"
baseUrl = { "http://localhost:8080" },
apiKey = { "1234" },
anthropicVersion = { "2024-03-01" }
)

assertEquals("http://localhost:8080", target.baseUrl())
Expand Down

0 comments on commit eb470f7

Please sign in to comment.