feat: Refactor CSV parsing in CsvAgentResponseHandler and CsvUnmarshallingAgentResponseHandler to use TextUtils for normalization
This commit is contained in:
@@ -10,6 +10,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
import de.extio.lmlib.agent.AgentContext;
|
import de.extio.lmlib.agent.AgentContext;
|
||||||
import de.extio.lmlib.client.Completion;
|
import de.extio.lmlib.client.Completion;
|
||||||
import de.extio.lmlib.client.Conversation;
|
import de.extio.lmlib.client.Conversation;
|
||||||
|
import de.extio.lmlib.util.TextUtils;
|
||||||
|
|
||||||
public class CsvAgentResponseHandler implements AgentResponseHandler {
|
public class CsvAgentResponseHandler implements AgentResponseHandler {
|
||||||
|
|
||||||
@@ -69,7 +70,7 @@ public class CsvAgentResponseHandler implements AgentResponseHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean parseCsv(final String csvText, final AgentContext context) {
|
private boolean parseCsv(final String csvText, final AgentContext context) {
|
||||||
final String[] lines = csvText.split("\n");
|
final String[] lines = TextUtils.normalizeModelResponse(csvText, false).split("\n");
|
||||||
|
|
||||||
int startLine = 0;
|
int startLine = 0;
|
||||||
|
|
||||||
@@ -86,10 +87,10 @@ public class CsvAgentResponseHandler implements AgentResponseHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final String lowerLine = line.toLowerCase();
|
final String lowerLine = line.toLowerCase();
|
||||||
boolean isHeader = false;
|
boolean isHeader = true;
|
||||||
for (final String heading : this.headings) {
|
for (final String heading : this.headings) {
|
||||||
if (lowerLine.contains(heading.toLowerCase())) {
|
if (!lowerLine.contains(heading.toLowerCase())) {
|
||||||
isHeader = true;
|
isHeader = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -164,13 +165,6 @@ public class CsvAgentResponseHandler implements AgentResponseHandler {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((value.startsWith("\"")
|
return TextUtils.normalizeModelResponse(value, false);
|
||||||
&& value.endsWith("\""))
|
|
||||||
|| (value.startsWith("'")
|
|
||||||
&& value.endsWith("'"))) {
|
|
||||||
return value.substring(1, value.length() - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
import de.extio.lmlib.agent.AgentContext;
|
import de.extio.lmlib.agent.AgentContext;
|
||||||
import de.extio.lmlib.client.Completion;
|
import de.extio.lmlib.client.Completion;
|
||||||
import de.extio.lmlib.client.Conversation;
|
import de.extio.lmlib.client.Conversation;
|
||||||
|
import de.extio.lmlib.util.TextUtils;
|
||||||
|
|
||||||
public class CsvUnmarshallingAgentResponseHandler implements AgentResponseHandler {
|
public class CsvUnmarshallingAgentResponseHandler implements AgentResponseHandler {
|
||||||
|
|
||||||
@@ -113,7 +114,7 @@ public class CsvUnmarshallingAgentResponseHandler implements AgentResponseHandle
|
|||||||
|
|
||||||
private List<Object> parseCsv(final String csvText) throws Exception {
|
private List<Object> parseCsv(final String csvText) throws Exception {
|
||||||
final List<Object> result = new ArrayList<>();
|
final List<Object> result = new ArrayList<>();
|
||||||
final String[] lines = csvText.split("\n");
|
final String[] lines = TextUtils.normalizeModelResponse(csvText, false).split("\n");
|
||||||
|
|
||||||
int startLine = 0;
|
int startLine = 0;
|
||||||
|
|
||||||
@@ -130,10 +131,10 @@ public class CsvUnmarshallingAgentResponseHandler implements AgentResponseHandle
|
|||||||
}
|
}
|
||||||
|
|
||||||
final String lowerLine = line.toLowerCase();
|
final String lowerLine = line.toLowerCase();
|
||||||
boolean isHeader = false;
|
boolean isHeader = true;
|
||||||
for (final String fieldName : this.fieldNames) {
|
for (final String fieldName : this.fieldNames) {
|
||||||
if (lowerLine.contains(fieldName.toLowerCase())) {
|
if (!lowerLine.contains(fieldName.toLowerCase())) {
|
||||||
isHeader = true;
|
isHeader = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -264,13 +265,6 @@ public class CsvUnmarshallingAgentResponseHandler implements AgentResponseHandle
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((value.startsWith("\"")
|
return TextUtils.normalizeModelResponse(value, false);
|
||||||
&& value.endsWith("\""))
|
|
||||||
|| (value.startsWith("'")
|
|
||||||
&& value.endsWith("'"))) {
|
|
||||||
return value.substring(1, value.length() - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user