From 7939ee165887ba4fbdff679f85f8f79ad5b34145 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 8 Mar 2023 20:34:39 +0100 Subject: [PATCH] OpenAI: added model option --- .../eu/faircode/email/FragmentCompose.java | 5 +++- .../faircode/email/FragmentOptionsMisc.java | 29 +++++++++++++++++-- .../main/java/eu/faircode/email/OpenAI.java | 4 +-- .../main/res/layout/fragment_options_misc.xml | 25 +++++++++++++++- app/src/main/res/values/strings.xml | 1 + 5 files changed, 58 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 27fc7dddfe..647eccd76a 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -2448,7 +2448,10 @@ public class FragmentCompose extends FragmentBase { if (result.size() == 0) return null; - return OpenAI.completeChat(context, result.toArray(new OpenAI.Message[0]), 1); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + String model = prefs.getString("openai_model", "gpt-3.5-turbo"); + + return OpenAI.completeChat(context, model, result.toArray(new OpenAI.Message[0]), 1); } @Override diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java b/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java index 8bffb64931..88c4b3549d 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java @@ -144,6 +144,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc private SwitchCompat swOpenAi; private TextView tvOpenAiPrivacy; private TextInputLayout tilOpenAi; + private EditText etOpenAiModel; private ImageButton ibOpenAi; private SwitchCompat swUpdates; private TextView tvGithubPrivacy; @@ -268,7 +269,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc "deepl_enabled", "vt_enabled", "vt_apikey", "send_enabled", "send_host", - "openai_enabled", "openai_apikey", + "openai_enabled", "openai_apikey", "openai_model", "updates", "weekly", "beta", "show_changelog", "announcements", "crash_reports", "cleanup_attachments", "watchdog", "experiments", "main_log", "main_log_memory", "protocol", "log_level", "debug", "leak_canary", @@ -374,6 +375,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc swOpenAi = view.findViewById(R.id.swOpenAi); tvOpenAiPrivacy = view.findViewById(R.id.tvOpenAiPrivacy); tilOpenAi = view.findViewById(R.id.tilOpenAi); + etOpenAiModel = view.findViewById(R.id.etOpenAiModel); ibOpenAi = view.findViewById(R.id.ibOpenAi); swUpdates = view.findViewById(R.id.swUpdates); tvGithubPrivacy = view.findViewById(R.id.tvGithubPrivacy); @@ -917,6 +919,27 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc } }); + etOpenAiModel.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + // Do nothing + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + // Do nothing + } + + @Override + public void afterTextChanged(Editable s) { + String model = s.toString().trim(); + if (TextUtils.isEmpty(model)) + prefs.edit().remove("openai_model").apply(); + else + prefs.edit().putString("openai_model", model).apply(); + } + }); + ibOpenAi.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -2112,7 +2135,8 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc "lt_key".equals(key) || "vt_apikey".equals(key) || "send_host".equals(key) || - "openai_apikey".equals(key)) + "openai_apikey".equals(key) || + "openai_model".equals(key)) return; if ("global_keywords".equals(key)) @@ -2279,6 +2303,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc etSend.setText(prefs.getString("send_host", null)); swOpenAi.setChecked(prefs.getBoolean("openai_enabled", false)); tilOpenAi.getEditText().setText(prefs.getString("openai_apikey", null)); + etOpenAiModel.setText(prefs.getString("openai_model", null)); swUpdates.setChecked(prefs.getBoolean("updates", true)); swCheckWeekly.setChecked(prefs.getBoolean("weekly", Helper.hasPlayStore(getContext()))); swCheckWeekly.setEnabled(swUpdates.isChecked()); diff --git a/app/src/main/java/eu/faircode/email/OpenAI.java b/app/src/main/java/eu/faircode/email/OpenAI.java index 01c765330e..c8be432336 100644 --- a/app/src/main/java/eu/faircode/email/OpenAI.java +++ b/app/src/main/java/eu/faircode/email/OpenAI.java @@ -53,7 +53,7 @@ public class OpenAI { return (enabled && !TextUtils.isEmpty(apikey)); } - static Message[] completeChat(Context context, Message[] messages, int n) throws JSONException, IOException { + static Message[] completeChat(Context context, String model, Message[] messages, int n) throws JSONException, IOException { // https://platform.openai.com/docs/guides/chat/introduction // https://platform.openai.com/docs/api-reference/chat/create @@ -66,7 +66,7 @@ public class OpenAI { } JSONObject jquestion = new JSONObject(); - jquestion.put("model", "gpt-3.5-turbo"); + jquestion.put("model", model); jquestion.put("messages", jmessages); jquestion.put("n", n); JSONObject jresponse = call(context, "v1/chat/completions", jquestion); diff --git a/app/src/main/res/layout/fragment_options_misc.xml b/app/src/main/res/layout/fragment_options_misc.xml index f13ddeec15..420383486c 100644 --- a/app/src/main/res/layout/fragment_options_misc.xml +++ b/app/src/main/res/layout/fragment_options_misc.xml @@ -610,6 +610,29 @@ android:textAppearance="@style/TextAppearance.AppCompat.Small" /> + + + + VirusTotal integration \'Send\' integration OpenAI (ChatGPT) integration + Model I want to use an sdcard Periodically check if FairEmail is still active Check for GitHub updates