Skip to content

Commit

Permalink
feat(gateway): move hard code Messages by getInstance() to param
Browse files Browse the repository at this point in the history
  • Loading branch information
hanrw committed Aug 21, 2024
1 parent 7738891 commit a9ff52c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.tddworks.anthropic.api

import com.tddworks.anthropic.api.internal.AnthropicApi
import com.tddworks.anthropic.api.messages.api.Messages
import com.tddworks.di.getInstance

/**
* Interface for interacting with the Anthropic API.
Expand All @@ -13,6 +14,26 @@ interface Anthropic : Messages {
companion object {
const val BASE_URL = "api.anthropic.com"
const val ANTHROPIC_VERSION = "2023-06-01"

/**
* Creates an instance of Anthropic API with the provided configurations.
*
* @param apiKey a function that returns the API key to be used for authentication. Defaults to "CONFIGURE_ME" if not provided.
* @param baseUrl a function that returns the base URL of the API. Defaults to the value specified in the Anthropic companion object if not provided.
* @param anthropicVersion a function that returns the version of the Anthropic API to be used. Defaults to "2023-06-01" if not provided.
* @return an instance of Anthropic API configured with the provided settings.
*/
fun create(
apiKey: () -> String = { "CONFIGURE_ME" },
baseUrl: () -> String = { BASE_URL },
anthropicVersion: () -> String = { ANTHROPIC_VERSION }
): Anthropic {
return AnthropicApi(
apiKey = apiKey(),
apiURL = baseUrl(),
anthropicVersion = anthropicVersion(),
)
}
}

/**
Expand Down Expand Up @@ -51,10 +72,12 @@ fun Anthropic(
apiKey: () -> String = { "CONFIGURE_ME" },
baseUrl: () -> String = { Anthropic.BASE_URL },
anthropicVersion: () -> String = { "2023-06-01" },
messages: Messages = getInstance()
): Anthropic {
return AnthropicApi(
apiKey = apiKey(),
apiURL = baseUrl(),
anthropicVersion = anthropicVersion()
anthropicVersion = anthropicVersion(),
messages = messages
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class AnthropicApi(
private val apiKey: String,
private val apiURL: String,
private val anthropicVersion: String,
) : Anthropic, Messages by getInstance() {
private val messages: Messages = getInstance()
) : Anthropic, Messages by messages {
/**
* Gets the API key.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AnthropicTest : AutoCloseKoinTest() {
val target = Anthropic(
baseUrl = { "http://localhost:8080" },
apiKey = { "1234" },
anthropicVersion = { "2024-03-01" }
anthropicVersion = { "2024-03-01" },
)

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

0 comments on commit a9ff52c

Please sign in to comment.