Skip to content

Commit

Permalink
Merge pull request #82 from studio-recoding/refactor/chat-completion
Browse files Browse the repository at this point in the history
Refactor: chat completion
  • Loading branch information
uommou authored Jul 18, 2024
2 parents 39b261e + 05aec5c commit 678ef7c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 28 deletions.
2 changes: 1 addition & 1 deletion app/database/connect_rds.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def fetch_previous_conversations(member_id):
sql = """
SELECT c.text, c.chat_type
FROM chat c
WHERE c.member_id = %s
WHERE c.member_id = %s AND c.created_date >= DATE_SUB(NOW(), INTERVAL 1 HOUR)
ORDER BY c.created_date DESC
LIMIT 5
"""
Expand Down
6 changes: 4 additions & 2 deletions app/prompt/report_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ class Template:
The keywords must capture the essence of the user's monthly activities, highlighting aspects that are both rewarding and enjoyable.
Your selections should be creative and personalized, aiming to reflect the user's unique experiences over the month.
Each explanation of the keywords must not exceed 50 characters in the {output_language}.
If no schedules are provided, you can improvise them.
Example:
User's monthly schedule: [Attended a programming bootcamp, Completed a marathon, Read three novels, Volunteered at the local food bank, Started a blog about sustainability]
AI Recommendation: "공부 매니아: 이번 달엔 공부를 정말 많이 하셨군요!, 환경 지킴이: 지속 가능한 생활에 대한 열정이 느껴져요., 자기 계발 홀릭: 성장을 위해 노력하는 모습이 멋져요."
AI Recommendation: [start]공부 매니아: 이번 달엔 공부를 정말 많이 하셨군요!||환경 지킴이: 지속 가능한 생활에 대한 열정이 느껴져요.||자기 계발 홀릭: 성장을 위해 노력하는 모습이 멋져요.
User's monthly schedule: [Took photography classes, Explored three new hiking trails, Organized a neighborhood clean-up, Experimented with vegan recipes]
AI Recommendation: "모험가: 새로운 활동에 많이 도전하셨네요!, 미식가: 레시피 실험을 했다니 멋져요., 도파민 중독자: 즐거운 일을 많이 만드시네요!"
AI Recommendation: [start]모험가: 새로운 활동에 많이 도전하셨네요!||미식가: 레시피 실험을 했다니 멋져요.||도파민 중독자: 즐거운 일을 많이 만드시네요!
User's monthly schedule: {schedule}
AI Recommendation:
Expand Down
33 changes: 9 additions & 24 deletions app/routers/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,20 @@ async def get_langchain_case(data: PromptRequest) -> ChatCaseResponse:
print(case)
case = int(case)
if case == 1:
response = await get_langchain_normal(data, chat_type_prompt)
response = await get_langchain_normal(data, chat_type_prompt, previous_conversations)
metadata = "null"

elif case == 2:
response_with_metadata = await get_langchain_schedule(data, chat_type_prompt)
response_with_metadata = await get_langchain_schedule(data, chat_type_prompt, previous_conversations)
response = response_with_metadata.split("<separate>")[0]
metadata = response_with_metadata.split("<separate>")[1]

elif case == 3:
response = await get_langchain_rag(data, chat_type_prompt)
response = await get_langchain_rag(data, chat_type_prompt, previous_conversations)
metadata = "null"

elif case == 4:
response_with_metadata = await delete_schedule(data, chat_type_prompt)
response_with_metadata = await delete_schedule(data, chat_type_prompt, previous_conversations)
response = response_with_metadata.split("<separate>")[0]
metadata = response_with_metadata.split("<separate>")[1]

Expand All @@ -110,7 +110,7 @@ async def get_langchain_case(data: PromptRequest) -> ChatCaseResponse:

# case 1 : normal
#@router.post("/case/normal") # 테스트용 엔드포인트
async def get_langchain_normal(data: PromptRequest, chat_type_prompt): # case 1 : normal
async def get_langchain_normal(data: PromptRequest, chat_type_prompt, previous_conversations): # case 1 : normal
print("running case 1")
# description: use langchain
config_normal = config['NESS_NORMAL']
Expand All @@ -131,10 +131,6 @@ async def get_langchain_normal(data: PromptRequest, chat_type_prompt): # case 1
current_time = datetime.now(seoul_timezone)
print(f'current time: {current_time}')

# 이전 대화내역 불러오기
previous_conversations = fetch_previous_conversations(data.member_id)
print(previous_conversations)

# case 1 프롬프트
case1_template = openai_prompt.Template.case1_template
#prompt = PromptTemplate.from_template(my_template)
Expand Down Expand Up @@ -162,7 +158,7 @@ async def get_langchain_normal(data: PromptRequest, chat_type_prompt): # case 1

# case 2 : 일정 생성
#@router.post("/case/make_schedule") # 테스트용 엔드포인트
async def get_langchain_schedule(data: PromptRequest, chat_type_prompt):
async def get_langchain_schedule(data: PromptRequest, chat_type_prompt, previous_conversations):
try:
print("running case 2")
config_normal = config['NESS_NORMAL']
Expand All @@ -188,10 +184,6 @@ async def get_langchain_schedule(data: PromptRequest, chat_type_prompt):
current_time = datetime.now(seoul_timezone)
print(f'current time: {current_time}')

# 이전 대화 내역 가져오기
previous_conversations = fetch_previous_conversations(member_id)
print(previous_conversations)

question = data.prompt
persona = data.persona
user_persona_prompt = persona_prompt.Template.from_persona(persona)
Expand Down Expand Up @@ -242,7 +234,7 @@ async def get_langchain_schedule(data: PromptRequest, chat_type_prompt):

# case 3 : rag
#@router.post("/case/rag") # 테스트용 엔드포인트
async def get_langchain_rag(data: PromptRequest, chat_type_prompt):
async def get_langchain_rag(data: PromptRequest, chat_type_prompt, previous_conversations):
print("running case 3")
# description: use langchain
config_normal = config['NESS_NORMAL']
Expand All @@ -265,10 +257,7 @@ async def get_langchain_rag(data: PromptRequest, chat_type_prompt):

# 관련 스케줄 가져오기
schedule = await vectordb.search_db_query(member_id, question) # vector db에서 검색

# 이전 대화 내역 불러오기
previous_conversations = fetch_previous_conversations(member_id)
print(previous_conversations)
print(schedule)

# description: give NESS's ideal instruction as template
case3_template = openai_prompt.Template.case3_template
Expand Down Expand Up @@ -302,7 +291,7 @@ async def get_langchain_rag(data: PromptRequest, chat_type_prompt):
return response.content

# case 4 : delete schedule
async def delete_schedule(data: PromptRequest, chat_type_prompt):
async def delete_schedule(data: PromptRequest, chat_type_prompt, previous_conversations):
print("running case 4: delete schedule")

config_normal = config['NESS_NORMAL']
Expand All @@ -325,10 +314,6 @@ async def delete_schedule(data: PromptRequest, chat_type_prompt):
# vectordb.search_db_query를 호출
schedule = await vectordb.search_db_query_delete(member_id, question)

# 이전 대화 내역 불러오기
previous_conversations = fetch_previous_conversations(member_id)
print(previous_conversations)

# description: give NESS's ideal instruction as template
case4_template = openai_prompt.Template.case4_template
case4_user = """
Expand Down
2 changes: 1 addition & 1 deletion app/routers/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async def get_tags(user_data: ReportTagsRequestDTO) -> TagsResponse:
print(result)

# 문자열 파싱해서 dto로 매핑
tag_entries = result.split("\"")[1].split(", ")
tag_entries = result.split("[start]")[1].split("||")
tags = []

for entry in tag_entries:
Expand Down

0 comments on commit 678ef7c

Please sign in to comment.