From 98a30c8d46591021a347187c014fdff2f5b44186 Mon Sep 17 00:00:00 2001 From: kalsteve Date: Sat, 20 Jul 2024 00:06:21 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20trimmer=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/config/langChain/langChainSetting.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/app/config/langChain/langChainSetting.py b/app/config/langChain/langChainSetting.py index ac0f3bf..3e245e0 100644 --- a/app/config/langChain/langChainSetting.py +++ b/app/config/langChain/langChainSetting.py @@ -1,10 +1,12 @@ from operator import itemgetter +from langchain_community.chat_message_histories import SQLChatMessageHistory from langchain_openai import ChatOpenAI from langchain_core.runnables.history import RunnableWithMessageHistory, RunnablePassthrough import os from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder -from langchain_community.chat_message_histories import SQLChatMessageHistory +# from app.config.langChain.SQLChatMessageHistoryCustom import SQLChatMessageHistory + from langchain_core.output_parsers import StrOutputParser from langchain_core.messages import ( trim_messages, @@ -44,13 +46,11 @@ def get_session_history(session_id): ] ) -# 토큰 제한 트리머 설정 -trimmer = trim_messages( - strategy="last", # 최근 메시지를 기준으로 토큰 제한 - max_tokens=20, # 최대 20토큰까지 제한 - token_counter=len, # 토큰의 길이를 계산, 임시적으로 길이 계산 적용 - include_system=True, # 시스템 메시지도 포함 -) +trimmer = trim_messages(strategy="last", + max_tokens=200, + token_counter=llm.get_num_tokens_from_messages, + ) + # 트리밍이 적용된 체인 설정 chain_with_trimming = ( @@ -61,8 +61,7 @@ def get_session_history(session_id): # 메시지 히스토리를 포함하여 넣어주는 러너블 생성 runnable_with_history = RunnableWithMessageHistory( - # chain_with_trimming, - prompt | llm, + chain_with_trimming, get_session_history, input_messages_key="input", history_messages_key="chat_history", From 3ce3e195388b048b72f63d95ec5989a2a96f2f6c Mon Sep 17 00:00:00 2001 From: kalsteve Date: Sat, 20 Jul 2024 00:19:55 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20todo=20summary=20memory=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20TODO?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/config/langChain/langChainSetting.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/config/langChain/langChainSetting.py b/app/config/langChain/langChainSetting.py index 3e245e0..011e8bb 100644 --- a/app/config/langChain/langChainSetting.py +++ b/app/config/langChain/langChainSetting.py @@ -66,3 +66,7 @@ def get_session_history(session_id): input_messages_key="input", history_messages_key="chat_history", ) + +# TODO: 메시지 정리 추가 +# Summary memory +# https://github.com/langchain-ai/langchain/blob/master/docs/docs/how_to/chatbots_memory.ipynb \ No newline at end of file From e84794d02eccd95d892358ab5db81b3d67de0c37 Mon Sep 17 00:00:00 2001 From: dlwhsk0 Date: Sat, 20 Jul 2024 00:37:50 +0900 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20=EB=A1=9C=EA=B7=B8=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20=EC=BD=94=EB=93=9C=20=EC=A3=BC=EC=84=9D=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/main.py | 36 +++++++++++------------------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/app/main.py b/app/main.py index 88010b1..80b8165 100644 --- a/app/main.py +++ b/app/main.py @@ -25,37 +25,23 @@ allow_headers=["*"], # 모든 헤더를 허용 ) + +# prometeus instrumentator = Instrumentator().instrument(app) instrumentator.expose(app, include_in_schema=False) -# 라우팅 설정 -logging.basicConfig( - filename='/app/logs/app.log', # 로그 파일 이름 - level=logging.INFO, # 로그 레벨 - format='%(asctime)s %(levelname)s %(message)s' # 로그 메시지 포맷 -) +# 로그 생성 +# logging.basicConfig( +# filename='/app/logs/app.log', # 로그 파일 이름 +# level=logging.INFO, # 로그 레벨 +# format='%(asctime)s %(levelname)s %(message)s' # 로그 메시지 포맷 +# ) + +# 라우팅 설정 """ 라우트란? - 클라이언트로부터 요청을 받았을 때, 해당 요청을 처리할 수 있는 함수를 매핑하는 것 """ -app.include_router(api.router) - -# # 첫 번째 질문 -# question1 = "What are the key features of Python?" -# answer1 = with_message_history1.invoke(question=question1) -# print("Answer 1: ", answer1) -# -# # 두 번째 질문, 첫 번째 응답을 기반으로 -# feature1 = answer1.split()[0] -# answer2 = with_message_history2.invoke(feature=feature1) -# print("Answer 2: ", answer2) -# -# # 최종 응답, 두 번째 응답을 기반으로 -# feature2 = answer2.split()[0] -# final_answer = with_message_history3.invoke(feature1=feature1, feature2=feature2) -# print("Final Answer: ", final_answer) -# -# # 디버깅 정보 출력 -# langsmith.debug_info() +app.include_router(api.router) \ No newline at end of file