openfree commited on
Commit
ab3556d
ยท
verified ยท
1 Parent(s): dc92e59

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -12
app.py CHANGED
@@ -563,7 +563,7 @@ class NovelWritingSystem:
563
  return full_content
564
 
565
  def call_llm_streaming(self, messages: List[Dict[str, str]], role: str, language: str) -> Generator[str, None, None]:
566
- """LLM ์ŠคํŠธ๋ฆฌ๋ฐ ํ˜ธ์ถœ (๋ฐ˜๋ณต ๋ฐฉ์ง€ ํŒŒ๋ผ๋ฏธํ„ฐ ์ถ”๊ฐ€)"""
567
  try:
568
  system_prompts = self.get_system_prompts(language)
569
  full_messages = [{"role": "system", "content": system_prompts.get(role, "You are a helpful assistant.")}, *messages]
@@ -578,32 +578,82 @@ class NovelWritingSystem:
578
  "stream": True,
579
  "stream_options": {"include_usage": True}
580
  }
 
 
581
  response = requests.post(self.api_url, headers=self.create_headers(), json=payload, stream=True, timeout=180)
582
  response.raise_for_status()
 
583
  buffer = ""
 
 
584
  for line in response.iter_lines():
585
  if line:
586
  line_str = line.decode('utf-8')
587
  if line_str.startswith("data: "):
588
  data = line_str[6:]
589
- if data == "[DONE]": break
 
 
 
590
  try:
591
  chunk = json.loads(data)
592
- content = chunk.get("choices", [{}])[0].get("delta", {}).get("content", "")
593
- if content:
594
- buffer += content
595
- if "\n" in buffer or len(buffer) > 50:
596
- yield buffer
597
- buffer = ""
598
- except json.JSONDecodeError:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
599
  continue
600
- if buffer: yield buffer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
601
  except requests.exceptions.RequestException as e:
602
- logger.error(f"API ์ŠคํŠธ๋ฆฌ๋ฐ ์ค‘ ์˜ค๋ฅ˜: {e}")
603
  yield f"โŒ API ์˜ค๋ฅ˜: {e}"
604
  except Exception as e:
605
- logger.error(f"์ŠคํŠธ๋ฆฌ๋ฐ ์ฒ˜๋ฆฌ ์ค‘ ์˜ˆ์™ธ: {e}", exc_info=True)
606
  yield f"โŒ ์ฒ˜๋ฆฌ ์˜ค๋ฅ˜: {e}"
 
 
 
607
 
608
  def get_system_prompts(self, language: str) -> Dict[str, str]:
609
  """์—ญํ• ๋ณ„ ์‹œ์Šคํ…œ ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ"""
 
563
  return full_content
564
 
565
  def call_llm_streaming(self, messages: List[Dict[str, str]], role: str, language: str) -> Generator[str, None, None]:
566
+ """LLM ์ŠคํŠธ๋ฆฌ๋ฐ ํ˜ธ์ถœ (์•ˆ์ „ํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ)"""
567
  try:
568
  system_prompts = self.get_system_prompts(language)
569
  full_messages = [{"role": "system", "content": system_prompts.get(role, "You are a helpful assistant.")}, *messages]
 
578
  "stream": True,
579
  "stream_options": {"include_usage": True}
580
  }
581
+
582
+ logger.info(f"API ํ˜ธ์ถœ ์‹œ์ž‘ - ์—ญํ• : {role}")
583
  response = requests.post(self.api_url, headers=self.create_headers(), json=payload, stream=True, timeout=180)
584
  response.raise_for_status()
585
+
586
  buffer = ""
587
+ chunk_count = 0
588
+
589
  for line in response.iter_lines():
590
  if line:
591
  line_str = line.decode('utf-8')
592
  if line_str.startswith("data: "):
593
  data = line_str[6:]
594
+ if data == "[DONE]":
595
+ logger.info(f"์ŠคํŠธ๋ฆฌ๋ฐ ์™„๋ฃŒ - ์ด {chunk_count}๊ฐœ ์ฒญํฌ ์ฒ˜๋ฆฌ")
596
+ break
597
+
598
  try:
599
  chunk = json.loads(data)
600
+
601
+ # ๋””๋ฒ„๊น…์„ ์œ„ํ•ด ์ฒซ ๋ช‡ ๊ฐœ ์ฒญํฌ ๋กœ๊น…
602
+ if chunk_count < 3:
603
+ logger.debug(f"์ฒญํฌ {chunk_count}: {json.dumps(chunk, ensure_ascii=False)[:200]}")
604
+
605
+ # ์•ˆ์ „ํ•œ content ์ถ”์ถœ
606
+ choices = chunk.get("choices", [])
607
+ if choices and len(choices) > 0:
608
+ delta = choices[0].get("delta", {})
609
+ content = delta.get("content", "")
610
+
611
+ if content:
612
+ buffer += content
613
+ chunk_count += 1
614
+
615
+ # ๋ฒ„ํผ๊ฐ€ ์ถฉ๋ถ„ํžˆ ์ฐจ๊ฑฐ๋‚˜ ์ค„๋ฐ”๊ฟˆ์ด ์žˆ์„ ๋•Œ yield
616
+ if len(buffer) > 100 or "\n" in buffer:
617
+ yield buffer
618
+ buffer = ""
619
+
620
+ # ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ์ฒดํฌ
621
+ elif chunk.get("error"):
622
+ error_msg = chunk.get("error", {}).get("message", "Unknown error")
623
+ logger.error(f"API ์—๋Ÿฌ ์‘๋‹ต: {error_msg}")
624
+ yield f"โŒ API ์˜ค๋ฅ˜: {error_msg}"
625
+ return
626
+
627
+ except json.JSONDecodeError as e:
628
+ logger.warning(f"JSON ํŒŒ์‹ฑ ์˜ค๋ฅ˜: {e}, ๋ฐ์ดํ„ฐ: {data[:100]}")
629
  continue
630
+ except Exception as e:
631
+ logger.error(f"์ฒญํฌ ์ฒ˜๋ฆฌ ์ค‘ ์˜ˆ์™ธ: {e}")
632
+ continue
633
+
634
+ # ๋‚จ์€ ๋ฒ„ํผ ๋‚ด์šฉ ์ถœ๋ ฅ
635
+ if buffer:
636
+ yield buffer
637
+
638
+ # ์ฒญํฌ๊ฐ€ ํ•˜๋‚˜๋„ ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด
639
+ if chunk_count == 0:
640
+ logger.error("์‘๋‹ต์—์„œ ์ฝ˜ํ…์ธ ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
641
+ yield "โŒ API ์‘๋‹ต์— ์ฝ˜ํ…์ธ ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค."
642
+
643
+ except requests.exceptions.HTTPError as e:
644
+ logger.error(f"HTTP ์˜ค๋ฅ˜: {e}")
645
+ logger.error(f"์‘๋‹ต ์ƒํƒœ ์ฝ”๋“œ: {e.response.status_code if e.response else 'N/A'}")
646
+ logger.error(f"์‘๋‹ต ๋‚ด์šฉ: {e.response.text[:500] if e.response else 'N/A'}")
647
+ yield f"โŒ HTTP ์˜ค๋ฅ˜: {e}"
648
  except requests.exceptions.RequestException as e:
649
+ logger.error(f"API ์š”์ฒญ ์˜ค๋ฅ˜: {e}")
650
  yield f"โŒ API ์˜ค๋ฅ˜: {e}"
651
  except Exception as e:
652
+ logger.error(f"์˜ˆ๊ธฐ์น˜ ์•Š์€ ์˜ค๋ฅ˜: {e}", exc_info=True)
653
  yield f"โŒ ์ฒ˜๋ฆฌ ์˜ค๋ฅ˜: {e}"
654
+
655
+
656
+
657
 
658
  def get_system_prompts(self, language: str) -> Dict[str, str]:
659
  """์—ญํ• ๋ณ„ ์‹œ์Šคํ…œ ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ"""