-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gemini): enhance module initialization and testing
Refactor the Gemini module for streamlined initialization with new configuration parameters and improve test coverage with additional integration tests. Removed unnecessary API key from requests to streamline configuration handling.
- Loading branch information
Showing
13 changed files
with
172 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...ni-client-core/src/commonMain/kotlin/com/tddworks/gemini/api/textGeneration/api/Gemini.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
package com.tddworks.gemini.api.textGeneration.api | ||
|
||
import com.tddworks.di.getInstance | ||
|
||
interface Gemini : TextGeneration { | ||
companion object { | ||
const val HOST = "generativelanguage.googleapis.com" | ||
const val BASE_URL = "https://$HOST" | ||
|
||
|
||
fun default(): Gemini { | ||
return object : Gemini, TextGeneration by getInstance() {} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 31 additions & 18 deletions
49
...ni-client/gemini-client-core/src/commonMain/kotlin/com/tddworks/gemini/di/GeminiModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,39 @@ | ||
package com.tddworks.gemini.di | ||
|
||
import com.tddworks.common.network.api.ktor.api.HttpRequester | ||
import com.tddworks.common.network.api.ktor.internal.* | ||
import com.tddworks.di.commonModule | ||
import kotlinx.serialization.json.Json | ||
import org.koin.core.context.startKoin | ||
import org.koin.core.qualifier.named | ||
import org.koin.dsl.KoinAppDeclaration | ||
import org.koin.dsl.module | ||
import com.tddworks.common.network.api.ktor.internal.ClientFeatures | ||
import com.tddworks.common.network.api.ktor.internal.UrlBasedConnectionConfig | ||
import com.tddworks.common.network.api.ktor.internal.createHttpClient | ||
import com.tddworks.common.network.api.ktor.internal.default | ||
import com.tddworks.di.createJson | ||
import com.tddworks.gemini.api.textGeneration.api.Gemini | ||
import com.tddworks.gemini.api.textGeneration.api.GeminiConfig | ||
import org.koin.core.annotation.ComponentScan | ||
import org.koin.core.annotation.Module | ||
|
||
// | ||
//fun initGemini( | ||
// appDeclaration: KoinAppDeclaration = {} | ||
//): HttpRequester { | ||
// return startKoin { | ||
// appDeclaration() | ||
// modules(commonModule(false) + geminiModules()) | ||
// }.koin.get<HttpRequester>() | ||
//} | ||
import org.koin.dsl.module | ||
import org.koin.ksp.generated.module | ||
|
||
@Module | ||
@ComponentScan("com.tddworks.gemini") | ||
class GeminiModule | ||
class GeminiModule { | ||
companion object { | ||
fun initGeminiModule(config: GeminiConfig, enableNetworkLogs: Boolean) = module { | ||
single { | ||
HttpRequester.default( | ||
createHttpClient( | ||
connectionConfig = UrlBasedConnectionConfig(config.baseUrl), | ||
features = ClientFeatures( | ||
json = createJson(), | ||
queryParams = mapOf("key" to config.apiKey()) | ||
) | ||
) | ||
) | ||
} | ||
|
||
includes(GeminiModule().module) | ||
|
||
single { Gemini.default() } | ||
} | ||
} | ||
} | ||
|
19 changes: 19 additions & 0 deletions
19
gemini-client/gemini-client-core/src/commonMain/kotlin/com/tddworks/gemini/di/Koin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.tddworks.gemini.di | ||
|
||
import com.tddworks.di.commonModule | ||
import com.tddworks.gemini.api.textGeneration.api.GeminiConfig | ||
import com.tddworks.gemini.di.GeminiModule.Companion.initGeminiModule | ||
import org.koin.core.context.startKoin | ||
import org.koin.dsl.KoinAppDeclaration | ||
|
||
fun initGemini( | ||
config: GeminiConfig, | ||
enableNetworkLogs: Boolean = false, | ||
appDeclaration: KoinAppDeclaration = {} | ||
) = startKoin { | ||
appDeclaration() | ||
modules( | ||
commonModule(enableNetworkLogs), | ||
initGeminiModule(config, enableNetworkLogs) | ||
) | ||
} |
56 changes: 56 additions & 0 deletions
56
gemini-client/gemini-client-core/src/jvmTest/kotlin/com/tddworks/gemini/api/GeminiITest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.tddworks.gemini.api | ||
|
||
import app.cash.turbine.test | ||
import com.tddworks.di.getInstance | ||
import com.tddworks.gemini.api.textGeneration.api.* | ||
import com.tddworks.gemini.di.initGemini | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.jupiter.api.Assertions.* | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable | ||
import org.koin.test.junit5.AutoCloseKoinTest | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
|
||
@EnabledIfEnvironmentVariable(named = "GEMINI_API_KEY", matches = ".+") | ||
class GeminiITest : AutoCloseKoinTest() { | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
initGemini( | ||
config = GeminiConfig( | ||
apiKey = { System.getenv("GEMINI_API_KEY") ?: "CONFIGURE_ME" }, | ||
baseUrl = { System.getenv("GEMINI_BASE_URL") ?: Gemini.BASE_URL } | ||
)) | ||
} | ||
|
||
@Test | ||
fun `should return stream generateContent response`() = runTest { | ||
val gemini = getInstance<Gemini>() | ||
|
||
gemini.streamGenerateContent( | ||
GenerateContentRequest( | ||
contents = listOf(Content(parts = listOf(Part(text = "hello")))), | ||
stream = true | ||
) | ||
).test(timeout = 10.seconds) { | ||
assertNotNull(awaitItem()) | ||
assertNotNull(awaitItem()) | ||
cancelAndIgnoreRemainingEvents() | ||
} | ||
} | ||
|
||
@Test | ||
fun `should return generateContent response`() = runTest { | ||
val gemini = getInstance<Gemini>() | ||
|
||
val response = gemini.generateContent( | ||
GenerateContentRequest( | ||
contents = listOf(Content(parts = listOf(Part(text = "hello")))), | ||
) | ||
) | ||
|
||
assertNotNull(response) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
gemini-client/gemini-client-core/src/jvmTest/kotlin/com/tddworks/gemini/api/GeminiTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.tddworks.gemini.api | ||
|
||
import com.tddworks.di.getInstance | ||
import com.tddworks.gemini.api.textGeneration.api.Gemini | ||
import com.tddworks.gemini.api.textGeneration.api.GeminiConfig | ||
import com.tddworks.gemini.di.initGemini | ||
import org.junit.jupiter.api.Assertions.* | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.koin.test.junit5.AutoCloseKoinTest | ||
|
||
|
||
class GeminiTest : AutoCloseKoinTest() { | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
initGemini(config = GeminiConfig()) | ||
} | ||
|
||
@Test | ||
fun `should get gemini default api`() { | ||
val gemini = getInstance<Gemini>() | ||
|
||
assertNotNull(gemini) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...ient/gemini-client-darwin/src/appleMain/kotlin/com/tddworks/gemini/api/DarwinAnthropic.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.tddworks.gemini.api | ||
|
||
import com.tddworks.gemini.api.textGeneration.api.Gemini | ||
import com.tddworks.gemini.api.textGeneration.api.GeminiConfig | ||
import com.tddworks.gemini.di.initGemini | ||
|
||
/** | ||
* Object responsible for setting up and initializing the Anthropoc API client. | ||
*/ | ||
object DarwinGemini { | ||
// | ||
/** | ||
* Initializes the Anthropic library with the provided configuration parameters. | ||
* | ||
* @param apiKey a lambda function that returns the API key to be used for authentication | ||
* @param baseUrl a lambda function that returns the base URL of the Anthropic API | ||
* @param anthropicVersion a lambda function that returns the version of the Anthropic API to use | ||
*/ | ||
fun anthropic( | ||
apiKey: () -> String = { "CONFIG_API_KEY" }, | ||
baseUrl: () -> String = { Gemini.BASE_URL }, | ||
) = initGemini(GeminiConfig(apiKey, baseUrl)) | ||
} |
18 changes: 0 additions & 18 deletions
18
gemini-client/gemini-client-darwin/src/appleMain/kotlin/com/tddworks/gemini/di/Koin.kt
This file was deleted.
Oops, something went wrong.