1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2024-12-21 23:32:51 +00:00

OpenAI: added temperature option

This commit is contained in:
M66B 2023-03-09 07:43:39 +01:00
parent 1c3e457a09
commit b94054dce2
5 changed files with 60 additions and 5 deletions

View file

@ -2450,8 +2450,9 @@ public class FragmentCompose extends FragmentBase {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String model = prefs.getString("openai_model", "gpt-3.5-turbo");
float temperatur = prefs.getFloat("openai_temperature", 1.0f);
return OpenAI.completeChat(context, model, result.toArray(new OpenAI.Message[0]), 1);
return OpenAI.completeChat(context, model, result.toArray(new OpenAI.Message[0]), temperatur, 1);
}
@Override

View file

@ -145,6 +145,8 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
private TextView tvOpenAiPrivacy;
private TextInputLayout tilOpenAi;
private EditText etOpenAiModel;
private TextView tvOpenAiTemperature;
private SeekBar sbOpenAiTemperature;
private ImageButton ibOpenAi;
private SwitchCompat swUpdates;
private TextView tvGithubPrivacy;
@ -269,7 +271,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
"deepl_enabled",
"vt_enabled", "vt_apikey",
"send_enabled", "send_host",
"openai_enabled", "openai_apikey", "openai_model",
"openai_enabled", "openai_apikey", "openai_model", "openai_temperature",
"updates", "weekly", "beta", "show_changelog", "announcements",
"crash_reports", "cleanup_attachments",
"watchdog", "experiments", "main_log", "main_log_memory", "protocol", "log_level", "debug", "leak_canary",
@ -376,6 +378,8 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
tvOpenAiPrivacy = view.findViewById(R.id.tvOpenAiPrivacy);
tilOpenAi = view.findViewById(R.id.tilOpenAi);
etOpenAiModel = view.findViewById(R.id.etOpenAiModel);
tvOpenAiTemperature = view.findViewById(R.id.tvOpenAiTemperature);
sbOpenAiTemperature = view.findViewById(R.id.sbOpenAiTemperature);
ibOpenAi = view.findViewById(R.id.ibOpenAi);
swUpdates = view.findViewById(R.id.swUpdates);
tvGithubPrivacy = view.findViewById(R.id.tvGithubPrivacy);
@ -940,6 +944,23 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
}
});
sbOpenAiTemperature.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
prefs.edit().putFloat("openai_temperature", progress / 10f).apply();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Do nothing
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Do nothing
}
});
ibOpenAi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
@ -2304,6 +2325,11 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
swOpenAi.setChecked(prefs.getBoolean("openai_enabled", false));
tilOpenAi.getEditText().setText(prefs.getString("openai_apikey", null));
etOpenAiModel.setText(prefs.getString("openai_model", null));
float temperature = prefs.getFloat("openai_temperature", 1f);
tvOpenAiTemperature.setText(getString(R.string.title_advanced_openai_temperature, NF.format(temperature)));
sbOpenAiTemperature.setProgress(Math.round(temperature * 10));
swUpdates.setChecked(prefs.getBoolean("updates", true));
swCheckWeekly.setChecked(prefs.getBoolean("weekly", Helper.hasPlayStore(getContext())));
swCheckWeekly.setEnabled(swUpdates.isChecked());

View file

@ -53,7 +53,7 @@ public class OpenAI {
return (enabled && !TextUtils.isEmpty(apikey));
}
static Message[] completeChat(Context context, String model, Message[] messages, int n) throws JSONException, IOException {
static Message[] completeChat(Context context, String model, Message[] messages, Float temperature, int n) throws JSONException, IOException {
// https://platform.openai.com/docs/guides/chat/introduction
// https://platform.openai.com/docs/api-reference/chat/create
@ -68,6 +68,8 @@ public class OpenAI {
JSONObject jquestion = new JSONObject();
jquestion.put("model", model);
jquestion.put("messages", jmessages);
if (temperature != null)
jquestion.put("temperature", temperature);
jquestion.put("n", n);
JSONObject jresponse = call(context, "v1/chat/completions", jquestion);

View file

@ -633,6 +633,31 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvOpenAiModel" />
<TextView
android:id="@+id/tvOpenAiTemperature"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="48dp"
android:text="@string/title_advanced_openai_temperature"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/etOpenAiModel" />
<SeekBar
android:id="@+id/sbOpenAiTemperature"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:max="20"
android:min="0"
android:progress="10"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvOpenAiTemperature" />
<ImageButton
android:id="@+id/ibOpenAi"
android:layout_width="wrap_content"
@ -641,7 +666,7 @@
android:contentDescription="@string/title_info"
android:tooltipText="@string/title_info"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/etOpenAiModel"
app:layout_constraintTop_toBottomOf="@id/sbOpenAiTemperature"
app:srcCompat="@drawable/twotone_info_24" />
<androidx.appcompat.widget.SwitchCompat
@ -892,7 +917,7 @@
android:id="@+id/grpOpenAi"
android:layout_width="0dp"
android:layout_height="0dp"
app:constraint_referenced_ids="swOpenAi,tvOpenAiPrivacy,tilOpenAi,ibOpenAi" />
app:constraint_referenced_ids="swOpenAi,tvOpenAiPrivacy,tilOpenAi,ibOpenAi,etOpenAiModel,tvOpenAiTemperature,sbOpenAiTemperature" />
<androidx.constraintlayout.widget.Group
android:id="@+id/grpUpdates"

View file

@ -795,6 +795,7 @@
<string name="title_advanced_send">\'Send\' integration</string>
<string name="title_advanced_openai">OpenAI (ChatGPT) integration</string>
<string name="title_advanced_openai_model">Model</string>
<string name="title_advanced_openai_temperature">Temperature: %1$s</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_updates">Check for GitHub updates</string>