### Build Project Source: https://github.com/danvega/compacting-chat-memory-advisor/blob/master/README.md Command to clean and install the project using Maven. ```bash ./mvnw clean install ``` -------------------------------- ### Example: Chat History Before Compaction Source: https://github.com/danvega/compacting-chat-memory-advisor/blob/master/README.md Shows a sample chat history with 80 messages before compaction is applied. ```text 1. User: What's the capital of France? 2. Assistant: The capital of France is Paris. 3. User: Tell me about its history. 4. Assistant: Paris has a rich history dating back to... ... 79. User: What about modern Paris? 80. Assistant: Modern Paris is a global center for... ``` -------------------------------- ### API Response: Compaction Summary Source: https://github.com/danvega/compacting-chat-memory-advisor/blob/master/README.md Example response after triggering manual compaction, detailing messages compacted and token savings. ```text Compacted 40 messages into summary. Messages: 80 -> 41, Tokens: 8234 -> 4521 (saved 3713 tokens) ``` -------------------------------- ### Example: Chat History After Compaction Source: https://github.com/danvega/compacting-chat-memory-advisor/blob/master/README.md Displays the chat history after compaction, showing a summarized initial part and recent messages. ```text 1. Assistant: [Summary of previous conversation]: Discussion covered French capital Paris, its history from Roman times, major landmarks, cultural significance... 41. User: What about modern Paris? 42. Assistant: Modern Paris is a global center for... ``` -------------------------------- ### /compact/clear (GET) Source: https://github.com/danvega/compacting-chat-memory-advisor/blob/master/README.md Clears the entire conversation history, effectively starting a new chat session. ```APIDOC ## GET /compact/clear ### Description Clear the conversation history and start fresh. This resets the chat memory to an empty state. ### Method GET ### Endpoint `/compact/clear` ### Request Example ```bash curl http://localhost:8080/compact/clear ``` ``` -------------------------------- ### Run Spring Boot Application Source: https://github.com/danvega/compacting-chat-memory-advisor/blob/master/CLAUDE.md Runs the Spring Boot application using the Maven wrapper. This command starts the embedded server and makes the application accessible. ```bash ./mvnw spring-boot:run ``` -------------------------------- ### /compact/memory (GET) Source: https://github.com/danvega/compacting-chat-memory-advisor/blob/master/README.md Enables chat with the compacting memory feature. Sends a user message and receives a response, with older messages being compacted to save tokens. ```APIDOC ## GET /compact/memory ### Description Chat with compacting memory enabled. This endpoint processes user messages, summarizing older parts of the conversation to stay within token limits while retaining essential context. ### Method GET ### Endpoint `/compact/memory` ### Parameters #### Query Parameters - **message** (string) - Required - Your message to the AI. ### Request Example ```bash curl "http://localhost:8080/compact/memory?message=Tell+me+about+Spring+AI" ``` ``` -------------------------------- ### /compact/trigger (GET) Source: https://github.com/danvega/compacting-chat-memory-advisor/blob/master/README.md Manually triggers the compaction of the conversation history. Returns a summary of the compaction process, including message and token savings. ```APIDOC ## GET /compact/trigger ### Description Manually trigger compaction of conversation history. This operation summarizes the oldest messages in the chat memory to reduce token count. ### Method GET ### Endpoint `/compact/trigger` ### Response #### Success Response (200) - **Summary** (string) - A message detailing the number of messages compacted and token savings achieved. ### Response Example ``` Compacted 40 messages into summary. Messages: 80 -> 41, Tokens: 8234 -> 4521 (saved 3713 tokens) ``` ### Request Example ```bash curl http://localhost:8080/compact/trigger ``` ``` -------------------------------- ### /memory (GET) Source: https://github.com/danvega/compacting-chat-memory-advisor/blob/master/README.md Standard chat endpoint using Spring AI's default `MessageChatMemoryAdvisor` for comparison purposes. This endpoint does not use message compaction. ```APIDOC ## GET /memory ### Description Standard chat endpoint using Spring AI's default `MessageChatMemoryAdvisor`. This is provided for comparison and does not utilize the message compaction feature. ### Method GET ### Endpoint `/memory` ### Parameters #### Query Parameters - **message** (string) - Required - Your message to the AI. ### Request Example ```bash curl "http://localhost:8080/memory?message=Hello" ``` ``` -------------------------------- ### Add Google GenAI Dependency Source: https://github.com/danvega/compacting-chat-memory-advisor/blob/master/README.md Add the Google GenAI starter dependency to your pom.xml to use a different LLM for summarization. ```xml org.springframework.ai spring-ai-starter-model-google-genai ``` -------------------------------- ### Run All Tests Source: https://github.com/danvega/compacting-chat-memory-advisor/blob/master/README.md Command to execute all tests in the project using Maven. ```bash ./mvnw test ``` -------------------------------- ### Build Application with Maven Source: https://github.com/danvega/compacting-chat-memory-advisor/blob/master/CLAUDE.md Builds the Spring Boot application using Maven. Ensures all dependencies are downloaded and the project is compiled. ```bash ./mvnw clean install ``` -------------------------------- ### Run All Tests with Maven Source: https://github.com/danvega/compacting-chat-memory-advisor/blob/master/CLAUDE.md Executes all tests defined in the project using Maven. This is useful for verifying the application's correctness. ```bash ./mvnw test ``` -------------------------------- ### Configure Compacting Memory Properties Source: https://github.com/danvega/compacting-chat-memory-advisor/blob/master/README.md Configure the behavior of the CompactingChatMemoryAdvisor in application.properties. Adjust these values based on your needs. ```properties compact.memory.max-messages=100 compact.memory.compact-threshold=80 compact.memory.messages-to-compact=40 ``` -------------------------------- ### API Call: Chat with Compacting Memory Source: https://github.com/danvega/compacting-chat-memory-advisor/blob/master/README.md Initiate a chat session with the compacting memory enabled by sending a message to this endpoint. ```bash curl "http://localhost:8080/compact/memory?message=Tell+me+about+Spring+AI" ``` -------------------------------- ### Compacting Chat Memory Architecture Source: https://github.com/danvega/compacting-chat-memory-advisor/blob/master/README.md Illustrates the flow of a user message through the CompactingChatMemoryAdvisor, including the compaction process. ```text User Message → CompactingChatMemoryAdvisor ├─ Check if compaction needed (message count ≥ threshold?) ├─ If yes: Summarize oldest messages via LLM ├─ Add message to chat memory ├─ Retrieve full conversation history └─ Pass to ChatModel for response ``` -------------------------------- ### Configure Multiple LLMs Source: https://github.com/danvega/compacting-chat-memory-advisor/blob/master/README.md Configure both the primary chat model and the summarization model in application.properties. This allows for cost and performance optimization. ```properties # Primary chat model (OpenAI GPT-5) spring.ai.openai.api-key=${OPENAI_API_KEY} spring.ai.openai.chat.options.model=gpt-5 spring.ai.openai.chat.options.temperature=1.0 # Summarization model (Gemini 2.5 Flash - cheaper/faster) spring.ai.google.genai.api-key=${GOOGLE_GENAI_API_KEY} spring.ai.google.genai.chat.options.model=gemini-2.5-flash ``` -------------------------------- ### Run Single Test Source: https://github.com/danvega/compacting-chat-memory-advisor/blob/master/README.md Command to run a specific test class using Maven, identified by its name. ```bash ./mvnw test -Dtest=CompactingChatMemoryAdvisorTest ``` -------------------------------- ### Run a Specific Test with Maven Source: https://github.com/danvega/compacting-chat-memory-advisor/blob/master/CLAUDE.md Runs a single test class or method using Maven. This is helpful for focused testing during development. ```bash ./mvnw test -Dtest=ClassName#methodName ``` -------------------------------- ### Set OpenAI API Key Source: https://github.com/danvega/compacting-chat-memory-advisor/blob/master/README.md Set the OPENAI_API_KEY environment variable before running the application. This is required for OpenAI integration. ```bash export OPENAI_API_KEY=your-api-key-here ``` -------------------------------- ### Manually Trigger Compaction Source: https://github.com/danvega/compacting-chat-memory-advisor/blob/master/README.md Manually trigger the compaction process for the chat memory. This forces summarization of older messages. ```bash curl http://localhost:8080/compact/trigger ``` -------------------------------- ### Send Message to Compacting Memory Source: https://github.com/danvega/compacting-chat-memory-advisor/blob/master/README.md Send a message to the endpoint using the CompactingChatMemoryAdvisor, which summarizes old messages to preserve context. ```bash curl "http://localhost:8080/compact/memory?message=Hello" ``` -------------------------------- ### Create Separate ChatModel Bean for Summarization Source: https://github.com/danvega/compacting-chat-memory-advisor/blob/master/README.md Define a separate ChatModel bean, qualified for the summarization LLM (e.g., Gemini), in your Spring configuration. This bean will be injected into the CompactingChatMemoryAdvisor. ```java @Bean @Qualifier("geminiChatModel") public ChatModel geminiChatModel(GoogleGenAiChatModel googleGenAiChatModel) { return googleGenAiChatModel; } @Bean public CompactingChatMemoryAdvisor compactingChatMemoryAdvisor( ChatMemory compactingChatMemory, @Qualifier("geminiChatModel") ChatModel geminiChatModel, // Use Gemini for summaries CompactingMemoryProperties properties) { return new CompactingChatMemoryAdvisor( compactingChatMemory, geminiChatModel, // Summaries use cheap/fast Gemini Flash properties.maxMessages(), properties.compactThreshold(), properties.messagesToCompact() ); } ``` -------------------------------- ### Send Message to Standard Memory Source: https://github.com/danvega/compacting-chat-memory-advisor/blob/master/README.md Send a message to the endpoint using standard memory, which discards old messages. Used for comparison. ```bash curl "http://localhost:8080/memory?message=Hello" ``` -------------------------------- ### Clear Conversation History Source: https://github.com/danvega/compacting-chat-memory-advisor/blob/master/README.md Clear all conversation history from the memory. Use this to reset the chat context. ```bash curl http://localhost:8080/compact/clear ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.