Skip to content

Commit

Permalink
Completions Stream Chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
amengus87 committed Jun 5, 2024
1 parent c978062 commit 015019b
Showing 1 changed file with 46 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,40 +32,60 @@ public class OpenAiCompatibleV1ApiController {

@PostMapping("/completions")
@Operation(summary = "Creates a completion", description = "Creates a completion for the provided prompt and parameters.")
public OpenAiCompletionResponse completions(@Valid @RequestBody OpenAiCompletionRequest request)
public Object completions(@Valid @RequestBody OpenAiCompletionRequest request)
throws Exception {
OpenAiCompletionResponse response = new OpenAiCompletionResponse();

response.setId(UUID.randomUUID().toString());
response.setModel(request.getModel());
response.setCreated(System.currentTimeMillis() / 1000);
response.setObject("text_completion");
response.setUsage(OpenAiCompletionUsage
.builder()
.completion_tokens(0)
.prompt_tokens(0)
.total_tokens(0)
.build());

List<OpenAiCompletionChoice> choices = new ArrayList<>();
choices.add(OpenAiCompletionChoice
.builder()
.index(0)
.finish_reason("stop")
.text("Hello, how can I help you today?")
.build());

response.setChoices(choices);

return response;
if (Boolean.TRUE.equals(request.getStream())) {
UUID emitterID = sseService.createEmitter();
for (int i = 0; i < 3; i++) {
OpenAiCompletionResponse responseChunk = new OpenAiCompletionResponse();
responseChunk.setId(emitterID.toString());
responseChunk.setModel(request.getModel());
responseChunk.setCreated(System.currentTimeMillis() / 1000);
responseChunk.setObject("text_completion");
List<OpenAiCompletionChoice> choices = new ArrayList<>();
choices.add(OpenAiCompletionChoice
.builder()
.index(0)
.finish_reason(i == 2 ? "stop" : null)
.text("Chunk : " + i + "\r\n")
.build());
responseChunk.setChoices(choices);
sseService.sendEvent(emitterID, responseChunk);
}
sseService.sendEvent(emitterID, "[DONE]");
new Thread(() -> {
sseService.complete(emitterID);
}).start();
return sseService.retrieveEmitter(emitterID);
} else {
OpenAiCompletionResponse response = new OpenAiCompletionResponse();
response.setId(UUID.randomUUID().toString());
response.setModel(request.getModel());
response.setCreated(System.currentTimeMillis() / 1000);
response.setObject("text_completion");
response.setUsage(OpenAiCompletionUsage
.builder()
.completion_tokens(0)
.prompt_tokens(0)
.total_tokens(0)
.build());
List<OpenAiCompletionChoice> choices = new ArrayList<>();
choices.add(OpenAiCompletionChoice
.builder()
.index(0)
.finish_reason("stop")
.text("Hello, how can I help you today?")
.build());
response.setChoices(choices);
return response;
}
}

@PostMapping("/chat/completions")
@Operation(summary = "Creates a chat completion", description = "Creates a chat completion for the provided prompt and parameters.")
public Object chatCompletions(@Valid @RequestBody OpenAiChatCompletionRequest request) throws Exception {
if (Boolean.TRUE.equals(request.getStream())) {
UUID emitterID = sseService.createEmitter();

for (int i = 0; i < 3; i++) {
OpenAiChatCompletionResponse responseChunk = new OpenAiChatCompletionResponse();
responseChunk.setId(emitterID.toString());
Expand Down

0 comments on commit 015019b

Please sign in to comment.