Skip to content

Commit

Permalink
- Add Assitant Object support in Java Generator
Browse files Browse the repository at this point in the history
- Change Embedding implementation to use common util classes to call saia.
  • Loading branch information
iroqueta committed Nov 5, 2024
1 parent 0267992 commit a02abfb
Show file tree
Hide file tree
Showing 12 changed files with 433 additions and 114 deletions.
14 changes: 11 additions & 3 deletions common/src/main/java/com/genexus/util/GXProperties.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.genexus.util;

import java.util.LinkedHashMap;
import java.util.*;

import com.genexus.internet.IGxJSONSerializable;

Expand All @@ -10,8 +10,6 @@
import com.genexus.CommonUtil;
import com.genexus.SdtMessages_Message;
import com.genexus.GXBaseCollection;
import java.util.Iterator;
import java.util.Map;

public class GXProperties implements IGxJSONSerializable {
private LinkedHashMap < String, GXProperty > properties = new LinkedHashMap < > ();
Expand Down Expand Up @@ -118,6 +116,16 @@ public String toJSonString() {
return jObj.toString();
}

public List<GXProperty> getList() {
List<GXProperty> list = new ArrayList<>();
int i = 0;
while (count() > i) {
list.add(item(i));
i++;
}
return list;
}

public boolean fromJSonString(String s) {
return fromJSonString(s, null);
}
Expand Down
27 changes: 0 additions & 27 deletions gxembedding/pom.xml

This file was deleted.

This file was deleted.

16 changes: 14 additions & 2 deletions java/src/main/java/com/genexus/GXProcedure.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import com.genexus.mock.GXMockProvider;
import com.genexus.performance.ProcedureInfo;
import com.genexus.performance.ProceduresInfo;
import com.genexus.util.ReorgSubmitThreadPool;
import com.genexus.util.SubmitThreadPool;
import com.genexus.util.*;
import com.genexus.util.saia.OpenAIRequest;
import com.genexus.util.saia.OpenAIResponse;
import com.genexus.util.saia.SaiaService;

public abstract class GXProcedure implements IErrorHandler, ISubmitteable {
public abstract void initialize();
Expand Down Expand Up @@ -257,4 +259,14 @@ protected void mockExecute() {
}
privateExecute( );
}

protected String callAssistant(String assistant, GXProperties properties, Object response) {
OpenAIRequest aiRequest = new OpenAIRequest();
aiRequest.setModel(String.format("saia:agent:%s", assistant));
aiRequest.setVariables(properties.getList());
OpenAIResponse aiResponse = SaiaService.call(aiRequest);
if (aiResponse != null)
return aiResponse.getChoices().get(0).getMessage().getContent();
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.genexus.CommonUtil;
import com.genexus.GXBaseCollection;
import com.genexus.SdtMessages_Message;
import com.genexus.embedding.EmbeddingService;
import com.genexus.util.saia.OpenAIRequest;
import com.genexus.util.saia.OpenAIResponse;
import com.genexus.util.saia.SaiaService;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -53,7 +55,7 @@ public Float[] getFloatArray() {

public static GXEmbedding generateEmbedding(GXEmbedding embeddingInfo, String text, GXBaseCollection<SdtMessages_Message> Messages) {
try {
List<Float> embedding = EmbeddingService.getInstance().getEmbedding(embeddingInfo.getModel(), embeddingInfo.getDimensions(), text);
List<Float> embedding = getEmbedding(embeddingInfo.getModel(), embeddingInfo.getDimensions(), text);
embeddingInfo.setEmbedding(embedding);
}
catch (Exception ex) {
Expand All @@ -62,6 +64,26 @@ public static GXEmbedding generateEmbedding(GXEmbedding embeddingInfo, String te
return embeddingInfo;
}

public static List<Float> getEmbedding(String model, int dimensions, String input) {
List<String> inputList = new ArrayList<>();
inputList.add(input);
return getEmbedding(model, dimensions, inputList);
}

public static List<Float> getEmbedding(String model, int dimensions, List<String> inputList) {
OpenAIRequest aiRequest = new OpenAIRequest();
aiRequest.setModel(model);
aiRequest.setInput(inputList);
aiRequest.setDimension(dimensions);
OpenAIResponse aiResponse = SaiaService.call(aiRequest, true);
if (aiResponse != null)
return aiResponse.getData().get(0).getEmbedding().stream()
.map(Double::floatValue)
.collect(Collectors.toList());

return new ArrayList<>();
}

public String toString()
{
return embedding.stream()
Expand Down
85 changes: 85 additions & 0 deletions java/src/main/java/com/genexus/util/saia/OpenAIRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.genexus.util.saia;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.genexus.util.GXProperty;

import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class OpenAIRequest {

@JsonProperty("model")
private String model;

@JsonProperty("prompt")
private String prompt;

@JsonProperty("input")
private List<String> input;

@JsonProperty("max_tokens")
private Integer maxTokens;

@JsonProperty("temperature")
private Double temperature;

@JsonProperty("stream")
private Boolean stream;

@JsonProperty("stop")
private List<String> stop;

@JsonProperty("presence_penalty")
private Double presencePenalty;

@JsonProperty("frequency_penalty")
private Double frequencyPenalty;

@JsonProperty("user")
private String user;

@JsonProperty("variables")
private List<GXProperty> variables;

@JsonProperty("dimensions")
private int dimension;

public String getModel() { return model; }
public void setModel(String model) { this.model = model; }

public String getPrompt() { return prompt; }
public void setPrompt(String prompt) { this.prompt = prompt; }

public List<String> getInput() { return input; }
public void setInput(List<String> input) { this.input = input; }

public Integer getMaxTokens() { return maxTokens; }
public void setMaxTokens(Integer maxTokens) { this.maxTokens = maxTokens; }

public Double getTemperature() { return temperature; }
public void setTemperature(Double temperature) { this.temperature = temperature; }

public Boolean getStream() { return stream; }
public void setStream(Boolean stream) { this.stream = stream; }

public List<String> getStop() { return stop; }
public void setStop(List<String> stop) { this.stop = stop; }

public Double getPresencePenalty() { return presencePenalty; }
public void setPresencePenalty(Double presencePenalty) { this.presencePenalty = presencePenalty; }

public Double getFrequencyPenalty() { return frequencyPenalty; }
public void setFrequencyPenalty(Double frequencyPenalty) { this.frequencyPenalty = frequencyPenalty; }

public String getUser() { return user; }
public void setUser(String user) { this.user = user; }

public List<GXProperty> getVariables() { return variables; }
public void setVariables(List<GXProperty> variables) { this.variables = variables; }

public int getDimension() { return dimension; }
public void setDimension(int dimension) { this.dimension = dimension; }
}
Loading

0 comments on commit a02abfb

Please sign in to comment.