Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ private static void run(Memory memory, ExampleDataLoader loader) {
ExamplePrinter.printExtractionResult(result);
}

ExamplePrinter.printSection("Step 4: Retrieve Synthesized Insights");
ExamplePrinter.printSection("Step 4: Flush Synthesized Insights");
log.info(" flushing insight tree...");
long flushStartedAt = System.currentTimeMillis();
memory.flushInsights(memoryId, config.language());
log.info(" flush completed in {}ms", System.currentTimeMillis() - flushStartedAt);

ExamplePrinter.printSection("Step 5: Retrieve Synthesized Insights");
var query = "这个用户在身份、关系、偏好和行为上有哪些稳定特征?";
log.info(" query: {}", query);
long startedAt = System.currentTimeMillis();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.openmemind.ai.memory.example.java.insight;

import static org.assertj.core.api.Assertions.assertThat;

import com.openmemind.ai.memory.core.Memory;
import com.openmemind.ai.memory.core.extraction.ExtractionResult;
import com.openmemind.ai.memory.core.retrieval.RetrievalResult;
import com.openmemind.ai.memory.example.java.support.ExampleDataLoader;
import java.lang.reflect.Proxy;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;

class InsightTreeExampleTest {

@Test
void flushesInsightTreeBeforeRetrieval() throws Exception {
List<String> events = new ArrayList<>();
Memory memory = recordingMemory(events);

var run =
InsightTreeExample.class.getDeclaredMethod(
"run", Memory.class, ExampleDataLoader.class);
run.setAccessible(true);
run.invoke(null, memory, new ExampleDataLoader());

assertThat(events)
.containsExactly(
"addMessages",
"addMessages",
"addMessages",
"flushInsights:Chinese",
"retrieve");
}

private Memory recordingMemory(List<String> events) {
return (Memory)
Proxy.newProxyInstance(
Memory.class.getClassLoader(),
new Class<?>[] {Memory.class},
(proxy, method, args) -> {
return switch (method.getName()) {
case "addMessages" -> {
events.add("addMessages");
yield Mono.just(
ExtractionResult.success(
null, null, null, null, Duration.ZERO));
}
case "flushInsights" -> {
events.add("flushInsights:" + args[1]);
yield null;
}
case "retrieve" -> {
events.add("retrieve");
yield Mono.just(RetrievalResult.empty("deep_retrieval", ""));
}
case "close" -> null;
case "toString" -> "recordingMemory";
case "hashCode" -> System.identityHashCode(proxy);
case "equals" -> proxy == args[0];
default ->
throw new UnsupportedOperationException(
"Unexpected method: " + method);
};
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.openmemind.ai.memory.server.controller.admin.buffer;

import com.openmemind.ai.memory.server.domain.buffer.query.ConversationBufferPageQuery;
import com.openmemind.ai.memory.server.domain.buffer.query.InsightBufferPageQuery;
import com.openmemind.ai.memory.server.domain.buffer.request.AdminIdsRequest;
import com.openmemind.ai.memory.server.domain.buffer.request.AdminLongIdsRequest;
import com.openmemind.ai.memory.server.domain.buffer.request.InsightBufferBuiltUpdateRequest;
import com.openmemind.ai.memory.server.domain.buffer.request.InsightBufferGroupUpdateRequest;
import com.openmemind.ai.memory.server.domain.buffer.view.ConversationBufferView;
import com.openmemind.ai.memory.server.domain.buffer.view.InsightBufferGroupView;
import com.openmemind.ai.memory.server.domain.buffer.view.InsightBufferView;
import com.openmemind.ai.memory.server.domain.common.AdminUpdateResult;
import com.openmemind.ai.memory.server.domain.common.ApiResult;
import com.openmemind.ai.memory.server.domain.common.BatchDeleteResult;
import com.openmemind.ai.memory.server.domain.common.PageResult;
import com.openmemind.ai.memory.server.service.buffer.BufferManagementService;
import com.openmemind.ai.memory.server.service.buffer.BufferQueryService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import java.util.List;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Validated
@RestController
@RequestMapping("/admin/v1/buffers")
public class AdminBufferController {

private final BufferQueryService queryService;
private final BufferManagementService managementService;

public AdminBufferController(
BufferQueryService queryService, BufferManagementService managementService) {
this.queryService = queryService;
this.managementService = managementService;
}

@GetMapping("/conversations")
public ApiResult<PageResult<ConversationBufferView>> conversations(
@RequestParam(defaultValue = "1") @Min(1) int pageNo,
@RequestParam(defaultValue = "20") @Min(1) @Max(100) int pageSize,
@RequestParam(required = false) String memoryId,
@RequestParam(required = false) String sessionId,
@RequestParam(defaultValue = "pending") String state) {
return ApiResult.success(
PageResult.from(
queryService.listConversations(
ConversationBufferPageQuery.of(
pageNo, pageSize, memoryId, sessionId, state))));
}

@GetMapping("/conversations/{id}")
public ApiResult<ConversationBufferView> conversationDetail(@PathVariable Long id) {
return ApiResult.success(queryService.getConversation(id));
}

@PatchMapping("/conversations/extracted")
public ApiResult<AdminUpdateResult> markConversationsExtracted(
@Valid @RequestBody AdminLongIdsRequest request) {
return ApiResult.success(managementService.markConversationsExtracted(request.ids()));
}

@DeleteMapping("/conversations")
public ApiResult<BatchDeleteResult> deleteConversations(
@Valid @RequestBody AdminLongIdsRequest request) {
return ApiResult.success(managementService.deleteConversations(request.ids()));
}

@GetMapping("/insights")
public ApiResult<PageResult<InsightBufferView>> insights(
@RequestParam(defaultValue = "1") @Min(1) int pageNo,
@RequestParam(defaultValue = "20") @Min(1) @Max(100) int pageSize,
@RequestParam(required = false) String memoryId,
@RequestParam(required = false) String insightTypeName,
@RequestParam(defaultValue = "unbuilt") String state) {
return ApiResult.success(
PageResult.from(
queryService.listInsights(
InsightBufferPageQuery.of(
pageNo, pageSize, memoryId, insightTypeName, state))));
}

@GetMapping("/insights/groups")
public ApiResult<List<InsightBufferGroupView>> insightGroups(
@RequestParam(required = false) String memoryId,
@RequestParam(required = false) String insightTypeName) {
return ApiResult.success(queryService.listInsightGroups(memoryId, insightTypeName));
}

@PatchMapping("/insights/group")
public ApiResult<AdminUpdateResult> updateInsightGroup(
@Valid @RequestBody InsightBufferGroupUpdateRequest request) {
return ApiResult.success(
managementService.updateInsightGroup(request.ids(), request.groupName()));
}

@PatchMapping("/insights/built")
public ApiResult<AdminUpdateResult> updateInsightBuilt(
@Valid @RequestBody InsightBufferBuiltUpdateRequest request) {
return ApiResult.success(
managementService.updateInsightBuilt(request.ids(), request.built()));
}

@DeleteMapping("/insights")
public ApiResult<BatchDeleteResult> deleteInsights(
@Valid @RequestBody AdminIdsRequest request) {
return ApiResult.success(managementService.deleteInsightBuffers(request.ids()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.openmemind.ai.memory.server.controller.admin.dashboard;

import com.openmemind.ai.memory.server.domain.common.ApiResult;
import com.openmemind.ai.memory.server.domain.dashboard.view.AdminDashboardView;
import com.openmemind.ai.memory.server.service.dashboard.DashboardQueryService;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Validated
@RestController
@RequestMapping("/admin/v1/dashboard")
public class AdminDashboardController {

private final DashboardQueryService queryService;

public AdminDashboardController(DashboardQueryService queryService) {
this.queryService = queryService;
}

@GetMapping
public ApiResult<AdminDashboardView> get(
@RequestParam(required = false) String memoryId,
@RequestParam(defaultValue = "7") @Min(1) @Max(30) int days) {
if (days < 1 || days > 30) {
throw new IllegalArgumentException("days must be between 1 and 30");
}
return ApiResult.success(queryService.getDashboard(memoryId, days));
}
}
Loading
Loading