Skip to content

Commit cfe97e3

Browse files
committed
Replace RestTemplate with RestClient
This commit replaces the RestTemplate usage in AgentChatClient with RestClient. It also removes the RestTemplate bean configured in AgentPlatformConfiguration, as it does not appear to be used anywhere. See: gh-1003 Signed-off-by: Arjen Poutsma <[email protected]>
1 parent 0e1be35 commit cfe97e3

File tree

2 files changed

+32
-36
lines changed

2 files changed

+32
-36
lines changed

embabel-agent-api/src/main/kotlin/com/embabel/agent/spi/config/spring/AgentPlatformConfiguration.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import org.springframework.context.ApplicationContext
4242
import org.springframework.context.annotation.Bean
4343
import org.springframework.context.annotation.Configuration
4444
import org.springframework.context.annotation.Primary
45-
import org.springframework.web.client.RestTemplate
4645

4746

4847
/**
@@ -100,9 +99,6 @@ class AgentPlatformConfiguration(
10099
@ConditionalOnMissingBean(ColorPalette::class)
101100
fun defaultColorPalette(): ColorPalette = DefaultColorPalette()
102101

103-
@Bean
104-
fun restTemplate() = RestTemplate()
105-
106102
@Bean
107103
fun ranker(
108104
llmOperations: LlmOperations,

embabel-agent-eval/src/main/kotlin/com/embabel/agent/eval/client/AgentChatClient.kt

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
*/
1616
package com.embabel.agent.eval.client
1717

18-
import org.springframework.http.HttpEntity
1918
import org.springframework.http.HttpHeaders
20-
import org.springframework.http.HttpMethod
2119
import org.springframework.stereotype.Service
22-
import org.springframework.web.client.RestTemplate
20+
import org.springframework.web.client.RestClient
21+
import org.springframework.web.client.body
2322

2423
data class KnowledgeContext(
2524
val name: String,
@@ -42,7 +41,7 @@ data class SessionCreationResponse(
4241
*/
4342
@Service
4443
class AgentChatClient(
45-
private val restTemplate: RestTemplate = RestTemplate(),
44+
private val restClient: RestClient = RestClient.create(),
4645
private val agentHost: String = "http://localhost:8081",
4746
private val agentChatPath: String = "/api/v1/chat",
4847
private val boogieHost: String = "http://localhost:8080",
@@ -57,25 +56,25 @@ class AgentChatClient(
5756
}
5857

5958
fun createKnowledgeContext(knowledgeContext: KnowledgeContext): String {
60-
val entity = HttpEntity(knowledgeContext, defaultHeaders)
61-
return restTemplate.exchange(
62-
"${boogieHost}/${boogieContextPath}",
63-
HttpMethod.PUT,
64-
entity,
65-
String::class.java,
66-
).body ?: throw IllegalStateException("No response body")
59+
return restClient
60+
.put()
61+
.uri("${boogieHost}/${boogieContextPath}")
62+
.headers { it.putAll(defaultHeaders) }
63+
.body(knowledgeContext)
64+
.retrieve()
65+
.body<String>()
66+
?: throw IllegalStateException("No response body")
6767
}
6868

6969
fun createSession(sessionCreationRequest: SessionCreationRequest): SessionCreationResponse {
70-
val url = "${agentHost}/${agentChatPath}/sessions"
71-
val entity = HttpEntity(sessionCreationRequest, defaultHeaders)
72-
val re = restTemplate.exchange(
73-
url,
74-
HttpMethod.PUT,
75-
entity,
76-
SessionCreationResponse::class.java,
77-
)
78-
return re.body ?: throw IllegalStateException("No response body")
70+
return restClient
71+
.put()
72+
.uri("${agentHost}/${agentChatPath}/sessions")
73+
.headers { it.putAll(defaultHeaders) }
74+
.body(sessionCreationRequest)
75+
.retrieve()
76+
.body<SessionCreationResponse>()
77+
?: throw IllegalStateException("No response body")
7978
}
8079

8180
// fun ingestDocument(knowledgeContext: KnowledgeContext): String {
@@ -89,21 +88,22 @@ class AgentChatClient(
8988
// }
9089

9190
fun getObjectContext(id: String): ObjectContext {
92-
return restTemplate.getForObject(
93-
"${agentHost}/${agentChatPath}/objectContexts/{id}",
94-
ObjectContext::class.java,
95-
id,
96-
) ?: throw IllegalStateException("No response body")
91+
return restClient
92+
.get()
93+
.uri("${agentHost}/${agentChatPath}/objectContexts/{id}", id)
94+
.retrieve()
95+
.body<ObjectContext>()
96+
?: throw IllegalStateException("No response body")
9797
}
9898

9999
fun respond(chatRequest: ChatRequest): MessageResponse {
100-
val entity = HttpEntity(chatRequest)
101-
return restTemplate.exchange(
102-
"${agentHost}/${agentChatPath}/messages",
103-
HttpMethod.PUT,
104-
entity,
105-
MessageResponse::class.java,
106-
).body ?: throw IllegalStateException("No response body")
100+
return restClient
101+
.put()
102+
.uri("${agentHost}/${agentChatPath}/messages")
103+
.body(chatRequest)
104+
.retrieve()
105+
.body<MessageResponse>()
106+
?: throw IllegalStateException("No response body")
107107
}
108108

109109
}

0 commit comments

Comments
 (0)