Added max tokens option

master
M66B 2 weeks ago
parent 03d3291f3b
commit 096835b3ae

@ -80,6 +80,7 @@ public class FragmentOptionsIntegrations extends FragmentBase implements SharedP
private EditText etOpenAi; private EditText etOpenAi;
private TextInputLayout tilOpenAi; private TextInputLayout tilOpenAi;
private EditText etOpenAiModel; private EditText etOpenAiModel;
private EditText etOpenAiMaxTokens;
private SwitchCompat swOpenMultiModal; private SwitchCompat swOpenMultiModal;
private TextView tvOpenAiTemperature; private TextView tvOpenAiTemperature;
private SeekBar sbOpenAiTemperature; private SeekBar sbOpenAiTemperature;
@ -156,6 +157,7 @@ public class FragmentOptionsIntegrations extends FragmentBase implements SharedP
etOpenAi = view.findViewById(R.id.etOpenAi); etOpenAi = view.findViewById(R.id.etOpenAi);
tilOpenAi = view.findViewById(R.id.tilOpenAi); tilOpenAi = view.findViewById(R.id.tilOpenAi);
etOpenAiModel = view.findViewById(R.id.etOpenAiModel); etOpenAiModel = view.findViewById(R.id.etOpenAiModel);
etOpenAiMaxTokens = view.findViewById(R.id.etOpenAiMaxTokens);
swOpenMultiModal = view.findViewById(R.id.swOpenMultiModal); swOpenMultiModal = view.findViewById(R.id.swOpenMultiModal);
tvOpenAiTemperature = view.findViewById(R.id.tvOpenAiTemperature); tvOpenAiTemperature = view.findViewById(R.id.tvOpenAiTemperature);
sbOpenAiTemperature = view.findViewById(R.id.sbOpenAiTemperature); sbOpenAiTemperature = view.findViewById(R.id.sbOpenAiTemperature);
@ -427,6 +429,7 @@ public class FragmentOptionsIntegrations extends FragmentBase implements SharedP
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("openai_enabled", checked).apply(); prefs.edit().putBoolean("openai_enabled", checked).apply();
etOpenAiModel.setEnabled(checked); etOpenAiModel.setEnabled(checked);
etOpenAiMaxTokens.setEnabled(checked);
swOpenMultiModal.setEnabled(checked); swOpenMultiModal.setEnabled(checked);
sbOpenAiTemperature.setEnabled(checked); sbOpenAiTemperature.setEnabled(checked);
etOpenAiSummarize.setEnabled(checked); etOpenAiSummarize.setEnabled(checked);
@ -510,6 +513,27 @@ public class FragmentOptionsIntegrations extends FragmentBase implements SharedP
} }
}); });
etOpenAiMaxTokens.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// Do nothing
}
@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) {
Integer max_tokens = Helper.parseInt(s.toString().trim());
if (max_tokens == null)
prefs.edit().remove("openai_max_tokens").apply();
else
prefs.edit().putInt("openai_max_tokens", max_tokens).apply();
}
});
swOpenMultiModal.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { swOpenMultiModal.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override @Override
public void onCheckedChanged(CompoundButton buttonView, boolean checked) { public void onCheckedChanged(CompoundButton buttonView, boolean checked) {
@ -864,6 +888,9 @@ public class FragmentOptionsIntegrations extends FragmentBase implements SharedP
etOpenAiModel.setText(prefs.getString("openai_model", null)); etOpenAiModel.setText(prefs.getString("openai_model", null));
etOpenAiModel.setEnabled(swOpenAi.isChecked()); etOpenAiModel.setEnabled(swOpenAi.isChecked());
int max_tokens = prefs.getInt("openai_max_tokens", 0);
etOpenAiMaxTokens.setText(max_tokens > 0 ? Integer.toString(max_tokens) : "");
swOpenMultiModal.setChecked(prefs.getBoolean("openai_multimodal", false)); swOpenMultiModal.setChecked(prefs.getBoolean("openai_multimodal", false));
swOpenMultiModal.setEnabled(swOpenAi.isChecked()); swOpenMultiModal.setEnabled(swOpenAi.isChecked());

@ -116,6 +116,9 @@ public class OpenAI {
} }
static Message[] completeChat(Context context, String model, Message[] messages, Float temperature, int n) throws JSONException, IOException { static Message[] completeChat(Context context, String model, Message[] messages, Float temperature, int n) throws JSONException, IOException {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
int max_tokens = prefs.getInt("openai_max_tokens", 0);
// 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
JSONArray jmessages = new JSONArray(); JSONArray jmessages = new JSONArray();
@ -146,6 +149,8 @@ public class OpenAI {
JSONObject jquestion = new JSONObject(); JSONObject jquestion = new JSONObject();
jquestion.put("model", model); jquestion.put("model", model);
if (max_tokens > 0)
jquestion.put("max_tokens", max_tokens);
jquestion.put("messages", jmessages); jquestion.put("messages", jmessages);
if (temperature != null) if (temperature != null)
jquestion.put("temperature", temperature); jquestion.put("temperature", temperature);

@ -667,6 +667,28 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvOpenAiModel" /> app:layout_constraintTop_toBottomOf="@id/tvOpenAiModel" />
<TextView
android:id="@+id/tvOpenAiMaxTokens"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="12dp"
android:text="@string/title_advanced_openai_max_tokens"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/etOpenAiModel" />
<EditText
android:id="@+id/etOpenAiMaxTokens"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:inputType="text"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvOpenAiMaxTokens" />
<androidx.appcompat.widget.SwitchCompat <androidx.appcompat.widget.SwitchCompat
android:id="@+id/swOpenMultiModal" android:id="@+id/swOpenMultiModal"
android:layout_width="0dp" android:layout_width="0dp"
@ -677,7 +699,7 @@
android:text="@string/title_advanced_multi_modal" android:text="@string/title_advanced_multi_modal"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/etOpenAiModel" app:layout_constraintTop_toBottomOf="@id/etOpenAiMaxTokens"
app:switchPadding="12dp" /> app:switchPadding="12dp" />
<TextView <TextView

@ -932,6 +932,7 @@
<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_openai_model">Model</string>
<string name="title_advanced_openai_max_tokens">Maximum tokens (leave blank for unlimited)</string>
<string name="title_advanced_openai_temperature">Temperature: %1$s</string> <string name="title_advanced_openai_temperature">Temperature: %1$s</string>
<string name="title_advanced_openai_moderation">Content moderation</string> <string name="title_advanced_openai_moderation">Content moderation</string>
<string name="title_advanced_gemini">Gemini integration</string> <string name="title_advanced_gemini">Gemini integration</string>

Loading…
Cancel
Save