Skip to content

Commit eee33bf

Browse files
committed
Add support for chat and tool call messages
(cherry picked from commit e4e1da5)
1 parent c53b855 commit eee33bf

File tree

5 files changed

+83
-27
lines changed

5 files changed

+83
-27
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import java.sql.SQLException;
55
import java.util.Date;
6+
import java.util.List;
7+
68
import com.genexus.db.Namespace;
79
import com.genexus.db.UserInformation;
810
import com.genexus.diagnostics.GXDebugInfo;
@@ -261,12 +263,22 @@ protected void mockExecute() {
261263
}
262264

263265
protected String callAssistant(String assistant, GXProperties properties, CallResult result) {
266+
return callAssistant(assistant, properties, null, result);
267+
}
268+
269+
protected String callAssistant(String assistant, GXProperties properties, List<OpenAIResponse.Message> messages, CallResult result) {
264270
OpenAIRequest aiRequest = new OpenAIRequest();
265271
aiRequest.setModel(String.format("saia:agent:%s", assistant));
272+
if (messages != null)
273+
aiRequest.setMessages(messages);
266274
aiRequest.setVariables(properties.getList());
267275
OpenAIResponse aiResponse = SaiaService.call(aiRequest, result);
268-
if (aiResponse != null)
269-
return aiResponse.getChoices().get(0).getMessage().getContent();
276+
if (aiResponse != null) {
277+
for (OpenAIResponse.Choice element : aiResponse.getChoices()) {
278+
if (element.getFinishReason().equals("stop"))
279+
return element.getMessage().getContent();
280+
}
281+
}
270282
return "";
271283
}
272284
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ public class OpenAIRequest {
1414
@JsonProperty("model")
1515
private String model;
1616

17+
@JsonProperty("messages")
18+
private List<OpenAIResponse.Message> messages;
19+
1720
@JsonProperty("prompt")
1821
private String prompt;
1922

@@ -50,6 +53,9 @@ public class OpenAIRequest {
5053
public String getModel() { return model; }
5154
public void setModel(String model) { this.model = model; }
5255

56+
public List<OpenAIResponse.Message> getMessages() { return messages; }
57+
public void setMessages(List<OpenAIResponse.Message> messages) { this.messages = messages; }
58+
5359
public String getPrompt() { return prompt; }
5460
public void setPrompt(String prompt) { this.prompt = prompt; }
5561

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

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public class OpenAIResponse {
2222
@JsonProperty("usage")
2323
private Usage usage;
2424

25+
@JsonProperty("tool_calls")
26+
private List<Message> tool_calls;
27+
2528
@JsonProperty("data")
2629
private List<DataItem> data;
2730

@@ -40,6 +43,9 @@ public class OpenAIResponse {
4043
public Usage getUsage() { return usage; }
4144
public void setUsage(Usage usage) { this.usage = usage; }
4245

46+
public List<Message> getToolCalls() { return tool_calls; }
47+
public void setToolCalls(List<Message> tool_calls) { this.tool_calls = tool_calls; }
48+
4349
public List<DataItem> getData() { return data; }
4450
public void setData(List<DataItem> data) { this.data = data; }
4551

@@ -77,6 +83,9 @@ public static class Message {
7783
@JsonProperty("tool_calls")
7884
private List<ToolCall> toolCalls;
7985

86+
@JsonProperty("tool_call_id")
87+
private String toolCallId;
88+
8089
public String getRole() { return role; }
8190
public void setRole(String role) { this.role = role; }
8291

@@ -85,44 +94,47 @@ public static class Message {
8594

8695
public List<ToolCall> getToolCalls() { return toolCalls; }
8796
public void setToolCalls(List<ToolCall> toolCalls) { this.toolCalls = toolCalls; }
97+
98+
public String getToolCallId() { return toolCallId; }
99+
public void setToolCallId(String toolCallId) { this.toolCallId = toolCallId; }
88100
}
89101

90102
@JsonIgnoreProperties(ignoreUnknown = true)
91103
public static class ToolCall {
92104

93-
@JsonProperty("tool_name")
94-
private String toolName;
105+
@JsonProperty("id")
106+
private String id;
95107

96-
@JsonProperty("tool_input")
97-
private ToolInput toolInput;
108+
@JsonProperty("type")
109+
private String type;
98110

99-
@JsonProperty("tool_output")
100-
private String toolOutput;
111+
@JsonProperty("function")
112+
private Function function;
101113

102-
public String getToolName() { return toolName; }
103-
public void setToolName(String toolName) { this.toolName = toolName; }
114+
public String getId() { return id; }
115+
public void setId(String id) { this.id = id; }
104116

105-
public ToolInput getToolInput() { return toolInput; }
106-
public void setToolInput(ToolInput toolInput) { this.toolInput = toolInput; }
117+
public String getType() { return type; }
118+
public void setType(String type) { this.type = type; }
107119

108-
public String getToolOutput() { return toolOutput; }
109-
public void setToolOutput(String toolOutput) { this.toolOutput = toolOutput; }
120+
public Function getFunction() { return function; }
121+
public void setFunction(Function function) { this.function = function; }
110122
}
111123

112124
@JsonIgnoreProperties(ignoreUnknown = true)
113-
public static class ToolInput {
125+
public static class Function {
114126

115-
@JsonProperty("prompt")
116-
private String prompt;
127+
@JsonProperty("name")
128+
private String name;
117129

118-
@JsonProperty("size")
119-
private String size;
130+
@JsonProperty("arguments")
131+
private String arguments;
120132

121-
public String getPrompt() { return prompt; }
122-
public void setPrompt(String prompt) { this.prompt = prompt; }
133+
public String getName() { return name; }
134+
public void setName(String name) { this.name = name; }
123135

124-
public String getSize() { return size; }
125-
public void setSize(String size) { this.size = size; }
136+
public String getArguments() { return arguments; }
137+
public void setArguments(String arguments) { this.arguments = arguments; }
126138
}
127139

128140
@JsonIgnoreProperties(ignoreUnknown = true)

java/src/test/java/com/genexus/assistant/Assistant.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import com.genexus.*;
44
import com.genexus.util.CallResult;
5+
import com.genexus.util.saia.OpenAIResponse;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
59

610
public final class Assistant extends GXProcedure
711
{
@@ -30,10 +34,28 @@ private void execute_int( String aP0 ,
3034
protected void privateExecute( )
3135
{
3236
Gxproperties = new com.genexus.util.GXProperties();
33-
Gxproperties.set("&Parameter1", AV3Parameter1);
34-
Gxproperties.set("&Parameter2", AV4Parameter2);
35-
Gxproperties.set("$context", "Los Angeles");
36-
AV5OutputVariable = callAssistant( "The weatherman", Gxproperties, new CallResult()) ;
37+
List<OpenAIResponse.Message> messages = null;
38+
if (AV3Parameter1.equals("chat")) {
39+
messages = new ArrayList<>();
40+
OpenAIResponse.Message message = new OpenAIResponse.Message();
41+
message.setRole("user");
42+
message.setContent("Dime el clima en Lima - Peru");
43+
messages.add(message);
44+
message = new OpenAIResponse.Message();
45+
message.setRole("assistant");
46+
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.");
47+
messages.add(message);
48+
message = new OpenAIResponse.Message();
49+
message.setRole("user");
50+
message.setContent("Que me puedes contar de la ciudad que te pedi el clima previamente?");
51+
messages.add(message);
52+
}
53+
else {
54+
Gxproperties.set("&Parameter1", AV3Parameter1);
55+
Gxproperties.set("&Parameter2", AV4Parameter2);
56+
Gxproperties.set("$context", "Los Angeles");
57+
}
58+
AV5OutputVariable = callAssistant( "The weatherman", Gxproperties, messages, new CallResult()) ;
3759
cleanup();
3860
}
3961

java/src/test/java/com/genexus/assistant/TestAssistant.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,9 @@ public void testAPICallAssistant() {
2121
String[] GXv_char4 = new String[1] ;
2222
new com.genexus.assistant.Assistant(-1).execute( "Today", "Tomorrow", GXv_char4) ;
2323
System.out.println(GXv_char4[0]);
24+
25+
String[] GXv_char5 = new String[1] ;
26+
new com.genexus.assistant.Assistant(-1).execute( "chat", "", GXv_char5) ;
27+
System.out.println(GXv_char5[0]);
2428
}
2529
}

0 commit comments

Comments
 (0)