Skip to content

Commit

Permalink
Merge pull request HamaWhiteGG#157 from dinsy008/main
Browse files Browse the repository at this point in the history
修复在使用Agent时解析Json以及参数转换的BUG
  • Loading branch information
HamaWhiteGG authored Mar 8, 2024
2 parents 43316d6 + bd12e16 commit df36bdc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,20 @@ public class ChatOutputParser extends AgentOutputParser {

private static final String FINAL_ANSWER_ACTION = "Final Answer:";

private static Gson GSON_JSON = new GsonBuilder()
.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
.create();
@Override
public AgentResult parse(String text) {
boolean includesAnswer = text.contains(FINAL_ANSWER_ACTION);
try {
String action = text.split("```")[1];
Type mapType = new TypeToken<Map<String, Object>>() {
}.getType();
Map<String, Object> response = new Gson().fromJson(action.strip(), mapType);

/*
* 20230308:修复通过GSON反序列化时将long类型的数值错误的转成了double类型科学计数法
* */
Map<String, Object> response = GSON_JSON.fromJson(action.strip(),mapType);
boolean includesAction = response.containsKey("action") && response.containsKey("action_input");
if (includesAnswer && includesAction) {
throw new OutputParserException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,15 @@ public Map<String, Object> args() {
public abstract Object innerRun(String args, Map<String, Object> kwargs);

public Pair<Object[], Map<String, Object>> toArgsAndKwargs(Object toolInput) {
if (toolInput instanceof String) {
return Pair.of(new Object[]{toolInput}, Maps.newHashMap());
} else {
@SuppressWarnings("unchecked")
/*
* 20230308:入参为Map时转换,否则不进行转换
* 原逻辑入参为非String和非Map时会出现将toolInput强转为Map报错
* */
if(toolInput instanceof Map){
Map<String, Object> mapInput = (Map<String, Object>) toolInput;
return Pair.of(new Object[]{}, mapInput);
}
return Pair.of(new Object[]{toolInput}, Maps.newHashMap());
}

/**
Expand Down

0 comments on commit df36bdc

Please sign in to comment.