Skip to content

Commit 57abad5

Browse files
committed
add advisor to set system and user prompts
1 parent aae4e1c commit 57abad5

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

bootstrap/src/main/kotlin/io/github/llmagentbuilder/bootstrap/AgentBootstrap.kt

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import org.slf4j.LoggerFactory
1212
import org.springframework.ai.chat.client.ChatClient
1313
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor
1414
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor
15-
import org.springframework.ai.chat.client.advisor.api.Advisor
15+
import org.springframework.ai.chat.client.advisor.api.*
1616
import org.springframework.ai.chat.memory.InMemoryChatMemory
17+
import org.springframework.core.Ordered
1718
import java.io.InputStream
1819
import java.nio.file.Path
1920
import java.util.*
@@ -45,6 +46,28 @@ object AgentBootstrap {
4546
)
4647
)
4748
advisors.addLast(SimpleLoggerAdvisor())
49+
50+
val providedSystemParams = mutableMapOf<String, Any>()
51+
val providedUserParams = mutableMapOf<String, Any>()
52+
ServiceLoader.load(PromptParamsProvider::class.java)
53+
.stream()
54+
.map { it.get() }
55+
.asSequence()
56+
.forEach {
57+
it.provideSystemParams()?.run {
58+
providedSystemParams.putAll(this)
59+
}
60+
it.provideUserParams()?.run {
61+
providedUserParams.putAll(this)
62+
}
63+
}
64+
65+
advisors.add(
66+
PromptParamsAdvisor(
67+
providedSystemParams,
68+
providedUserParams
69+
)
70+
)
4871
val observationEnabled = agentConfig.observation?.enabled == true
4972
val observationRegistry =
5073
if (observationEnabled) ObservationRegistry.create() else ObservationRegistry.NOOP
@@ -113,4 +136,33 @@ object AgentBootstrap {
113136
MessageChatMemoryAdvisor(InMemoryChatMemory())
114137
} else null
115138
}
139+
140+
private class PromptParamsAdvisor(
141+
private val providedSystemParams: Map<String, Any>,
142+
private val providedUserParams: Map<String, Any>
143+
) : CallAroundAdvisor {
144+
override fun getOrder(): Int {
145+
return Ordered.HIGHEST_PRECEDENCE + 100
146+
}
147+
148+
override fun getName(): String {
149+
return javaClass.simpleName
150+
}
151+
152+
override fun aroundCall(
153+
advisedRequest: AdvisedRequest,
154+
chain: CallAroundAdvisorChain
155+
): AdvisedResponse {
156+
val systemParams = HashMap(advisedRequest.systemParams ?: mapOf())
157+
systemParams.putAll(providedSystemParams)
158+
val userParams = HashMap(advisedRequest.userParams ?: mapOf())
159+
userParams.putAll(providedUserParams)
160+
val request = AdvisedRequest.from(advisedRequest)
161+
.withSystemParams(systemParams)
162+
.withUserParams(userParams)
163+
.build()
164+
return chain.nextAroundCall(request)
165+
}
166+
167+
}
116168
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.github.llmagentbuilder.core
2+
3+
interface PromptParamsProvider {
4+
fun provideSystemParams(): Map<String, Any>? {
5+
return mapOf()
6+
}
7+
8+
fun provideUserParams(): Map<String, Any>? {
9+
return mapOf()
10+
}
11+
}

0 commit comments

Comments
 (0)