Refactoring

This commit is contained in:
M66B 2024-05-27 12:49:17 +02:00
parent 65039f941a
commit 196614c39f
1 changed files with 40 additions and 29 deletions

View File

@ -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 {
String reply = null;
if (body == null || TextUtils.isEmpty(body.toString().trim())) {
body = "?";
File file = EntityMessage.getFile(context, id); File file = EntityMessage.getFile(context, id);
if (file.exists()) {
Document d = JsoupEx.parse(file); Document d = JsoupEx.parse(file);
Elements ref = d.select("div[fairemail=reference]"); Elements ref = d.select("div[fairemail=reference]");
if (!ref.isEmpty()) {
d = Document.createShell(""); d = Document.createShell("");
d.appendChildren(ref); d.appendChildren(ref);
HtmlHelper.removeSignatures(d); HtmlHelper.removeSignatures(d);
HtmlHelper.truncate(d, MAX_SUMMARIZE_TEXT_SIZE); 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 (reply == null) {
if (body instanceof Spannable && multimodal) if (body instanceof Spannable && multimodal)
messages.add(new OpenAI.Message(OpenAI.USER, messages.add(new OpenAI.Message(OpenAI.USER,
OpenAI.Content.get((Spannable) body, id, context))); OpenAI.Content.get((Spannable) body, id, context)));
else 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, body.toString())}));
} else {
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, answer)}));
messages.add(new OpenAI.Message(OpenAI.USER, new OpenAI.Content[]{
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<>();
if (reply == null)
messages.add(new Gemini.Message(Gemini.USER, messages.add(new Gemini.Message(Gemini.USER,
new String[]{Gemini.truncateParagraphs(body.toString())})); new String[]{Gemini.truncateParagraphs(body.toString())}));
else {
if (!ref.isEmpty()) messages.add(new Gemini.Message(Gemini.USER, new String[]{answer}));
messages.add(new Gemini.Message(Gemini.USER, new String[]{ref.text()})); 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);