Skip to content

Commit

Permalink
更改自定义模型,添加显示OpenAI协议的模型列表 fix #19
Browse files Browse the repository at this point in the history
  • Loading branch information
flyun committed Sep 7, 2024
1 parent fa5a92c commit 90fa82d
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ private void verifyKey() {
chatMessageList.add(sendChatMessage);

ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
.model("gpt-3.5-turbo")
.model("gpt-4o-mini")
.temperature(0.1)
.maxTokens(256)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,34 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.theokanning.openai.OpenAiHttpException;
import com.theokanning.openai.OpenAiResponse;
import com.theokanning.openai.model.Model;
import com.theokanning.openai.service.LLMType;
import com.theokanning.openai.service.OpenAiService;

import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.LocaleController;
import org.telegram.messenger.MessagesController;
import org.telegram.messenger.NotificationCenter;
import org.telegram.messenger.R;
import org.telegram.messenger.SendMessagesHelper;
import org.telegram.messenger.UserConfig;
import org.telegram.tgnet.TLRPC;
import org.telegram.ui.ActionBar.ActionBar;
import org.telegram.ui.ActionBar.ActionBarMenu;
import org.telegram.ui.ActionBar.AlertDialog;
import org.telegram.ui.ActionBar.BaseFragment;
import org.telegram.ui.ActionBar.Theme;
import org.telegram.ui.ActionBar.ThemeDescription;
import org.telegram.ui.Components.AlertsCreator;
import org.telegram.ui.Components.EditTextBoldCursor;
import org.telegram.ui.Components.LayoutHelper;

import java.util.ArrayList;
import java.util.List;

/**
* Created by flyun on 2023/9/17.
Expand All @@ -37,6 +48,8 @@ class ChangeCustomModelActivity extends BaseFragment {
private View doneButton;

private long userId = 0;

private OpenAiService openAiService;
private String customModel;

private Theme.ResourcesProvider resourcesProvider;
Expand All @@ -62,6 +75,11 @@ public boolean onFragmentCreate() {
} else {
customModel = UserConfig.getInstance(currentAccount).customModel;
}

String token = UserConfig.getInstance(currentAccount).apiKey;
String apiServer = UserConfig.getInstance(currentAccount).apiServer;
openAiService = new OpenAiService(token, 5, apiServer, LLMType.openAi);

return super.onFragmentCreate();
}

Expand Down Expand Up @@ -122,9 +140,85 @@ protected Theme.ResourcesProvider getResourcesProvider() {

linearLayout.addView(firstNameField, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 36, 24, 24, 24, 0));

// 配置自定义模型
TextView moreModelTextView = new TextView(context);

moreModelTextView.setPadding(AndroidUtilities.dp(34), 0,
AndroidUtilities.dp(34), 0);
moreModelTextView.setGravity(Gravity.CENTER);
moreModelTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
moreModelTextView.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));

moreModelTextView.setText(LocaleController.getString("ChangeMoreModel",
R.string.ChangeMoreModel));

moreModelTextView.setTextColor(Theme.getColor(Theme.key_featuredStickers_buttonText));
moreModelTextView.setBackgroundDrawable(Theme.createSimpleSelectorRoundRectDrawable(
AndroidUtilities.dp(6), Theme.getColor(Theme.key_featuredStickers_addButton),
Theme.getColor(Theme.key_featuredStickers_addButtonPressed)));

moreModelTextView.setOnClickListener(view -> {

openAiService.baseCompletion(openAiService.listModels,
new OpenAiService.CompletionCallBack<OpenAiResponse<Model>>() {
@Override
public void onSuccess(Object o) {

if (o instanceof OpenAiResponse) {
showModel(((OpenAiResponse<Model>) o).data);
}
}

@Override
public void onError(OpenAiHttpException error, Throwable throwable) {

AndroidUtilities.runOnUIThread(() -> {
String info = SendMessagesHelper.formatError(throwable);
getNotificationCenter().postNotificationName(NotificationCenter.showAlert,
AlertsCreator.TYPE_ALERT_ERROR, info);
});

}
});


});

linearLayout.addView(moreModelTextView, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT,
48, Gravity.BOTTOM, 16, 16, 16, 16));

return fragmentView;
}

private void showModel(List<Model> modelList) {

AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
builder.setTitle(LocaleController.getString("AiModelTitle", R.string.AiModelTitle));

if (modelList != null && !modelList.isEmpty()) {

CharSequence[] charSequences = new CharSequence[modelList.size()];

int i = 0;
for (Model model: modelList) {
charSequences[i] = model.id;
i++;
}

builder.setItems(charSequences, (dialog, which) -> {
String model = charSequences[which].toString();
if (!TextUtils.isEmpty(model)) {
firstNameField.setText(model);
firstNameField.setSelection(model.length());
}
});
builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
showDialog(builder.create());
}


}

@Override
public void onResume() {
super.onResume();
Expand Down
1 change: 1 addition & 0 deletions TMessagesProj/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4205,6 +4205,7 @@
<string name="ChangeApiServerTips">符合OpenAI协议,可配置自定义Server。输入聊天请求接口网址,会自动匹配自定义Server。\n\nOpenAI可配置 https://api.openai.com/v1/chat/completions/ 或者 https://api.openai.com/</string>
<string name="ChangeApiServerMoreInfoUrl">https://github.com/flyun/chatAir/wiki/%E9%85%8D%E7%BD%AE%E8%87%AA%E5%AE%9A%E4%B9%89%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%95%99%E7%A8%8B</string>
<string name="ChangeClaudeApiServerTips">符合Claude协议,可配置自定义Server。输入聊天请求接口网址,会自动匹配自定义Server。\n\nClaude可配置 https://api.anthropic.com/v1/ 或者 https://api.anthropic.com/</string>
<string name="ChangeMoreModel">显示模型列表</string>
<!--chatAir End-->

<!--Prompt Start-->
Expand Down
2 changes: 1 addition & 1 deletion TMessagesProj/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3606,7 +3606,7 @@
<string name="ChangeApiServerTips">Complies with the OpenAI protocol and can configure a custom Server.Enter the chat request interface URL and it will automatically match the custom Server.\n\nOpenAI can be configured with https://api.openai.com/v1/chat/completions/ or https://api.openai.com/</string>
<string name="ChangeApiServerMoreInfoUrl">https://github.com/flyun/chatAir/wiki/Configuring-a-custom-server-tutorial</string>
<string name="ChangeClaudeApiServerTips">Complies with the Calude protocol and can configure a custom Server.Enter the chat request interface URL and it will automatically match the custom Server.\n\nCalude can be configured with https://api.anthropic.com/v1/ messages or https://api.anthropic.com/</string>

<string name="ChangeMoreModel">Show model list</string>
<!--chatAir End-->

<!--Prompt Start-->
Expand Down

0 comments on commit 90fa82d

Please sign in to comment.