mirror of
https://github.com/M66B/FairEmail.git
synced 2025-02-24 23:12:55 +00:00
OpenAI: added model option
This commit is contained in:
parent
6b819dcd64
commit
7939ee1658
5 changed files with 58 additions and 6 deletions
|
@ -2448,7 +2448,10 @@ public class FragmentCompose extends FragmentBase {
|
||||||
if (result.size() == 0)
|
if (result.size() == 0)
|
||||||
return null;
|
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
|
@Override
|
||||||
|
|
|
@ -144,6 +144,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
|
||||||
private SwitchCompat swOpenAi;
|
private SwitchCompat swOpenAi;
|
||||||
private TextView tvOpenAiPrivacy;
|
private TextView tvOpenAiPrivacy;
|
||||||
private TextInputLayout tilOpenAi;
|
private TextInputLayout tilOpenAi;
|
||||||
|
private EditText etOpenAiModel;
|
||||||
private ImageButton ibOpenAi;
|
private ImageButton ibOpenAi;
|
||||||
private SwitchCompat swUpdates;
|
private SwitchCompat swUpdates;
|
||||||
private TextView tvGithubPrivacy;
|
private TextView tvGithubPrivacy;
|
||||||
|
@ -268,7 +269,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
|
||||||
"deepl_enabled",
|
"deepl_enabled",
|
||||||
"vt_enabled", "vt_apikey",
|
"vt_enabled", "vt_apikey",
|
||||||
"send_enabled", "send_host",
|
"send_enabled", "send_host",
|
||||||
"openai_enabled", "openai_apikey",
|
"openai_enabled", "openai_apikey", "openai_model",
|
||||||
"updates", "weekly", "beta", "show_changelog", "announcements",
|
"updates", "weekly", "beta", "show_changelog", "announcements",
|
||||||
"crash_reports", "cleanup_attachments",
|
"crash_reports", "cleanup_attachments",
|
||||||
"watchdog", "experiments", "main_log", "main_log_memory", "protocol", "log_level", "debug", "leak_canary",
|
"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);
|
swOpenAi = view.findViewById(R.id.swOpenAi);
|
||||||
tvOpenAiPrivacy = view.findViewById(R.id.tvOpenAiPrivacy);
|
tvOpenAiPrivacy = view.findViewById(R.id.tvOpenAiPrivacy);
|
||||||
tilOpenAi = view.findViewById(R.id.tilOpenAi);
|
tilOpenAi = view.findViewById(R.id.tilOpenAi);
|
||||||
|
etOpenAiModel = view.findViewById(R.id.etOpenAiModel);
|
||||||
ibOpenAi = view.findViewById(R.id.ibOpenAi);
|
ibOpenAi = view.findViewById(R.id.ibOpenAi);
|
||||||
swUpdates = view.findViewById(R.id.swUpdates);
|
swUpdates = view.findViewById(R.id.swUpdates);
|
||||||
tvGithubPrivacy = view.findViewById(R.id.tvGithubPrivacy);
|
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() {
|
ibOpenAi.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
|
@ -2112,7 +2135,8 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
|
||||||
"lt_key".equals(key) ||
|
"lt_key".equals(key) ||
|
||||||
"vt_apikey".equals(key) ||
|
"vt_apikey".equals(key) ||
|
||||||
"send_host".equals(key) ||
|
"send_host".equals(key) ||
|
||||||
"openai_apikey".equals(key))
|
"openai_apikey".equals(key) ||
|
||||||
|
"openai_model".equals(key))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if ("global_keywords".equals(key))
|
if ("global_keywords".equals(key))
|
||||||
|
@ -2279,6 +2303,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
|
||||||
etSend.setText(prefs.getString("send_host", null));
|
etSend.setText(prefs.getString("send_host", null));
|
||||||
swOpenAi.setChecked(prefs.getBoolean("openai_enabled", false));
|
swOpenAi.setChecked(prefs.getBoolean("openai_enabled", false));
|
||||||
tilOpenAi.getEditText().setText(prefs.getString("openai_apikey", null));
|
tilOpenAi.getEditText().setText(prefs.getString("openai_apikey", null));
|
||||||
|
etOpenAiModel.setText(prefs.getString("openai_model", null));
|
||||||
swUpdates.setChecked(prefs.getBoolean("updates", true));
|
swUpdates.setChecked(prefs.getBoolean("updates", true));
|
||||||
swCheckWeekly.setChecked(prefs.getBoolean("weekly", Helper.hasPlayStore(getContext())));
|
swCheckWeekly.setChecked(prefs.getBoolean("weekly", Helper.hasPlayStore(getContext())));
|
||||||
swCheckWeekly.setEnabled(swUpdates.isChecked());
|
swCheckWeekly.setEnabled(swUpdates.isChecked());
|
||||||
|
|
|
@ -53,7 +53,7 @@ public class OpenAI {
|
||||||
return (enabled && !TextUtils.isEmpty(apikey));
|
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/guides/chat/introduction
|
||||||
// https://platform.openai.com/docs/api-reference/chat/create
|
// https://platform.openai.com/docs/api-reference/chat/create
|
||||||
|
|
||||||
|
@ -66,7 +66,7 @@ public class OpenAI {
|
||||||
}
|
}
|
||||||
|
|
||||||
JSONObject jquestion = new JSONObject();
|
JSONObject jquestion = new JSONObject();
|
||||||
jquestion.put("model", "gpt-3.5-turbo");
|
jquestion.put("model", model);
|
||||||
jquestion.put("messages", jmessages);
|
jquestion.put("messages", jmessages);
|
||||||
jquestion.put("n", n);
|
jquestion.put("n", n);
|
||||||
JSONObject jresponse = call(context, "v1/chat/completions", jquestion);
|
JSONObject jresponse = call(context, "v1/chat/completions", jquestion);
|
||||||
|
|
|
@ -610,6 +610,29 @@
|
||||||
android:textAppearance="@style/TextAppearance.AppCompat.Small" />
|
android:textAppearance="@style/TextAppearance.AppCompat.Small" />
|
||||||
</com.google.android.material.textfield.TextInputLayout>
|
</com.google.android.material.textfield.TextInputLayout>
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tvOpenAiModel"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginStart="12dp"
|
||||||
|
android:layout_marginTop="12dp"
|
||||||
|
android:text="@string/title_advanced_openai_model"
|
||||||
|
android:textAppearance="@style/TextAppearance.AppCompat.Small"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@id/tilOpenAi" />
|
||||||
|
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/etOpenAiModel"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginStart="12dp"
|
||||||
|
android:hint="gpt-3.5-turbo"
|
||||||
|
android:inputType="text"
|
||||||
|
android:textAppearance="@style/TextAppearance.AppCompat.Small"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@id/tvOpenAiModel" />
|
||||||
|
|
||||||
<ImageButton
|
<ImageButton
|
||||||
android:id="@+id/ibOpenAi"
|
android:id="@+id/ibOpenAi"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
|
@ -618,7 +641,7 @@
|
||||||
android:contentDescription="@string/title_info"
|
android:contentDescription="@string/title_info"
|
||||||
android:tooltipText="@string/title_info"
|
android:tooltipText="@string/title_info"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toBottomOf="@id/tilOpenAi"
|
app:layout_constraintTop_toBottomOf="@id/etOpenAiModel"
|
||||||
app:srcCompat="@drawable/twotone_info_24" />
|
app:srcCompat="@drawable/twotone_info_24" />
|
||||||
|
|
||||||
<androidx.appcompat.widget.SwitchCompat
|
<androidx.appcompat.widget.SwitchCompat
|
||||||
|
|
|
@ -794,6 +794,7 @@
|
||||||
<string name="title_advanced_virus_total">VirusTotal integration</string>
|
<string name="title_advanced_virus_total">VirusTotal integration</string>
|
||||||
<string name="title_advanced_send">\'Send\' integration</string>
|
<string name="title_advanced_send">\'Send\' integration</string>
|
||||||
<string name="title_advanced_openai">OpenAI (ChatGPT) integration</string>
|
<string name="title_advanced_openai">OpenAI (ChatGPT) integration</string>
|
||||||
|
<string name="title_advanced_openai_model">Model</string>
|
||||||
<string name="title_advanced_sdcard">I want to use an sdcard</string>
|
<string name="title_advanced_sdcard">I want to use an sdcard</string>
|
||||||
<string name="title_advanced_watchdog">Periodically check if FairEmail is still active</string>
|
<string name="title_advanced_watchdog">Periodically check if FairEmail is still active</string>
|
||||||
<string name="title_advanced_updates">Check for GitHub updates</string>
|
<string name="title_advanced_updates">Check for GitHub updates</string>
|
||||||
|
|
Loading…
Reference in a new issue