Skip to content

Commit

Permalink
MARP-188 update to render chat conversations
Browse files Browse the repository at this point in the history
  • Loading branch information
tvtphuc-axonivy committed May 30, 2024
1 parent 2cc5203 commit 69d1c13
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

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

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.apache.commons.lang3.StringUtils;
import com.axonivy.connector.vertexai.entities.*;
import com.axonivy.connector.vertexai.service.GeminiDataRequestService;
Expand All @@ -19,19 +16,18 @@
public class GeminiDataBean {
private String inputtedMessage;
private Model model;
private List<Content> requestContents;
private List<Conversation> conversations;
private GeminiDataRequestService geminiDataRequestService = new GeminiDataRequestService();

@PostConstruct
public void init() {
requestContents = new ArrayList<>();
conversations = new ArrayList<>();
geminiDataRequestService.cleanData();
}

public void onSendRequest() throws Exception {
Ivy.log().warn(inputtedMessage);
RequestRoot requestRoot = geminiDataRequestService.sendRequestToGemini(inputtedMessage, model);
requestContents = Optional.ofNullable(requestRoot).map(RequestRoot::getContents).orElse(new ArrayList<>());
conversations = geminiDataRequestService.sendRequestToGemini(inputtedMessage, model);
inputtedMessage = StringUtils.EMPTY;
}

Expand All @@ -44,20 +40,12 @@ public Model[] onSelectModel() {
return Model.values();
}

public List<Content> getRequestContents() {
return requestContents;
}

public void setRequestContents(List<Content> requestContents) {
this.requestContents = requestContents;
}

public String getInputedMessage() {
public String getInputtedMessage() {
return inputtedMessage;
}

public void setInputedMessage(String inputedMessage) {
this.inputtedMessage = inputedMessage;
public void setInputtedMessage(String inputtedMessage) {
this.inputtedMessage = inputtedMessage;
}

public Model getModel() {
Expand All @@ -68,4 +56,11 @@ public void setModel(Model model) {
this.model = model;
}

public List<Conversation> getConversations() {
return conversations;
}

public void setConversations(List<Conversation> conversations) {
this.conversations = conversations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@
</div>
<p:panelGrid columns="1" layout="grid"
styleClass="ui-panelgrid-blank ui-fluid">
<ui:repeat var="content" value="#{managedBean.requestContents}">
<p:panel rendered="#{content.role == 'user'}">
<h:outputText value="#{content.role}" style="font-weight:bold;" />
<h:outputText value="#{content.parts.get(0).text}" escape="false"
<ui:repeat var="conversation" value="#{managedBean.conversations}">
<p:panel rendered="#{conversation.role == 'user'}">
<h:outputText value="#{conversation.role}" style="font-weight:bold;" />
<h:outputText value="#{conversation.text}" escape="false"
style="white-space: pre-line; display:block; margin-bottom:20px;" />
</p:panel>

<p:panel rendered="#{content.role == 'model'}">
<h:outputText value="#{content.role}"
<p:panel rendered="#{conversation.role == 'model'}">
<h:outputText value="#{conversation.role}"
style="font-weight:bold; color: red" />
<h:outputText value="#{content.parts.get(0).text}"
<h:outputText value="#{conversation.text}"
style="white-space: pre-line; display:block; margin-bottom:20px;" />
</p:panel>
</ui:repeat>
<p:textEditor widgetVar="editor" id="editor"
value="#{managedBean.inputedMessage}" allowImages="true"
value="#{managedBean.inputtedMessage}" allowImages="true"
height="150" style="margin-bottom:10px"
placeholder="How can I help you today?">
<f:facet name="toolbar">
Expand All @@ -45,8 +45,6 @@
</span>
</f:facet>
</p:textEditor>


</p:panelGrid>
<br />
<div class="command-btns">
Expand Down Expand Up @@ -86,5 +84,4 @@
</ui:define>
</ui:composition>
</h:body>

</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.axonivy.connector.vertexai.entities;

public class Conversation {
private String role;
private String text;

public Conversation(String role, String text) {
this.role = role;
this.text = text;
}

public String getRole() {
return role;
}

public void setRole(String role) {
this.role = role;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class GeminiDataRequestService {
public static final String IMG_SRC_ATTR_PATTERN = "data:image\\/[^;]+;base64,([^\"]+)";

private static List<Content> historyContent = new ArrayList<>();
private static List<Conversation> conversations = new ArrayList<>();
private static final String VERTEX_URL = "https://{0}-aiplatform.googleapis.com/v1/projects/{1}/locations/{0}/publishers/google/models/{2}:generateContent";
private static final String GEMINI_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent?key={0}";

Expand Down Expand Up @@ -74,11 +75,12 @@ private static Content formatRequest(String message) {

private static RequestRoot createRequestBody(String message) {
Content requestContent = formatRequest(message);
conversations.add(new Conversation(Role.USER.getName(), message));
historyContent.add(requestContent);
return new RequestRoot(historyContent);
}

public RequestRoot sendRequestToGemini(String message, Model platFormModel) throws Exception {
public List<Conversation> sendRequestToGemini(String message, Model platFormModel) throws Exception {
RequestRoot bodyRequestContent = createRequestBody(message);
// Create HTTP client
HttpClient client = HttpClient.newHttpClient();
Expand All @@ -95,6 +97,7 @@ public RequestRoot sendRequestToGemini(String message, Model platFormModel) thro
.map(Collection::stream).flatMap(Stream::findFirst).map(Candidate::getContent)
.map(Content::getParts).map(Collection::stream).flatMap(Stream::findFirst).map(Part::getText)
.map(text -> {
conversations.add(new Conversation(Role.MODEL.getName(), text.trim()));
Part currentPart = new Part(text.trim());
return new Content(Role.MODEL.getName(), List.of(currentPart));
}).orElse(null);
Expand All @@ -105,18 +108,23 @@ public RequestRoot sendRequestToGemini(String message, Model platFormModel) thro
Part currentPart = new Part("The server is now overloaded. Please try again later");
Content contentResponse = new Content(Role.MODEL.getName(), List.of(currentPart));
historyContent.add(contentResponse);
conversations.add(
new Conversation(Role.MODEL.getName(), "The server is now overloaded. Please try again later"));
} else {
Ivy.log().error("Request failed: " + response.statusCode());
Ivy.log().error(response.body());
Part currentPart = new Part("There are some issue in server. Please try again later");
Content contentResponse = new Content(Role.MODEL.getName(), List.of(currentPart));
historyContent.add(contentResponse);
conversations.add(
new Conversation(Role.MODEL.getName(), "There are some issue in server. Please try again later"));
}
return new RequestRoot(historyContent);
return conversations;
}

public void cleanData() {
historyContent = new ArrayList<>();
conversations = new ArrayList<>();
}

private static Set<String> extractImgTagsFromArticleContent(String content) {
Expand Down

0 comments on commit 69d1c13

Please sign in to comment.