Skip to content

Commit

Permalink
Handle case where AI returns bare Ref
Browse files Browse the repository at this point in the history
  • Loading branch information
cjmalloy committed Jul 25, 2023
1 parent 6aa78cf commit f1b00a1
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/main/java/jasper/component/delta/Ai.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -197,14 +196,25 @@ You may only use public tags (starting with a lowercase letter or number) and yo
List<Ref> refArray = List.of(response);
Exception ex = null;
try {
var aiReply = objectMapper.readValue(response.getComment(), new TypeReference<AiReply>() {});
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<AiReply>() {});
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<String>();
Expand Down

0 comments on commit f1b00a1

Please sign in to comment.