Skip to content

Commit

Permalink
refactor: Update OpenAI Gateway initialization and configuration
Browse files Browse the repository at this point in the history
- Update the initialization of OpenAI Gateway with new configurations and remove mock objects in favor of actual instances.
- Also, adjust the README file to reflect the changes in the initialization process and provide examples of stream and chat completions.
  • Loading branch information
hanrw committed Apr 18, 2024
1 parent d2a5a19 commit 958e2e6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.tddworks.openai.api.OpenAIConfig
import com.tddworks.openai.gateway.api.OpenAIGateway
import com.tddworks.openai.gateway.di.initOpenAIGatewayKoin

openAIGateway = initOpenAIGatewayKoin(
val openAIGateway = initOpenAIGateway(
OpenAIConfig(
baseUrl = { "YOUR_OPENAI_BASE_URL" },
apiKey = { "YOUR_OPENAI_API_KEY" }
Expand All @@ -36,5 +36,25 @@ openAIGateway = initOpenAIGatewayKoin(
apiKey = { "YOUR_ANTHROPIC_API_KEY" },
anthropicVersion = { "YOUR_ANTHROPIC_VERSION" }
)
).koin.get<OpenAIGateway>()
)

// stream completions
openAIGateway.streamCompletions(
OpenAIChatCompletionRequest(
messages = listOf(ChatMessage.UserMessage("hello")),
maxTokens = 1024,
model = OpenAIModel(OllamaModel.LLAMA2.value)
)
).collect {
println(it)
}

// chat completions
val chatCompletion = gateway.completions(
OpenAIChatCompletionRequest(
messages = listOf(ChatMessage.UserMessage("hello")),
maxTokens = 1024,
model = OpenAIModel(Model.GPT_3_5_TURBO.value)
)
)
```
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import org.koin.dsl.KoinAppDeclaration
import org.koin.dsl.module

@ExperimentalSerializationApi
fun initOpenAIGatewayKoin(
fun initOpenAIGateway(
openAIConfig: OpenAIConfig,
anthropicConfig: AnthropicConfig,
ollamaConfig: OllamaConfig,
Expand All @@ -31,7 +31,7 @@ fun initOpenAIGatewayKoin(
openAIGatewayModules() +
ollamaModules(ollamaConfig)
)
}
}.koin.get<OpenAIGateway>()

@ExperimentalSerializationApi
fun openAIGatewayModules() = module {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.tddworks.ollama.api.OllamaConfig
import com.tddworks.ollama.api.OllamaModel
import com.tddworks.openai.api.OpenAIConfig
import com.tddworks.openai.api.chat.api.ChatMessage
import com.tddworks.openai.gateway.di.initOpenAIGatewayKoin
import com.tddworks.openai.gateway.di.initOpenAIGateway
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.ExperimentalSerializationApi
import org.junit.jupiter.api.BeforeEach
Expand All @@ -30,7 +30,7 @@ class OpenAIGatewayITest : AutoCloseKoinTest() {

@BeforeEach
fun setUp() {
initOpenAIGatewayKoin(
initOpenAIGateway(
openAIConfig = OpenAIConfig(
baseUrl = { "api.openai.com" },
apiKey = { System.getenv("OPENAI_API_KEY") ?: "" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ import org.junit.jupiter.api.Test
import org.koin.dsl.koinApplication
import org.koin.test.KoinTest
import org.koin.test.check.checkModules
import org.mockito.Mockito.mock

class OpenAIGatewayKoinTest : KoinTest {

@Test
fun `should initialize OpenAI Gateway Koin modules`() {
val openAIConfig = mock<OpenAIConfig>()
val anthropicConfig = mock<AnthropicConfig>()
val ollamaConfig = mock<OllamaConfig>()
val openAIConfig = OpenAIConfig()
val anthropicConfig = AnthropicConfig()
val ollamaConfig = OllamaConfig()

koinApplication {
initOpenAIGatewayKoin(openAIConfig, anthropicConfig, ollamaConfig)
initOpenAIGateway(openAIConfig, anthropicConfig, ollamaConfig)
}.checkModules()
}
}
Expand Down

0 comments on commit 958e2e6

Please sign in to comment.