AI: answer message

pull/215/head
M66B 7 months ago
parent a7b82bedf6
commit ef425fd0b1

@ -29,6 +29,7 @@ import androidx.preference.PreferenceManager;
import org.json.JSONException; import org.json.JSONException;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -43,8 +44,22 @@ 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);
Document d = JsoupEx.parse(file);
Elements ref = d.select("div[fairemail=reference]");
d = Document.createShell("");
d.appendChildren(ref);
HtmlHelper.removeSignatures(d);
HtmlHelper.truncate(d, MAX_SUMMARIZE_TEXT_SIZE);
if (body == null || TextUtils.isEmpty(body.toString().trim())) if (body == null || TextUtils.isEmpty(body.toString().trim()))
body = "?"; if (OpenAI.isAvailable(context))
body = OpenAI.DEFAULT_ANSWER_PROMPT;
else if (Gemini.isAvailable(context))
body = Gemini.DEFAULT_ANSWER_PROMPT;
else
body = "?";
if (OpenAI.isAvailable(context)) { if (OpenAI.isAvailable(context)) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
@ -52,16 +67,21 @@ public class AI {
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);
OpenAI.Message message; List<OpenAI.Message> messages = new ArrayList<>();
if (body instanceof Spannable && multimodal) if (body instanceof Spannable && multimodal)
message = 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
message = 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())}));
OpenAI.Message[] completions = if (!ref.isEmpty())
OpenAI.completeChat(context, model, new OpenAI.Message[]{message}, temperature, 1); messages.add(new OpenAI.Message(OpenAI.USER, new OpenAI.Content[]{
new OpenAI.Content(OpenAI.CONTENT_TEXT, ref.text())}));
OpenAI.Message[] completions = OpenAI.completeChat(context,
model, messages.toArray(new OpenAI.Message[0]), temperature, 1);
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (OpenAI.Message completion : completions) for (OpenAI.Message completion : completions)
@ -79,9 +99,16 @@ public class AI {
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);
Gemini.Message message = new Gemini.Message(Gemini.USER, List<Gemini.Message> messages = new ArrayList<>();
new String[]{Gemini.truncateParagraphs(body.toString())});
Gemini.Message[] completions = Gemini.generate(context, model, new Gemini.Message[]{message}, temperature, 1); messages.add(new Gemini.Message(Gemini.USER,
new String[]{Gemini.truncateParagraphs(body.toString())}));
if (!ref.isEmpty())
messages.add(new Gemini.Message(Gemini.USER, new String[]{ref.text()}));
Gemini.Message[] completions = Gemini.generate(context,
model, messages.toArray(new Gemini.Message[0]), temperature, 1);
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (Gemini.Message completion : completions) for (Gemini.Message completion : completions)

@ -45,6 +45,7 @@ public class Gemini {
static final String DEFAULT_MODEL = "gemini-pro"; static final String DEFAULT_MODEL = "gemini-pro";
static final float DEFAULT_TEMPERATURE = 0.9f; static final float DEFAULT_TEMPERATURE = 0.9f;
static final String DEFAULT_SUMMARY_PROMPT = "Summarize the following text:"; static final String DEFAULT_SUMMARY_PROMPT = "Summarize the following text:";
static final String DEFAULT_ANSWER_PROMPT = "Answer this message:";
static final String MODEL = "model"; static final String MODEL = "model";
static final String USER = "user"; static final String USER = "user";

@ -50,6 +50,7 @@ public class OpenAI {
static final String DEFAULT_MODEL = "gpt-4o"; static final String DEFAULT_MODEL = "gpt-4o";
static final float DEFAULT_TEMPERATURE = 0.5f; static final float DEFAULT_TEMPERATURE = 0.5f;
static final String DEFAULT_SUMMARY_PROMPT = "Summarize the following text:"; static final String DEFAULT_SUMMARY_PROMPT = "Summarize the following text:";
static final String DEFAULT_ANSWER_PROMPT = "Answer this message:";
static final String ASSISTANT = "assistant"; static final String ASSISTANT = "assistant";
static final String USER = "user"; static final String USER = "user";

Loading…
Cancel
Save