Skip to content

Commit

Permalink
Merge pull request #89 from 2024-SummerBootcamp-Team/develop
Browse files Browse the repository at this point in the history
[main] merge, main branch 최신화
  • Loading branch information
kalsteve authored Jul 20, 2024
2 parents 9d57c16 + b85c4fa commit 588d0f7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 35 deletions.
23 changes: 13 additions & 10 deletions app/config/langChain/langChainSetting.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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 = (
Expand All @@ -61,9 +61,12 @@ 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",
)

# TODO: 메시지 정리 추가
# Summary memory
# https://github.com/langchain-ai/langchain/blob/master/docs/docs/how_to/chatbots_memory.ipynb
36 changes: 11 additions & 25 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 588d0f7

Please sign in to comment.