mirror of
https://gitcode.com/ageerle/ruoyi-ai.git
synced 2026-04-14 12:23:39 +00:00
feat: 兼容多平台模型
This commit is contained in:
@@ -10,6 +10,8 @@ public class OpenAIConst {
|
|||||||
|
|
||||||
public final static String OPENAI_HOST = "https://api.openai.com/";
|
public final static String OPENAI_HOST = "https://api.openai.com/";
|
||||||
|
|
||||||
|
public final static String apiUrl = "v1/chat/completions";
|
||||||
|
|
||||||
public final static int SUCCEED_CODE = 200;
|
public final static int SUCCEED_CODE = 200;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,6 +71,11 @@ public class OpenAiStreamClient {
|
|||||||
*/
|
*/
|
||||||
private String apiHost;
|
private String apiHost;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义url 兼容多个平台
|
||||||
|
*/
|
||||||
|
private String apiUrl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自定义的okHttpClient
|
* 自定义的okHttpClient
|
||||||
* 如果不自定义 ,就是用sdk默认的OkHttpClient实例
|
* 如果不自定义 ,就是用sdk默认的OkHttpClient实例
|
||||||
@@ -112,6 +117,11 @@ public class OpenAiStreamClient {
|
|||||||
}
|
}
|
||||||
apiHost = builder.apiHost;
|
apiHost = builder.apiHost;
|
||||||
|
|
||||||
|
if (StrUtil.isBlank(builder.apiUrl)) {
|
||||||
|
builder.apiUrl = OpenAIConst.apiUrl;
|
||||||
|
}
|
||||||
|
apiUrl = builder.apiUrl;
|
||||||
|
|
||||||
if (Objects.isNull(builder.keyStrategy)) {
|
if (Objects.isNull(builder.keyStrategy)) {
|
||||||
builder.keyStrategy = new KeyRandomStrategy();
|
builder.keyStrategy = new KeyRandomStrategy();
|
||||||
}
|
}
|
||||||
@@ -180,7 +190,7 @@ public class OpenAiStreamClient {
|
|||||||
ObjectMapper mapper = new ObjectMapper();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
String requestBody = mapper.writeValueAsString(chatCompletion);
|
String requestBody = mapper.writeValueAsString(chatCompletion);
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
.url(this.apiHost + "v1/chat/completions")
|
.url(this.apiHost + apiUrl)
|
||||||
.post(RequestBody.create(MediaType.parse(ContentType.JSON.getValue()), requestBody))
|
.post(RequestBody.create(MediaType.parse(ContentType.JSON.getValue()), requestBody))
|
||||||
.build();
|
.build();
|
||||||
factory.newEventSource(request, eventSourceListener);
|
factory.newEventSource(request, eventSourceListener);
|
||||||
@@ -611,6 +621,8 @@ public class OpenAiStreamClient {
|
|||||||
*/
|
*/
|
||||||
private String apiHost;
|
private String apiHost;
|
||||||
|
|
||||||
|
private String apiUrl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自定义OkhttpClient
|
* 自定义OkhttpClient
|
||||||
*/
|
*/
|
||||||
@@ -645,6 +657,16 @@ public class OpenAiStreamClient {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param val 自定义请求后缀
|
||||||
|
* @return Builder
|
||||||
|
* @see OpenAIConst
|
||||||
|
*/
|
||||||
|
public Builder apiUrl(String val) {
|
||||||
|
apiUrl = val;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public Builder keyStrategy(KeyStrategyFunction val) {
|
public Builder keyStrategy(KeyStrategyFunction val) {
|
||||||
keyStrategy = val;
|
keyStrategy = val;
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package org.ruoyi.domain;
|
package org.ruoyi.domain;
|
||||||
|
|
||||||
|
|
||||||
|
import com.alibaba.excel.annotation.ExcelProperty;
|
||||||
import com.baomidou.mybatisplus.annotation.*;
|
import com.baomidou.mybatisplus.annotation.*;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
@@ -68,6 +69,12 @@ public class ChatModel extends BaseEntity {
|
|||||||
*/
|
*/
|
||||||
private String apiHost;
|
private String apiHost;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求地址后缀 - 兼容多平台
|
||||||
|
*/
|
||||||
|
@ExcelProperty(value = "请求地址后缀")
|
||||||
|
private String apiUrl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 密钥
|
* 密钥
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package org.ruoyi.domain.bo;
|
package org.ruoyi.domain.bo;
|
||||||
|
|
||||||
|
import com.alibaba.excel.annotation.ExcelProperty;
|
||||||
import org.ruoyi.common.core.validate.AddGroup;
|
import org.ruoyi.common.core.validate.AddGroup;
|
||||||
import org.ruoyi.common.core.validate.EditGroup;
|
import org.ruoyi.common.core.validate.EditGroup;
|
||||||
import org.ruoyi.domain.ChatModel;
|
import org.ruoyi.domain.ChatModel;
|
||||||
@@ -79,6 +80,12 @@ public class ChatModelBo extends BaseEntity {
|
|||||||
@NotBlank(message = "密钥不能为空", groups = { AddGroup.class, EditGroup.class })
|
@NotBlank(message = "密钥不能为空", groups = { AddGroup.class, EditGroup.class })
|
||||||
private String apiKey;
|
private String apiKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求地址后缀 - 兼容多平台
|
||||||
|
*/
|
||||||
|
@ExcelProperty(value = "请求地址后缀")
|
||||||
|
private String apiUrl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 备注
|
* 备注
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -88,11 +88,16 @@ public class ChatModelVo implements Serializable {
|
|||||||
@ExcelProperty(value = "密钥")
|
@ExcelProperty(value = "密钥")
|
||||||
private String apiKey;
|
private String apiKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求地址后缀 - 兼容多平台
|
||||||
|
*/
|
||||||
|
@ExcelProperty(value = "请求地址后缀")
|
||||||
|
private String apiUrl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 备注
|
* 备注
|
||||||
*/
|
*/
|
||||||
@ExcelProperty(value = "备注")
|
@ExcelProperty(value = "备注")
|
||||||
private String remark;
|
private String remark;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,11 +32,12 @@ public class ChatConfig {
|
|||||||
public OpenAiStreamClient openAiStreamClient() {
|
public OpenAiStreamClient openAiStreamClient() {
|
||||||
String apiHost = configService.getConfigValue("chat", "apiHost");
|
String apiHost = configService.getConfigValue("chat", "apiHost");
|
||||||
String apiKey = configService.getConfigValue("chat", "apiKey");
|
String apiKey = configService.getConfigValue("chat", "apiKey");
|
||||||
openAiStreamClient = createOpenAiStreamClient(apiHost,apiKey);
|
String url = configService.getConfigValue("chat", "apiUrl");
|
||||||
|
openAiStreamClient = createOpenAiStreamClient(apiHost,apiKey,url);
|
||||||
return openAiStreamClient;
|
return openAiStreamClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static OpenAiStreamClient createOpenAiStreamClient(String apiHost, String apiKey) {
|
public static OpenAiStreamClient createOpenAiStreamClient(String apiHost, String apiKey,String url) {
|
||||||
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new OpenAILogger());
|
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new OpenAILogger());
|
||||||
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
|
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
|
||||||
OkHttpClient okHttpClient = new OkHttpClient.Builder()
|
OkHttpClient okHttpClient = new OkHttpClient.Builder()
|
||||||
@@ -47,6 +48,7 @@ public class ChatConfig {
|
|||||||
.build();
|
.build();
|
||||||
return OpenAiStreamClient.builder()
|
return OpenAiStreamClient.builder()
|
||||||
.apiHost(apiHost)
|
.apiHost(apiHost)
|
||||||
|
.apiUrl(url)
|
||||||
.apiKey(Collections.singletonList(apiKey))
|
.apiKey(Collections.singletonList(apiKey))
|
||||||
.keyStrategy(new KeyRandomStrategy())
|
.keyStrategy(new KeyRandomStrategy())
|
||||||
.okHttpClient(okHttpClient)
|
.okHttpClient(okHttpClient)
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ public class OpenAIServiceImpl implements IChatService {
|
|||||||
@Override
|
@Override
|
||||||
public SseEmitter chat(ChatRequest chatRequest,SseEmitter emitter) {
|
public SseEmitter chat(ChatRequest chatRequest,SseEmitter emitter) {
|
||||||
ChatModelVo chatModelVo = chatModelService.selectModelByName(chatRequest.getModel());
|
ChatModelVo chatModelVo = chatModelService.selectModelByName(chatRequest.getModel());
|
||||||
openAiStreamClient = ChatConfig.createOpenAiStreamClient(chatModelVo.getApiHost(), chatModelVo.getApiKey());
|
openAiStreamClient = ChatConfig.createOpenAiStreamClient(chatModelVo.getApiHost(), chatModelVo.getApiKey(),chatModelVo.getApiUrl());
|
||||||
List<Message> messages = chatRequest.getMessages();
|
List<Message> messages = chatRequest.getMessages();
|
||||||
if (enabled) {
|
if (enabled) {
|
||||||
String toolString = mcpChat(chatRequest.getPrompt());
|
String toolString = mcpChat(chatRequest.getPrompt());
|
||||||
|
|||||||
4
script/sql/update/20250509.sql
Normal file
4
script/sql/update/20250509.sql
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
ALTER TABLE `chat_model`
|
||||||
|
ADD COLUMN `api_url` varchar(50) NULL COMMENT '请求后缀' AFTER `api_key`;
|
||||||
|
|
||||||
|
INSERT INTO `chat_config` (`id`, `category`, `config_name`, `config_value`, `config_dict`, `create_dept`, `create_time`, `create_by`, `update_by`, `update_time`, `remark`, `version`, `del_flag`, `update_ip`, `tenant_id`) VALUES (1779450794872414211, 'chat', 'apiUrl', 'v1/chat/completions', 'API 请求后缀', 103, '2024-04-14 18:05:05', '1', '1', '2025-04-23 22:29:04', NULL, NULL, '0', NULL, 0);
|
||||||
Reference in New Issue
Block a user