-
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #2 from penguineer/mvp-handler
MVP: Respond to a chat request via RabbitMQ
- Loading branch information
Showing
7 changed files
with
150 additions
and
0 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
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
19 changes: 19 additions & 0 deletions
19
src/main/java/com/penguineering/hareairis/model/ChatRequest.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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.penguineering.hareairis.model; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.UUID; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class ChatRequest { | ||
@JsonProperty("request_id") | ||
private UUID requestId; | ||
|
||
@JsonProperty("message") | ||
private String message; | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/penguineering/hareairis/model/ChatResponse.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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.penguineering.hareairis.model; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.UUID; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class ChatResponse { | ||
@JsonProperty("request_id") | ||
private UUID requestId; | ||
|
||
@JsonProperty("response") | ||
private String response; | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/com/penguineering/hareairis/rmq/ChatRequestHandler.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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.penguineering.hareairis.rmq; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.penguineering.hareairis.model.ChatRequest; | ||
import com.penguineering.hareairis.model.ChatResponse; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.ai.chat.client.ChatClient; | ||
import org.springframework.amqp.core.Message; | ||
import org.springframework.amqp.core.MessageProperties; | ||
import org.springframework.amqp.rabbit.core.RabbitTemplate; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class ChatRequestHandler { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ChatRequestHandler.class); | ||
private final ObjectMapper objectMapper; | ||
private final ChatClient.Builder chatClientBuilder; | ||
private final RabbitTemplate rabbitTemplate; | ||
|
||
public ChatRequestHandler(ObjectMapper objectMapper, | ||
ChatClient.Builder builder, | ||
RabbitTemplate rabbitTemplate) { | ||
this.objectMapper = objectMapper; | ||
this.chatClientBuilder = builder; | ||
this.rabbitTemplate = rabbitTemplate; | ||
} | ||
|
||
public void handleMessage(Message message) { | ||
try { | ||
logger.info("Received message: {}", new String(message.getBody())); | ||
ChatRequest chatRequest = objectMapper.readValue(message.getBody(), ChatRequest.class); | ||
|
||
// Extract the "reply-to" header | ||
MessageProperties properties = message.getMessageProperties(); | ||
String replyTo = properties.getReplyTo(); | ||
logger.info("Reply-to header: {}", replyTo); | ||
|
||
ChatClient chatClient = chatClientBuilder.build(); | ||
String response = chatClient | ||
.prompt(chatRequest.getMessage()) | ||
.call() | ||
.content(); | ||
|
||
logger.info("Received response from OpenAI: {}", response); | ||
|
||
ChatResponse chatResponse = new ChatResponse(chatRequest.getRequestId(), response); | ||
|
||
// Convert ChatResponse to JSON | ||
String jsonResponse = objectMapper.writeValueAsString(chatResponse); | ||
|
||
// Send the response to the replyTo queue | ||
rabbitTemplate.convertAndSend(replyTo, jsonResponse); | ||
} catch (Exception e) { | ||
logger.error("Failed to process message", e); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/penguineering/hareairis/rmq/RabbitMQConfig.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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.penguineering.hareairis.rmq; | ||
|
||
import org.springframework.amqp.core.Queue; | ||
import org.springframework.amqp.rabbit.annotation.EnableRabbit; | ||
import org.springframework.amqp.rabbit.connection.ConnectionFactory; | ||
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; | ||
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.amqp.support.converter.MessageConverter; | ||
import org.springframework.amqp.support.converter.SimpleMessageConverter; | ||
|
||
@Configuration | ||
@EnableRabbit | ||
public class RabbitMQConfig { | ||
|
||
@Value("${hareairis.rabbitmq.queue-chat-requests}") | ||
private String queueChatRequests; | ||
|
||
@Bean | ||
public Queue chatRequestsQueue() { | ||
return new Queue(queueChatRequests, true); | ||
} | ||
|
||
@Bean | ||
public SimpleMessageListenerContainer chatRequestsContainer(ConnectionFactory connectionFactory, | ||
MessageListenerAdapter listenerAdapter) { | ||
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); | ||
container.setConnectionFactory(connectionFactory); | ||
container.setQueueNames(queueChatRequests); | ||
container.setMessageListener(listenerAdapter); | ||
return container; | ||
} | ||
|
||
@Bean | ||
public MessageListenerAdapter chatRequestsListenerAdapter(ChatRequestHandler handler) { | ||
MessageListenerAdapter adapter = new MessageListenerAdapter(handler, "handleMessage"); | ||
adapter.setMessageConverter(null); // Ensure the whole message is passed | ||
return adapter; | ||
} | ||
} |
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