Skip to content

Commit

Permalink
HTTP logging can be configured (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
morisil authored Oct 15, 2024
1 parent ba76710 commit 9374e62
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/commonMain/kotlin/Anthropic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ const val ANTHROPIC_API_BASE: String = "https://api.anthropic.com/"
*/
const val DEFAULT_ANTHROPIC_VERSION: String = "2023-06-01"

/**
* The default model to be used if no model is specified.
*/
const val DEFAULT_MODEL = "claude-3-5-sonnet-20240620"

/**
* An exception thrown when API requests returns error.
*/
Expand Down Expand Up @@ -78,14 +83,15 @@ fun Anthropic(
val config = Anthropic.Config().apply(block)
val apiKey = if (config.apiKey != null) config.apiKey else envApiKey
requireNotNull(apiKey) { missingApiKeyMessage }
val defaultModel = if (config.defaultModel != null) config.defaultModel!! else "claude-3-5-sonnet-20240620"
val defaultModel = if (config.defaultModel != null) config.defaultModel!! else DEFAULT_MODEL
return Anthropic(
apiKey = apiKey,
anthropicVersion = config.anthropicVersion,
anthropicBeta = config.anthropicBeta,
apiBase = config.apiBase,
defaultModel = defaultModel,
directBrowserAccess = config.directBrowserAccess
directBrowserAccess = config.directBrowserAccess,
logLevel = if (config.logHttp) LogLevel.ALL else LogLevel.NONE
).apply {
toolEntryMap = (config.usableTools as List<Anthropic.ToolEntry<UsableTool>>).associateBy { it.tool.name }
}
Expand All @@ -97,7 +103,8 @@ class Anthropic internal constructor(
val anthropicBeta: String?,
val apiBase: String,
val defaultModel: String,
val directBrowserAccess: Boolean
val directBrowserAccess: Boolean,
val logLevel: LogLevel
) {

class Config {
Expand All @@ -107,6 +114,8 @@ class Anthropic internal constructor(
var apiBase: String = ANTHROPIC_API_BASE
var defaultModel: String? = null
var directBrowserAccess: Boolean = false
var logHttp: Boolean = false

@PublishedApi
internal var usableTools: List<ToolEntry<out UsableTool>> = emptyList()

Expand All @@ -130,13 +139,19 @@ class Anthropic internal constructor(
internal var toolEntryMap = mapOf<String, ToolEntry<UsableTool>>()

private val client = HttpClient {

install(ContentNegotiation) {
json(anthropicJson)
}

install(SSE)
install(Logging) {
level = LogLevel.BODY

if (logLevel != LogLevel.NONE) {
install(Logging) {
level = logLevel
}
}

install(HttpRequestRetry) {
retryOnServerErrors(maxRetries = 5)
exponentialDelay()
Expand All @@ -145,6 +160,7 @@ class Anthropic internal constructor(
response.status == HttpStatusCode.TooManyRequests
}
}

defaultRequest {
url(apiBase)
header("x-api-key", apiKey)
Expand All @@ -156,6 +172,7 @@ class Anthropic internal constructor(
header("anthropic-dangerous-direct-browser-access", true)
}
}

}

inner class Messages {
Expand Down
6 changes: 6 additions & 0 deletions src/commonMain/kotlin/message/Messages.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ import kotlinx.serialization.json.JsonObject
import kotlin.collections.mutableListOf
import kotlin.reflect.typeOf

/**
* The roles that can be taken by entities in a conversation.
*/
enum class Role {

@SerialName("user")
USER,

@SerialName("assistant")
ASSISTANT

}

@Serializable
Expand Down

0 comments on commit 9374e62

Please sign in to comment.