OpenAI: refactoring

pull/212/head
M66B 2 years ago
parent b075824fd7
commit a50ae9d0cf

@ -2432,11 +2432,11 @@ public class FragmentCompose extends FragmentBase {
Document parsed = JsoupEx.parse(inreplyto.get(0).getFile(context));
Document document = HtmlHelper.sanitizeView(context, parsed, false);
Spanned spanned = HtmlHelper.fromDocument(context, document, null, null);
result.add(new OpenAI.Message(role, truncate(spanned.toString(), MAX_OPENAI_LEN)));
result.add(new OpenAI.Message(role, OpenAI.truncateParagraphs(spanned.toString(), MAX_OPENAI_LEN)));
}
if (!TextUtils.isEmpty(body))
result.add(new OpenAI.Message("assistant", truncate(body, MAX_OPENAI_LEN)));
result.add(new OpenAI.Message("assistant", OpenAI.truncateParagraphs(body, MAX_OPENAI_LEN)));
if (result.size() == 0)
return null;
@ -2464,19 +2464,6 @@ public class FragmentCompose extends FragmentBase {
return completions;
}
@NonNull
private String truncate(@NonNull String text, int maxlen) {
String[] paragraphs = text.split("[\\r\\n]+");
int i = 0;
StringBuilder sb = new StringBuilder();
while (i < paragraphs.length &&
sb.length() + paragraphs[i].length() + 1 < maxlen)
sb.append(paragraphs[i++]).append('\n');
return sb.toString();
}
@Override
protected void onExecuted(Bundle args, OpenAI.Message[] messages) {
if (messages == null || messages.length == 0)

@ -229,6 +229,18 @@ public class OpenAI {
}
}
static String truncateParagraphs(@NonNull String text, int maxlen) {
String[] paragraphs = text.split("[\\r\\n]+");
int i = 0;
StringBuilder sb = new StringBuilder();
while (i < paragraphs.length &&
sb.length() + paragraphs[i].length() + 1 < maxlen)
sb.append(paragraphs[i++]).append('\n');
return sb.toString();
}
static class Message {
private final String role; // // system, user, assistant
private final String content;

Loading…
Cancel
Save