Skip to content

Commit 97d0252

Browse files
committed
Comments addressed
1 parent b4e8b03 commit 97d0252

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

core/src/commonMain/kotlin/com/xebia/functional/xef/auto/PromptConfiguration.kt

+17
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class PromptConfiguration(
1212
val docsInContext: Int = 5,
1313
val memoryLimit: Int = 5,
1414
val minResponseTokens: Int = 500,
15+
val messagePolicy: MessagePolicy = MessagePolicy(),
1516
) {
1617
companion object {
1718

@@ -23,6 +24,7 @@ class PromptConfiguration(
2324
private var docsInContext: Int = 20
2425
private var minResponseTokens: Int = 500
2526
private var memoryLimit: Int = 5
27+
private var messagePolicy: MessagePolicy = MessagePolicy()
2628

2729
fun maxDeserializationAttempts(maxDeserializationAttempts: Int) = apply {
2830
this.maxDeserializationAttempts = maxDeserializationAttempts
@@ -44,6 +46,8 @@ class PromptConfiguration(
4446

4547
fun memoryLimit(memoryLimit: Int) = apply { this.memoryLimit = memoryLimit }
4648

49+
fun messagePolicy(messagePolicy: MessagePolicy) = apply { this.messagePolicy = messagePolicy }
50+
4751
fun build() =
4852
PromptConfiguration(
4953
maxDeserializationAttempts = maxDeserializationAttempts,
@@ -53,6 +57,7 @@ class PromptConfiguration(
5357
docsInContext = docsInContext,
5458
memoryLimit = memoryLimit,
5559
minResponseTokens = minResponseTokens,
60+
messagePolicy = messagePolicy,
5661
)
5762
}
5863

@@ -62,3 +67,15 @@ class PromptConfiguration(
6267
@JvmField val DEFAULTS = PromptConfiguration()
6368
}
6469
}
70+
71+
/**
72+
* The [MessagePolicy] encapsulates the message selection policy for sending to the server. Allows
73+
* defining the percentages of historical and contextual messages to include in the final list.
74+
*
75+
* @property historyPercent Percentage of historical messages
76+
* @property contextPercent Percentage of context messages
77+
*/
78+
class MessagePolicy(
79+
val historyPercent: Int = 50,
80+
val contextPercent: Int = 50,
81+
)

core/src/commonMain/kotlin/com/xebia/functional/xef/llm/Chat.kt

+3-4
Original file line numberDiff line numberDiff line change
@@ -301,16 +301,15 @@ interface Chat : LLM {
301301

302302
val remainingTokensForContexts = remainingTokens - messagesTokens
303303

304-
// TODO we should move this to PromptConfiguration
305-
val historyPercent = 50
306-
val contextPercent = 50
304+
val historyPercent = promptConfiguration.messagePolicy.historyPercent
305+
val contextPercent = promptConfiguration.messagePolicy.contextPercent
307306

308307
val maxHistoryTokens = (remainingTokensForContexts * historyPercent) / 100
309308

310309
val historyMessagesWithTokens = history.map { Pair(it, tokensFromMessages(listOf(it))) }
311310

312311
val totalTokenWithMessages =
313-
historyMessagesWithTokens.reversed().fold(Pair(0, emptyList<Message>())) { acc, pair ->
312+
historyMessagesWithTokens.foldRight(Pair(0, emptyList<Message>())) { pair, acc ->
314313
if (acc.first + pair.second > maxHistoryTokens) {
315314
acc
316315
} else {

0 commit comments

Comments
 (0)