Chat Memory
LLM에게 대화 전반의 컨텍스트를 전달하는 기능
- chat memory 생성
// Posgresql 기반 Chat Memory Repository 생성
ChatMemoryRepository chatMemoryRepository = JdbcChatMemoryRepository.builder()
.jdbcTemplate(jdbcTemplate)
.dialect(new PostgresChatMemoryRepositoryDialect())
.build();
ChatMemory chatMemory = MessageWindowChatMemory.builder()
.chatMemoryRepository(chatMemoryRepository)
.maxMessages(10)
.build();
chatClient.prompt(input)
.advisors(PromptChatMemoryAdvisor.builder(chatMemory).build())
.call()
.content();
- max messages: 컨텍스트가 길어지면 LLM이 집중을 못하고, 토큰을 아끼기 위해 제한을 걸 수 있다.
- PromptChatMemoryAdvisor: 프롬프트에 이전 대화 내용을 추가해준다.
schema
- Chat Memory repository는 자동으로 table을 생성하는데, 그 스키마는 아래와 같다.
create table spring_ai_chat_memory
(
conversation_id varchar(36) not null,
content text not null,
type varchar(10) not null
constraint spring_ai_chat_memory_type_check
check ((type)::text = ANY
((ARRAY ['USER'::character varying, 'ASSISTANT'::character varying, 'SYSTEM'::character varying, 'TOOL'::character varying])::text[])),
timestamp timestamp not null
);
create index spring_ai_chat_memory_conversation_id_timestamp_idx
on spring_ai_chat_memory (conversation_id, timestamp);