Skip to content

Commit

Permalink
✨ Extrapolating state of conversation
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGraversen committed Nov 3, 2024
1 parent 2f9ed12 commit a8470a3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Value
public class TextConversation {
Expand Down Expand Up @@ -46,4 +47,8 @@ public List<TextMessage> getFirstMessages(@NonNull Integer conversationSize) {
.limit(conversationSize)
.toList();
}

public Optional<TextMessage> getLastMessage() {
return messages.isEmpty() ? Optional.empty() : Optional.of(messages.getLast());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public CompletableFuture<PredictionResponseAndModel> createPrediction(
) {
return CompletableFuture
.supplyAsync(doCreatePrediction(model, createPrediction), executorService)
.thenApplyAsync(checkAndEmitPredictionCreationTask, executorService)
.thenApplyAsync(pollPredictionStatusTask, executorService)
.whenCompleteAsync(emitPredictionResponseTask, executorService);
.thenApply(checkAndEmitPredictionCreationTask)
.thenApply(pollPredictionStatusTask)
.whenComplete(emitPredictionResponseTask);
}

public CompletableFuture<PredictionResponseAndModel> createPrediction(
Expand All @@ -41,9 +41,9 @@ public CompletableFuture<PredictionResponseAndModel> createPrediction(
) {
return CompletableFuture
.supplyAsync(doCreatePrediction(model, createPrediction), executorService)
.thenApplyAsync(checkAndEmitPredictionCreationTask, executorService)
.thenApplyAsync(pollPredictionStatusTask, executorService)
.whenCompleteAsync(emitPredictionResponseTask, executorService);
.thenApply(checkAndEmitPredictionCreationTask)
.thenApply(pollPredictionStatusTask)
.whenComplete(emitPredictionResponseTask);
}

Supplier<Optional<PredictionResponseAndModel>> doCreatePrediction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import io.graversen.replicate.common.ReplicateModel;
import io.graversen.replicate.common.TextConversation;
import io.graversen.replicate.common.TextMessage;
import lombok.AccessLevel;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import io.graversen.replicate.common.TextPredictionRoles;
import lombok.*;

import java.util.UUID;

Expand Down Expand Up @@ -45,7 +43,20 @@ public Conversation update(@NonNull TextConversation conversation) {
);
}

@ToString.Include
public ConversationStates getState() {
if (conversation.getMessages().isEmpty()) {
return ConversationStates.IDLE;
}

final var isLastMessageFromUser = conversation.getLastMessage()
.map(message -> message.getRole().equals(TextPredictionRoles.USER.asString()))
.orElse(false);

return isLastMessageFromUser ? ConversationStates.WAITING_FOR_ASSISTANT : ConversationStates.WAITING_FOR_USER;
}

private static String createId() {
return String.format("c_%s", UUID.randomUUID().toString());
return String.format("c_%s", UUID.randomUUID());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.graversen.replicate.service;

public enum ConversationStates {
IDLE,
WAITING_FOR_ASSISTANT,
WAITING_FOR_USER
}

0 comments on commit a8470a3

Please sign in to comment.