AI: use selected prompt

pull/215/head
M66B 6 months ago
parent c1f4e42b3f
commit 35cbcb8f02

@ -45,8 +45,11 @@ public class AI {
} }
static Spanned completeChat(Context context, long id, CharSequence body, long template) throws JSONException, IOException { static Spanned completeChat(Context context, long id, CharSequence body, long template) throws JSONException, IOException {
if (body == null)
body = "";
String reply = null; String reply = null;
if (body == null || body.length() == 0 || template < 0L /* Default */) { if (body.length() == 0 || template < 0L /* Default */) {
File file = EntityMessage.getFile(context, id); File file = EntityMessage.getFile(context, id);
if (file.exists()) { if (file.exists()) {
Document d = JsoupEx.parse(file); Document d = JsoupEx.parse(file);
@ -65,37 +68,35 @@ public class AI {
} }
} }
String prompt = null; String templatePrompt = null;
if (template < 0L) if (template > 0L) {
prompt = (body == null ? null : body.toString());
else {
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
EntityAnswer t = db.answer().getAnswer(template); EntityAnswer t = db.answer().getAnswer(template);
if (t != null) { if (t != null) {
String html = t.getData(context, null).getHtml(); String html = t.getData(context, null).getHtml();
prompt = JsoupEx.parse(html).body().text(); templatePrompt = JsoupEx.parse(html).body().text();
} }
} }
if (body == null || body.length() == 0)
body = "?";
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
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); String defaultPrompt = prefs.getString("openai_answer", OpenAI.DEFAULT_ANSWER_PROMPT);
String system = prefs.getString("openai_system", null); String systemPrompt = prefs.getString("openai_system", null);
List<OpenAI.Message> messages = new ArrayList<>(); List<OpenAI.Message> messages = new ArrayList<>();
if (!TextUtils.isEmpty(system)) if (!TextUtils.isEmpty(systemPrompt))
messages.add(new OpenAI.Message(OpenAI.SYSTEM, new OpenAI.Content[]{ messages.add(new OpenAI.Message(OpenAI.SYSTEM, new OpenAI.Content[]{
new OpenAI.Content(OpenAI.CONTENT_TEXT, system)})); new OpenAI.Content(OpenAI.CONTENT_TEXT, systemPrompt)}));
if (reply == null) { if (reply == null) {
if (templatePrompt != null)
messages.add(new OpenAI.Message(OpenAI.USER, new OpenAI.Content[]{
new OpenAI.Content(OpenAI.CONTENT_TEXT, templatePrompt)}));
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)));
@ -103,8 +104,10 @@ public class AI {
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 { } else {
if (templatePrompt == null && body.length() > 0)
templatePrompt = body.toString();
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, prompt == null ? answer : prompt)})); new OpenAI.Content(OpenAI.CONTENT_TEXT, templatePrompt == null ? defaultPrompt : templatePrompt)}));
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, reply)})); new OpenAI.Content(OpenAI.CONTENT_TEXT, reply)}));
} }
@ -124,15 +127,19 @@ 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); String defaultPrompt = prefs.getString("gemini_answer", Gemini.DEFAULT_ANSWER_PROMPT);
List<Gemini.Message> messages = new ArrayList<>(); List<Gemini.Message> messages = new ArrayList<>();
if (reply == null) if (reply == null) {
if (templatePrompt != null)
messages.add(new Gemini.Message(Gemini.USER, new String[]{templatePrompt}));
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 { } else {
messages.add(new Gemini.Message(Gemini.USER, new String[]{prompt == null ? answer : prompt})); if (templatePrompt == null && body.length() > 0)
templatePrompt = body.toString();
messages.add(new Gemini.Message(Gemini.USER, new String[]{templatePrompt == null ? defaultPrompt : templatePrompt}));
messages.add(new Gemini.Message(Gemini.USER, new String[]{reply})); messages.add(new Gemini.Message(Gemini.USER, new String[]{reply}));
} }

Loading…
Cancel
Save