From f1b00a159b10658c9ef926ccea0b7e124abed21e Mon Sep 17 00:00:00 2001 From: Chris Malloy Date: Mon, 24 Jul 2023 23:20:40 -0300 Subject: [PATCH] Handle case where AI returns bare Ref --- src/main/java/jasper/component/delta/Ai.java | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/java/jasper/component/delta/Ai.java b/src/main/java/jasper/component/delta/Ai.java index 2414cd3a..31f5c49e 100644 --- a/src/main/java/jasper/component/delta/Ai.java +++ b/src/main/java/jasper/component/delta/Ai.java @@ -152,10 +152,9 @@ You may only use public tags (starting with a lowercase letter or number) and yo agent with your Ref as the prompt). Include your response as the comment field of a Ref. Do not add metadata to a response, that is generated by Jasper. - Only reply with pure JSON. + Only reply with valid JSON. Do not include any text outside of the JSON Ref. Your reply should always start with {"ref":[{ - Only output valid JSON. """; var response = new Ref(); try { @@ -197,14 +196,25 @@ You may only use public tags (starting with a lowercase letter or number) and yo List refArray = List.of(response); Exception ex = null; try { - var aiReply = objectMapper.readValue(response.getComment(), new TypeReference() {}); - refArray = List.of(aiReply.ref); + var json = objectMapper.readValue(response.getComment(), JsonNode.class); + if (json.has("ref")) { + var aiReply = objectMapper.readValue(response.getComment(), new TypeReference() {}); + refArray = List.of(aiReply.ref); + } else if (json.has("url")) { + // Returned bare Ref + var bareRef = objectMapper.readValue(response.getComment(), Ref.class); + refArray = List.of(bareRef); + } else { + logger.warn("Falling back: AI did not reply with expected format."); + logger.warn(response.getComment()); + response.setTags(new ArrayList<>(List.of("plugin/debug", "ai", "+plugin/openai"))); + } refArray.get(0).setUrl(response.getUrl()); refArray.get(0).setPlugin("+plugin/openai", response.getPlugins().get("+plugin/openai")); } catch (Exception e) { logger.warn("Falling back: AI did not reply with JSON."); logger.warn(response.getComment(), ex); - response.setTags(new ArrayList<>(List.of("plugin/debug", "ai", "+plugin/openai", "plugin/latex"))); + response.setTags(new ArrayList<>(List.of("plugin/debug", "ai", "+plugin/openai"))); } response = refArray.get(0); var tags = new ArrayList();