-
-
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
- add koin-core support - add generic ChatApi and unit test
- Loading branch information
Showing
25 changed files
with
428 additions
and
94 deletions.
There are no files selected for viewing
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
10 changes: 9 additions & 1 deletion
10
...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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,20 @@ | ||
package com.tddworks.anthropic.api.messages.api | ||
|
||
import com.tddworks.common.network.api.StreamableRequest | ||
import com.tddworks.openllm.api.ChatRequest | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
@SerialName("CreateMessageRequest") | ||
data class CreateMessageRequest( | ||
val messages: List<Message>, | ||
val systemPrompt: String? = null, | ||
) : ChatRequest | ||
) : ChatRequest, StreamableRequest { | ||
companion object { | ||
fun streamRequest(messages: List<Message>, systemPrompt: String? = null) = | ||
CreateMessageRequest(messages, systemPrompt) as StreamableRequest | ||
} | ||
} | ||
|
||
|
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
25 changes: 24 additions & 1 deletion
25
.../commonMain/kotlin/com/tddworks/anthropic/api/messages/api/internal/DefaultMessagesApi.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
21 changes: 21 additions & 0 deletions
21
...ore/src/commonMain/kotlin/com/tddworks/anthropic/api/messages/api/internal/JsonLenient.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,21 @@ | ||
package com.tddworks.anthropic.api.messages.api.internal | ||
|
||
import com.tddworks.anthropic.api.messages.api.internal.json.chatModule | ||
import kotlinx.serialization.json.Json | ||
|
||
|
||
/** | ||
* Represents a JSON object that allows for leniency and ignores unknown keys. | ||
* | ||
* @property isLenient Removes JSON specification restriction (RFC-4627) and makes parser more liberal to the malformed input. In lenient mode quoted boolean literals, and unquoted string literals are allowed. | ||
* Its relaxations can be expanded in the future, so that lenient parser becomes even more permissive to invalid value in the input, replacing them with defaults. | ||
* false by default. | ||
* @property ignoreUnknownKeys Specifies whether encounters of unknown properties in the input JSON should be ignored instead of throwing SerializationException. false by default.. | ||
*/ | ||
val JsonLenient = Json { | ||
isLenient = true | ||
ignoreUnknownKeys = true | ||
// https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/json.md#class-discriminator-for-polymorphism | ||
classDiscriminator = "#class" | ||
serializersModule = chatModule | ||
} |
45 changes: 0 additions & 45 deletions
45
...rc/commonMain/kotlin/com/tddworks/anthropic/api/messages/api/internal/json/Serializers.kt
This file was deleted.
Oops, something went wrong.
89 changes: 89 additions & 0 deletions
89
...monMain/kotlin/com/tddworks/anthropic/api/messages/api/internal/json/SerializersConfig.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,89 @@ | ||
package com.tddworks.anthropic.api.messages.api.internal.json | ||
|
||
import com.tddworks.anthropic.api.messages.api.CreateMessageRequest | ||
import com.tddworks.anthropic.api.messages.api.CreateMessageResponse | ||
import com.tddworks.anthropic.api.messages.api.stream.* | ||
import com.tddworks.common.network.api.StreamChatResponse | ||
import com.tddworks.common.network.api.StreamableRequest | ||
import com.tddworks.openllm.api.ChatRequest | ||
import com.tddworks.openllm.api.ChatResponse | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.SerializationStrategy | ||
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 chatModule = SerializersModule { | ||
|
||
// polymorphicDefaultSerializer(StreamableRequest::class) { instance -> | ||
// @Suppress("UNCHECKED_CAST") | ||
// when (instance) { | ||
// is CreateMessageRequest -> CreateMessageRequest.serializer() as SerializationStrategy<StreamableRequest> | ||
// else -> null | ||
// } | ||
// } | ||
|
||
polymorphic(StreamableRequest::class) { | ||
subclass(CreateMessageRequest::class, CreateMessageRequest.serializer()) | ||
defaultDeserializer { CreateMessageRequest.serializer() } | ||
} | ||
// | ||
polymorphic(ChatRequest::class) { | ||
subclass(CreateMessageRequest::class, CreateMessageRequest.serializer()) | ||
defaultDeserializer { CreateMessageRequest.serializer() } | ||
} | ||
// | ||
// polymorphic(ChatResponse::class) { | ||
// subclass(CreateMessageResponse::class, CreateMessageResponse.serializer()) | ||
// defaultDeserializer { ChatResponseSerializer } | ||
// } | ||
|
||
polymorphic(StreamChatResponse::class) { | ||
defaultDeserializer { StreamChatResponseSerializer } | ||
} | ||
|
||
// polymorphicDefaultSerializer(ChatResponse::class) { instance -> | ||
// @Suppress("UNCHECKED_CAST") | ||
// when (instance) { | ||
// is CreateMessageResponse -> CreateMessageResponse.serializer() as SerializationStrategy<ChatResponse> | ||
// else -> null | ||
// } | ||
// } | ||
// | ||
polymorphic(ChatResponse::class) { | ||
defaultDeserializer { ChatResponseSerializer } | ||
} | ||
} | ||
|
||
object ChatResponseSerializer : | ||
JsonContentPolymorphicSerializer<ChatResponse>(ChatResponse::class) { | ||
override fun selectDeserializer(element: JsonElement): KSerializer<out ChatResponse> { | ||
val content = element.jsonObject["content"] | ||
|
||
return when { | ||
content != null -> CreateMessageResponse.serializer() | ||
else -> throw IllegalArgumentException("Unknown type of message") | ||
} | ||
} | ||
} | ||
|
||
object StreamChatResponseSerializer : | ||
JsonContentPolymorphicSerializer<StreamChatResponse>(StreamChatResponse::class) { | ||
override fun selectDeserializer(element: JsonElement): KSerializer<out StreamChatResponse> { | ||
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") | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...core/src/commonMain/kotlin/com/tddworks/anthropic/api/messages/api/stream/StreamModule.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,71 @@ | ||
package com.tddworks.anthropic.api.messages.api.stream | ||
|
||
import com.tddworks.anthropic.api.messages.api.CreateMessageResponse | ||
import com.tddworks.common.network.api.StreamChatResponse | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@SerialName("message_start") | ||
@Serializable | ||
data class MessageStart( | ||
override val type: String, | ||
val message: CreateMessageResponse, | ||
) : StreamChatResponse | ||
|
||
@Serializable | ||
@SerialName("content_block_start") | ||
data class ContentBlockStart( | ||
override val type: String, | ||
val index: Int, | ||
@SerialName("content_block") | ||
val contentBlock: ContentBlock, | ||
) : StreamChatResponse | ||
|
||
@Serializable | ||
@SerialName("content_block_delta") | ||
data class ContentBlock( | ||
override val type: String, | ||
val text: String, | ||
): StreamChatResponse | ||
|
||
@Serializable | ||
@SerialName("content_block_delta") | ||
data class ContentBlockDelta( | ||
override val type: String, | ||
val index: Int, | ||
val delta: Delta, | ||
): StreamChatResponse | ||
|
||
@Serializable | ||
@SerialName("content_block_stop") | ||
data class ContentBlockStop( | ||
override val type: String, | ||
val index: Int, | ||
): StreamChatResponse | ||
|
||
@Serializable | ||
@SerialName("message_delta") | ||
data class MessageDelta( | ||
override val type: String, | ||
val delta: Delta, | ||
): StreamChatResponse | ||
|
||
@Serializable | ||
@SerialName("message_stop") | ||
data class MessageStop( | ||
override val type: String, | ||
): StreamChatResponse | ||
|
||
@Serializable | ||
@SerialName("ping") | ||
data class Ping( | ||
override val type: String, | ||
): StreamChatResponse | ||
|
||
@Serializable | ||
data class Delta( | ||
val type: String, | ||
val text: String, | ||
) | ||
|
||
|
30 changes: 30 additions & 0 deletions
30
...opic-client/anthropic-client-core/src/commonMain/kotlin/com/tddworks/anthropic/di/Koin.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,30 @@ | ||
package com.tddworks.anthropic.di | ||
|
||
import com.tddworks.anthropic.api.messages.api.internal.JsonLenient | ||
import io.ktor.client.* | ||
import io.ktor.client.engine.* | ||
import io.ktor.client.plugins.contentnegotiation.* | ||
import io.ktor.client.plugins.logging.* | ||
import io.ktor.serialization.kotlinx.json.* | ||
import kotlinx.serialization.json.Json | ||
import org.koin.core.component.KoinComponent | ||
import org.koin.core.component.inject | ||
import org.koin.core.context.startKoin | ||
import org.koin.core.module.Module | ||
import org.koin.core.module.dsl.singleOf | ||
import org.koin.dsl.KoinAppDeclaration | ||
import org.koin.dsl.module | ||
|
||
expect fun platformModule(): Module | ||
|
||
fun initKoin(enableNetworkLogs: Boolean = false, appDeclaration: KoinAppDeclaration = {}) = | ||
startKoin { | ||
appDeclaration() | ||
modules(commonModule(enableNetworkLogs = enableNetworkLogs)) | ||
} | ||
|
||
fun commonModule(enableNetworkLogs: Boolean) = module { | ||
singleOf(::createJson) | ||
} | ||
|
||
fun createJson() = JsonLenient |
7 changes: 7 additions & 0 deletions
7
...pic-client/anthropic-client-core/src/jvmMain/kotlin/com/tddworks/anthropic/di/Koin.jvm.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,7 @@ | ||
package com.tddworks.anthropic.di | ||
|
||
import org.koin.core.module.Module | ||
|
||
actual fun platformModule(): Module { | ||
TODO("Not yet implemented") | ||
} |
Oops, something went wrong.