Skip to content

Commit 3e2b9e7

Browse files
authored
- Tools calls was not beeing called when chat agent method was executed (#985)
- Implement ToJson and FromJon methods of external types to allow calling them on the ChatMessage type
1 parent 07ca4a4 commit 3e2b9e7

File tree

8 files changed

+64
-10
lines changed

8 files changed

+64
-10
lines changed

common/src/main/java/com/genexus/internet/GXHttpClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,8 @@ protected void resetState()
483483

484484
public abstract String getString();
485485

486+
public abstract void unreadChunk();
487+
486488
public abstract String readChunk();
487489

488490
public abstract boolean getEof();

common/src/main/java/com/genexus/internet/HttpClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,11 @@ public String getString()
253253
return session.getString();
254254
}
255255

256+
public void unreadChunk()
257+
{
258+
session.unreadChunk();
259+
}
260+
256261
public String readChunk()
257262
{
258263
return session.readChunk();

common/src/main/java/com/genexus/internet/HttpClientManual.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,9 @@ public boolean getEof() {
436436
return true;
437437
}
438438

439+
public void unreadChunk() {
440+
}
441+
439442
public String readChunk() {
440443
return getString();
441444
}

common/src/main/java/com/genexus/internet/IHttpClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public interface IHttpClient {
4848
public void getHeader(String name, double[] value);
4949
public InputStream getInputStream() throws IOException;
5050
public String getString();
51+
public void unreadChunk();
5152
public String readChunk();
5253
public boolean getEof();
5354
public void toFile(String fileName);

java/src/main/java/com/genexus/GxUserType.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.genexus;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
34
import com.genexus.xml.GXXMLSerializable;
45
import java.util.HashMap;
56
import com.genexus.json.JSONObjectWrapper;
@@ -38,6 +39,11 @@ protected Object getJsonObjectFromHashMap( Object userType) {
3839
try {
3940
if (userType instanceof HashMap)
4041
jsonObj = new JSONObjectWrapper((HashMap)userType);
42+
else {
43+
ObjectMapper mapper = new ObjectMapper();
44+
String jsonString = mapper.writeValueAsString(userType);
45+
jsonObj = new JSONObjectWrapper(jsonString);
46+
}
4147
}
4248
catch(Exception e) {
4349
log.error("Could not create Json Object", e);
@@ -49,5 +55,14 @@ protected void setHashMapFromJson(String json) {
4955
fromjson(json);
5056
}
5157

52-
protected void fromjson(String json){}
58+
protected void fromjson(String json){
59+
try {
60+
Object instance = this.getClass().getMethod("getExternalInstance").invoke(this);
61+
ObjectMapper mapper = new ObjectMapper();
62+
mapper.readerForUpdating(instance).readValue(json);
63+
}
64+
catch(Exception e) {
65+
log.error("Error executing FromJson() method", e);
66+
}
67+
}
5368
}

java/src/main/java/com/genexus/internet/HttpClientJavaLib.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,14 +742,25 @@ public String getString() {
742742
}
743743

744744
private boolean eof;
745+
private String previousChunkReaded;
746+
private boolean usePreviousChunkReaded;
745747
public boolean getEof() {
746748
return eof;
747749
}
748750

751+
public void unreadChunk() {
752+
usePreviousChunkReaded = true;
753+
}
754+
749755
public String readChunk() {
750756
if (!isChunkedResponse)
751757
return getString();
752758

759+
if (usePreviousChunkReaded) {
760+
usePreviousChunkReaded = false;
761+
return previousChunkReaded;
762+
}
763+
753764
if (response == null)
754765
return "";
755766
try {
@@ -759,6 +770,7 @@ public String readChunk() {
759770
eof = true;
760771
res = "";
761772
}
773+
previousChunkReaded = res;
762774
return res;
763775
} catch (IOException e) {
764776
setExceptionsCatch(e);

java/src/main/java/com/genexus/util/ChatResult.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ public String getMoreData() {
4242
JSONObject jsonResponse = new JSONObject(chunkJson);
4343
OpenAIResponse chunkResponse = new ObjectMapper().readValue(jsonResponse.toString(), OpenAIResponse.class);
4444
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-
}
4945
String chunkString = choise.getDelta().getStringContent();
5046
if (chunkString == null)
5147
return "";

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,35 @@ public static OpenAIResponse call(OpenAIRequest request, boolean isEmbedding, Ht
4242
client.addString(jsonRequest);
4343
client.execute("POST", providerURL);
4444
if (client.getStatusCode() == 200) {
45+
String saiaResponse;
4546
if (client.getHeader("Content-Type").contains("text/event-stream")){
46-
return null;
47+
saiaResponse = client.readChunk();
48+
int index = saiaResponse.indexOf("data:") + "data:".length();
49+
String chunkJson = saiaResponse.substring(index).trim();
50+
try {
51+
JSONObject jsonResponse = new JSONObject(chunkJson);
52+
OpenAIResponse chunkResponse = new ObjectMapper().readValue(jsonResponse.toString(), OpenAIResponse.class);
53+
OpenAIResponse.Choice choise = chunkResponse.getChoices().get(0);
54+
if (choise.getFinishReason() != null && choise.getFinishReason().equals("tool_calls")){
55+
saiaResponse = chunkJson;
56+
}
57+
else {
58+
client.unreadChunk();
59+
return null;
60+
}
61+
}
62+
catch (Exception e) {
63+
client.unreadChunk();
64+
return null;
65+
}
4766
}
4867
else {
49-
String saiaResponse = client.getString();
50-
logger.debug("Agent response: " + saiaResponse);
51-
JSONObject jsonResponse = new JSONObject(saiaResponse);
52-
return new ObjectMapper().readValue(jsonResponse.toString(), OpenAIResponse.class);
68+
saiaResponse = client.getString();
5369
}
70+
71+
logger.debug("Agent response: " + saiaResponse);
72+
JSONObject jsonResponse = new JSONObject(saiaResponse);
73+
return new ObjectMapper().readValue(jsonResponse.toString(), OpenAIResponse.class);
5474
}
5575
else {
5676
String errorDescription = String.format("Error calling Enterprise AI API, StatusCode: %d, ReasonLine: %s",

0 commit comments

Comments
 (0)