Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Value class Roll not best solution for mapping. Why not a sealed class with data objects? #362

Open
wolfscowl opened this issue Jun 22, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@wolfscowl
Copy link

wolfscowl commented Jun 22, 2024

Feature Description

If you want to map the role of a message for the presentation layer, for example, this is not possible with an exact mapping using the value class. Because a role could also theoretically be Role("Tom"). But the only accepted values of OpenAi are all contained in the class. You always need an unnecessary else branch, which then throws an exception or you introduce an unknown role in the presentantion layer for mapping in else branch.

@JvmInline
@Serializable
public value class Role(public val role: String) {
    public companion object {
        public val System: Role = Role("system")
        public val User: Role = Role("user")
        public val Assistant: Role = Role("assistant")
        public val Function: Role = Role("function")
        public val Tool: Role = Role("tool")
    }
}

Problem it Solves

Message(
  role = when(chatMessage.role) {
                  ChatRole.System -> Message.Role.System
                  ChatRole.Assistant -> Message.Role.Assistant
                  ChatRole.User -> Message.Role.User
                  ChatRole.Tool -> Message.Role.Tool
                  ChatRole.Function -> Message.Role.Function
                  else -> throw IllegalArgumentException("OpenAI Mapper: Unknown Role ${chatMessage.role} in" +  currentFileName())
                },
  ...
)

Proposed Solution

sealed class Role(val role: String) {
    data object System: Role("system")
    data object Assistant: Role("assistant")
    data object User: Role("user")
    data object Tool: Role("tool")
    data object Function: Role("function")
}

Additional Context

If anyone has a better solution on how to solve the problem, I would be very grateful.

@wolfscowl wolfscowl added the enhancement New feature or request label Jun 22, 2024
@wolfscowl wolfscowl changed the title Value class roll no suitable for mapping, Why not a sealed class with data objects? Value class roll not best solution for mapping, Why not a sealed class with data objects? Jun 22, 2024
@wolfscowl wolfscowl changed the title Value class roll not best solution for mapping, Why not a sealed class with data objects? Value class Roll not best solution for mapping. Why not a sealed class with data objects? Jun 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant