6
6
import org .slf4j .Logger ;
7
7
import org .slf4j .LoggerFactory ;
8
8
import org .springframework .ai .chat .client .ChatClient ;
9
- import org .springframework .ai .chat .client .advisor .QuestionAnswerAdvisor ;
9
+ import org .springframework .ai .chat .client .advisor .RetrievalAugmentationAdvisor ;
10
+ import org .springframework .ai .rag .retrieval .source .DocumentRetriever ;
11
+ import org .springframework .ai .rag .retrieval .source .VectorStoreDocumentRetriever ;
10
12
import org .springframework .ai .reader .TextReader ;
11
13
import org .springframework .ai .transformer .splitter .TokenTextSplitter ;
12
14
import org .springframework .ai .vectorstore .VectorStore ;
16
18
import org .springframework .boot .context .event .ApplicationReadyEvent ;
17
19
import org .springframework .context .ApplicationListener ;
18
20
import org .springframework .context .annotation .Bean ;
19
- import org .springframework .context .annotation .Configuration ;
20
21
import org .springframework .core .io .Resource ;
21
22
import org .springframework .jdbc .core .simple .JdbcClient ;
22
23
import org .springframework .stereotype .Component ;
23
24
import org .springframework .web .bind .annotation .PostMapping ;
24
25
import org .springframework .web .bind .annotation .RequestBody ;
25
26
import org .springframework .web .bind .annotation .RestController ;
26
27
27
- import java .time .Duration ;
28
-
29
28
@ SpringBootApplication
30
29
public class Application {
31
30
@@ -46,18 +45,22 @@ class ChatController {
46
45
private static final Logger logger = LoggerFactory .getLogger (ChatController .class );
47
46
48
47
private final ChatClient chatClient ;
49
- private final VectorStore vectorStore ;
48
+ private final DocumentRetriever documentRetriever ;
50
49
51
50
ChatController (ChatClient .Builder chatClientBuilder , VectorStore vectorStore ) {
52
51
this .chatClient = chatClientBuilder .build ();
53
- this .vectorStore = vectorStore ;
52
+ this .documentRetriever = VectorStoreDocumentRetriever .builder ()
53
+ .vectorStore (vectorStore )
54
+ .build ();
54
55
}
55
56
56
57
@ PostMapping ("/chat" )
57
58
String chatWithDocument (@ RequestBody String message ) {
58
59
logger .info ("Received user message: {}" , message );
59
60
return chatClient .prompt ()
60
- .advisors (new QuestionAnswerAdvisor (vectorStore ))
61
+ .advisors (RetrievalAugmentationAdvisor .builder ()
62
+ .documentRetriever (documentRetriever )
63
+ .build ())
61
64
.user (message )
62
65
.call ()
63
66
.content ();
0 commit comments