Refactoring

This commit is contained in:
M66B 2024-05-19 17:57:05 +02:00
parent f0f5fd0b31
commit cd14ca4577
4 changed files with 54 additions and 50 deletions

View File

@ -30,11 +30,14 @@ import androidx.preference.PreferenceManager;
import org.json.JSONException;
import org.jsoup.nodes.Document;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class AI {
private static final int MAX_SUMMARIZE_TEXT_SIZE = 10 * 1024;
static boolean isAvailable(Context context) {
return (OpenAI.isAvailable(context) || Gemini.isAvailable(context));
}
@ -47,10 +50,12 @@ public class AI {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String model = prefs.getString("openai_model", OpenAI.DEFAULT_MODEL);
float temperature = prefs.getFloat("openai_temperature", OpenAI.DEFAULT_TEMPERATURE);
boolean multimodal = prefs.getBoolean("openai_multimodal", true);
OpenAI.Message message;
if (body instanceof Spannable)
message = new OpenAI.Message(OpenAI.USER, OpenAI.Content.get((Spannable) body, id, context));
message = new OpenAI.Message(OpenAI.USER,
OpenAI.Content.get((Spannable) body, id, multimodal, context));
else
message = new OpenAI.Message(OpenAI.USER, new OpenAI.Content[]{
new OpenAI.Content(OpenAI.CONTENT_TEXT, body.toString())});
@ -65,9 +70,9 @@ public class AI {
if (sb.length() > 0)
sb.append('\n');
sb.append(content.getContent()
.replaceAll("^\\n+", "").replaceAll("\\n+$", ""));
.replaceAll("^\\n+", "")
.replaceAll("\\n+$", ""));
}
return sb.toString();
} else if (Gemini.isAvailable(context)) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
@ -77,11 +82,17 @@ public class AI {
Gemini.Message message = new Gemini.Message(Gemini.USER,
new String[]{Gemini.truncateParagraphs(body.toString())});
Gemini.Message[] completions = Gemini.generate(context, model, new Gemini.Message[]{message}, temperature, 1);
if (completions.length == 0)
return null;
return TextUtils.join("\n", completions[0].getContent())
.replaceAll("^\\n+", "").replaceAll("\\n+$", "");
StringBuilder sb = new StringBuilder();
for (Gemini.Message completion : completions)
for (String result : completion.getContent()) {
if (sb.length() > 0)
sb.append('\n');
sb.append(result
.replaceAll("^\\n+", "")
.replaceAll("\\n+$", ""));
}
return sb.toString();
} else
return null;
}
@ -96,33 +107,47 @@ public class AI {
return context.getString(R.string.title_summarize);
}
static String summarize(Context context, long id, String subject, Document d) throws JSONException, IOException {
static String getSummaryText(Context context, EntityMessage message) throws JSONException, IOException {
File file = message.getFile(context);
if (!file.exists())
return null;
Document d = JsoupEx.parse(file);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean remove_signatures = prefs.getBoolean("remove_signatures", false);
if (remove_signatures)
HtmlHelper.removeSignatures(d);
HtmlHelper.removeQuotes(d);
d = HtmlHelper.sanitizeView(context, d, false);
HtmlHelper.truncate(d, MAX_SUMMARIZE_TEXT_SIZE);
if (OpenAI.isAvailable(context)) {
String model = prefs.getString("openai_model", OpenAI.DEFAULT_MODEL);
float temperature = prefs.getFloat("openai_temperature", OpenAI.DEFAULT_TEMPERATURE);
String prompt = prefs.getString("openai_summarize", OpenAI.DEFAULT_SUMMARY_PROMPT);
boolean multimodal = prefs.getBoolean("openai_multimodal", true);
List<OpenAI.Message> input = new ArrayList<>();
input.add(new OpenAI.Message(OpenAI.USER,
new OpenAI.Content[]{new OpenAI.Content(OpenAI.CONTENT_TEXT, prompt)}));
if (!TextUtils.isEmpty(subject))
if (!TextUtils.isEmpty(message.subject))
input.add(new OpenAI.Message(OpenAI.USER,
new OpenAI.Content[]{new OpenAI.Content(OpenAI.CONTENT_TEXT, subject)}));
new OpenAI.Content[]{new OpenAI.Content(OpenAI.CONTENT_TEXT, message.subject)}));
SpannableStringBuilder ssb = HtmlHelper.fromDocument(context, d, null, null);
input.add(new OpenAI.Message(OpenAI.USER,
OpenAI.Content.get(ssb, id, context)));
OpenAI.Content.get(ssb, message.id, multimodal, context)));
OpenAI.Message[] result =
OpenAI.Message[] completions =
OpenAI.completeChat(context, model, input.toArray(new OpenAI.Message[0]), temperature, 1);
if (result.length == 0)
return null;
StringBuilder sb = new StringBuilder();
for (OpenAI.Message completion : result)
for (OpenAI.Message completion : completions)
for (OpenAI.Content content : completion.getContent())
if (OpenAI.CONTENT_TEXT.equals(content.getType())) {
if (sb.length() != 0)
@ -140,13 +165,17 @@ public class AI {
return null;
Gemini.Message content = new Gemini.Message(Gemini.USER, new String[]{prompt, text});
Gemini.Message[] result =
Gemini.Message[] completions =
Gemini.generate(context, model, new Gemini.Message[]{content}, temperature, 1);
if (result.length == 0)
return null;
return TextUtils.join("\n", result[0].getContent());
StringBuilder sb = new StringBuilder();
for (Gemini.Message completion : completions)
for (String result : completion.getContent()) {
if (sb.length() != 0)
sb.append('\n');
sb.append(result);
}
return sb.toString();
} else
return null;
}

View File

@ -117,8 +117,8 @@ abstract class ActivityBase extends AppCompatActivity implements SharedPreferenc
toolbar.setPopupTheme(getThemeId());
if (hide_toolbar) {
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL
| AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL |
AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
toolbar.setLayoutParams(params);
getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {

View File

@ -34,14 +34,9 @@ import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.FragmentManager;
import androidx.preference.PreferenceManager;
import org.jsoup.nodes.Document;
import java.io.File;
import java.util.Date;
public class FragmentDialogSummarize extends FragmentDialogBase {
private static final int MAX_SUMMARIZE_TEXT_SIZE = 10 * 1024;
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
@ -92,25 +87,8 @@ public class FragmentDialogSummarize extends FragmentDialogBase {
if (message == null || !message.content)
return null;
File file = EntityMessage.getFile(context, id);
if (!file.exists())
return null;
Document d = JsoupEx.parse(file);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean remove_signatures = prefs.getBoolean("remove_signatures", false);
if (remove_signatures)
HtmlHelper.removeSignatures(d);
HtmlHelper.removeQuotes(d);
d = HtmlHelper.sanitizeView(context, d, false);
HtmlHelper.truncate(d, MAX_SUMMARIZE_TEXT_SIZE);
long start = new Date().getTime();
String summary = AI.summarize(context, id, message.subject, d);
String summary = AI.getSummaryText(context, message);
args.putLong("elapsed", new Date().getTime() - start);
return summary;

View File

@ -277,10 +277,7 @@ public class OpenAI {
return this.content;
}
static Content[] get(Spannable ssb, long id, Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean multimodal = prefs.getBoolean("openai_multimodal", true);
static Content[] get(Spannable ssb, long id, boolean multimodal, Context context) {
DB db = DB.getInstance(context);
List<OpenAI.Content> contents = new ArrayList<>();
int start = 0;