Skip to content

Commit

Permalink
Merge pull request #43 from studio-recoding/feat/memory
Browse files Browse the repository at this point in the history
[feat] generate emoji that represents today's schedule
  • Loading branch information
uommou authored Apr 7, 2024
2 parents c723048 + 4226567 commit 5f93d90
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/database/chroma_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async def add_db_data(schedule_data: AddScheduleDTO):

# 메인페이지 한 줄 추천 기능에 사용하는 함수
# 유저의 id, 해당 날짜로 필터링
async def db_recommendation_main(user_data: RecommendationMainRequestDTO):
async def db_daily_schedule(user_data: RecommendationMainRequestDTO):
member = user_data.member_id
schedule_datetime_start = user_data.schedule_datetime_start
schedule_datetime_end = user_data.schedule_datetime_end
Expand Down
5 changes: 5 additions & 0 deletions app/dto/db_dto.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ class RecommendationMainRequestDTO(BaseModel):
schedule_datetime_start: str
schedule_datetime_end: str

class ReportMemoryEmojiRequestDTO(BaseModel):
member_id: int
user_persona: str
schedule_datetime_start: str
schedule_datetime_end: str
3 changes: 3 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
from app.routers import recommendation
app.include_router(recommendation.router)

from app.routers import report
app.include_router(report.router)

# description: prevent CORS error
origins = [
"*",
Expand Down
17 changes: 17 additions & 0 deletions app/prompt/report_prompt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Template:
memory_emoji_template = """
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:
YOU MUST RESPOND WITH A SINGLE EMOJI without any additional commentary.
The emoji must reflect the overall theme or mood of the day's activities.
Your recommendation should be intuitive, making it easy for the user to see the connection between the schedule and the chosen emoji.
Example:
User schedule: [Practice guitar, Calculate accuracy, Study backend development, Run AI models in the lab, Study NEST.JS]
AI Recommendation: 🎓
User schedule: [Morning jog, Office work, Lunch with friends, Evening yoga]
AI Recommendation: ☀️
User schedule: {schedule}
AI Recommendation:
"""
2 changes: 1 addition & 1 deletion app/routers/recommendation.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async def get_recommendation(user_data: RecommendationMainRequestDTO) -> ChatRes
)

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

print(schedule)

Expand Down
55 changes: 55 additions & 0 deletions app/routers/report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import configparser
import os

from dotenv import load_dotenv
from fastapi import APIRouter, Depends, status, HTTPException
from langchain_community.chat_models import ChatOpenAI
from langchain_core.prompts import PromptTemplate

from app.dto.db_dto import ReportMemoryEmojiRequestDTO
from app.dto.openai_dto import ChatResponse
from app.prompt import report_prompt
import app.database.chroma_db as vectordb

router = APIRouter(
prefix="/report",
tags=["report"]
)

# description: load env variables from .env file
load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

# description: load config variables from openai_config.ini file
CONFIG_FILE_PATH = "app/prompt/openai_config.ini"
config = configparser.ConfigParser()
config.read(CONFIG_FILE_PATH)

@router.post("/memory_emoji", status_code=status.HTTP_200_OK)
async def get_memory_emoji(user_data: ReportMemoryEmojiRequestDTO) -> ChatResponse:
try:
# 모델
config_report_emoji = config['NESS_RECOMMENDATION']

chat_model = ChatOpenAI(temperature=config_report_emoji['TEMPERATURE'], # 창의성 (0.0 ~ 2.0)
max_tokens=config_report_emoji['MAX_TOKENS'], # 최대 토큰수
model_name=config_report_emoji['MODEL_NAME'], # 모델명
openai_api_key=OPENAI_API_KEY # API 키
)

# vectordb에서 유저의 정보를 가져온다.
schedule = await vectordb.db_daily_schedule(user_data)

print(schedule)

# 템플릿
memory_emoji_template = report_prompt.Template.memory_emoji_template

prompt = PromptTemplate.from_template(memory_emoji_template)
result = chat_model.predict(prompt.format(output_language="Korean", schedule=schedule))
print(result)
return ChatResponse(ness=result)

except Exception as e:
raise HTTPException(status_code=500, detail=str(e))

0 comments on commit 5f93d90

Please sign in to comment.