Skip to content

Commit

Permalink
Merge pull request #81 from studio-recoding/refactor/chat-completion
Browse files Browse the repository at this point in the history
feat: chat 분류에 completion 추가
  • Loading branch information
uommou authored Jul 17, 2024
2 parents 5187ed7 + aa6b9ea commit 39b261e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 42 deletions.
77 changes: 40 additions & 37 deletions app/prompt/openai_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,43 +60,46 @@ class Template:
"""

case_classify_template = """
Task: User Chat Classification
You are a case classifier integrated in scheduler application.
Please analyze User Chat according to the following criteria and return the appropriate case number (1, 2, 3, 4).
{chat_type}
- Case 1: \
The question is a general information request, advice, or simple conversation, and does not require accessing the user's schedule database.
- Case 2: \
The question involves a request to create a new schedule for the user, including setting up events for specific dates or times.
- Case 3: \
The question requires accessing or searching through the user's previous schedule information. This might involve past schedules, preferences, or other relevant details.
- Case 4: \
The question involves a request to delete an event or events from the user's schedule, necessitating identification and removal of specific entries from the database.
After analyzing the content of the question, return the most suitable case number.
YOU MUST ANSWER ONLY WITH NUMBER (1, 2, 3, 4). OTHER WORDS ARE PROHIBITED. IT IS VERY IMPORTANT TO RETURN ONLY THE NUMBERS. NO YAPPING!
Task: Analyze the content of the question and return the most suitable case number.
Example 1:
User Chat: "What's the weather like tomorrow?"
Answer: 1
Example 2:
User Chat: "I have a meeting with Dr. Lee next Monday at 10 AM."
Answer: 2
Example 3:
User Chat: "Did I have any appointments on the last Friday?"
Answer: 3
Example 4:
User Chat: "Please delete my appointment with Dr. Smith next Tuesday."
Answer: 4
Example 5:
User Chat: {question}
Answer:
Task: User Chat and Context Classification
You are a case classifier integrated into a scheduler application.
Please analyze User Chat along with its context from previous interactions according to the following criteria and return the appropriate case number (1, 2, 3, 4).
{chat_type}
- Case 1:
The question is a general information request, advice, or simple conversation, and does not require accessing the user's schedule database.
- Case 2:
The question involves a request to create a new schedule for the user, including setting up events for specific dates or times.
- Case 3:
The question requires accessing or searching through the user's previous schedule information or any related conversation context. This might involve past schedules, preferences, or other relevant details from earlier chats.
- Case 4:
The question involves a request to delete an event or events from the user's schedule, necessitating identification and removal of specific entries from the database, potentially including information about past conversations regarding the event.
After analyzing the content of the question and its context, return the most suitable case number.
YOU MUST ANSWER ONLY WITH NUMBER (1, 2, 3, 4). OTHER WORDS ARE PROHIBITED. IT IS VERY IMPORTANT TO RETURN ONLY THE NUMBERS. NO YAPPING!
Task: Analyze the content of the question along with the chat history and return the most suitable case number.
Example 1:
User Chat: "What's the weather like tomorrow?"
Context: None
Answer: 1
Example 2:
User Chat: "I have a meeting with Dr. Lee next Monday at 10 AM."
Context: User previously asked for a free slot on Monday.
Answer: 2
Example 3:
User Chat: "Did I have any appointments on the last Friday?"
Context: User often asks about weekly appointments to reflect on time management.
Answer: 3
Example 4:
User Chat: "Please delete my appointment with Dr. Smith next Tuesday."
Context: User mentioned dissatisfaction with this appointment in a previous chat.
Answer: 4
"""

chat_type_stt_template = """
Expand Down
31 changes: 26 additions & 5 deletions app/routers/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ async def get_langchain_case(data: PromptRequest) -> ChatCaseResponse:
model_name=config_chat['MODEL_NAME'], # 모델명
openai_api_key=OPENAI_API_KEY # API 키
)

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

question = data.prompt # 유저로부터 받은 채팅의 내용
chat_type = data.chatType # 위스퍼 사용 여부 [STT, USER]

# description: give NESS's instruction as for case analysis
my_template = openai_prompt.Template.case_classify_template

# chat type에 따라 적합한 프롬프트를 삽입
if chat_type == "STT":
chat_type_prompt = openai_prompt.Template.chat_type_stt_template
Expand All @@ -55,8 +57,27 @@ async def get_langchain_case(data: PromptRequest) -> ChatCaseResponse:
else:
raise HTTPException(status_code=500, detail="WRONG CHAT TYPE")

prompt = PromptTemplate.from_template(my_template)
case = chat_model.predict(prompt.format(question=question, chat_type=chat_type_prompt))
# description: give NESS's instruction as for case analysis
case_classify_template = openai_prompt.Template.case_classify_template
# prompt = PromptTemplate.from_template(my_template)

# system, human, ai
chat_prompt = ChatPromptTemplate.from_messages(
previous_conversations + [
("system", case_classify_template),
("human", "User Chat: {question}\nAnswer: ")
]
)
# case = chat_model.predict(prompt.format(question=question, chat_type=chat_type_prompt))
# 프롬프트와 모델을 chaining
chain = chat_prompt | chat_model

response = chain.invoke({
"chat_type": chat_type_prompt,
"question": question
})

case = response.content

# 각 케이스에도 chat type에 따라 적합한 프롬프트 삽입 필요
print(case)
Expand Down

0 comments on commit 39b261e

Please sign in to comment.