-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
30c718f
commit bfdb98d
Showing
12 changed files
with
264 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
jtokkit/src/main/java/xyz/felh/openai/jtokkit/utils/ArgumentFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package xyz.felh.openai.jtokkit.utils; | ||
|
||
import com.alibaba.fastjson2.JSONArray; | ||
import com.alibaba.fastjson2.JSONObject; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class ArgumentFormat { | ||
|
||
public static String formatArguments(String arguments) { | ||
List<String> lines = new ArrayList<>(); | ||
lines.add("{"); | ||
JSONObject jsonObject = JSONObject.parseObject(arguments); | ||
List<String> properties = new ArrayList<>(); | ||
for (String fieldName : jsonObject.keySet()) { | ||
properties.add(String.format("\"%s\":%s", fieldName, formatValue(jsonObject.get(fieldName)))); | ||
} | ||
lines.add(String.join(",\n", properties)); | ||
lines.add("}"); | ||
return String.join("\n", lines); | ||
} | ||
|
||
private static String formatValue(Object value) { | ||
if (value instanceof String || value instanceof Number) { | ||
return String.format("\"%s\"", value); | ||
} | ||
if (value instanceof JSONArray array) { | ||
String result = "["; | ||
if (!array.isEmpty()) { | ||
result += array.stream().map(it -> { | ||
if (it instanceof Number || it instanceof String) { | ||
return String.format("\"%s\"", it); | ||
} else { | ||
return "\"\""; | ||
} | ||
}).collect(Collectors.joining(",")); | ||
} | ||
result += "]"; | ||
return result; | ||
} | ||
return "\"\""; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
jtokkit/src/main/java/xyz/felh/openai/jtokkit/utils/ToolContentFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package xyz.felh.openai.jtokkit.utils; | ||
|
||
import com.alibaba.fastjson2.JSONObject; | ||
|
||
public class ToolContentFormat { | ||
|
||
public static boolean isJSONString(String content) { | ||
try { | ||
JSONObject.parseObject(content); | ||
return true; | ||
} catch (Exception ignored) { | ||
} | ||
return false; | ||
} | ||
|
||
public static String format(Object content) { | ||
try { | ||
JSONObject.parseObject(content.toString()); | ||
return ArgumentFormat.formatArguments(content.toString()); | ||
} catch (Exception ex) { | ||
// error | ||
} | ||
return content.toString(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.