Refactoring

pull/215/head
M66B 7 months ago
parent 65039f941a
commit 196614c39f

@ -44,42 +44,50 @@ public class AI {
} }
static String completeChat(Context context, long id, CharSequence body) throws JSONException, IOException { static String completeChat(Context context, long id, CharSequence body) throws JSONException, IOException {
File file = EntityMessage.getFile(context, id); String reply = null;
Document d = JsoupEx.parse(file); if (body == null || TextUtils.isEmpty(body.toString().trim())) {
Elements ref = d.select("div[fairemail=reference]"); body = "?";
d = Document.createShell("");
d.appendChildren(ref); File file = EntityMessage.getFile(context, id);
if (file.exists()) {
HtmlHelper.removeSignatures(d); Document d = JsoupEx.parse(file);
HtmlHelper.truncate(d, MAX_SUMMARIZE_TEXT_SIZE); Elements ref = d.select("div[fairemail=reference]");
if (!ref.isEmpty()) {
d = Document.createShell("");
d.appendChildren(ref);
HtmlHelper.removeSignatures(d);
HtmlHelper.truncate(d, MAX_SUMMARIZE_TEXT_SIZE);
reply = d.text();
if (TextUtils.isEmpty(reply.trim()))
reply = null;
}
}
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (body == null || TextUtils.isEmpty(body.toString().trim()))
if (OpenAI.isAvailable(context) && !ref.isEmpty())
body = prefs.getString("openai_answer", OpenAI.DEFAULT_ANSWER_PROMPT);
else if (Gemini.isAvailable(context) && !ref.isEmpty())
body = prefs.getString("gemini_answer", Gemini.DEFAULT_ANSWER_PROMPT);
else
body = "?";
if (OpenAI.isAvailable(context)) { if (OpenAI.isAvailable(context)) {
String model = prefs.getString("openai_model", OpenAI.DEFAULT_MODEL); String model = prefs.getString("openai_model", OpenAI.DEFAULT_MODEL);
float temperature = prefs.getFloat("openai_temperature", OpenAI.DEFAULT_TEMPERATURE); float temperature = prefs.getFloat("openai_temperature", OpenAI.DEFAULT_TEMPERATURE);
boolean multimodal = prefs.getBoolean("openai_multimodal", false); boolean multimodal = prefs.getBoolean("openai_multimodal", false);
String answer = prefs.getString("openai_answer", OpenAI.DEFAULT_ANSWER_PROMPT);
List<OpenAI.Message> messages = new ArrayList<>(); List<OpenAI.Message> messages = new ArrayList<>();
if (body instanceof Spannable && multimodal) if (reply == null) {
messages.add(new OpenAI.Message(OpenAI.USER, if (body instanceof Spannable && multimodal)
OpenAI.Content.get((Spannable) body, id, context))); messages.add(new OpenAI.Message(OpenAI.USER,
else OpenAI.Content.get((Spannable) body, id, context)));
else
messages.add(new OpenAI.Message(OpenAI.USER, new OpenAI.Content[]{
new OpenAI.Content(OpenAI.CONTENT_TEXT, body.toString())}));
} else {
messages.add(new OpenAI.Message(OpenAI.USER, new OpenAI.Content[]{ messages.add(new OpenAI.Message(OpenAI.USER, new OpenAI.Content[]{
new OpenAI.Content(OpenAI.CONTENT_TEXT, body.toString())})); new OpenAI.Content(OpenAI.CONTENT_TEXT, answer)}));
if (!ref.isEmpty())
messages.add(new OpenAI.Message(OpenAI.USER, new OpenAI.Content[]{ messages.add(new OpenAI.Message(OpenAI.USER, new OpenAI.Content[]{
new OpenAI.Content(OpenAI.CONTENT_TEXT, ref.text())})); new OpenAI.Content(OpenAI.CONTENT_TEXT, reply)}));
}
OpenAI.Message[] completions = OpenAI.completeChat(context, OpenAI.Message[] completions = OpenAI.completeChat(context,
model, messages.toArray(new OpenAI.Message[0]), temperature, 1); model, messages.toArray(new OpenAI.Message[0]), temperature, 1);
@ -98,14 +106,17 @@ public class AI {
} else if (Gemini.isAvailable(context)) { } else if (Gemini.isAvailable(context)) {
String model = prefs.getString("gemini_model", Gemini.DEFAULT_MODEL); String model = prefs.getString("gemini_model", Gemini.DEFAULT_MODEL);
float temperature = prefs.getFloat("gemini_temperature", Gemini.DEFAULT_TEMPERATURE); float temperature = prefs.getFloat("gemini_temperature", Gemini.DEFAULT_TEMPERATURE);
String answer = prefs.getString("gemini_answer", Gemini.DEFAULT_ANSWER_PROMPT);
List<Gemini.Message> messages = new ArrayList<>(); List<Gemini.Message> messages = new ArrayList<>();
messages.add(new Gemini.Message(Gemini.USER, if (reply == null)
new String[]{Gemini.truncateParagraphs(body.toString())})); messages.add(new Gemini.Message(Gemini.USER,
new String[]{Gemini.truncateParagraphs(body.toString())}));
if (!ref.isEmpty()) else {
messages.add(new Gemini.Message(Gemini.USER, new String[]{ref.text()})); messages.add(new Gemini.Message(Gemini.USER, new String[]{answer}));
messages.add(new Gemini.Message(Gemini.USER, new String[]{reply}));
}
Gemini.Message[] completions = Gemini.generate(context, Gemini.Message[] completions = Gemini.generate(context,
model, messages.toArray(new Gemini.Message[0]), temperature, 1); model, messages.toArray(new Gemini.Message[0]), temperature, 1);

Loading…
Cancel
Save