Skip to content

Instantly share code, notes, and snippets.

@fairjm
Last active November 14, 2023 18:49
Show Gist options
  • Save fairjm/f7d0e2aa25efc499d5817dd1fc84c80f to your computer and use it in GitHub Desktop.
Save fairjm/f7d0e2aa25efc499d5817dd1fc84c80f to your computer and use it in GitHub Desktop.
azure openai java sdk function stream handle example
// just see the logic, some class and annotation is project related just ignore it.
@RestController
@Slf4j
@RequestMapping("/api/chat")
public class ChatController {
... ...
@LoginCheck
@PostMapping(value = "/stream-sse", produces = "text/event-stream")
public ResponseBodyEmitter chat(@RequestBody ChatBodyVO bodyVO) {
final Long userId = RequestContextHolder.getContext().getUserId();
SseEmitter emitter = new SseEmitter();
SSE_POOL.execute(() -> {
try {
// call OpenAIClient to get stream
final IterableStream<ChatCompletions> stream = ... ...
String role = null;
StringBuilder contentBuilder = new StringBuilder();
StringBuilder functionCallNameBuilder = new StringBuilder();
StringBuilder functionCallArgumentsBuilder = new StringBuilder();
for (ChatCompletions e : stream) {
try {
emitter.send(e);
final List<ChatChoice> choices = e.getChoices();
if (CollectionUtils.isEmpty(choices)) {
continue;
}
final ChatChoice chatChoice = choices.get(0);
if (chatChoice.getFinishReason() != null) {
emitter.complete();
break;
}
final ChatMessage chatMessage = chatChoice.getDelta();
if (chatMessage == null) {
continue;
}
if (chatMessage.getRole() != null) {
role = chatMessage.getRole().toString();
}
final FunctionCall functionCall = chatMessage.getFunctionCall();
if (functionCall != null) {
if (functionCall.getName() != null) {
functionCallNameBuilder.append(functionCall.getName());
}
if (functionCall.getArguments() != null) {
functionCallArgumentsBuilder.append(functionCall.getArguments());
}
}
final String content = chatMessage.getContent();
if (content != null) {
contentBuilder.append(content);
}
} catch (IOException ex) {
log.error("chat - for loop", ex);
}
}
if (role == null) {
log.warn("chat - role is null");
return;
}
String content = null;
if (!contentBuilder.isEmpty()) {
content = contentBuilder.toString();
}
String functionMap = null;
if (!functionCallNameBuilder.isEmpty()) {
Map<String, String> map = new HashMap<>();
map.put("name", functionCallNameBuilder.toString());
if (!functionCallArgumentsBuilder.isEmpty()) {
map.put("arguments", functionCallArgumentsBuilder.toString());
}
functionMap = JsonUtils.toString(map).orElseThrow();
}
chatMessageRepository.saveResponseMessage(userId, role, content, functionMap);
} catch (Exception e) {
emitter.complete();
log.error("chat", e);
}
});
return emitter;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment