Skip to content

Commit 0ca2a2b

Browse files
v1.0.1
1.优化:EmbeddingModelClient的异步请求(并发默认支持2个okhttp * maxRequests 64默认) 2.修复:mysql的@@sql_mode查询开启ONLY_FULL_GROUP_BY后报异常,GROUP BY clause and contains nonaggregated column 3.优化:给实体类都实现下序列化接口 4.功能:LlmJsonSchemaApiService可以在java中独立使用jsonschaema(AiBuilders#jsonSchemaApiService) 5.功能:增加OpenAiChatClient可以在java中独立使用聊天,开发者可以填写账号密码搭配StreamingResponseHandlerAdapter直接使用。(AiBuilders#openAiChatClient)
1 parent 571c722 commit 0ca2a2b

File tree

16 files changed

+259
-105
lines changed

16 files changed

+259
-105
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.github.wangzihaogithub</groupId>
77
<artifactId>ai-assistant</artifactId>
8-
<version>1.0.1-SNAPSHOT</version>
8+
<version>1.0.1</version>
99
<packaging>jar</packaging>
1010

1111
<name>ai-assistant</name>

src/main/java/com/github/aiassistant/AiApplication.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class AiApplication {
5252
private final AiMemorySearchDocMapper aiMemorySearchDocMapper;
5353
private final AiChatAbortMapper aiChatAbortMapper;
5454
private final AiAssistantFewshotMapper aiAssistantFewshotMapper;
55-
private final AiAssistantJsonschemaMapper aiAssistantJsonschemaMapper;
55+
private final AiJsonschemaMapper aiJsonschemaMapper;
5656
private final AiAssistantKnMapper aiAssistantKnMapper;
5757
private final AiAssistantMapper aiAssistantMapper;
5858
private final AiAssistantMstateMapper aiAssistantMstateMapper;
@@ -127,7 +127,7 @@ public AiApplication(Executor functionCallThreadPool,
127127
this.aiMemoryMessageToolMapper = daoProvider.getMapper(AiMemoryMessageToolMapper.class);
128128
this.aiChatAbortMapper = daoProvider.getMapper(AiChatAbortMapper.class);
129129
this.aiAssistantFewshotMapper = daoProvider.getMapper(AiAssistantFewshotMapper.class);
130-
this.aiAssistantJsonschemaMapper = daoProvider.getMapper(AiAssistantJsonschemaMapper.class);
130+
this.aiJsonschemaMapper = daoProvider.getMapper(AiJsonschemaMapper.class);
131131
this.aiAssistantKnMapper = daoProvider.getMapper(AiAssistantKnMapper.class);
132132
this.aiAssistantMapper = daoProvider.getMapper(AiAssistantMapper.class);
133133
this.aiAssistantMstateMapper = daoProvider.getMapper(AiAssistantMstateMapper.class);
@@ -145,7 +145,7 @@ public AiApplication(Executor functionCallThreadPool,
145145
this.aiMemorySearchMapper = daoProvider.getMapper(AiMemorySearchMapper.class);
146146

147147
this.aiToolService = new AiToolServiceImpl(aiToolMapper, aiToolParameterMapper, toolsMap);
148-
this.llmJsonSchemaApiService = new LlmJsonSchemaApiService(aiToolService);
148+
this.llmJsonSchemaApiService = new LlmJsonSchemaApiService(aiJsonschemaMapper, aiToolService);
149149
this.actingService = new ActingService(llmJsonSchemaApiService);
150150
this.reasoningService = new ReasoningService(llmJsonSchemaApiService);
151151
this.knnApiService = new KnnApiService(embeddingStore, aiEmbeddingMapper);
@@ -167,7 +167,7 @@ public AiApplication(Executor functionCallThreadPool,
167167
this.aiMemorySearchService = new AiMemorySearchServiceImpl(aiMemorySearchMapper, aiMemorySearchDocMapper);
168168

169169
this.aiVariablesService = new AiVariablesService(aiMemoryMstateService, aiVariablesMapper, getServiceInterceptSupplier(AiVariablesServiceIntercept.class, interceptMap));
170-
this.llmTextApiService = new LlmTextApiService(llmJsonSchemaApiService, aiQuestionClassifyService, aiAssistantJsonschemaMapper, aiAssistantFewshotMapper, aiToolService, aiVariablesService, knnApiService, actingService, reasoningService, knSettingWebsearchBlacklistServiceImpl,
170+
this.llmTextApiService = new LlmTextApiService(llmJsonSchemaApiService, aiQuestionClassifyService, aiJsonschemaMapper, aiAssistantFewshotMapper, aiToolService, aiVariablesService, knnApiService, actingService, reasoningService, knSettingWebsearchBlacklistServiceImpl,
171171
functionCallThreadPool, getServiceInterceptSupplier(LlmTextApiServiceIntercept.class, interceptMap));
172172
}
173173

@@ -261,8 +261,8 @@ public AiAssistantFewshotMapper getAiAssistantFewshotMapper() {
261261
return aiAssistantFewshotMapper;
262262
}
263263

264-
public AiAssistantJsonschemaMapper getAiAssistantJsonschemaMapper() {
265-
return aiAssistantJsonschemaMapper;
264+
public AiJsonschemaMapper getAiAssistantJsonschemaMapper() {
265+
return aiJsonschemaMapper;
266266
}
267267

268268
public AiAssistantKnMapper getAiAssistantKnMapper() {

src/main/java/com/github/aiassistant/AiBuilders.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.aiassistant;
22

3+
import com.github.aiassistant.dao.AiJsonschemaMapper;
34
import com.github.aiassistant.dao.AiToolMapper;
45
import com.github.aiassistant.dao.AiToolParameterMapper;
56
import com.github.aiassistant.platform.AliyunAppsClient;
@@ -95,19 +96,22 @@ public static AliyunOpenNluModel aliyunNlu(String apiKey) {
9596
return new AliyunOpenNluModel(apiKey);
9697
}
9798

98-
public static AiToolServiceImpl aiToolService(DataSource dataSource, Function<String, Tools> toolsMap) {
99-
Mybatis3DAOProvider provider = new Mybatis3DAOProvider(dataSource);
100-
AiToolMapper aiToolMapper = provider.getMapper(AiToolMapper.class);
101-
AiToolParameterMapper aiToolParameterMapper = provider.getMapper(AiToolParameterMapper.class);
99+
public static AiToolServiceImpl aiToolService(Mybatis3DAOProvider daoProvider, Function<String, Tools> toolsMap) {
100+
AiToolMapper aiToolMapper = daoProvider.getMapper(AiToolMapper.class);
101+
AiToolParameterMapper aiToolParameterMapper = daoProvider.getMapper(AiToolParameterMapper.class);
102102
return new AiToolServiceImpl(aiToolMapper, aiToolParameterMapper, toolsMap);
103103
}
104104

105-
public static LlmJsonSchemaApiService llmJsonSchemaApiService(DataSource dataSource, Function<String, Tools> toolsMap) {
106-
return new LlmJsonSchemaApiService(aiToolService(dataSource, toolsMap));
105+
public static Mybatis3DAOProvider daoProvider(DataSource dataSource) {
106+
return new Mybatis3DAOProvider(dataSource);
107107
}
108108

109-
public static LlmJsonSchemaApiService llmJsonSchemaApiService(AiToolServiceImpl aiToolService) {
110-
return new LlmJsonSchemaApiService(aiToolService);
109+
public static LlmJsonSchemaApiService jsonSchemaApiService(DataSource dataSource, Function<String, Tools> toolsMap) {
110+
return jsonSchemaApiService(daoProvider(dataSource), toolsMap);
111+
}
112+
113+
public static LlmJsonSchemaApiService jsonSchemaApiService(Mybatis3DAOProvider daoProvider, Function<String, Tools> toolsMap) {
114+
return new LlmJsonSchemaApiService(daoProvider.getMapper(AiJsonschemaMapper.class), aiToolService(daoProvider, toolsMap));
111115
}
112116

113117
public static KnnApiService knnApiService(String elasticsearchUrl, String apiKey) {
@@ -124,10 +128,16 @@ public static RestClientBuilder elasticsearchClient(String url, String apiKey) {
124128
return builder;
125129
}
126130

131+
public static AiApplication aiApplication(DAOProvider daoProvider,
132+
RestClient embeddingStore,
133+
Function<String, Tools> toolsMap) {
134+
return new AiApplication(null, daoProvider, embeddingStore, toolsMap, null);
135+
}
136+
127137
public static AiApplication aiApplication(DataSource dataSource,
128138
RestClient embeddingStore,
129139
Function<String, Tools> toolsMap) {
130-
return new AiApplication(dataSource, embeddingStore, toolsMap, null);
140+
return new AiApplication(null, daoProvider(dataSource), embeddingStore, toolsMap, null);
131141
}
132142

133143
public static WebSearchService webSearchService() {

src/main/java/com/github/aiassistant/dao/AiAssistantJsonschemaMapper.java renamed to src/main/java/com/github/aiassistant/dao/AiJsonschemaMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.util.Collections;
88
import java.util.List;
99

10-
public interface AiAssistantJsonschemaMapper {
10+
public interface AiJsonschemaMapper {
1111
List<AiJsonschema> selectBatchIds(Collection<? extends Serializable> idList);
1212

1313
default AiJsonschema selectById(Serializable id) {

src/main/java/com/github/aiassistant/entity/model/chat/MemoryIdVO.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,25 @@
1010
import java.util.Map;
1111

1212
public class MemoryIdVO implements Cloneable {
13+
public static final MemoryIdVO NULL = new MemoryIdVO() {
14+
@Override
15+
public void setAiAssistant(AiAssistant aiAssistant) {
16+
}
17+
18+
@Override
19+
public void setAssistantKnMap(Map<String, List<AiAssistantKn>> assistantKnMap) {
20+
}
21+
22+
@Override
23+
public void setAiChat(AiChat aiChat) {
24+
25+
}
26+
27+
@Override
28+
public String toString() {
29+
return "NULLMemoryIdVO";
30+
}
31+
};
1332
/**
1433
* 聊天
1534
*/
@@ -42,6 +61,9 @@ public void setAiChat(AiChat aiChat) {
4261
}
4362

4463
public String getAiAssistantId() {
64+
if (aiChat == null) {
65+
return null;
66+
}
4567
return aiChat.getAssistantId();
4668
}
4769

@@ -67,22 +89,33 @@ public AiAssistantKn getAssistantKn(AiAssistantKnTypeEnum name) {
6789
}
6890

6991
public List<AiAssistantKn> getAssistantKnList(AiAssistantKnTypeEnum name) {
92+
if (assistantKnMap == null) {
93+
return null;
94+
}
7095
return assistantKnMap.getOrDefault(name.getCode(), Collections.emptyList());
7196
}
7297

7398
public <T> T indexAt(T[] arrays) {
99+
if (aiChat == null) {
100+
return arrays[0];
101+
}
74102
return arrays[getChatId() % arrays.length];
75103
}
76104

77105
public Integer getMemoryId() {
106+
if (aiChat == null) {
107+
return null;
108+
}
78109
return aiChat.getAiMemoryId();
79110
}
80111

81112
public Integer getChatId() {
113+
if (aiChat == null) {
114+
return null;
115+
}
82116
return aiChat.getId();
83117
}
84118

85-
86119
@Override
87120
public String toString() {
88121
return aiChat == null ? super.toString() : aiChat.getId() + "#" + aiChat.getName();

src/main/java/com/github/aiassistant/platform/Mybatis3DAOProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
public class Mybatis3DAOProvider extends DAOProvider {
2525
public static final List<String> MAPPERS = Collections.unmodifiableList(Arrays.asList(
2626
"mybatis3/AiAssistantFewshotMapper.xml",
27-
"mybatis3/AiAssistantJsonschemaMapper.xml",
27+
"mybatis3/AiJsonschemaMapper.xml",
2828
"mybatis3/AiAssistantKnMapper.xml",
2929
"mybatis3/AiAssistantMstateMapper.xml",
3030
"mybatis3/AiChatAbortMapper.xml",

src/main/java/com/github/aiassistant/service/jsonschema/LlmJsonSchemaApiService.java

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.github.aiassistant.service.jsonschema;
22

3-
import com.github.aiassistant.dao.AiAssistantJsonschemaMapper;
3+
import com.github.aiassistant.dao.AiJsonschemaMapper;
44
import com.github.aiassistant.entity.AiJsonschema;
55
import com.github.aiassistant.entity.model.chat.AiModelVO;
66
import com.github.aiassistant.entity.model.chat.AiVariablesVO;
@@ -20,6 +20,7 @@
2020
import dev.langchain4j.service.AiServices;
2121
import dev.langchain4j.service.FunctionalInterfaceAiServices;
2222

23+
import java.io.Serializable;
2324
import java.time.Duration;
2425
import java.util.*;
2526
import java.util.concurrent.ConcurrentHashMap;
@@ -44,16 +45,19 @@ public class LlmJsonSchemaApiService {
4445
private final Map<Class, AtomicInteger> schemasIndexMap = Collections.synchronizedMap(new WeakHashMap<>());
4546
// @Autowired
4647
private final AiToolServiceImpl aiToolService;
48+
private final AiJsonschemaMapper aiJsonschemaMapper;
49+
4750
/**
4851
* 大模型建立socket链接超时时间
4952
*/
5053
private Duration connectTimeout = Duration.ofSeconds(3);
5154

52-
public LlmJsonSchemaApiService(AiToolServiceImpl aiToolService) {
53-
this(aiToolService, 5);
55+
public LlmJsonSchemaApiService(AiJsonschemaMapper aiJsonschemaMapper, AiToolServiceImpl aiToolService) {
56+
this(aiJsonschemaMapper, aiToolService, 5);
5457
}
5558

56-
public LlmJsonSchemaApiService(AiToolServiceImpl aiToolService, int schemaInstanceCount) {
59+
public LlmJsonSchemaApiService(AiJsonschemaMapper aiJsonschemaMapper, AiToolServiceImpl aiToolService, int schemaInstanceCount) {
60+
this.aiJsonschemaMapper = aiJsonschemaMapper;
5761
this.aiToolService = aiToolService;
5862
this.schemaInstanceCount = schemaInstanceCount;
5963
}
@@ -110,7 +114,7 @@ public void putSessionVariables(MemoryIdVO memoryIdVO,
110114

111115
public void addSessionJsonSchema(MemoryIdVO memoryIdVO,
112116
String aiJsonschemaIds,
113-
AiAssistantJsonschemaMapper aiAssistantJsonschemaMapper,
117+
AiJsonschemaMapper aiJsonschemaMapper,
114118
Executor threadPoolTaskExecutor) {
115119
Session session = getSession(memoryIdVO, true);
116120
session.threadPoolTaskExecutor = threadPoolTaskExecutor;
@@ -119,7 +123,7 @@ public void addSessionJsonSchema(MemoryIdVO memoryIdVO,
119123
.map(e -> Arrays.asList(e.split(",")))
120124
.map(e -> e.stream().filter(o -> !session.jsonschemaMap.containsKey(o)).collect(Collectors.toList()))
121125
.filter(e -> !e.isEmpty())
122-
.map(aiAssistantJsonschemaMapper::selectBatchIds)
126+
.map(aiJsonschemaMapper::selectBatchIds)
123127
.orElseGet(Collections::emptyList);
124128
for (AiJsonschema jsonschema : jsonschemaList) {
125129
session.jsonschemaMap.put(Objects.toString(jsonschema.getId(), ""), jsonschema);
@@ -186,6 +190,20 @@ public <T> T getSchemaNoMemory(AiJsonschema jsonschema, Class<T> type) throws Js
186190
return getSchema(null, jsonschema, type, false);
187191
}
188192

193+
/**
194+
* 获取JsonSchema类型的模型(不要记忆)
195+
*
196+
* @param aiJsonschemaId jsonschema配置ID
197+
* @param type type
198+
* @param <T> 类型
199+
* @return JsonSchema类型的模型
200+
* @throws JsonSchemaCreateException JsonSchema创建失败
201+
*/
202+
public <T> T getSchemaByIdNoMemory(Integer aiJsonschemaId, Class<T> type) throws JsonSchemaCreateException {
203+
AiJsonschema jsonschema = selectJsonschemaById(aiJsonschemaId);
204+
return getSchema(null, jsonschema, type, false);
205+
}
206+
189207
/**
190208
* 获取JsonSchema类型的模型
191209
*
@@ -267,6 +285,9 @@ public <T> T getSchema(Object memoryIdVO, AiJsonschema jsonschema, Class<T> type
267285

268286
private <T> T newInstance(AiModelVO aiModel, Class<T> type, boolean useChatMemoryProvider,
269287
Object memoryIdVO, AiJsonschema jsonschema) throws JsonSchemaCreateException {
288+
if (memoryIdVO == null) {
289+
memoryIdVO = MemoryIdVO.NULL;
290+
}
270291
try {
271292
Session session = getSession(memoryIdVO, false);
272293
ChatStreamingResponseHandler responseHandler;
@@ -299,11 +320,11 @@ private <T> T newInstance(AiModelVO aiModel, Class<T> type, boolean useChatMemor
299320
String systemPromptText = jsonschema.getSystemPromptText();
300321
String userPromptText = jsonschema.getUserPromptText();
301322
List<Tools.ToolMethod> toolMethodList = AiUtil.initTool(aiToolService.selectToolMethodList(StringUtils.split(jsonschema.getAiToolIds(), ",")), variables, user);
302-
AiServices<T> aiServices = new FunctionalInterfaceAiServices<>(new AiServiceContext(type), systemPromptText, userPromptText,
323+
AiServices<T> aiServices = new FunctionalInterfaceAiServices<>(new AiServiceContext(type), jsonschema.getId(), jsonschema.getJsonSchemaEnum(), systemPromptText, userPromptText,
303324
variablesMap, responseHandler, toolMethodList, aiModel.isSupportChineseToolName(),
304325
classifyListVO, websearch, reasoning, aiModel.modelName, memoryIdVO, executor);
305326
aiServices.streamingChatLanguageModel(FunctionalInterfaceAiServices.adapter(aiModel.streaming));
306-
if (useChatMemoryProvider) {
327+
if (useChatMemoryProvider && memoryIdVO != MemoryIdVO.NULL) {
307328
aiServices.chatMemoryProvider(this::getChatMemory);
308329
}
309330
return aiServices.build();
@@ -432,6 +453,14 @@ public void setConnectTimeout(Duration connectTimeout) {
432453
this.connectTimeout = connectTimeout;
433454
}
434455

456+
public AiJsonschema selectJsonschemaById(Serializable id) {
457+
return aiJsonschemaMapper.selectById(id);
458+
}
459+
460+
public List<AiJsonschema> selectJsonschemaByIds(Collection<? extends Serializable> idList) {
461+
return aiJsonschemaMapper.selectBatchIds(idList);
462+
}
463+
435464
public static class Session {
436465
final Map<String, AiJsonschema> jsonschemaMap = new ConcurrentHashMap<>();
437466
Executor threadPoolTaskExecutor;

src/main/java/com/github/aiassistant/service/text/LlmTextApiService.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.github.aiassistant.service.text;
22

33
import com.github.aiassistant.dao.AiAssistantFewshotMapper;
4-
import com.github.aiassistant.dao.AiAssistantJsonschemaMapper;
4+
import com.github.aiassistant.dao.AiJsonschemaMapper;
55
import com.github.aiassistant.entity.AiAssistantKn;
66
import com.github.aiassistant.entity.model.chat.*;
77
import com.github.aiassistant.entity.model.langchain4j.KnowledgeAiMessage;
@@ -77,7 +77,7 @@ public class LlmTextApiService {
7777
*/
7878
private final AiVariablesService aiVariablesService;
7979
private final AiToolServiceImpl aiToolService;
80-
private final AiAssistantJsonschemaMapper aiAssistantJsonschemaMapper;
80+
private final AiJsonschemaMapper aiJsonschemaMapper;
8181
private final AiAssistantFewshotMapper aiAssistantFewshotMapper;
8282
/**
8383
* 向量模型服务
@@ -126,7 +126,7 @@ public class LlmTextApiService {
126126

127127
public LlmTextApiService(LlmJsonSchemaApiService llmJsonSchemaApiService,
128128
AiQuestionClassifyService aiQuestionClassifyService,
129-
AiAssistantJsonschemaMapper aiAssistantJsonschemaMapper,
129+
AiJsonschemaMapper aiJsonschemaMapper,
130130
AiAssistantFewshotMapper aiAssistantFewshotMapper,
131131
AiToolServiceImpl aiToolService,
132132
AiVariablesService aiVariablesService,
@@ -148,7 +148,7 @@ public LlmTextApiService(LlmJsonSchemaApiService llmJsonSchemaApiService,
148148
}
149149
this.aiAssistantFewshotMapper = aiAssistantFewshotMapper;
150150
this.aiToolService = aiToolService;
151-
this.aiAssistantJsonschemaMapper = aiAssistantJsonschemaMapper;
151+
this.aiJsonschemaMapper = aiJsonschemaMapper;
152152
this.llmJsonSchemaApiService = llmJsonSchemaApiService;
153153
this.aiQuestionClassifyService = aiQuestionClassifyService;
154154
this.aiVariablesService = aiVariablesService;
@@ -273,7 +273,7 @@ public CompletableFuture<FunctionCallStreamingResponseHandler> question(AiAccess
273273
ChatStreamingResponseHandler responseHandler) {
274274
try {
275275
// jsonschema模型
276-
llmJsonSchemaApiService.addSessionJsonSchema(memoryId, memoryId.getAiAssistant().getAiJsonschemaIds(), aiAssistantJsonschemaMapper, functionCallingThreadPool);
276+
llmJsonSchemaApiService.addSessionJsonSchema(memoryId, memoryId.getAiAssistant().getAiJsonschemaIds(), aiJsonschemaMapper, functionCallingThreadPool);
277277
// 持久化
278278
ChatStreamingResponseHandler mergeResponseHandler = new MergeChatStreamingResponseHandler(
279279
Arrays.asList(responseHandler, new RepositoryChatStreamingResponseHandler(repository)),
@@ -624,7 +624,7 @@ private FunctionCallStreamingResponseHandler newFunctionCallStreamingResponseHan
624624
Boolean reasoning
625625
) throws AssistantConfigException, FewshotConfigException, ToolCreateException, JsonSchemaCreateException {
626626
// jsonschema模型
627-
llmJsonSchemaApiService.addSessionJsonSchema(memoryId, assistantConfig.getAiJsonschemaIds(), aiAssistantJsonschemaMapper, functionCallingThreadPool);
627+
llmJsonSchemaApiService.addSessionJsonSchema(memoryId, assistantConfig.getAiJsonschemaIds(), aiJsonschemaMapper, functionCallingThreadPool);
628628

629629
// 系统消息
630630
SystemMessage systemMessage = buildSystemMessage(assistantConfig.getSystemPromptText(), responseHandler, variables, assistantConfig);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package dev.langchain4j.model.openai;
2+
3+
import okhttp3.Call;
4+
5+
/**
6+
* Http返回监听,在吐字前(你可以在这里记日志)
7+
*/
8+
public interface HttpResponseHandler {
9+
10+
void onHttpResponse(Call call, okhttp3.Response response);
11+
12+
}

0 commit comments

Comments
 (0)