Commit 333517ee authored by NBx03's avatar NBx03
Browse files

Changes

parent 9562d906
Branches gigachat-wip
No related merge requests found
Pipeline #770 failed with stages
in 10 minutes and 57 seconds
Showing with 22 additions and 8 deletions
package com.gigachatwoot.connectors.llm.controllers;
import com.gigachatwoot.connectors.chatwoot.dto.CSMLMessageDTO;
import com.gigachatwoot.connectors.llm.services.LLMConnectorService;
import kong.unirest.json.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Slf4j
@RestController
@RequiredArgsConstructor
......@@ -16,7 +14,7 @@ public class LLMController {
private final LLMConnectorService llmConnectorService;
@PostMapping("/get-llm-answer")
public String getLLMAnswer(@RequestBody String messageHistory) {
public String getLLMAnswer(@RequestBody JSONObject messageHistory) {
return llmConnectorService.getLLMAnswer(messageHistory);
}
}
package com.gigachatwoot.connectors.llm.services;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.gigachatwoot.connectors.chatwoot.dto.CSMLMessageDTO;
import com.gigachatwoot.connectors.llm.config.LLMConfig;
import com.gigachatwoot.connectors.llm.mapper.UnirestMapper;
......@@ -8,13 +7,15 @@ import com.gigachatwoot.connectors.llm.strategy.GigaChatConnectorStrategy;
import com.gigachatwoot.connectors.llm.strategy.LLMConnectorStrategy;
import com.gigachatwoot.connectors.llm.strategy.LLaMaConnectorStrategy;
import jakarta.validation.constraints.NotNull;
import kong.unirest.json.JSONArray;
import kong.unirest.json.JSONObject;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
@Slf4j
......@@ -54,10 +55,25 @@ public class LLMConnectorService {
this.objectMapper = objectMapper;
}
public String getLLMAnswer(String messageHistory) {
public String getLLMAnswer(JSONObject messageHistory) {
log.info("Received input: {}", messageHistory);
List<CSMLMessageDTO> messages = Arrays.asList(objectMapper.readValue(messageHistory, CSMLMessageDTO[].class));
List<CSMLMessageDTO> messages = new ArrayList<>();
try {
JSONArray jsonArray = messageHistory.getJSONArray("message_history");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String id = (String) jsonObject.get("id");
String sender = (String) jsonObject.get("sender");
String content = (String) jsonObject.get("content");
String messageType = (String) jsonObject.get("messageType");
messages.add(new CSMLMessageDTO(id, sender, content, messageType));
}
} catch (Exception e) {
log.error("getLLMAnswer: Error parsing message_history string: ", e);
return "Error parsing message_history string";
}
log.info("Parsed messages: {}", messages);
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment