Skip to content

Commit 0d66b44

Browse files
committed
Add stream in chat Agent calls.
(cherry picked from commit e33217a)
1 parent 4e50bbd commit 0d66b44

File tree

3 files changed

+72
-42
lines changed

3 files changed

+72
-42
lines changed

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

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,11 @@ protected String callAssistant(String agent, GXProperties properties, ArrayList<
274274
return callAgent(agent, properties, messages, result);
275275
}
276276

277+
protected ChatResult chatAgent(String agent, GXProperties properties, ArrayList<OpenAIResponse.Message> messages, CallResult result) {
278+
callAgent(agent, true, properties, messages, result);
279+
return new ChatResult(this, agent, properties, messages, result, client);
280+
}
281+
277282
protected String callAgent(String agent, GXProperties properties, ArrayList<OpenAIResponse.Message> messages, CallResult result) {
278283
return callAgent(agent, false, properties, messages, result);
279284
}
@@ -299,12 +304,12 @@ protected String callAgent(String agent, boolean stream, GXProperties properties
299304
}
300305
}
301306
} else if (client.getStatusCode() == 200) {
302-
return readChunk(agent, properties, messages, result);
307+
return "";
303308
}
304309
return "";
305310
}
306311

307-
private String processNotChunkedResponse(String agent, boolean stream, GXProperties properties, ArrayList<OpenAIResponse.Message> messages, CallResult result, ArrayList<OpenAIResponse.ToolCall> toolCalls) {
312+
public String processNotChunkedResponse(String agent, boolean stream, GXProperties properties, ArrayList<OpenAIResponse.Message> messages, CallResult result, ArrayList<OpenAIResponse.ToolCall> toolCalls) {
308313
for (OpenAIResponse.ToolCall tollCall : toolCalls) {
309314
processToolCall(tollCall, messages);
310315
}
@@ -326,36 +331,4 @@ private void processToolCall(OpenAIResponse.ToolCall toolCall, ArrayList<OpenAIR
326331
toolCallMessage.setToolCallId(toolCall.getId());
327332
messages.add(toolCallMessage);
328333
}
329-
330-
protected String readChunk() {
331-
return readChunk(null, null, null, null);
332-
}
333-
334-
protected String readChunk(String agent, GXProperties properties, ArrayList<OpenAIResponse.Message> messages, CallResult result) {
335-
String data = client.readChunk();
336-
if (data.isEmpty())
337-
return "";
338-
int index = data.indexOf("data:") + "data:".length();
339-
String chunkJson = data.substring(index).trim();
340-
try {
341-
JSONObject jsonResponse = new JSONObject(chunkJson);
342-
OpenAIResponse chunkResponse = new ObjectMapper().readValue(jsonResponse.toString(), OpenAIResponse.class);
343-
OpenAIResponse.Choice choise = chunkResponse.getChoices().get(0);
344-
if (choise.getFinishReason() != null && choise.getFinishReason().equals("tool_calls") && agent != null) {
345-
messages.add(choise.getMessage());
346-
return processNotChunkedResponse(agent, true, properties, messages, result, choise.getMessage().getToolCalls());
347-
}
348-
String chunkString = choise.getDelta().getContent();
349-
if (chunkString == null)
350-
return "";
351-
return chunkString;
352-
}
353-
catch (Exception e) {
354-
return "";
355-
}
356-
}
357-
358-
protected boolean isStreamEOF() {
359-
return client.getEof();
360-
}
361334
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.genexus.util;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.genexus.GXProcedure;
5+
import com.genexus.internet.HttpClient;
6+
import com.genexus.util.saia.OpenAIResponse;
7+
import org.json.JSONObject;
8+
9+
import java.util.ArrayList;
10+
11+
public class ChatResult {
12+
private HttpClient client = null;
13+
private String agent = null;
14+
private GXProperties properties = null;
15+
private ArrayList<OpenAIResponse.Message> messages = null;
16+
private CallResult result = null;
17+
private GXProcedure agentProcedure = null;
18+
19+
public ChatResult() {
20+
}
21+
22+
public ChatResult(GXProcedure agentProcedure, String agent, GXProperties properties, ArrayList<OpenAIResponse.Message> messages, CallResult result, HttpClient client) {
23+
this.agentProcedure = agentProcedure;
24+
this.agent = agent;
25+
this.properties = properties;
26+
this.messages = messages;
27+
this.result = result;
28+
this.client = client;
29+
}
30+
31+
public boolean hasMoreData() {
32+
return !client.getEof();
33+
}
34+
35+
public String getMoreData() {
36+
String data = client.readChunk();
37+
if (data.isEmpty())
38+
return "";
39+
int index = data.indexOf("data:") + "data:".length();
40+
String chunkJson = data.substring(index).trim();
41+
try {
42+
JSONObject jsonResponse = new JSONObject(chunkJson);
43+
OpenAIResponse chunkResponse = new ObjectMapper().readValue(jsonResponse.toString(), OpenAIResponse.class);
44+
OpenAIResponse.Choice choise = chunkResponse.getChoices().get(0);
45+
if (choise.getFinishReason() != null && choise.getFinishReason().equals("tool_calls") && agent != null) {
46+
messages.add(choise.getMessage());
47+
return agentProcedure.processNotChunkedResponse(agent, true, properties, messages, result, choise.getMessage().getToolCalls());
48+
}
49+
String chunkString = choise.getDelta().getContent();
50+
if (chunkString == null)
51+
return "";
52+
return chunkString;
53+
}
54+
catch (Exception e) {
55+
return "";
56+
}
57+
}
58+
}

java/src/test/java/com/genexus/agent/Agent.java

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

33
import com.genexus.*;
44
import com.genexus.util.CallResult;
5+
import com.genexus.util.ChatResult;
56
import com.genexus.util.saia.OpenAIResponse;
67
import java.util.ArrayList;
78

@@ -61,10 +62,9 @@ else if (AV3Parameter1.equals("chat_stream")) {
6162
message.setRole("user");
6263
message.setContent("Que me puedes contar de la ciudad que te pedi el clima previamente?");
6364
messages.add(message);
64-
AV5OutputVariable = callAgent( "The weatherman", true, Gxproperties, messages, new CallResult()) ;
65-
System.out.print(AV5OutputVariable);
66-
while (!isStreamEOF()) {
67-
System.out.print(readChunk());
65+
ChatResult chatResult = chatAgent( "The weatherman", Gxproperties, messages, new CallResult()) ;
66+
while (chatResult.hasMoreData()) {
67+
System.out.print(chatResult.hasMoreData());
6868
}
6969
}
7070
else if (AV3Parameter1.equals("toolcall")) {
@@ -88,10 +88,9 @@ else if (AV3Parameter1.equals("toolcall_stream")) {
8888
message.setRole("user");
8989
message.setContent("Necesito nombre y descripcion del producto 1779");
9090
messages.add(message);
91-
AV5OutputVariable = callAgent( "ProductInfo", true, Gxproperties, messages, new CallResult()) ;
92-
System.out.print(AV5OutputVariable);
93-
while (!isStreamEOF()) {
94-
System.out.print(readChunk());
91+
ChatResult chatResult = chatAgent( "ProductInfo", Gxproperties, messages, new CallResult()) ;
92+
while (chatResult.hasMoreData()) {
93+
System.out.print(chatResult.hasMoreData());
9594
}
9695
}
9796
else {

0 commit comments

Comments
 (0)