code-slicer commited on
Commit
30eb937
ยท
verified ยท
1 Parent(s): 2381854

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -15
app.py CHANGED
@@ -194,13 +194,30 @@ def _build_structured_user_prompt(user_text: str) -> str:
194
  # ๋ถˆํ•„์š”ํ•œ ๋ž˜ํ•‘ ์—†์ด, ๋ชจ๋ธ์ด JSON๋งŒ ๋‚ด๋„๋ก ๊น”๋”ํžˆ ์ „๋‹ฌ
195
  return user_text.strip()
196
 
197
- def _call_ollama_chat(
198
- messages,
199
- model=OLLAMA_MODEL,
200
- temperature=0.8, top_p=0.9, top_k=40, repeat_penalty=1.1,
201
- system_prompt=None
202
- ):
203
- url = f"{OLLAMA_HOST}/api/chat"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204
  _msgs = []
205
  if system_prompt:
206
  _msgs.append({"role": "system", "content": system_prompt})
@@ -209,20 +226,26 @@ def _call_ollama_chat(
209
  payload = {
210
  "model": model,
211
  "messages": _msgs,
212
- "options": {
213
- "temperature": temperature,
214
- "top_p": top_p,
215
- "top_k": top_k,
216
- "repeat_penalty": repeat_penalty,
217
- },
218
  "stream": False,
219
  }
220
  try:
221
  r = requests.post(url, json=payload, timeout=OLLAMA_TIMEOUT)
222
  r.raise_for_status()
223
  return (r.json().get("message") or {}).get("content", "") or ""
224
- except requests.RequestException:
225
- return ""
 
 
 
 
 
 
 
 
 
 
 
226
 
227
  def _llm_structured_extract(user_text: str):
228
  out = _call_ollama_chat(
@@ -1684,6 +1707,9 @@ def main():
1684
  init_session()
1685
  chat_container = st.container()
1686
 
 
 
 
1687
  # โœ… ํ’€์Šคํฌ๋ฆฐ์ผ ๋•Œ๋งŒ ์กฐ๊ธฐ ๋ฆฌํ„ด
1688
  if st.session_state.get("llm_mode") and not st.session_state.get("llm_inline", False):
1689
  render_llm_followup(chat_container, inline=False)
 
194
  # ๋ถˆํ•„์š”ํ•œ ๋ž˜ํ•‘ ์—†์ด, ๋ชจ๋ธ์ด JSON๋งŒ ๋‚ด๋„๋ก ๊น”๋”ํžˆ ์ „๋‹ฌ
195
  return user_text.strip()
196
 
197
+ def _ollama_healthcheck():
198
+ base = OLLAMA_HOST.rstrip("/")
199
+ # 1) ์„œ๋ฒ„ ์‚ด์•„์žˆ๋Š”์ง€
200
+ try:
201
+ r = requests.get(f"{base}/api/version", timeout=5)
202
+ r.raise_for_status()
203
+ except requests.RequestException as e:
204
+ st.error(f"โŒ Ollama ์—ฐ๊ฒฐ ์‹คํŒจ: {e} (host={OLLAMA_HOST})")
205
+ return False
206
+
207
+ # 2) ๋ชจ๋ธ ์„ค์น˜ ์—ฌ๋ถ€
208
+ try:
209
+ tags = requests.get(f"{base}/api/tags", timeout=5).json()
210
+ names = [m.get("name") for m in tags.get("models", [])]
211
+ if OLLAMA_MODEL not in names:
212
+ st.warning(f"โš ๏ธ ๋ชจ๋ธ ๋ฏธ์„ค์น˜: `{OLLAMA_MODEL}`. ์„œ๋ฒ„์—์„œ `ollama pull {OLLAMA_MODEL}` ์‹คํ–‰ ํ•„์š”.")
213
+ except requests.RequestException as e:
214
+ st.warning(f"๋ชจ๋ธ ๋ชฉ๋ก ์กฐํšŒ ์‹คํŒจ: {e}")
215
+
216
+ return True
217
+
218
+
219
+ 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):
220
+ url = f"{OLLAMA_HOST.rstrip('/')}/api/chat"
221
  _msgs = []
222
  if system_prompt:
223
  _msgs.append({"role": "system", "content": system_prompt})
 
226
  payload = {
227
  "model": model,
228
  "messages": _msgs,
229
+ "options": {"temperature": temperature, "top_p": top_p, "top_k": top_k, "repeat_penalty": repeat_penalty},
 
 
 
 
 
230
  "stream": False,
231
  }
232
  try:
233
  r = requests.post(url, json=payload, timeout=OLLAMA_TIMEOUT)
234
  r.raise_for_status()
235
  return (r.json().get("message") or {}).get("content", "") or ""
236
+ except requests.Timeout:
237
+ st.error(f"โฑ๏ธ Ollama ํƒ€์ž„์•„์›ƒ({OLLAMA_TIMEOUT}s). host={OLLAMA_HOST}, model={model}")
238
+ except requests.ConnectionError as e:
239
+ st.error(f"๐Ÿ”Œ ์—ฐ๊ฒฐ ์‹คํŒจ: {e} (host={OLLAMA_HOST})")
240
+ except requests.HTTPError as e:
241
+ try:
242
+ detail = r.text[:500]
243
+ except Exception:
244
+ detail = str(e)
245
+ st.error(f"HTTP {r.status_code}: {detail}")
246
+ except requests.RequestException as e:
247
+ st.error(f"์š”์ฒญ ์˜ค๋ฅ˜: {e}")
248
+ return ""
249
 
250
  def _llm_structured_extract(user_text: str):
251
  out = _call_ollama_chat(
 
1707
  init_session()
1708
  chat_container = st.container()
1709
 
1710
+ if not _ollama_healthcheck():
1711
+ st.stop()
1712
+
1713
  # โœ… ํ’€์Šคํฌ๋ฆฐ์ผ ๋•Œ๋งŒ ์กฐ๊ธฐ ๋ฆฌํ„ด
1714
  if st.session_state.get("llm_mode") and not st.session_state.get("llm_inline", False):
1715
  render_llm_followup(chat_container, inline=False)