-
-
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.
- Loading branch information
1 parent
5381bc0
commit dfab459
Showing
5 changed files
with
99 additions
and
19 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
44 changes: 44 additions & 0 deletions
44
core/src/main/kotlin/io/github/llmagentbuilder/core/planner/ChatHistoryCustomizer.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,44 @@ | ||
package io.github.llmagentbuilder.core.planner | ||
|
||
import org.springframework.ai.chat.messages.Message | ||
import org.springframework.ai.chat.messages.UserMessage | ||
import java.util.function.BiFunction | ||
|
||
/** | ||
* Customize chat history before sending to LLM | ||
*/ | ||
interface ChatHistoryCustomizer { | ||
fun customize(messages: List<Message>): List<Message> | ||
|
||
companion object { | ||
val DEFAULT = object : ChatHistoryCustomizer { | ||
override fun customize(messages: List<Message>): List<Message> { | ||
return messages | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Patch content of last [UserMessage] in chat history | ||
*/ | ||
open class PatchLastUserMessageChatHistoryCustomizer( | ||
private val messageContentCustomizer: BiFunction<List<Message>, String, String> | ||
) : | ||
ChatHistoryCustomizer { | ||
override fun customize(messages: List<Message>): List<Message> { | ||
val lastUserMessage = messages.lastOrNull { | ||
it is UserMessage | ||
} | ||
lastUserMessage?.let { | ||
val index = messages.lastIndexOf(it) | ||
val updatedMessages = messages.toMutableList() | ||
updatedMessages[index] = UserMessage( | ||
messageContentCustomizer.apply(messages, it.content) | ||
) | ||
return updatedMessages | ||
} | ||
return messages | ||
} | ||
|
||
} |
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