diff --git a/src/main/java/com/salessparrow/api/lib/GetCrmActionSuggestions.java b/src/main/java/com/salessparrow/api/lib/GetCrmActionSuggestions.java index d28da757..b39ad738 100644 --- a/src/main/java/com/salessparrow/api/lib/GetCrmActionSuggestions.java +++ b/src/main/java/com/salessparrow/api/lib/GetCrmActionSuggestions.java @@ -63,36 +63,40 @@ private CrmActionSuggestionsFormatterDto parseResponse(String responseBody) { try { Util util = new Util(); - JsonNode rootNode = util.getJsonNode(responseBody); - JsonNode argumentsNode = rootNode.get("choices") - .get(0) - .get("message") - .get("function_call") - .get("arguments"); - ObjectMapper objectMapper = new ObjectMapper(); - objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); - String argumentsJson = objectMapper.convertValue(argumentsNode, String.class); + JsonNode rootNode = util.getJsonNode(responseBody); - Map> arguments = objectMapper.readValue(argumentsJson, - new TypeReference>>() { - }); - List addTaskList = arguments.get("add_task"); + JsonNode functionNode = rootNode.get("choices").get(0).get("message").get("function_call"); List formattedTaskSuggestionEntityDtos = new ArrayList<>(); - if (addTaskList != null) { - for (AddTaskSuggestionEntityDto addTask : addTaskList) { - AddTaskSuggestionEntityDto addTaskSuggestionEntityDto = new AddTaskSuggestionEntityDto(); - addTaskSuggestionEntityDto.setDescription(addTask.getDescription()); - - // Format the response check if duedate format is YYYY-MM-DD else - // remove duedate - String dueDate = addTask.getDueDate(); - if (dateFormatValidator.isValid(dueDate, null)) { - addTaskSuggestionEntityDto.setDueDate(dueDate); - } - formattedTaskSuggestionEntityDtos.add(addTaskSuggestionEntityDto); + if (functionNode != null) { + + JsonNode argumentsNode = functionNode.get("arguments"); + + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); + String argumentsJson = objectMapper.convertValue(argumentsNode, String.class); + + Map> arguments = objectMapper.readValue(argumentsJson, + new TypeReference>>() { + }); + List addTaskList = arguments.get("add_task"); + + if (addTaskList != null) { + for (AddTaskSuggestionEntityDto addTask : addTaskList) { + AddTaskSuggestionEntityDto addTaskSuggestionEntityDto = new AddTaskSuggestionEntityDto(); + addTaskSuggestionEntityDto.setDescription(addTask.getDescription()); + + // Format the response check if duedate format is YYYY-MM-DD else + // remove duedate + String dueDate = addTask.getDueDate(); + if (dateFormatValidator.isValid(dueDate, null)) { + addTaskSuggestionEntityDto.setDueDate(dueDate); + } + + formattedTaskSuggestionEntityDtos.add(addTaskSuggestionEntityDto); + } } } diff --git a/src/main/java/com/salessparrow/api/lib/openAi/OpenAiPayloadBuilder.java b/src/main/java/com/salessparrow/api/lib/openAi/OpenAiPayloadBuilder.java index 9fc56960..4603d099 100644 --- a/src/main/java/com/salessparrow/api/lib/openAi/OpenAiPayloadBuilder.java +++ b/src/main/java/com/salessparrow/api/lib/openAi/OpenAiPayloadBuilder.java @@ -18,25 +18,28 @@ public class OpenAiPayloadBuilder { */ public String payloadForCrmActionsSuggestions(String text) { + String todayDate = getTodaysDate(); + return "{\n" + " \"model\": \"gpt-3.5-turbo-0613\",\n" + " \"messages\": [\n" + " {\n" - + " \"role\": \"user\",\n" - + " \"content\": \"You are an AI assistant which gives suggestion on creating task in crm using the input message.Only use the functions you have been provided with. \\nInput message: \\n" + + " \"role\": \"system\",\n" + + " \"content\": \"You are an AI assistant that provides suggestions for creating tasks in CRM based solely on the content of the input message. The content of task if any found should only be from input message. If no task suggestions are found in the input message, return empty data. If task suggestions are found, they should include description and due date. Due Date format should be YYYY-MM-DD. Today's date is " + + todayDate + + ". Use the functions provided to determine task suggestions. If no tasks are possible for the given input message, return an empty response. For example, If Input Message: Had a call with product team. They need a pitch desk by tomorrow. Then it should return description: Create pitch deck. For 2nd Example, If Input Message: 3 tasks. Then it should return empty response as no tasks can be suggested from input message\"\n" + + " },\n" + " {\n" + " \"role\": \"user\",\n" + " \"content\": \" Input message: \\n" + text + "\\n\"\n" + " }\n" + " ],\n" + " \"functions\": [\n" + " {\n" + " \"name\": \"suggest_actions\",\n" + " \"description\": \"This is function for suggesting actions in crm(example salesforce, freshsales) based on input message.\",\n" + " \"parameters\": {\n" + " \"type\": \"object\",\n" + " \"properties\": {\n" + " \"add_task\": {\n" + " \"name\": \"add_task\",\n" - + " \"description\": \"Tasks using input message.\",\n" + + " \"description\": \"Tasks using input message. The task should be created only from the input message and if no task can be generated from input message then empty data should be returned.\",\n" + " \"type\": \"array\",\n" + " \"items\": {\n" + " \"type\": \"object\",\n" + " \"properties\": {\n" + " \"description\": {\n" + " \"type\": \"string\",\n" - + " \"description\": \"Description for task to add. This is mandatory\"\n" - + " },\n" + " \"due_date\": {\n" - + " \"type\": \"string\",\n" + + " \"description\": \"Description for task to add.\"\n" + " },\n" + + " \"due_date\": {\n" + " \"type\": \"string\",\n" + " \"description\": \"Due date for task in YYYY-MM-DD format. Today's date is " - + getTodaysDate() + ". This is mandatory\"\n" + " }\n" + " },\n" - + " \"required\": [\"description\", \"due_date\"]\n" + " }\n" + " }\n" - + " }\n" + " }\n" + " }\n" + " ]\n" + "}"; + + todayDate + ".\"\n" + " }\n" + " }\n" + " }\n" + + " }\n" + " }\n" + " }\n" + " }\n" + " ]\n" + "}"; } /**