Skip to content

Commit e1e5190

Browse files
committed
Change callAssistant to callAgent
Add getExternalInstance method in EO Collections. Support ChatMessage EO
1 parent a14331b commit e1e5190

File tree

8 files changed

+162
-39
lines changed

8 files changed

+162
-39
lines changed

common/src/main/java/com/genexus/GXExternalCollection.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,22 @@ public Vector getStruct()
9898
}
9999
return struct;
100100
}
101-
101+
102+
public ArrayList getExternalInstance() {
103+
ArrayList list = new ArrayList();
104+
for (T Item : this)
105+
{
106+
try
107+
{
108+
list.add(Item.getClass().getMethod("getExternalInstance", new Class[]{}).invoke(Item));
109+
}
110+
catch (Exception e)
111+
{
112+
e.printStackTrace();
113+
}
114+
}
115+
return list;
116+
}
117+
102118
}
103119

common/src/main/java/com/genexus/util/GXProperties.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ public String toJSonString() {
116116
return jObj.toString();
117117
}
118118

119-
public List<GXProperty> getList() {
120-
List<GXProperty> list = new ArrayList<>();
119+
public ArrayList<GXProperty> getList() {
120+
ArrayList<GXProperty> list = new ArrayList<>();
121121
int i = 0;
122122
while (count() > i) {
123123
list.add(item(i));

java/src/main/java/com/genexus/GXProcedure.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package com.genexus;
33

44
import java.sql.SQLException;
5+
import java.util.ArrayList;
56
import java.util.Date;
67
import java.util.List;
78

@@ -262,14 +263,14 @@ protected void mockExecute() {
262263
privateExecute( );
263264
}
264265

265-
protected String callAssistant(String assistant, GXProperties properties, CallResult result) {
266-
return callAssistant(assistant, properties, null, result);
266+
protected String callAssistant(String agent, GXProperties properties, ArrayList<OpenAIResponse.Message> messages, CallResult result) {
267+
return callAgent(agent, properties, messages, result);
267268
}
268269

269-
protected String callAssistant(String assistant, GXProperties properties, List<OpenAIResponse.Message> messages, CallResult result) {
270+
protected String callAgent(String agent, GXProperties properties, ArrayList<OpenAIResponse.Message> messages, CallResult result) {
270271
OpenAIRequest aiRequest = new OpenAIRequest();
271-
aiRequest.setModel(String.format("saia:agent:%s", assistant));
272-
if (messages != null)
272+
aiRequest.setModel(String.format("saia:agent:%s", agent));
273+
if (!messages.isEmpty())
273274
aiRequest.setMessages(messages);
274275
aiRequest.setVariables(properties.getList());
275276
OpenAIResponse aiResponse = SaiaService.call(aiRequest, result);

java/src/main/java/com/genexus/db/GXEmbedding.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ public static GXEmbedding generateEmbedding(GXEmbedding embeddingInfo, String te
6666
}
6767

6868
public static List<Float> getEmbedding(String model, int dimensions, String input) {
69-
List<String> inputList = new ArrayList<>();
69+
ArrayList<String> inputList = new ArrayList<>();
7070
inputList.add(input);
7171
return getEmbedding(model, dimensions, inputList);
7272
}
7373

74-
public static List<Float> getEmbedding(String model, int dimensions, List<String> inputList) {
74+
public static List<Float> getEmbedding(String model, int dimensions, ArrayList<String> inputList) {
7575
OpenAIRequest aiRequest = new OpenAIRequest();
7676
aiRequest.setModel(model);
7777
aiRequest.setInput(inputList);

java/src/main/java/com/genexus/util/saia/OpenAIRequest.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.fasterxml.jackson.annotation.JsonProperty;
66
import com.genexus.util.GXProperty;
77

8-
import java.util.List;
8+
import java.util.ArrayList;
99

1010
@JsonIgnoreProperties(ignoreUnknown = true)
1111
@JsonInclude(JsonInclude.Include.NON_NULL)
@@ -15,13 +15,13 @@ public class OpenAIRequest {
1515
private String model;
1616

1717
@JsonProperty("messages")
18-
private List<OpenAIResponse.Message> messages;
18+
private ArrayList<OpenAIResponse.Message> messages;
1919

2020
@JsonProperty("prompt")
2121
private String prompt;
2222

2323
@JsonProperty("input")
24-
private List<String> input;
24+
private ArrayList<String> input;
2525

2626
@JsonProperty("max_tokens")
2727
private Integer maxTokens;
@@ -33,7 +33,7 @@ public class OpenAIRequest {
3333
private Boolean stream;
3434

3535
@JsonProperty("stop")
36-
private List<String> stop;
36+
private ArrayList<String> stop;
3737

3838
@JsonProperty("presence_penalty")
3939
private Double presencePenalty;
@@ -45,22 +45,22 @@ public class OpenAIRequest {
4545
private String user;
4646

4747
@JsonProperty("variables")
48-
private List<GXProperty> variables;
48+
private ArrayList<GXProperty> variables;
4949

5050
@JsonProperty("dimensions")
5151
private int dimension;
5252

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

56-
public List<OpenAIResponse.Message> getMessages() { return messages; }
57-
public void setMessages(List<OpenAIResponse.Message> messages) { this.messages = messages; }
56+
public ArrayList<OpenAIResponse.Message> getMessages() { return messages; }
57+
public void setMessages(ArrayList<OpenAIResponse.Message> messages) { this.messages = messages; }
5858

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

62-
public List<String> getInput() { return input; }
63-
public void setInput(List<String> input) { this.input = input; }
62+
public ArrayList<String> getInput() { return input; }
63+
public void setInput(ArrayList<String> input) { this.input = input; }
6464

6565
public Integer getMaxTokens() { return maxTokens; }
6666
public void setMaxTokens(Integer maxTokens) { this.maxTokens = maxTokens; }
@@ -71,8 +71,8 @@ public class OpenAIRequest {
7171
public Boolean getStream() { return stream; }
7272
public void setStream(Boolean stream) { this.stream = stream; }
7373

74-
public List<String> getStop() { return stop; }
75-
public void setStop(List<String> stop) { this.stop = stop; }
74+
public ArrayList<String> getStop() { return stop; }
75+
public void setStop(ArrayList<String> stop) { this.stop = stop; }
7676

7777
public Double getPresencePenalty() { return presencePenalty; }
7878
public void setPresencePenalty(Double presencePenalty) { this.presencePenalty = presencePenalty; }
@@ -83,8 +83,8 @@ public class OpenAIRequest {
8383
public String getUser() { return user; }
8484
public void setUser(String user) { this.user = user; }
8585

86-
public List<GXProperty> getVariables() { return variables; }
87-
public void setVariables(List<GXProperty> variables) { this.variables = variables; }
86+
public ArrayList<GXProperty> getVariables() { return variables; }
87+
public void setVariables(ArrayList<GXProperty> variables) { this.variables = variables; }
8888

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

java/src/main/java/com/genexus/util/saia/OpenAIResponse.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
44
import com.fasterxml.jackson.annotation.JsonProperty;
5-
import java.util.List;
5+
6+
import java.util.ArrayList;
67

78
@JsonIgnoreProperties(ignoreUnknown = true)
89
public class OpenAIResponse {
@@ -17,16 +18,16 @@ public class OpenAIResponse {
1718
private long created;
1819

1920
@JsonProperty("choices")
20-
private List<Choice> choices;
21+
private ArrayList<Choice> choices;
2122

2223
@JsonProperty("usage")
2324
private Usage usage;
2425

2526
@JsonProperty("tool_calls")
26-
private List<Message> tool_calls;
27+
private ArrayList<Message> tool_calls;
2728

2829
@JsonProperty("data")
29-
private List<DataItem> data;
30+
private ArrayList<DataItem> data;
3031

3132
public String getId() { return id; }
3233
public void setId(String id) { this.id = id; }
@@ -37,17 +38,17 @@ public class OpenAIResponse {
3738
public long getCreated() { return created; }
3839
public void setCreated(long created) { this.created = created; }
3940

40-
public List<Choice> getChoices() { return choices; }
41-
public void setChoices(List<Choice> choices) { this.choices = choices; }
41+
public ArrayList<Choice> getChoices() { return choices; }
42+
public void setChoices(ArrayList<Choice> choices) { this.choices = choices; }
4243

4344
public Usage getUsage() { return usage; }
4445
public void setUsage(Usage usage) { this.usage = usage; }
4546

46-
public List<Message> getToolCalls() { return tool_calls; }
47-
public void setToolCalls(List<Message> tool_calls) { this.tool_calls = tool_calls; }
47+
public ArrayList<Message> getToolCalls() { return tool_calls; }
48+
public void setToolCalls(ArrayList<Message> tool_calls) { this.tool_calls = tool_calls; }
4849

49-
public List<DataItem> getData() { return data; }
50-
public void setData(List<DataItem> data) { this.data = data; }
50+
public ArrayList<DataItem> getData() { return data; }
51+
public void setData(ArrayList<DataItem> data) { this.data = data; }
5152

5253
@JsonIgnoreProperties(ignoreUnknown = true)
5354
public static class Choice {
@@ -81,7 +82,7 @@ public static class Message {
8182
private String content;
8283

8384
@JsonProperty("tool_calls")
84-
private List<ToolCall> toolCalls;
85+
private ArrayList<ToolCall> toolCalls;
8586

8687
@JsonProperty("tool_call_id")
8788
private String toolCallId;
@@ -92,8 +93,8 @@ public static class Message {
9293
public String getContent() { return content; }
9394
public void setContent(String content) { this.content = content; }
9495

95-
public List<ToolCall> getToolCalls() { return toolCalls; }
96-
public void setToolCalls(List<ToolCall> toolCalls) { this.toolCalls = toolCalls; }
96+
public ArrayList<ToolCall> getToolCalls() { return toolCalls; }
97+
public void setToolCalls(ArrayList<ToolCall> toolCalls) { this.toolCalls = toolCalls; }
9798

9899
public String getToolCallId() { return toolCallId; }
99100
public void setToolCallId(String toolCallId) { this.toolCallId = toolCallId; }
@@ -169,15 +170,15 @@ public static class DataItem {
169170
private String object;
170171

171172
@JsonProperty("embedding")
172-
private List<Double> embedding;
173+
private ArrayList<Double> embedding;
173174

174175
public String getId() { return id; }
175176
public void setId(String id) { this.id = id; }
176177

177178
public String getObject() { return object; }
178179
public void setObject(String object) { this.object = object; }
179180

180-
public List<Double> getEmbedding() { return embedding; }
181-
public void setEmbedding(List<Double> embedding) { this.embedding = embedding; }
181+
public ArrayList<Double> getEmbedding() { return embedding; }
182+
public void setEmbedding(ArrayList<Double> embedding) { this.embedding = embedding; }
182183
}
183184
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.genexus.agent;
2+
3+
import com.genexus.*;
4+
import com.genexus.util.CallResult;
5+
import com.genexus.util.saia.OpenAIResponse;
6+
7+
import java.util.ArrayList;
8+
9+
public final class Agent extends GXProcedure
10+
{
11+
public Agent(int remoteHandle )
12+
{
13+
super( remoteHandle , new ModelContext( Agent.class ), "" );
14+
}
15+
16+
public void execute( String aP0 ,
17+
String aP1 ,
18+
String[] aP2 )
19+
{
20+
execute_int(aP0, aP1, aP2);
21+
}
22+
23+
private void execute_int( String aP0 ,
24+
String aP1 ,
25+
String[] aP2 )
26+
{
27+
AV3Parameter1 = aP0;
28+
AV4Parameter2 = aP1;
29+
this.aP2 = aP2;
30+
privateExecute();
31+
}
32+
33+
protected void privateExecute( )
34+
{
35+
Gxproperties = new com.genexus.util.GXProperties();
36+
ArrayList messages = new ArrayList();;
37+
if (AV3Parameter1.equals("chat")) {
38+
OpenAIResponse.Message message = new OpenAIResponse.Message();
39+
message.setRole("user");
40+
message.setContent("Dime el clima en Lima - Peru");
41+
messages.add(message);
42+
message = new OpenAIResponse.Message();
43+
message.setRole("assistant");
44+
message.setContent("El clima actual en Lima, Perú, es soleado con una temperatura de 20.9°C (69.6°F). La dirección del viento es del suroeste (SSW) a 15.1 km/h (9.4 mph), y la humedad relativa es del 68%. La presión atmosférica es de 1013 mb. La visibilidad es de 10 km y el índice UV es de 12.5.");
45+
messages.add(message);
46+
message = new OpenAIResponse.Message();
47+
message.setRole("user");
48+
message.setContent("Que me puedes contar de la ciudad que te pedi el clima previamente?");
49+
messages.add(message);
50+
}
51+
else {
52+
Gxproperties.set("&Parameter1", AV3Parameter1);
53+
Gxproperties.set("&Parameter2", AV4Parameter2);
54+
Gxproperties.set("$context", "Los Angeles");
55+
}
56+
AV5OutputVariable = callAgent( "The weatherman", Gxproperties, messages, new CallResult()) ;
57+
cleanup();
58+
}
59+
60+
protected void cleanup( )
61+
{
62+
this.aP2[0] = AV5OutputVariable;
63+
}
64+
65+
public void initialize( )
66+
{
67+
}
68+
69+
String AV3Parameter1 ;
70+
String AV4Parameter2 ;
71+
String AV5OutputVariable ;
72+
com.genexus.util.GXProperties Gxproperties ;
73+
String[] aP2 ;
74+
}
75+
76+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.genexus.agent;
2+
3+
import com.genexus.Application;
4+
import com.genexus.sampleapp.GXcfg;
5+
import com.genexus.specific.java.Connect;
6+
import com.genexus.specific.java.LogManager;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
public class TestAgent {
11+
12+
@Before
13+
public void setUpStreams() {
14+
Connect.init();
15+
LogManager.initialize(".");
16+
Application.init(GXcfg.class);
17+
}
18+
19+
@Test
20+
public void testAPICallAgent() {
21+
String[] GXv_char4 = new String[1] ;
22+
new Agent(-1).execute( "Today", "Tomorrow", GXv_char4) ;
23+
System.out.println(GXv_char4[0]);
24+
25+
String[] GXv_char5 = new String[1] ;
26+
new Agent(-1).execute( "chat", "", GXv_char5) ;
27+
System.out.println(GXv_char5[0]);
28+
}
29+
}

0 commit comments

Comments
 (0)