Amazon Bedrock 으로 RAG(retrieval augmented generation) Chatbot을 제작한 프로젝트입니다. 본 프로젝트를 이용해서 사용자는 LLM 모델을 사용해서 질문에 대한 응답을 생성할 수 있습니다. 또한 참고할 문서를 업로드 하고, 응답 생성시 참고하여 문서의 도메인 특화된 응답을 얻을 수 있습니다.
docker buildx build --no-cache --platform=linux/amd64 -t qarag .
docker tag qarag:latest [ACCOUNT ID].dkr.ecr.us-west-2.amazonaws.com/qarag:latest
docker push [ACCOUNT ID].dkr.ecr.us-west-2.amazonaws.com/qarag:latest
![cloud-architecture-02](https://private-user-images.githubusercontent.com/1788481/263707509-f33fa6c6-48e7-409a-b745-ce6364e33bd6.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjU3MDQzODAsIm5iZiI6MTcyNTcwNDA4MCwicGF0aCI6Ii8xNzg4NDgxLzI2MzcwNzUwOS1mMzNmYTZjNi00OGU3LTQwOWEtYjc0NS1jZTYzNjRlMzNiZDYucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDkwNyUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA5MDdUMTAxNDQwWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9Y2VhODFjYjM5MzQ3ZWQwNDg4NDljZjkyOWUxNzVlMmE4NmJkYmUzNGFhZWZlMmY4NGUzNDc0YzY0NjllNjM1NCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.WHkSL0pdkaHHhavQ2BdyyAwq5BxmbMP6jSBLSgZHrqc)
Amazon Bedrock을 이용해서 응답을 생성하고, Amazon OpenSearch를 이용해서 문서와 임베딩 벡터 값을 저장합니다. Frontend 와 Backend가 포함된 본 애플리케이션은 Amazon ECS에 배포되어 운영됩니다.
애플리케이션을 구성하고 있는 아키텍처에 대해 살펴보도록 하겠습니다.
![usecase-01](https://private-user-images.githubusercontent.com/1788481/259589979-2fc41ea0-c573-497c-a08e-7bf9a65dd79e.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjU3MDQzODAsIm5iZiI6MTcyNTcwNDA4MCwicGF0aCI6Ii8xNzg4NDgxLzI1OTU4OTk3OS0yZmM0MWVhMC1jNTczLTQ5N2MtYTA4ZS03YmY5YTY1ZGQ3OWUucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDkwNyUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA5MDdUMTAxNDQwWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9YmU1NDg1ODlmYzNiMDk1NDBkODVjNDU2NWE3NGUyZTQ2NmY5YzNjZGYwM2E2NTgzMGMxM2U1MTM4MDIzZWEyNyZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.rK89EHd8CWlnaFbUsgd0HICqNkv_zz0d_U4dmk6DZSA)
- Store Document: 응답 생성시 참고할 문서를 업로드합니다.
- Delete Document: 업로드한 문서를 삭제합니다.
- Get Answer: LLM 과 업로드한 문서를 참고하여 응답을 생성합니다.
![app-architecture-overview](https://private-user-images.githubusercontent.com/1788481/259589968-31a89bc6-bbd4-44e0-afb6-34d6a4b63c2e.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjU3MDQzODAsIm5iZiI6MTcyNTcwNDA4MCwicGF0aCI6Ii8xNzg4NDgxLzI1OTU4OTk2OC0zMWE4OWJjNi1iYmQ0LTQ0ZTAtYWZiNi0zNGQ2YTRiNjNjMmUucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDkwNyUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA5MDdUMTAxNDQwWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9Mjc0NDFlMjA3ZGFhNmZmYWFhM2FjOWVjOTQ5OTE2ODUyYWM1ZWEwNjc4NGJiYzM5MTA0ZjA5N2MyYmY2MDUyZiZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.WkJUMpSwNS68UeiLU7KpRsTNY8fNqtG4HFCVRCIy_74)
- Chatbot Interface: 사용자와 상호작용 할 수 있는 Chatbot 형태의 인터페이스를 제공합니다.
- Generator: Prompt 를 생성해서 LLM 으로 부터 응답 생성을 요청합니다.
- Retriever: Prompt 생성에 팔요한 사용자의 질문과 가장 관련이 있는 문서를 검색합니다.
- Knowledge Source: 문서를 임베딩 벡터와 함께 저장합니다.
- LLM(Large Language Model): 응답을 생성합니다.
![app-architecture-diagram](https://private-user-images.githubusercontent.com/1788481/259589962-cbed519c-9380-4887-808f-275635700948.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjU3MDQzODAsIm5iZiI6MTcyNTcwNDA4MCwicGF0aCI6Ii8xNzg4NDgxLzI1OTU4OTk2Mi1jYmVkNTE5Yy05MzgwLTQ4ODctODA4Zi0yNzU2MzU3MDA5NDgucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDkwNyUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA5MDdUMTAxNDQwWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZDNhOTc2MmUwYzNhMjk3NWJjYzJkNTlkNGIwMzk2ODU0NDgxODRmNjIxZDQ2NDZjMjFjNjRkZDU5NDNiY2ExNCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.F_zMdFrAM5ihgNBl0k8grM18FL2uobbLsEu1vvrSEU8)
참고할 문서를 Chunk 단위로 나누고 Embedding Vector 로 변환하여 원본과 함께 저장합니다.
![app-architecture-sequence-01](https://private-user-images.githubusercontent.com/1788481/259681222-589dfe14-56d1-4efa-b079-c481c2bee165.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjU3MDQzODAsIm5iZiI6MTcyNTcwNDA4MCwicGF0aCI6Ii8xNzg4NDgxLzI1OTY4MTIyMi01ODlkZmUxNC01NmQxLTRlZmEtYjA3OS1jNDgxYzJiZWUxNjUucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDkwNyUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA5MDdUMTAxNDQwWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9MmZkZmJmNDBjOTI3MjI3NDM2YmZhY2E1ZDNkZTc5YWI0NTBkYzAwMjU0OWU1YjUxMTZlY2RhYWI5ZDVjMGMwZCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.wgmHbvrAm64o41VStIzgf316108TybQEpUuHmjRFOn4)
저장된 문서와 Embedding Vector 를 제거합니다.
![app-architecture-sequence-02](https://private-user-images.githubusercontent.com/1788481/259681239-6c9ce6d4-6b8b-44d3-9326-6b3315e3491d.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjU3MDQzODAsIm5iZiI6MTcyNTcwNDA4MCwicGF0aCI6Ii8xNzg4NDgxLzI1OTY4MTIzOS02YzljZTZkNC02YjhiLTQ0ZDMtOTMyNi02YjMzMTVlMzQ5MWQucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDkwNyUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA5MDdUMTAxNDQwWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZmQ1ZGEzNjVlNzk4Y2IzMDcxYTAyZTM1NmM4YjUyMmU2ZjhkNDdjMjYzOTk5YTg0YTJhMWVhNmNmNjc1YmE1NyZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.GaR4hqa7yBrgF6h7c1rqtvG5C1YG66B8wyVDtrVpyJU)
질문과 연관된 문서를 찾아서 Prompt 를 만들어서 응답을 생성하는데 사용합니다.
![app-architecture-sequence-03](https://private-user-images.githubusercontent.com/1788481/259681246-3aad96ec-cd25-474d-8580-3589e2694194.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjU3MDQzODAsIm5iZiI6MTcyNTcwNDA4MCwicGF0aCI6Ii8xNzg4NDgxLzI1OTY4MTI0Ni0zYWFkOTZlYy1jZDI1LTQ3NGQtODU4MC0zNTg5ZTI2OTQxOTQucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDkwNyUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA5MDdUMTAxNDQwWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9YWVlYjViNTg2NjMwOGQ2ODRkMmYyM2MzYjAyN2I1NWJiYjE3MjdhMDBmZDQ5MmY1ZGFhZjc1M2Q2YWRhM2M5YSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.zxHEzhd8qtn4y2XzkTpwEFko7tT4MDU7zcBmmk0lMy0)