Skip to content

Commit

Permalink
feat(gateway): ai provider
Browse files Browse the repository at this point in the history
 - move default base url to interface
  • Loading branch information
hanrw committed Aug 25, 2024
1 parent 66da2b3 commit ebbad1c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.tddworks.openai.api.legacy.completions.api.internal.DefaultCompletion

interface OpenAI : Chat, Images, Completions {
companion object {
const val BASE_URL = "api.openai.com"
const val BASE_URL = "https://api.openai.com"

fun create(config: OpenAIConfig): OpenAI {
val requester = HttpRequester.default(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,10 @@ import kotlinx.serialization.ExperimentalSerializationApi

data class AzureAIProviderConfig(
override val apiKey: () -> String,
override val baseUrl: () -> String = { DEFAULT_BASE_URL },
override val baseUrl: () -> String = { AzureChatApi.BASE_URL },
val deploymentId: () -> String,
val apiVersion: () -> String,
) : OpenAIProviderConfig {
companion object {
const val DEFAULT_BASE_URL = "https://YOUR_RESOURCE_NAME.openai.azure.com"
}
}
) : OpenAIProviderConfig

fun OpenAIProviderConfig.Companion.azure(
apiKey: () -> String,
Expand Down Expand Up @@ -103,6 +99,11 @@ class AzureChatApi(
private val requester: HttpRequester,
private val chatCompletionPath: String = CHAT_COMPLETIONS_PATH
) : Chat {

companion object {
const val BASE_URL = "https://YOUR_RESOURCE_NAME.openai.azure.com"
}

override suspend fun chatCompletions(request: ChatCompletionRequest): ChatCompletion {
return requester.performRequest<ChatCompletion> {
method = HttpMethod.Post
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package com.tddworks.openai.gateway.api.internal

import com.tddworks.anthropic.api.Anthropic
import com.tddworks.anthropic.api.AnthropicConfig
import com.tddworks.openai.gateway.api.OpenAIProviderConfig

class AnthropicOpenAIProviderConfig(
val anthropicVersion: () -> String = { "2023-06-01" },
val anthropicVersion: () -> String = { Anthropic.ANTHROPIC_VERSION },
override val apiKey: () -> String,
override val baseUrl: () -> String = { DEFAULT_BASE_URL }
) : OpenAIProviderConfig {
companion object {
const val DEFAULT_BASE_URL = "https://api.anthropic.com"
const val DEFAULT_ANTHROPIC_VERSION = "2023-06-01"
}
}
override val baseUrl: () -> String = { Anthropic.BASE_URL }
) : OpenAIProviderConfig

fun AnthropicOpenAIProviderConfig.toAnthropicOpenAIConfig() =
AnthropicConfig(
Expand All @@ -23,6 +19,6 @@ fun AnthropicOpenAIProviderConfig.toAnthropicOpenAIConfig() =

fun OpenAIProviderConfig.Companion.anthropic(
apiKey: () -> String,
baseUrl: () -> String = { AnthropicOpenAIProviderConfig.DEFAULT_BASE_URL },
anthropicVersion: () -> String = { AnthropicOpenAIProviderConfig.DEFAULT_ANTHROPIC_VERSION }
baseUrl: () -> String = { Anthropic.BASE_URL },
anthropicVersion: () -> String = { Anthropic.ANTHROPIC_VERSION }
) = AnthropicOpenAIProviderConfig(anthropicVersion, apiKey, baseUrl)
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package com.tddworks.openai.gateway.api.internal

import com.tddworks.openai.api.OpenAI
import com.tddworks.openai.api.OpenAIConfig
import com.tddworks.openai.gateway.api.OpenAIProviderConfig

data class DefaultOpenAIProviderConfig(
override val apiKey: () -> String,
override val baseUrl: () -> String = { DEFAULT_BASE_URL }
) : OpenAIProviderConfig {
companion object {
const val DEFAULT_BASE_URL = "https://api.openai.com"
}
}
override val baseUrl: () -> String = { OpenAI.BASE_URL }
) : OpenAIProviderConfig

fun OpenAIProviderConfig.toOpenAIConfig() = OpenAIConfig(apiKey, baseUrl)

fun OpenAIProviderConfig.Companion.default(
apiKey: () -> String,
baseUrl: () -> String = { DefaultOpenAIProviderConfig.DEFAULT_BASE_URL }
baseUrl: () -> String = { OpenAI.BASE_URL }
) = DefaultOpenAIProviderConfig(apiKey, baseUrl)

0 comments on commit ebbad1c

Please sign in to comment.