-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLLMController.java
More file actions
39 lines (32 loc) · 1.14 KB
/
LLMController.java
File metadata and controls
39 lines (32 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.example.spring.app.llm;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
@CrossOrigin
@RestController
@RequestMapping("/v1")
public class LLMController {
private final ChatClient chatClient;
public LLMController(ChatClient.Builder chatClientBuilder) {
this.chatClient = chatClientBuilder.build();
}
@GetMapping("/ask-ai")
LLMAnswerDTO generation(String userInput) {
LLMAnswerDTO llmAnswerDTO = new LLMAnswerDTO();
String response = this.chatClient.prompt()
.user(userInput)
.call()
.content();
llmAnswerDTO.setAnswer(response);
return llmAnswerDTO;
}
@GetMapping(value = "/stream-ai", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ChatResponse> streamGeneration(@RequestParam String userInput) {
return chatClient.prompt()
.user(userInput)
.stream()
.chatResponse();
}
}