Spaces:
Sleeping
Sleeping
Create llm_ollama.py
Browse files- llm_ollama.py +87 -0
llm_ollama.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# llm_ollama.py νμΌμ λ€μ λ΄μ©μ μΆκ°ν©λλ€.
|
| 2 |
+
import json
|
| 3 |
+
import requests
|
| 4 |
+
import streamlit as st
|
| 5 |
+
|
| 6 |
+
OLLAMA_HOST = "http://localhost:11434"
|
| 7 |
+
OLLAMA_MODEL = "gemma2:9b"
|
| 8 |
+
OLLAMA_TIMEOUT = 60
|
| 9 |
+
|
| 10 |
+
KOREAN_SYSTEM_PROMPT = """λΉμ μ νκ΅μ΄ μ΄μμ€ν΄νΈμ
λλ€. νμ νκ΅μ΄λ‘ λ΅νμΈμ."""
|
| 11 |
+
STRUCTURED_EXTRACTION_SYSTEM = """\
|
| 12 |
+
You are a travel assistant that extracts structured fields from Korean user queries.
|
| 13 |
+
Return ONLY a valid JSON object:
|
| 14 |
+
{
|
| 15 |
+
"emotion": "happy|sad|stressed|excited|tired|none",
|
| 16 |
+
"intent": "beach|hiking|shopping|food|museum|relaxing|none",
|
| 17 |
+
"country_hint": "",
|
| 18 |
+
"city_hint": "",
|
| 19 |
+
"themes_hint": ["<0..3 words>"],
|
| 20 |
+
"notes": "<very short reasoning in Korean>"
|
| 21 |
+
}
|
| 22 |
+
If unknown, use "none" or "" and NEVER add extra text outside JSON.
|
| 23 |
+
"""
|
| 24 |
+
|
| 25 |
+
def _call_ollama_chat(messages, model=OLLAMA_MODEL,
|
| 26 |
+
temperature=0.8, top_p=0.9, top_k=40, repeat_penalty=1.1,
|
| 27 |
+
system_prompt=None):
|
| 28 |
+
url = f"{OLLAMA_HOST}/api/chat"
|
| 29 |
+
_msgs = []
|
| 30 |
+
if system_prompt:
|
| 31 |
+
_msgs.append({"role":"system","content":system_prompt})
|
| 32 |
+
_msgs.extend(messages)
|
| 33 |
+
payload = {
|
| 34 |
+
"model": model,
|
| 35 |
+
"messages": _msgs,
|
| 36 |
+
"options": {
|
| 37 |
+
"temperature": temperature,
|
| 38 |
+
"top_p": top_p,
|
| 39 |
+
"top_k": top_k,
|
| 40 |
+
"repeat_penalty": repeat_penalty,
|
| 41 |
+
},
|
| 42 |
+
"stream": False,
|
| 43 |
+
}
|
| 44 |
+
try:
|
| 45 |
+
r = requests.post(url, json=payload, timeout=OLLAMA_TIMEOUT)
|
| 46 |
+
r.raise_for_status()
|
| 47 |
+
return (r.json().get("message") or {}).get("content","") or ""
|
| 48 |
+
except requests.RequestException:
|
| 49 |
+
return ""
|
| 50 |
+
|
| 51 |
+
def llm_followup_loop():
|
| 52 |
+
# Streamlitμ λ§€λ² μλ‘ μ€νλλ―λ‘, λν κΈ°λ‘μ μΈμ
μνμ μ μ₯ν΄μΌ ν¨
|
| 53 |
+
if "llm_history" not in st.session_state:
|
| 54 |
+
st.session_state.llm_history = [{"role": "bot", "content": "μ΄λ€ μ μ΄ λ κΆκΈνμ κ°μ?"}]
|
| 55 |
+
|
| 56 |
+
# μ± λ©μμ§ κΈ°λ‘
|
| 57 |
+
for message in st.session_state.llm_history:
|
| 58 |
+
with st.chat_message(message["role"]):
|
| 59 |
+
st.markdown(message["content"])
|
| 60 |
+
|
| 61 |
+
# μ¬μ©μ μ
λ ₯ λ°κΈ°
|
| 62 |
+
q = st.chat_input("μ¬νμ λν΄ λ κΆκΈν μ μ΄ μλμ?")
|
| 63 |
+
|
| 64 |
+
if q:
|
| 65 |
+
# μ¬μ©μ λ©μμ§ νμ λ° κΈ°λ‘
|
| 66 |
+
st.session_state.llm_history.append({"role": "user", "content": q})
|
| 67 |
+
with st.chat_message("user"):
|
| 68 |
+
st.markdown(q)
|
| 69 |
+
|
| 70 |
+
# Ollama API νΈμΆ
|
| 71 |
+
with st.spinner("μκ° μ€..."):
|
| 72 |
+
messages = [{"role": m["role"], "content": m["content"]} for m in st.session_state.llm_history]
|
| 73 |
+
|
| 74 |
+
a = _call_ollama_chat(
|
| 75 |
+
messages,
|
| 76 |
+
system_prompt=KOREAN_SYSTEM_PROMPT,
|
| 77 |
+
temperature=0.8, top_p=0.9, top_k=40, repeat_penalty=1.1
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
# λ΄ λ©μμ§ νμ λ° κΈ°λ‘
|
| 81 |
+
if not a:
|
| 82 |
+
st.session_state.llm_history.append({"role": "assistant", "content": "β οΈ Ollama μλ΅μ λ°μ§ λͺ»νμ΅λλ€. μλ² μνλ₯Ό νμΈνμΈμ."})
|
| 83 |
+
else:
|
| 84 |
+
st.session_state.llm_history.append({"role": "assistant", "content": a})
|
| 85 |
+
|
| 86 |
+
# νμ΄μ§ 리λ‘λ
|
| 87 |
+
st.rerun()
|