From e5030727b7f840f80e4c5ecae400a85e41e1930d Mon Sep 17 00:00:00 2001
From: hejianjun <942156265@qq.com>
Date: Thu, 1 Feb 2024 10:11:15 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=8A=A8=E8=8F=9C?=
=?UTF-8?q?=E5=8D=95=EF=BC=8C=E8=A7=A3=E5=86=B3=E8=A7=A3=E9=87=8Asql?=
=?UTF-8?q?=E6=97=B6=E6=B2=A1=E6=9C=89=E6=95=B0=E6=8D=AE=E5=BA=93=E5=90=8D?=
=?UTF-8?q?=E7=9A=84bug=EF=BC=8C=E8=A7=A3=E5=86=B3=E8=A7=A3=E9=87=8Asql?=
=?UTF-8?q?=E6=97=B6openai=E8=B6=85=E6=97=B6=E7=9A=84=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../ConsoleEditor/components/ChatInput/index.tsx | 7 +++----
.../components/SelectBoundInfo/index.tsx | 2 +-
.../web/api/controller/ai/ChatController.java | 14 +++++++++++---
.../controller/ai/openai/client/OpenAIClient.java | 13 ++++++++++++-
.../openai/listener/OpenAIEventSourceListener.java | 8 ++++++--
5 files changed, 33 insertions(+), 11 deletions(-)
diff --git a/chat2db-client/src/components/ConsoleEditor/components/ChatInput/index.tsx b/chat2db-client/src/components/ConsoleEditor/components/ChatInput/index.tsx
index 592755979..997b8bd3d 100644
--- a/chat2db-client/src/components/ConsoleEditor/components/ChatInput/index.tsx
+++ b/chat2db-client/src/components/ConsoleEditor/components/ChatInput/index.tsx
@@ -42,18 +42,17 @@ const ChatInput = (props: IProps) => {
};
const renderSelectTable = () => {
- const { tables, onSelectTableSyncModel, selectedTables, onSelectTables } = props;
+ const { tables, onSelectTableSyncModel, selectedTables, onSelectTables,syncTableModel } = props;
const options = (tables || []).map((t) => ({ value: t, label: t }));
return (
onSelectTableSyncModel(v.target.value)}
- // value={syncTableModel}
- value={SyncModelType.MANUAL}
+ value={syncTableModel}
style={{ marginBottom: '8px' }}
>
- {/* 自动 */}
+ 自动
手动
diff --git a/chat2db-client/src/components/ConsoleEditor/components/SelectBoundInfo/index.tsx b/chat2db-client/src/components/ConsoleEditor/components/SelectBoundInfo/index.tsx
index e4318a2a9..4be3b4b20 100644
--- a/chat2db-client/src/components/ConsoleEditor/components/SelectBoundInfo/index.tsx
+++ b/chat2db-client/src/components/ConsoleEditor/components/SelectBoundInfo/index.tsx
@@ -186,7 +186,7 @@ const SelectBoundInfo = memo((props: IProps) => {
boundInfo.databaseName,
boundInfo.schemaName,
);
- setSelectedTables(tableNameListTemp.slice(0, 1));
+ //setSelectedTables(tableNameListTemp.slice(0, 1));
}
}, [allTableList, isActive]);
diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/ChatController.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/ChatController.java
index 2f90ec2e8..c2df8f6cb 100644
--- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/ChatController.java
+++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/ChatController.java
@@ -293,7 +293,7 @@ private SseEmitter chatWithOpenAi(ChatQueryRequest queryRequest, SseEmitter sseE
messages.add(currentMessage);
buildSseEmitter(sseEmitter, uid);
ConnectInfo connectInfo = Chat2DBContext.getConnectInfo();
- OpenAIEventSourceListener openAIEventSourceListener = new OpenAIEventSourceListener(sseEmitter, messages, connectInfo);
+ OpenAIEventSourceListener openAIEventSourceListener = new OpenAIEventSourceListener(sseEmitter, messages, connectInfo, queryRequest);
ToolsFunction function = ToolsFunction.builder()
.name("get_table_columns")
.description("获取指定表的字段名,类型")
@@ -799,8 +799,16 @@ public String queryDatabaseSchema(ChatQueryRequest queryRequest) {
*/
public String queryDatabaseSchema2(ChatQueryRequest queryRequest) {
MetaData metaSchema = Chat2DBContext.getMetaData();
- List
tables = metaSchema.tables(Chat2DBContext.getConnection(), queryRequest.getDatabaseName(), queryRequest.getSchemaName(), null);
- return tables.stream().map(Table::getName).collect(Collectors.joining(","));
+ try {
+ List tables = metaSchema.tables(Chat2DBContext.getConnection(), queryRequest.getDatabaseName(), queryRequest.getSchemaName(), null);
+ return tables.stream()
+ .map(table -> StringUtils.isBlank(table.getComment()) ? table.getName()
+ : table.getName() + "(" + table.getComment() + ")")
+ .collect(Collectors.joining(","));
+ } catch (Exception e) {
+ log.error("query table error:{}, do nothing", e.getMessage());
+ return "";
+ }
}
/**
diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/openai/client/OpenAIClient.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/openai/client/OpenAIClient.java
index 9ebf711c2..1d3de3bc7 100644
--- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/openai/client/OpenAIClient.java
+++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/openai/client/OpenAIClient.java
@@ -4,6 +4,7 @@
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.Objects;
+import java.util.concurrent.TimeUnit;
import ai.chat2db.server.domain.api.model.Config;
import ai.chat2db.server.domain.api.service.ConfigService;
@@ -93,7 +94,17 @@ public static void refresh() {
log.info("refresh openai apikey:{}", maskApiKey(apikey));
if (Objects.nonNull(host) && Objects.nonNull(port)) {
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
- OkHttpClient okHttpClient = new OkHttpClient.Builder().proxy(proxy).build();
+ OkHttpClient okHttpClient = new OkHttpClient.Builder()
+ // 设置连接超时为10秒
+ .connectTimeout(10, TimeUnit.SECONDS)
+ // 设置读取超时为30秒
+ .readTimeout(30, TimeUnit.SECONDS)
+ // 设置写入超时为15秒
+ .writeTimeout(15, TimeUnit.SECONDS)
+ // 设置整个调用的超时为1分钟
+ .callTimeout(1, TimeUnit.MINUTES)
+ .proxy(proxy)
+ .build();
OPEN_AI_STREAM_CLIENT = OpenAiStreamClient.builder().apiHost(apiHost).apiKey(
Lists.newArrayList(apikey)).okHttpClient(okHttpClient).build();
} else {
diff --git a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/openai/listener/OpenAIEventSourceListener.java b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/openai/listener/OpenAIEventSourceListener.java
index 9014b12b9..099fd76b5 100644
--- a/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/openai/listener/OpenAIEventSourceListener.java
+++ b/chat2db-server/chat2db-server-web/chat2db-server-web-api/src/main/java/ai/chat2db/server/web/api/controller/ai/openai/listener/OpenAIEventSourceListener.java
@@ -1,6 +1,7 @@
package ai.chat2db.server.web.api.controller.ai.openai.listener;
import ai.chat2db.server.web.api.controller.ai.openai.client.OpenAIClient;
+import ai.chat2db.server.web.api.controller.ai.request.ChatQueryRequest;
import ai.chat2db.server.web.api.controller.ai.response.ChatCompletionResponse;
import ai.chat2db.spi.MetaData;
import ai.chat2db.spi.sql.Chat2DBContext;
@@ -39,13 +40,16 @@ public class OpenAIEventSourceListener extends EventSourceListener {
private final ConnectInfo connectInfo;
+ private final ChatQueryRequest queryRequest;
+
private List toolCalls = new ArrayList<>();
- public OpenAIEventSourceListener(SseEmitter sseEmitter, List messages, ConnectInfo connectInfo) {
+ public OpenAIEventSourceListener(SseEmitter sseEmitter, List messages, ConnectInfo connectInfo, ChatQueryRequest queryRequest) {
this.sseEmitter = sseEmitter;
this.messages = messages;
this.connectInfo = connectInfo;
+ this.queryRequest = queryRequest;
}
public static List mergeToolCallsLists(List list1, List list2) {
@@ -142,7 +146,7 @@ public void onEvent(EventSource eventSource, String id, String type, String data
JSONObject arguments = JSONObject.parse(function.getArguments());
if ("get_table_columns".equals(functionName)) {
MetaData metaSchema = Chat2DBContext.getMetaData();
- String ddl = metaSchema.tableDDL(Chat2DBContext.getConnection(), connectInfo.getDatabaseName(), connectInfo.getSchemaName(), arguments.getString("table_name"));
+ String ddl = metaSchema.tableDDL(Chat2DBContext.getConnection(), queryRequest.getDatabaseName(), queryRequest.getSchemaName(), arguments.getString("table_name"));
messages.add(Message.builder().role(BaseMessage.Role.TOOL)
.toolCallId(callId)
.name(functionName)