-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from arey/feature/streaming
Response Streaming and SSE #2
- Loading branch information
Showing
9 changed files
with
145 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
src/main/java/org/springframework/samples/petclinic/chat/Assistant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
package org.springframework.samples.petclinic.chat; | ||
|
||
import dev.langchain4j.service.SystemMessage; | ||
import dev.langchain4j.service.TokenStream; | ||
import dev.langchain4j.service.spring.AiService; | ||
|
||
@AiService | ||
interface Assistant { | ||
|
||
@SystemMessage(fromResource = "/prompts/system.st") | ||
String chat(String userMessage); | ||
TokenStream chat(String userMessage); | ||
|
||
} |
46 changes: 43 additions & 3 deletions
46
src/main/java/org/springframework/samples/petclinic/chat/AssistantController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,61 @@ | ||
package org.springframework.samples.petclinic.chat; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
@RestController | ||
class AssistantController { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(AssistantController.class); | ||
|
||
private final Assistant assistant; | ||
|
||
private final ExecutorService nonBlockingService = Executors.newCachedThreadPool(); | ||
|
||
AssistantController(Assistant assistant) { | ||
this.assistant = assistant; | ||
} | ||
|
||
@PostMapping("/chat") | ||
public String chat(@RequestBody String query) { | ||
return assistant.chat(query); | ||
// Using the POST method due to chat memory capabilities | ||
@PostMapping(value = "/chat") | ||
public SseEmitter chat(@RequestBody String query) { | ||
SseEmitter emitter = new SseEmitter(); | ||
nonBlockingService.execute(() -> assistant.chat(query).onNext(message -> { | ||
try { | ||
sendMessage(emitter, message); | ||
} | ||
catch (IOException e) { | ||
LOGGER.error("Error while writing next token", e); | ||
emitter.completeWithError(e); | ||
} | ||
}).onComplete(token -> emitter.complete()).onError(error -> { | ||
LOGGER.error("Unexpected chat error", error); | ||
try { | ||
sendMessage(emitter, error.getMessage()); | ||
} | ||
catch (IOException e) { | ||
LOGGER.error("Error while writing next token", e); | ||
} | ||
emitter.completeWithError(error); | ||
}).start()); | ||
return emitter; | ||
} | ||
|
||
private static void sendMessage(SseEmitter emitter, String message) throws IOException { | ||
String token = message | ||
// Hack line break problem when using Server Sent Events (SSE) | ||
.replace("\n", "<br>") | ||
// Escape JSON quotes | ||
.replace("\"", "\\\""); | ||
emitter.send("{\"t\": \"" + token + "\"}"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters