Skip to content

Commit

Permalink
MARP-188 Add Validation
Browse files Browse the repository at this point in the history
  • Loading branch information
tvtphuc-axonivy committed May 31, 2024
1 parent 69d1c13 commit b52600e
Show file tree
Hide file tree
Showing 9 changed files with 420 additions and 26 deletions.
37 changes: 34 additions & 3 deletions vertexai-google-demo/processes/vertexAi.p.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"elements" : [ {
"id" : "f0",
"type" : "RequestStart",
"name" : "start.ivp",
"name" : "UsingTextEditor.ivp",
"config" : {
"callSignature" : "start",
"outLink" : "start.ivp",
"callSignature" : "UsingTextEditor",
"outLink" : "UsingTextEditor.ivp",
"case" : { }
},
"visual" : {
Expand All @@ -35,5 +35,36 @@
"at" : { "x" : 264, "y" : 64 }
},
"connect" : { "id" : "f2", "to" : "f1" }
}, {
"id" : "f5",
"type" : "RequestStart",
"name" : "UsingFileUpload",
"config" : {
"callSignature" : "UsingFileUpload",
"outLink" : "UsingFileUpload.ivp",
"case" : { }
},
"visual" : {
"at" : { "x" : 120, "y" : 192 }
},
"connect" : { "id" : "f9", "to" : "f8" }
}, {
"id" : "f7",
"type" : "TaskEnd",
"visual" : {
"at" : { "x" : 400, "y" : 192 }
}
}, {
"id" : "f8",
"type" : "DialogCall",
"name" : "chatWithAi2",
"config" : {
"dialogId" : "com.axonivy.connector.vertexai.vertexai.google.demo.chatWithAi2",
"startMethod" : "start()"
},
"visual" : {
"at" : { "x" : 248, "y" : 192 }
},
"connect" : { "id" : "f6", "to" : "f7" }
} ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import com.axonivy.connector.vertexai.entities.*;
import com.axonivy.connector.vertexai.service.GeminiDataRequestService;

import ch.ivyteam.ivy.environment.Ivy;

@ManagedBean
@ViewScoped
public class GeminiDataBean {
Expand All @@ -26,7 +24,6 @@ public void init() {
}

public void onSendRequest() throws Exception {
Ivy.log().warn(inputtedMessage);
conversations = geminiDataRequestService.sendRequestToGemini(inputtedMessage, model);
inputtedMessage = StringUtils.EMPTY;
}
Expand Down
108 changes: 108 additions & 0 deletions vertexai-google-demo/src/com/axonivy/managedBean/GeminiDataBean2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.axonivy.managedBean;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.event.FilesUploadEvent;
import org.primefaces.model.file.UploadedFile;
import org.primefaces.model.file.UploadedFiles;

import com.axonivy.connector.vertexai.entities.*;
import com.axonivy.connector.vertexai.service.GeminiDataRequestService;
import com.axonivy.connector.vertexai.service.GeminiDataRequestService2;

import ch.ivyteam.ivy.environment.Ivy;

@ManagedBean
@ViewScoped
public class GeminiDataBean2 {
private String inputtedMessage;
private Model model;
private List<Conversation> conversations;
private GeminiDataRequestService2 geminiDataRequestService2 = new GeminiDataRequestService2();

private UploadedFiles files;
private List<String> base64Images;

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

public void onSendRequest() throws Exception {
handleFilesUpload();
conversations = geminiDataRequestService2.sendRequestToGemini(inputtedMessage, base64Images, model);
inputtedMessage = StringUtils.EMPTY;
}

public void onCleanText() {
init();
geminiDataRequestService2.cleanData();
base64Images = new ArrayList<>();
}

public void handleFilesUpload() {
if (files != null) {
for (UploadedFile file : files.getFiles()) {
try (InputStream input = file.getInputStream()) {
base64Images.add(java.util.Base64.getEncoder().encodeToString(input.readAllBytes()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

public Model[] onSelectModel() {
return Model.values();
}

public String getInputtedMessage() {
return inputtedMessage;
}

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

public Model getModel() {
return model;
}

public void setModel(Model model) {
this.model = model;
}

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

public void setConversations(List<Conversation> conversations) {
this.conversations = conversations;
}

public UploadedFiles getFiles() {
return files;
}

public void setFiles(UploadedFiles files) {
this.files = files;
}

public List<String> getBase64Images() {
return base64Images;
}

public void setBase64Images(List<String> base64Images) {
this.base64Images = base64Images;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,59 @@
xmlns:ic="http://ivyteam.ch/jsf/component"
xmlns:p="http://primefaces.org/ui"
xmlns:pe="http://primefaces.org/ui/extensions">

<h:body>
<ui:composition template="/layouts/basic-10.xhtml">
<ui:param name="managedBean" value="#{geminiDataBean}" />
<ui:define name="title">ChatWithAi</ui:define>
<ui:define name="content">
<h3>AI Chat</h3>
<h:form id="form">
<div>
<p:outputLabel for="option" value="Model" />
<p:selectOneMenu id="option" value="#{managedBean.model}">
<f:selectItems value="#{managedBean.onSelectModel()}" var="type"
itemLabel="#{type.getName()}" itemValue="#{type}" />
</p:selectOneMenu>
</div>
<h:form id="form" styleClass="form-container">
<p:panelGrid columns="1" layout="grid"
styleClass="ui-panelgrid-blank ui-fluid">
styleClass="ui-panelgrid-blank ui-noborder">
<div id="model-option">
<p:outputLabel for="option" value="Model:" />
<p:selectOneMenu id="option" value="#{managedBean.model}"
autoWidth="false" style="width: 150px;">
<f:selectItems value="#{managedBean.onSelectModel()}" var="type"
itemLabel="#{type.getName()}" itemValue="#{type}" />
</p:selectOneMenu>
</div>
<ui:repeat var="conversation" value="#{managedBean.conversations}">
<p:panel rendered="#{conversation.role == 'user'}">
<h:outputText value="#{conversation.role}" style="font-weight:bold;" />
<p:panel rendered="#{conversation.role == 'user'}"
styleClass="model-panel ui-fluid" style="text-align:right;">
<h:outputText value="#{ivy.session.sessionUser.displayName}"
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="#{conversation.role == 'model'}">
<p:panel rendered="#{conversation.role == 'model'}"
styleClass="ui-fluid">
<h:outputText value="#{conversation.role}"
style="font-weight:bold; color: red" />
<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.inputtedMessage}" allowImages="true"
height="150" style="margin-bottom:10px"
required="true" toolbarVisible="false" height="100"
requiredMessage="This field is required"
placeholder="How can I help you today?">
<f:facet name="toolbar">
<span class="ql-formats">
<button class="ql-image"></button>
</span>
</f:facet>
</p:textEditor>
<p:messages id="messages" closable="true" for="editor" />
</p:panelGrid>
<br />
<div class="command-btns">
<p:commandButton id="refresh" value="refresh" update="form"
actionListener="#{managedBean.onCleanText()}" icon="pi pi-refresh" />
<p:commandButton id="proceed" value="send" update="form"
actionListener="#{managedBean.onSendRequest()}" icon="pi pi-check" />
</div>
</h:form>
<script type="text/javascript">

<script type="text/javascript">
function handlePaste(event) {
var clipboardData = event.clipboardData || window.clipboardData;
var items = clipboardData.items;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<richDialogDescriptor>
<property>
<name>viewTechnology</name>
<value>JSF</value>
</property>
</richDialogDescriptor>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:ic="http://ivyteam.ch/jsf/component"
xmlns:p="http://primefaces.org/ui"
xmlns:pe="http://primefaces.org/ui/extensions">
<h:body>
<ui:composition template="/layouts/basic-10.xhtml">
<ui:param name="managedBean" value="#{geminiDataBean2}" />
<ui:define name="title">ChatWithAi</ui:define>
<ui:define name="content">
<h3>AI Chat</h3>
<h:form id="form" enctype="multipart/form-data">
<div>
<p:outputLabel for="option" value="Model" />
<p:selectOneMenu id="option" value="#{managedBean.model}">
<f:selectItems value="#{managedBean.onSelectModel()}" var="type"
itemLabel="#{type.getName()}" itemValue="#{type}" />
</p:selectOneMenu>
</div>
<p:panelGrid columns="1" layout="grid"
styleClass="ui-panelgrid-blank ui-fluid">
<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="#{conversation.role == 'model'}">
<h:outputText value="#{conversation.role}"
style="font-weight:bold; color: red" />
<h:outputText value="#{conversation.text}"
style="white-space: pre-line; display:block; margin-bottom:20px;" />
</p:panel>
</ui:repeat>
<p:inputTextarea widgetVar="editor" id="editor"
value="#{managedBean.inputtedMessage}" height="150"
style="margin-bottom:10px" placeholder="How can I help you today?">
</p:inputTextarea>
</p:panelGrid>
<br />
<div class="command-btns">
<p:fileUpload value="#{managedBean.files}" mode="simple" listener="#{managedBean.dandleFilesUpload()}"
skinSimple="true" multiple="true"/>
<p:commandButton id="refresh" value="refresh" update="form"
actionListener="#{managedBean.onCleanText()}" icon="pi pi-refresh" />
<p:commandButton id="proceed" value="send" update="form"
actionListener="#{managedBean.onSendRequest()}" icon="pi pi-check" />
</div>
</h:form>
</ui:define>
</ui:composition>
</h:body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
chatWithAi2Data #class
com.axonivy.connector.vertexai.vertexai.google.demo.chatWithAi2 #namespace
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"format" : "10.0.0",
"id" : "18FC83D4B4D20E99",
"kind" : "HTML_DIALOG",
"config" : {
"data" : "com.axonivy.connector.vertexai.vertexai.google.demo.chatWithAi2.chatWithAi2Data"
},
"elements" : [ {
"id" : "f0",
"type" : "HtmlDialogStart",
"name" : "start()",
"config" : {
"callSignature" : "start",
"guid" : "18FC83D4B501EC7A"
},
"visual" : {
"at" : { "x" : 96, "y" : 64 }
},
"connect" : { "id" : "f2", "to" : "f1" }
}, {
"id" : "f1",
"type" : "HtmlDialogEnd",
"visual" : {
"at" : { "x" : 224, "y" : 64 }
}
}, {
"id" : "f3",
"type" : "HtmlDialogEventStart",
"name" : "close",
"config" : {
"guid" : "18FC83D4B8371B51"
},
"visual" : {
"at" : { "x" : 96, "y" : 160 }
},
"connect" : { "id" : "f5", "to" : "f4" }
}, {
"id" : "f4",
"type" : "HtmlDialogExit",
"visual" : {
"at" : { "x" : 224, "y" : 160 }
}
} ]
}
Loading

0 comments on commit b52600e

Please sign in to comment.