Skip to content

Commit 7260995

Browse files
authored
Merge pull request #31 from LangMem/improve/some_improve
refactor: simplify array checks for embeddings and content, enhancing…
2 parents 3fdc5c5 + 4b63a6e commit 7260995

File tree

4 files changed

+6
-6
lines changed

4 files changed

+6
-6
lines changed

mem4j-core/src/main/java/io/github/mem4j/embeddings/DashScopeEmbeddingService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public Double[] embed(String text) {
8686
JsonNode responseJson = objectMapper.readTree(response.getBody());
8787
JsonNode embeddings = responseJson.path("output").path("embeddings");
8888

89-
if (embeddings.isArray() && embeddings.size() > 0) {
89+
if (embeddings.isArray() && !embeddings.isEmpty()) {
9090
JsonNode firstEmbedding = embeddings.get(0);
9191
JsonNode embeddingArray = firstEmbedding.path("embedding");
9292

mem4j-core/src/main/java/io/github/mem4j/memory/Memory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ private List<String> extractMemories(List<Message> messages, boolean infer) {
226226

227227
String prompt = String.format("""
228228
Extract key memories from this conversation. Focus on:
229+
- Keep the original language and tone
230+
- Be concise and specific
229231
- Important facts about the user
230232
- User preferences and behaviors
231233
- Significant events or experiences

mem4j-core/src/main/java/io/github/mem4j/util/AnthropicValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ private String makeRequest(Map<String, Object> requestBody) throws Exception {
319319
JsonNode responseJson = objectMapper.readTree(response.getBody());
320320
JsonNode content = responseJson.path("content");
321321

322-
if (content.isArray() && content.size() > 0) {
322+
if (content.isArray() && !content.isEmpty()) {
323323
JsonNode firstContent = content.get(0);
324324
return firstContent.path("text").asText();
325325
}

mem4j-core/src/main/java/io/github/mem4j/vectorstores/MilvusVectorStoreService.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,7 @@ public List<MemoryItem> search(Double[] queryEmbedding, Map<String, Object> filt
275275
String searchExpr = buildSearchExpression(filters);
276276

277277
// 转换查询向量为Float类型
278-
List<Float> queryVector = Arrays.stream(queryEmbedding)
279-
.map(Double::floatValue)
280-
.collect(Collectors.toList());
278+
List<Float> queryVector = Arrays.stream(queryEmbedding).map(Double::floatValue).toList();
281279

282280
// 构建搜索参数
283281
SearchParam searchParam = SearchParam.newBuilder()
@@ -306,7 +304,7 @@ public List<MemoryItem> search(Double[] queryEmbedding, Map<String, Object> filt
306304
// 获取搜索结果的字段数据
307305
List<FieldData> fieldsData = searchResults.getResults().getFieldsDataList();
308306

309-
if (fieldsData != null && !fieldsData.isEmpty()) {
307+
if (!fieldsData.isEmpty()) {
310308
// 创建字段映射
311309
Map<String, List<Object>> fieldMap = new HashMap<>();
312310

0 commit comments

Comments
 (0)