Skip to content

Commit 5f93d90

Browse files
authored
Merge pull request #43 from studio-recoding/feat/memory
[feat] generate emoji that represents today's schedule
2 parents c723048 + 4226567 commit 5f93d90

File tree

6 files changed

+82
-2
lines changed

6 files changed

+82
-2
lines changed

app/database/chroma_db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ async def add_db_data(schedule_data: AddScheduleDTO):
5757

5858
# 메인페이지 한 줄 추천 기능에 사용하는 함수
5959
# 유저의 id, 해당 날짜로 필터링
60-
async def db_recommendation_main(user_data: RecommendationMainRequestDTO):
60+
async def db_daily_schedule(user_data: RecommendationMainRequestDTO):
6161
member = user_data.member_id
6262
schedule_datetime_start = user_data.schedule_datetime_start
6363
schedule_datetime_end = user_data.schedule_datetime_end

app/dto/db_dto.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ class RecommendationMainRequestDTO(BaseModel):
1717
schedule_datetime_start: str
1818
schedule_datetime_end: str
1919

20+
class ReportMemoryEmojiRequestDTO(BaseModel):
21+
member_id: int
22+
user_persona: str
23+
schedule_datetime_start: str
24+
schedule_datetime_end: str

app/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
from app.routers import recommendation
3333
app.include_router(recommendation.router)
3434

35+
from app.routers import report
36+
app.include_router(report.router)
37+
3538
# description: prevent CORS error
3639
origins = [
3740
"*",

app/prompt/report_prompt.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Template:
2+
memory_emoji_template = """
3+
You are an AI assistant designed to return an emoji that represents a user's day based on their schedule. You will receive information about the user's daily schedule. Your task is to analyze this schedule and select a single emoji that encapsulates the day's activities. There are a few guidelines you must adhere to in your selection:
4+
5+
YOU MUST RESPOND WITH A SINGLE EMOJI without any additional commentary.
6+
The emoji must reflect the overall theme or mood of the day's activities.
7+
Your recommendation should be intuitive, making it easy for the user to see the connection between the schedule and the chosen emoji.
8+
Example:
9+
User schedule: [Practice guitar, Calculate accuracy, Study backend development, Run AI models in the lab, Study NEST.JS]
10+
AI Recommendation: 🎓
11+
12+
User schedule: [Morning jog, Office work, Lunch with friends, Evening yoga]
13+
AI Recommendation: ☀️
14+
15+
User schedule: {schedule}
16+
AI Recommendation:
17+
"""

app/routers/recommendation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ async def get_recommendation(user_data: RecommendationMainRequestDTO) -> ChatRes
3838
)
3939

4040
# vectordb에서 유저의 정보를 가져온다.
41-
schedule = await vectordb.db_recommendation_main(user_data)
41+
schedule = await vectordb.db_daily_schedule(user_data)
4242

4343
print(schedule)
4444

app/routers/report.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import configparser
2+
import os
3+
4+
from dotenv import load_dotenv
5+
from fastapi import APIRouter, Depends, status, HTTPException
6+
from langchain_community.chat_models import ChatOpenAI
7+
from langchain_core.prompts import PromptTemplate
8+
9+
from app.dto.db_dto import ReportMemoryEmojiRequestDTO
10+
from app.dto.openai_dto import ChatResponse
11+
from app.prompt import report_prompt
12+
import app.database.chroma_db as vectordb
13+
14+
router = APIRouter(
15+
prefix="/report",
16+
tags=["report"]
17+
)
18+
19+
# description: load env variables from .env file
20+
load_dotenv()
21+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
22+
23+
# description: load config variables from openai_config.ini file
24+
CONFIG_FILE_PATH = "app/prompt/openai_config.ini"
25+
config = configparser.ConfigParser()
26+
config.read(CONFIG_FILE_PATH)
27+
28+
@router.post("/memory_emoji", status_code=status.HTTP_200_OK)
29+
async def get_memory_emoji(user_data: ReportMemoryEmojiRequestDTO) -> ChatResponse:
30+
try:
31+
# 모델
32+
config_report_emoji = config['NESS_RECOMMENDATION']
33+
34+
chat_model = ChatOpenAI(temperature=config_report_emoji['TEMPERATURE'], # 창의성 (0.0 ~ 2.0)
35+
max_tokens=config_report_emoji['MAX_TOKENS'], # 최대 토큰수
36+
model_name=config_report_emoji['MODEL_NAME'], # 모델명
37+
openai_api_key=OPENAI_API_KEY # API 키
38+
)
39+
40+
# vectordb에서 유저의 정보를 가져온다.
41+
schedule = await vectordb.db_daily_schedule(user_data)
42+
43+
print(schedule)
44+
45+
# 템플릿
46+
memory_emoji_template = report_prompt.Template.memory_emoji_template
47+
48+
prompt = PromptTemplate.from_template(memory_emoji_template)
49+
result = chat_model.predict(prompt.format(output_language="Korean", schedule=schedule))
50+
print(result)
51+
return ChatResponse(ness=result)
52+
53+
except Exception as e:
54+
raise HTTPException(status_code=500, detail=str(e))
55+

0 commit comments

Comments
 (0)