# llm_ollama.py 파일에 다음 내용을 추가합니다. import json import requests import streamlit as st OLLAMA_HOST = "http://localhost:11434" OLLAMA_MODEL = "gemma2:9b" OLLAMA_TIMEOUT = 60 KOREAN_SYSTEM_PROMPT = """당신은 한국어 어시스턴트입니다. 항상 한국어로 답하세요.""" STRUCTURED_EXTRACTION_SYSTEM = """\ You are a travel assistant that extracts structured fields from Korean user queries. Return ONLY a valid JSON object: { "emotion": "happy|sad|stressed|excited|tired|none", "intent": "beach|hiking|shopping|food|museum|relaxing|none", "country_hint": "", "city_hint": "", "themes_hint": ["<0..3 words>"], "notes": "" } If unknown, use "none" or "" and NEVER add extra text outside JSON. """ def _call_ollama_chat(messages, model=OLLAMA_MODEL, temperature=0.8, top_p=0.9, top_k=40, repeat_penalty=1.1, system_prompt=None): url = f"{OLLAMA_HOST}/api/chat" _msgs = [] if system_prompt: _msgs.append({"role":"system","content":system_prompt}) _msgs.extend(messages) payload = { "model": model, "messages": _msgs, "options": { "temperature": temperature, "top_p": top_p, "top_k": top_k, "repeat_penalty": repeat_penalty, }, "stream": False, } try: r = requests.post(url, json=payload, timeout=OLLAMA_TIMEOUT) r.raise_for_status() return (r.json().get("message") or {}).get("content","") or "" except requests.RequestException: return "" def llm_followup_loop(): # Streamlit은 매번 새로 실행되므로, 대화 기록을 세션 상태에 저장해야 함 if "llm_history" not in st.session_state: st.session_state.llm_history = [{"role": "bot", "content": "어떤 점이 더 궁금하신가요?"}] # 챗 메시지 기록 for message in st.session_state.llm_history: with st.chat_message(message["role"]): st.markdown(message["content"]) # 사용자 입력 받기 q = st.chat_input("여행에 대해 더 궁금한 점이 있나요?") if q: # 사용자 메시지 표시 및 기록 st.session_state.llm_history.append({"role": "user", "content": q}) with st.chat_message("user"): st.markdown(q) # Ollama API 호출 with st.spinner("생각 중..."): messages = [{"role": m["role"], "content": m["content"]} for m in st.session_state.llm_history] a = _call_ollama_chat( messages, system_prompt=KOREAN_SYSTEM_PROMPT, temperature=0.8, top_p=0.9, top_k=40, repeat_penalty=1.1 ) # 봇 메시지 표시 및 기록 if not a: st.session_state.llm_history.append({"role": "assistant", "content": "⚠️ Ollama 응답을 받지 못했습니다. 서버 상태를 확인하세요."}) else: st.session_state.llm_history.append({"role": "assistant", "content": a}) # 페이지 리로드 st.rerun()