Skip to content

Commit

Permalink
feat: add replace_ingredient
Browse files Browse the repository at this point in the history
  • Loading branch information
sspzoa committed Nov 7, 2024
1 parent 75196ca commit dc0d771
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from app.dependencies.auth import verify_token
from app.routes import ping, root
from app.routes.preference import preference
from app.routes.recipe import recipe, ingredient_info, cooking_step, chat, search
from app.routes.recipe import recipe, ingredient_info, cooking_step, chat, search, replace_ingredient
from app.routes.refrigerator import ingredient_detect, refrigerator
from app.routes.refrigerator import rearrange_refrigerator

Expand Down Expand Up @@ -36,6 +36,7 @@
app.include_router(cooking_step.router, dependencies=[Depends(verify_token)])
app.include_router(chat.router, dependencies=[Depends(verify_token)])
app.include_router(search.router, dependencies=[Depends(verify_token)])
app.include_router(replace_ingredient.router, dependencies=[Depends(verify_token)])
app.include_router(ingredient_detect.router, dependencies=[Depends(verify_token)])
app.include_router(refrigerator.router, dependencies=[Depends(verify_token)])
app.include_router(rearrange_refrigerator.router, dependencies=[Depends(verify_token)])
Expand Down
10 changes: 10 additions & 0 deletions app/models/recipe/replace_ingredient_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from pydantic import BaseModel


class ReplaceIngredientRequest(BaseModel):
recipe_id: str
ingredient_name: str


class ReplaceIngredientResponse(BaseModel):
replaced_ingredient: str
59 changes: 59 additions & 0 deletions app/routes/recipe/replace_ingredient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import logging

from bson import ObjectId
from fastapi import APIRouter, HTTPException

from app.config import client as openai_client
from app.database import recipe_collection
from app.models.recipe.replace_ingredient_models import ReplaceIngredientRequest, ReplaceIngredientResponse

router = APIRouter()


@router.post("/recipe/replace-ingredient", tags=["Recipe"], response_model=ReplaceIngredientResponse)
async def replace_ingredient(request: ReplaceIngredientRequest):
"""
레시피의 특정 재료에 대한 대체 재료를 추천합니다.
"""
try:
# 데이터베이스에서 레시피 검색
recipe = await recipe_collection.find_one({"_id": ObjectId(request.recipe_id)})
if not recipe:
raise HTTPException(status_code=404, detail="레시피를 찾을 수 없습니다.")

# 재료 존재 여부 확인
ingredient_exists = any(ing["name"] == request.ingredient_name for ing in recipe["ingredients"])
if not ingredient_exists:
raise HTTPException(status_code=404, detail="지정된 재료를 레시피에서 찾을 수 없습니다.")

# 대체 재료 추천을 위한 프롬프트
prompt = f"""다음 레시피의 '{request.ingredient_name}'를 대체할 수 있는 가장 적합한 재료 하나만 추천해주세요.
레시피: {recipe['name']}
레시피 설명: {recipe['description']}
대체할 재료: {request.ingredient_name}
다음 사항을 고려해주세요:
1. 원재료와 비슷한 맛과 식감을 제공할 수 있는 재료
2. 레시피의 전반적인 특성을 해치지 않는 재료
3. 조리 방법이 크게 달라지지 않는 재료
재료 이름만 답변해주세요.
"""

response = openai_client.chat.completions.create(
model="chatgpt-4o-latest",
messages=[
{"role": "system", "content": "당신은 요리 전문가로서 재료 대체에 대한 전문적인 지식을 가지고 있습니다."},
{"role": "user", "content": prompt}
],
temperature=0.7
)

replaced_ingredient = response.choices[0].message.content.strip()
return ReplaceIngredientResponse(replaced_ingredient=replaced_ingredient)

except Exception as e:
logging.error(f"Unexpected error: {e}", exc_info=True)
raise HTTPException(status_code=400, detail=str(e))

0 comments on commit dc0d771

Please sign in to comment.