Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
flyun committed May 26, 2024
2 parents 5746478 + 479da5f commit 9c36e97
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5787,6 +5787,7 @@ protected void performSendMessageRequest(final TLObject req, final MessageObject
boolean isGeminiProVision;

boolean isClaude;
boolean isOpenAIVision;

if ((user.flags2 & MessagesController.UPDATE_MASK_CHAT_AIR_PROMPT) != 0
&& !TextUtils.isEmpty(user.prompt)) {
Expand All @@ -5810,6 +5811,7 @@ protected void performSendMessageRequest(final TLObject req, final MessageObject
isGeminiProVision = UserConfig.getInstance(currentAccount).isJudgeByModelGeminiProVision(aiModel);

isClaude = UserConfig.getInstance(currentAccount).isJudgeByModelClaude(aiModel);
isOpenAIVision = UserConfig.getInstance(currentAccount).isJudgeByModelOpenAIVision(aiModel);

if ((user.flags2 & MessagesController.UPDATE_MASK_CHAT_AIR_AI_TEMPERATURE) != 0
&& user.flags2 != -1) {
Expand Down Expand Up @@ -5841,7 +5843,7 @@ protected void performSendMessageRequest(final TLObject req, final MessageObject
ChatCompletionRequest chatCompletionRequest;

// 发送请求格式切换,多模态请求,以及旧模态请求(只能发送文本)
if (isMultiCompletionRequest(aiModelReal)) {
if (isMultiCompletionRequest(aiModelReal, isOpenAIVision)) {
List<ChatMultiMessage> chatMessageList = getChatMultiCompletionRequest(prompt, msgObj);

chatCompletionRequest = ChatCompletionRequest.builder()
Expand Down Expand Up @@ -6552,7 +6554,7 @@ private List<ChatMessage> getChatCompletionRequest(
}

// 控制多模态与旧模态切换
private boolean isMultiCompletionRequest(String aiModelReal) {
private boolean isMultiCompletionRequest(String aiModelReal, boolean isOpenAIVision) {

boolean isMulti = false;

Expand All @@ -6564,9 +6566,9 @@ private boolean isMultiCompletionRequest(String aiModelReal) {
// 截止2023.11.11 openAi官方的聊天模型都适配多模态
// 对于只是转发openAi的网站,没有问题。但比如OpenRouter,还是使用旧的模型,没有针对openAi所有模型进行更新
boolean isModelReal = false;
if (!TextUtils.isEmpty(aiModelReal)) isModelReal = aiModelReal.contains("gpt-4-vision");
if (!TextUtils.isEmpty(aiModelReal)) isModelReal = UserConfig.isOpenAiMulti(aiModelReal);

isMulti = !isOpenRouter && isModelReal;
isMulti = !isOpenRouter && (isModelReal || isOpenAIVision);

return isMulti;

Expand Down Expand Up @@ -6757,6 +6759,7 @@ private void sendMessageGoogle(String aiModelReal, final String prompt, Double t
.build();

completionRequest.setGenerationConfig(chatGGenerationConfig);
String version = UserConfig.getGoogleVersion(aiModelReal);

// 配置聊天
List<ChatGMessage> contents = getChatChatGMessageList(prompt, originalPath, isGeminiProVision, msgObj);
Expand All @@ -6774,7 +6777,7 @@ private void sendMessageGoogle(String aiModelReal, final String prompt, Double t

String geminiId = "Gemini_" + SystemClock.elapsedRealtime();

openAiService.streamChatGCompletion(completionRequest, aiModelReal,
openAiService.streamChatGCompletion(completionRequest, aiModelReal, version,
new OpenAiService.StreamGCallBack() {
@Override
public void onSuccess(ChatGCompletionResponse result) {
Expand Down Expand Up @@ -6975,7 +6978,7 @@ public void onLoading(boolean isLoading) {
}
});
} else {
openAiService.createChatGCompletion(completionRequest, aiModelReal, baseMessage,
openAiService.createChatGCompletion(completionRequest, aiModelReal, version, baseMessage,
new OpenAiService.ResultGCallBack() {
@Override
public void onSuccess(ChatGCompletionResponse result) {
Expand Down
52 changes: 49 additions & 3 deletions TMessagesProj/src/main/java/org/telegram/messenger/UserConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -510,13 +510,18 @@ private void initAiModelList() {
aiModelList.put(2, new AiModelBean("GPT-3.5-0613", "gpt-3.5-turbo-0613", false));
aiModelList.put(3, new AiModelBean("GPT-3.5-16k", "gpt-3.5-turbo-16k", true));
aiModelList.put(4, new AiModelBean("GPT-3.5-16k-0613", "gpt-3.5-turbo-16k-0613", false));
aiModelList.put(12, new AiModelBean("GPT-3.5-0125", "gpt-3.5-turbo-0125", true));
aiModelList.put(5, new AiModelBean("GPT-4", "gpt-4", true));
aiModelList.put(6, new AiModelBean("GPT-4-0613", "gpt-4-0613", false));
aiModelList.put(7, new AiModelBean("GPT-4-32k", "gpt-4-32k", true));
aiModelList.put(8, new AiModelBean("GPT-4-32k-0613", "gpt-4-32k-0613", false));
aiModelList.put(9, new AiModelBean("GPT-4-1106-preview", "gpt-4-1106-preview", true));
aiModelList.put(9, new AiModelBean("GPT-4-1106-preview", "gpt-4-1106-preview", false));
aiModelList.put(11, new AiModelBean("GPT-4-0125-preview", "gpt-4-0125-preview", true));
aiModelList.put(13, new AiModelBean("GPT-4o", "gpt-4o", true));
aiModelList.put(10, new AiModelBean("GPT-4-vision-preview", "gpt-4-vision-preview",
"GPT-4-vision-preview (Picture model)", true));
aiModelList.put(14, new AiModelBean("GPT-4o-Picture", "gpt-4o",
"GPT-4o (Picture model)", true));
// initOpenrouter();
initGoogle();
initClaude();
Expand Down Expand Up @@ -551,9 +556,15 @@ private void initOpenrouter() {
// https://ai.google.dev/models/gemini
public void initGoogle() {
if (aiModelList == null) return;
aiModelList.put(801, new AiModelBean("Gemini Pro", "gemini-pro", true));
aiModelList.put(801, new AiModelBean("Gemini Pro 1.0", "gemini-pro", true));
aiModelList.put(803, new AiModelBean("Gemini Pro 1.5", "gemini-1.5-pro-latest", true));
aiModelList.put(804, new AiModelBean("Gemini Pro 1.5 Flash", "gemini-1.5-flash-latest", true));
aiModelList.put(802, new AiModelBean("Gemini Pro Vision", "gemini-pro-vision",
"Gemini Pro Vision (Picture model)", true));
aiModelList.put(805, new AiModelBean("Gemini Pro 1.5 Picture", "gemini-1.5-pro-latest",
"Gemini Pro 1.5 (Picture model)", true));
aiModelList.put(806, new AiModelBean("Gemini 1.5 Flash Picture", "gemini-1.5-flash-latest",
"Gemini 1.5 Flash (Picture model)", true));

}

Expand All @@ -562,8 +573,26 @@ public void initClaude() {
aiModelList.put(901, new AiModelBean("Claude 3 haiku", "claude-3-haiku-20240307",
true));
aiModelList.put(902, new AiModelBean("Claude 3 opus", "claude-3-opus-20240229",
true));
true));

}

public static boolean isOpenAiMulti(String model) {
if (!TextUtils.isEmpty(model)
&& (model.equals("gpt-4-vision-preview")
)) {
return true;
}
return false;
}
public static String getGoogleVersion(String model) {
if (!TextUtils.isEmpty(model)
&& (model.equals("gemini-1.5-pro-latest")
|| model.equals("gemini-1.5-flash-latest")
)) {
return "v1beta";
}
return "v1";
}

public String getAiModelName(int aiModel) {
Expand Down Expand Up @@ -724,6 +753,10 @@ public boolean isJudgeByModelGemini(int aiModel) {

if (aiModel == 801) return true;
if (aiModel == 802) return true;
if (aiModel == 803) return true;
if (aiModel == 804) return true;
if (aiModel == 805) return true;
if (aiModel == 806) return true;

return false;
}
Expand All @@ -744,6 +777,16 @@ public static boolean isUserGeminiProVision(int currentAccount, long userId) {
public boolean isJudgeByModelGeminiProVision(int aiModel) {

if (aiModel == 802) return true;
if (aiModel == 805) return true;
if (aiModel == 806) return true;

return false;
}

public boolean isJudgeByModelOpenAIVision(int aiModel) {

if (aiModel == 10) return true;
if (aiModel == 14) return true;

return false;
}
Expand All @@ -765,7 +808,10 @@ public static boolean isUserVision(int currentAccount, long userId) {
public boolean isJudgeByModelVision(int aiModel) {

if (aiModel == 802) return true;
if (aiModel == 805) return true;
if (aiModel == 806) return true;
if (aiModel == 10) return true;
if (aiModel == 14) return true;

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ private void verifyKey() {
completionRequest.setContents(chatGMessageList);

openAiService.createChatGCompletion(completionRequest, "gemini-pro",
UserConfig.getGoogleVersion("gemini-pro"),
new BaseMessage(), new OpenAiService.ResultGCallBack() {
@Override
public void onSuccess(ChatGCompletionResponse result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ private void verifyKey() {
completionRequest.setContents(chatGMessageList);

openAiService.createChatGCompletion(completionRequest, "gemini-pro",
UserConfig.getGoogleVersion("gemini-pro"),
new BaseMessage(), new OpenAiService.ResultGCallBack() {
@Override
public void onSuccess(ChatGCompletionResponse result) {
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
#Sat Mar 12 05:53:50 MSK 2016
APP_VERSION_CODE=4
APP_VERSION_NAME=1.0.3
APP_VERSION_CODE=6
APP_VERSION_NAME=1.0.6
APP_PACKAGE=info.flyun.chatair
RELEASE_KEY_PASSWORD=android
RELEASE_KEY_ALIAS=androidkey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,18 @@ public interface OpenAiApi {

// -----Gemini-----
// @POST("v1beta/models/gemini-pro:generateContent?key={api_key}")
@POST("v1/models/{model}:generateContent")
@POST("{version}/models/{model}:generateContent")
Single<ChatGCompletionResponse> createGChatCompletion(
@Path("model") String model,
@Path("version") String version,
// @Query("key") String api_key,
@Body ChatGCompletionRequest request);

@Streaming
@POST("v1/models/{model}:streamGenerateContent?alt=sse")
@POST("{version}/models/{model}:streamGenerateContent?alt=sse")
Call<ResponseBody> createGChatCompletionStream(
@Path("model") String model,
@Path("version") String version,
@Body ChatGCompletionRequest request);

@POST("v1/messages")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,10 @@ public void createChatCompletion(ChatCompletionRequest request, final BaseMessag
createChatCompletion(api.createChatCompletion(request), baseMessage, callBack);
}

public void createChatGCompletion(ChatGCompletionRequest request, String model,
public void createChatGCompletion(ChatGCompletionRequest request, String model, String version,
final BaseMessage baseMessage, final ResultGCallBack callBack) {

createChatGCompletion(api.createGChatCompletion(model, request), baseMessage, callBack);
createChatGCompletion(api.createGChatCompletion(model, version, request), baseMessage, callBack);
}
public void createChatACompletion(ChatACompletionRequest request,
final ResultACallBack callBack) {
Expand Down Expand Up @@ -991,14 +991,15 @@ public void streamChatCompletion(ChatCompletionRequest request, StreamCallBack c


private StreamGCallBack streamGCallBack;
public void streamChatGCompletion(ChatGCompletionRequest request, String model, StreamGCallBack callBack) {
public void streamChatGCompletion(ChatGCompletionRequest request, String model, String version,
StreamGCallBack callBack) {

isStreamFirstLoading = true;
loadingG(callBack);
//因为on在取消stream请求的问题(具体看ResponseBodyCallback),无法执行onCompletion,所以需要折中调用onCompletion
streamGCallBack = callBack;

Call<ResponseBody> apiCall = api.createGChatCompletionStream(model, request);
Call<ResponseBody> apiCall = api.createGChatCompletionStream(model, version, request);
Disposable disposable = streamG(apiCall, ChatGCompletionResponse.class)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
Expand Down

0 comments on commit 9c36e97

Please sign in to comment.