gloco-backend / apps /recommend /controller.py
MingDoan's picture
feat: First commit
65aa0cb
import json
from fastapi import HTTPException, status
from .provider import model
prompt_template = '''Think and generate content about how to recycle {type}
The recycle recommendation should be easy to understand and follow.
- For example, for the trash type "plastic bottle", provide how to use the plastic for household items.
- For the trash type "paper", provide how to do origami, etc.
You must answer clearly, easy to understand and step by step.
You must prompt professional and reliable information. No yapping.
You must provide an answer as a following JSON format:
{{
"recommendations": [
{{
"title": "Foo Bar",
"steps": [
"Foo",
"Bar"
]
}}
]
}}
'''
def recycle_recommendation(trash_type: str):
prompt = prompt_template.format(type=trash_type)
answer = model.generate_content(prompt)
try:
parsed_data = json.loads(answer.text)
except json.JSONDecodeError:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to parse the answer from the model"
)
return parsed_data