| | |
| | | |
| | | 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; |
| | | |
| | | } |
| | |
| | | private String apiHost; |
| | | |
| | | /** |
| | | * èªå®ä¹url å
¼å®¹å¤ä¸ªå¹³å° |
| | | */ |
| | | private String apiUrl; |
| | | |
| | | /** |
| | | * èªå®ä¹çokHttpClient |
| | | * 妿ä¸èªå®ä¹ ï¼å°±æ¯ç¨sdké»è®¤çOkHttpClientå®ä¾ |
| | | */ |
| | |
| | | builder.apiHost = OpenAIConst.OPENAI_HOST; |
| | | } |
| | | apiHost = builder.apiHost; |
| | | |
| | | if (StrUtil.isBlank(builder.apiUrl)) { |
| | | builder.apiUrl = OpenAIConst.apiUrl; |
| | | } |
| | | apiUrl = builder.apiUrl; |
| | | |
| | | if (Objects.isNull(builder.keyStrategy)) { |
| | | builder.keyStrategy = new KeyRandomStrategy(); |
| | |
| | | ObjectMapper mapper = new ObjectMapper(); |
| | | String requestBody = mapper.writeValueAsString(chatCompletion); |
| | | Request request = new Request.Builder() |
| | | .url(this.apiHost + "v1/chat/completions") |
| | | .url(this.apiHost + apiUrl) |
| | | .post(RequestBody.create(MediaType.parse(ContentType.JSON.getValue()), requestBody)) |
| | | .build(); |
| | | factory.newEventSource(request, eventSourceListener); |
| | |
| | | */ |
| | | private String apiHost; |
| | | |
| | | private String apiUrl; |
| | | |
| | | /** |
| | | * èªå®ä¹OkhttpClient |
| | | */ |
| | |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * @param val èªå®ä¹è¯·æ±åç¼ |
| | | * @return Builder |
| | | * @see OpenAIConst |
| | | */ |
| | | public Builder apiUrl(String val) { |
| | | apiUrl = val; |
| | | return this; |
| | | } |
| | | |
| | | public Builder keyStrategy(KeyStrategyFunction val) { |
| | | keyStrategy = val; |
| | | return this; |
| | |
| | | package org.ruoyi.domain; |
| | | |
| | | |
| | | import com.alibaba.excel.annotation.ExcelProperty; |
| | | import com.baomidou.mybatisplus.annotation.*; |
| | | import lombok.Data; |
| | | import lombok.EqualsAndHashCode; |
| | |
| | | private String apiHost; |
| | | |
| | | /** |
| | | * 请æ±å°ååç¼ - å
¼å®¹å¤å¹³å° |
| | | */ |
| | | @ExcelProperty(value = "请æ±å°ååç¼") |
| | | private String apiUrl; |
| | | |
| | | /** |
| | | * å¯é¥ |
| | | */ |
| | | private String apiKey; |
| | |
| | | package org.ruoyi.domain.bo; |
| | | |
| | | import com.alibaba.excel.annotation.ExcelProperty; |
| | | import org.ruoyi.common.core.validate.AddGroup; |
| | | import org.ruoyi.common.core.validate.EditGroup; |
| | | import org.ruoyi.domain.ChatModel; |
| | |
| | | private String apiKey; |
| | | |
| | | /** |
| | | * 请æ±å°ååç¼ - å
¼å®¹å¤å¹³å° |
| | | */ |
| | | @ExcelProperty(value = "请æ±å°ååç¼") |
| | | private String apiUrl; |
| | | |
| | | /** |
| | | * 夿³¨ |
| | | */ |
| | | @NotBlank(message = "夿³¨ä¸è½ä¸ºç©º", groups = { AddGroup.class, EditGroup.class }) |
| | |
| | | private String apiKey; |
| | | |
| | | /** |
| | | * 请æ±å°ååç¼ - å
¼å®¹å¤å¹³å° |
| | | */ |
| | | @ExcelProperty(value = "请æ±å°ååç¼") |
| | | private String apiUrl; |
| | | |
| | | /** |
| | | * 夿³¨ |
| | | */ |
| | | @ExcelProperty(value = "夿³¨") |
| | | private String remark; |
| | | |
| | | |
| | | } |
| | |
| | | public OpenAiStreamClient openAiStreamClient() { |
| | | String apiHost = configService.getConfigValue("chat", "apiHost"); |
| | | String apiKey = configService.getConfigValue("chat", "apiKey"); |
| | | openAiStreamClient = createOpenAiStreamClient(apiHost,apiKey); |
| | | String url = configService.getConfigValue("chat", "apiUrl"); |
| | | openAiStreamClient = createOpenAiStreamClient(apiHost,apiKey,url); |
| | | 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.setLevel(HttpLoggingInterceptor.Level.HEADERS); |
| | | OkHttpClient okHttpClient = new OkHttpClient.Builder() |
| | |
| | | .build(); |
| | | return OpenAiStreamClient.builder() |
| | | .apiHost(apiHost) |
| | | .apiUrl(url) |
| | | .apiKey(Collections.singletonList(apiKey)) |
| | | .keyStrategy(new KeyRandomStrategy()) |
| | | .okHttpClient(okHttpClient) |
| | |
| | | @Override |
| | | public SseEmitter chat(ChatRequest chatRequest,SseEmitter emitter) { |
| | | 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(); |
| | | if (enabled) { |
| | | String toolString = mcpChat(chatRequest.getPrompt()); |
¶Ô±ÈÐÂÎļþ |
| | |
| | | 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); |