|
| 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