-
-
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(BE-174): integrate anthropic api
- code refactor. - make create api working.
- Loading branch information
Showing
20 changed files
with
250 additions
and
157 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
...lient/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/api/Anthropic.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,35 @@ | ||
package com.tddworks.anthropic.api | ||
|
||
import com.tddworks.anthropic.api.Anthropic.Companion.BASE_URL | ||
import com.tddworks.anthropic.api.messages.api.AnthropicConfig | ||
import com.tddworks.anthropic.api.messages.api.Messages | ||
import com.tddworks.anthropic.api.messages.api.internal.DefaultMessagesApi | ||
import com.tddworks.common.network.api.ktor.api.HttpRequester | ||
import com.tddworks.common.network.api.ktor.internal.createHttpClient | ||
import com.tddworks.common.network.api.ktor.internal.default | ||
import io.ktor.client.engine.* | ||
|
||
interface Anthropic : Messages { | ||
companion object { | ||
const val BASE_URL = "klaude.asusual.life" | ||
} | ||
} | ||
|
||
fun Anthropic(apiKey: String, engine: HttpClientEngineFactory<HttpClientEngineConfig>): Anthropic = AnthropicApi( | ||
apiKey = apiKey, | ||
HttpRequester.default( | ||
createHttpClient( | ||
url = BASE_URL, engine = engine | ||
) | ||
), | ||
) | ||
|
||
class AnthropicApi( | ||
private val apiKey: String, | ||
private val requester: HttpRequester, | ||
) : Anthropic, Messages by DefaultMessagesApi( | ||
AnthropicConfig( | ||
apiKey = apiKey | ||
), | ||
requester | ||
) |
6 changes: 6 additions & 0 deletions
6
...ent-core/src/commonMain/kotlin/com/tddworks/anthropic/api/messages/api/AnthropicConfig.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,6 @@ | ||
package com.tddworks.anthropic.api.messages.api | ||
|
||
data class AnthropicConfig( | ||
val apiKey: String = "CONFIG_API_KEY", | ||
val anthropicVersion: String = "2023-06-01", | ||
) |
12 changes: 7 additions & 5 deletions
12
...ore/src/commonMain/kotlin/com/tddworks/anthropic/api/messages/api/CreateMessageRequest.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
12 changes: 12 additions & 0 deletions
12
...pic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/api/messages/api/Messages.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,12 @@ | ||
package com.tddworks.anthropic.api.messages.api | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
|
||
/** | ||
* * Anthropic Messages API -https://docs.anthropic.com/claude/reference/messages_post | ||
*/ | ||
interface Messages { | ||
suspend fun create(request: CreateMessageRequest): CreateMessageResponse | ||
|
||
fun stream(request: StreamMessageRequest): Flow<StreamMessageResponse> | ||
} |
5 changes: 5 additions & 0 deletions
5
...ore/src/commonMain/kotlin/com/tddworks/anthropic/api/messages/api/StreamMessageRequest.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,5 @@ | ||
package com.tddworks.anthropic.api.messages.api | ||
|
||
import com.tddworks.common.network.api.StreamableRequest | ||
|
||
interface StreamMessageRequest : StreamableRequest |
36 changes: 16 additions & 20 deletions
36
...c/api/messages/api/stream/StreamModule.kt → ...api/messages/api/StreamMessageResponse.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
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
37 changes: 37 additions & 0 deletions
37
...ommonMain/kotlin/com/tddworks/anthropic/api/messages/api/internal/json/AnthropicModule.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,37 @@ | ||
package com.tddworks.anthropic.api.messages.api.internal.json | ||
|
||
import com.tddworks.anthropic.api.messages.api.* | ||
import com.tddworks.common.network.api.StreamableRequest | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.json.JsonContentPolymorphicSerializer | ||
import kotlinx.serialization.json.JsonElement | ||
import kotlinx.serialization.json.jsonObject | ||
import kotlinx.serialization.json.jsonPrimitive | ||
import kotlinx.serialization.modules.SerializersModule | ||
import kotlinx.serialization.modules.polymorphic | ||
|
||
val anthropicModule = SerializersModule { | ||
|
||
polymorphic(StreamableRequest::class) { | ||
subclass(CreateMessageRequest::class, CreateMessageRequest.serializer()) | ||
defaultDeserializer { CreateMessageRequest.serializer() } | ||
} | ||
} | ||
|
||
object StreamMessageResponseSerializer : | ||
JsonContentPolymorphicSerializer<StreamMessageResponse>(StreamMessageResponse::class) { | ||
override fun selectDeserializer(element: JsonElement): KSerializer<out StreamMessageResponse> { | ||
val type = element.jsonObject["type"]?.jsonPrimitive?.content | ||
|
||
return when (type) { | ||
"message_start" -> MessageStart.serializer() | ||
"content_block_start" -> ContentBlockStart.serializer() | ||
"content_block_delta" -> ContentBlockDelta.serializer() | ||
"content_block_stop" -> ContentBlockStop.serializer() | ||
"message_delta" -> MessageDelta.serializer() | ||
"message_stop" -> MessageStop.serializer() | ||
"ping" -> Ping.serializer() | ||
else -> throw IllegalArgumentException("Unknown type of message") | ||
} | ||
} | ||
} |
89 changes: 0 additions & 89 deletions
89
...monMain/kotlin/com/tddworks/anthropic/api/messages/api/internal/json/SerializersConfig.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.