1515 */
1616package com.embabel.agent.eval.client
1717
18- import org.springframework.http.HttpEntity
1918import org.springframework.http.HttpHeaders
20- import org.springframework.http.HttpMethod
2119import 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
2423data class KnowledgeContext (
2524 val name : String ,
@@ -42,7 +41,7 @@ data class SessionCreationResponse(
4241 */
4342@Service
4443class 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