feat: 동물약 챗봇 RAG 연동 (PostgreSQL llm_pharm)
This commit is contained in:
parent
009d133aef
commit
e12328ec17
@ -2699,6 +2699,48 @@ ANIMAL_CHAT_SYSTEM_PROMPT = """당신은 약국의 동물약 전문 상담사입
|
||||
{knowledge_base}"""
|
||||
|
||||
|
||||
def _get_animal_drug_rag(apc_codes):
|
||||
"""PostgreSQL에서 동물약 상세 정보 조회 (RAG용)"""
|
||||
if not apc_codes:
|
||||
return {}
|
||||
|
||||
try:
|
||||
from sqlalchemy import create_engine
|
||||
pg_engine = create_engine('postgresql://admin:trajet6640@192.168.0.87:5432/apdb_master')
|
||||
with pg_engine.connect() as conn:
|
||||
# APC 코드들로 상세 정보 조회
|
||||
placeholders = ','.join([f"'{apc}'" for apc in apc_codes if apc])
|
||||
if not placeholders:
|
||||
return {}
|
||||
|
||||
result = conn.execute(text(f"""
|
||||
SELECT apc, product_name, main_ingredient,
|
||||
llm_pharm->>'사용가능 동물' as target_animals,
|
||||
llm_pharm->>'분류' as category,
|
||||
llm_pharm->>'체중/부위' as dosage_weight,
|
||||
llm_pharm->>'기간/용법' as usage_period,
|
||||
llm_pharm->>'월령금기' as age_restriction,
|
||||
llm_pharm->>'반려인주의' as owner_caution
|
||||
FROM apc
|
||||
WHERE apc IN ({placeholders})
|
||||
"""))
|
||||
|
||||
rag_data = {}
|
||||
for row in result:
|
||||
rag_data[row.apc] = {
|
||||
'target_animals': row.target_animals or '정보 없음',
|
||||
'category': row.category or '',
|
||||
'dosage_weight': row.dosage_weight or '',
|
||||
'usage_period': row.usage_period or '',
|
||||
'age_restriction': row.age_restriction or '',
|
||||
'owner_caution': row.owner_caution or ''
|
||||
}
|
||||
return rag_data
|
||||
except Exception as e:
|
||||
logging.warning(f"동물약 RAG 조회 실패: {e}")
|
||||
return {}
|
||||
|
||||
|
||||
def _get_animal_drugs():
|
||||
"""보유 중인 동물약 목록 조회 (APC 이미지 포함)"""
|
||||
try:
|
||||
@ -2784,14 +2826,33 @@ def api_animal_chat():
|
||||
|
||||
# 보유 동물약 목록 조회
|
||||
animal_drugs = _get_animal_drugs()
|
||||
|
||||
# APC가 있는 제품의 상세 정보 조회 (RAG)
|
||||
apc_codes = [d['apc'] for d in animal_drugs if d.get('apc')]
|
||||
rag_data = _get_animal_drug_rag(apc_codes) if apc_codes else {}
|
||||
|
||||
available_products_text = ""
|
||||
if animal_drugs:
|
||||
product_list = "\n".join([
|
||||
f"- {d['name']} ({d['price']:,.0f}원)" for d in animal_drugs
|
||||
])
|
||||
product_lines = []
|
||||
for d in animal_drugs:
|
||||
line = f"- {d['name']} ({d['price']:,.0f}원)"
|
||||
# RAG 정보 추가
|
||||
if d.get('apc') and d['apc'] in rag_data:
|
||||
info = rag_data[d['apc']]
|
||||
details = []
|
||||
if info.get('target_animals'):
|
||||
details.append(f"대상: {info['target_animals']}")
|
||||
if info.get('dosage_weight'):
|
||||
details.append(f"용량: {info['dosage_weight']}")
|
||||
if info.get('age_restriction'):
|
||||
details.append(f"금기: {info['age_restriction']}")
|
||||
if details:
|
||||
line += f" [{', '.join(details)}]"
|
||||
product_lines.append(line)
|
||||
|
||||
available_products_text = f"""
|
||||
**현재 보유 동물약:**
|
||||
{product_list}
|
||||
{chr(10).join(product_lines)}
|
||||
"""
|
||||
|
||||
# System Prompt 구성
|
||||
|
||||
Loading…
Reference in New Issue
Block a user