Update app.py
Browse files
app.py
CHANGED
|
@@ -470,2320 +470,14 @@ class ResponseCleaner:
|
|
| 470 |
@staticmethod
|
| 471 |
def clean_response(response: str) -> str:
|
| 472 |
"""๋ถํ์ํ ๋งํฌ์
์ ๊ฑฐ ๊ฐํ"""
|
| 473 |
-
# ๋งํฌ๋ค์ด
|
| 474 |
-
|
| 475 |
-
response = re.sub(r'^\|.*\|.*
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
# ============================================================================
|
| 479 |
-
# ํตํฉ ์ต์ ํ ๋ฉํฐ ์์ด์ ํธ ์์คํ
(์บ์ฑ ์ ๊ฑฐ ๋ฒ์ )
|
| 480 |
-
# ============================================================================
|
| 481 |
-
|
| 482 |
-
class SpeedOptimizedMultiAgentSystem:
|
| 483 |
-
"""์๋ ์ต์ ํ๋ ๋ฉํฐ ์์ด์ ํธ ์์คํ
(์บ์ฑ ์์)"""
|
| 484 |
-
|
| 485 |
-
def __init__(self):
|
| 486 |
-
self.llm = OptimizedFireworksClient()
|
| 487 |
-
self.search = AsyncBraveSearch()
|
| 488 |
-
self.reasoning = LightweightReasoningChain()
|
| 489 |
-
self.quality_checker = QualityChecker()
|
| 490 |
-
self.streaming = OptimizedStreaming()
|
| 491 |
-
self.language_detector = LanguageDetector()
|
| 492 |
-
self.response_cleaner = ResponseCleaner()
|
| 493 |
-
|
| 494 |
-
# ๋ณ๋ ฌ ์ฒ๋ฆฌ ํ
|
| 495 |
-
self.executor = ThreadPoolExecutor(max_workers=4)
|
| 496 |
-
|
| 497 |
-
def _init_compact_prompts(self, lang: str = 'ko') -> Dict:
|
| 498 |
-
"""์์ถ๋ ๊ณ ํจ์จ ํ๋กฌํํธ (์ธ์ด๋ณ)"""
|
| 499 |
-
prompts = {
|
| 500 |
-
'ko': {
|
| 501 |
-
AgentRole.SUPERVISOR: """[๊ฐ๋
์-๊ตฌ์กฐ์ค๊ณ]
|
| 502 |
-
์ฆ์๋ถ์: ํต์ฌ์๋+ํ์์ ๋ณด+๋ต๋ณ๊ตฌ์กฐ
|
| 503 |
-
์ถ๋ ฅ: 5๊ฐ ํต์ฌํฌ์ธํธ(๊ฐ 1๋ฌธ์ฅ)
|
| 504 |
-
์ถ๋ก ์ฒด๊ณ ๋ช
์""",
|
| 505 |
-
|
| 506 |
-
AgentRole.CREATIVE: """[์ฐฝ์์ฑ์์ฑ์]
|
| 507 |
-
์
๋ ฅ๊ตฌ์กฐ ๋ฐ๋ผ ์ฐฝ์์ ํ์ฅ
|
| 508 |
-
์ค์ฉ์์+ํ์ ์ ๊ทผ+๊ตฌ์ฒด์กฐ์ธ
|
| 509 |
-
๋ถํ์์ค๋ช
์ ๊ฑฐ""",
|
| 510 |
-
|
| 511 |
-
AgentRole.CRITIC: """[๋นํ์-๊ฒ์ฆ]
|
| 512 |
-
์ ์๊ฒํ : ์ ํ์ฑ/๋
ผ๋ฆฌ์ฑ/์ค์ฉ์ฑ
|
| 513 |
-
๊ฐ์ ํฌ์ธํธ 3๊ฐ๋ง
|
| 514 |
-
๊ฐ 2๋ฌธ์ฅ ์ด๋ด""",
|
| 515 |
-
|
| 516 |
-
AgentRole.FINALIZER: """[์ต์ข
ํตํฉ]
|
| 517 |
-
๋ชจ๋ ์๊ฒฌ ์ข
ํฉโ์ต์ ๋ต๋ณ
|
| 518 |
-
๋ช
ํ๊ตฌ์กฐ+์ค์ฉ์ ๋ณด+์ฐฝ์๊ท ํ
|
| 519 |
-
๋ฐ๋ก ํต์ฌ ๋ด์ฉ๋ถํฐ ์์. ๋ถํ์ํ ํค๋๋ ๋งํฌ์
์์ด. ๋งํฌ๋ค์ด ํค๋(#, ##, ###) ์ฌ์ฉ ๊ธ์ง."""
|
| 520 |
-
},
|
| 521 |
-
'en': {
|
| 522 |
-
AgentRole.SUPERVISOR: """[Supervisor-Structure]
|
| 523 |
-
Immediate analysis: core intent+required info+answer structure
|
| 524 |
-
Output: 5 key points (1 sentence each)
|
| 525 |
-
Clear reasoning framework""",
|
| 526 |
-
|
| 527 |
-
AgentRole.CREATIVE: """[Creative Generator]
|
| 528 |
-
Follow structure, expand creatively
|
| 529 |
-
Practical examples+innovative approach+specific advice
|
| 530 |
-
Remove unnecessary explanations""",
|
| 531 |
-
|
| 532 |
-
AgentRole.CRITIC: """[Critic-Verification]
|
| 533 |
-
Quick review: accuracy/logic/practicality
|
| 534 |
-
Only 3 improvement points
|
| 535 |
-
Max 2 sentences each""",
|
| 536 |
-
|
| 537 |
-
AgentRole.FINALIZER: """[Final Integration]
|
| 538 |
-
Synthesize all inputsโoptimal answer
|
| 539 |
-
Clear structure+practical info+creative balance
|
| 540 |
-
Start with core content directly. No unnecessary headers or markup. No markdown headers (#, ##, ###)."""
|
| 541 |
-
},
|
| 542 |
-
'ja': {
|
| 543 |
-
AgentRole.SUPERVISOR: """[็ฃ็ฃ่
-ๆง้ ่จญ่จ]
|
| 544 |
-
ๅณๆๅๆ๏ผๆ ธๅฟๆๅณ+ๅฟ
่ฆๆ
ๅ ฑ+ๅ็ญๆง้
|
| 545 |
-
ๅบๅ๏ผ5ใคใฎๆ ธๅฟใใคใณใ๏ผๅ1ๆ๏ผ
|
| 546 |
-
ๆจ่ซไฝ็ณปๆ็คบ""",
|
| 547 |
-
|
| 548 |
-
AgentRole.CREATIVE: """[ๅต้ ๆง็ๆ่
]
|
| 549 |
-
ๅ
ฅๅๆง้ ใซๅพใฃใฆๅต้ ็ๆกๅผต
|
| 550 |
-
ๅฎ็จไพ+้ฉๆฐ็ใขใใญใผใ+ๅ
ทไฝ็ใขใใใคใน
|
| 551 |
-
ไธ่ฆใช่ชฌๆๅ้ค""",
|
| 552 |
-
|
| 553 |
-
AgentRole.CRITIC: """[ๆน่ฉ่
-ๆค่จผ]
|
| 554 |
-
่ฟ
้ใฌใใฅใผ๏ผๆญฃ็ขบๆง/่ซ็ๆง/ๅฎ็จๆง
|
| 555 |
-
ๆนๅใใคใณใ3ใคใฎใฟ
|
| 556 |
-
ๅ2ๆไปฅๅ
""",
|
| 557 |
-
|
| 558 |
-
AgentRole.FINALIZER: """[ๆ็ต็ตฑๅ]
|
| 559 |
-
ๅ
จๆ่ฆ็ตฑๅโๆ้ฉๅ็ญ
|
| 560 |
-
ๆ็ขบๆง้ +ๅฎ็จๆ
ๅ ฑ+ๅต้ ๆงใใฉใณใน
|
| 561 |
-
ๆ ธๅฟๅ
ๅฎนใใ็ดๆฅ้ๅงใไธ่ฆใชใใใใผใใใผใฏใขใใใชใใใใผใฏใใฆใณใใใใผ๏ผ#ใ##ใ###๏ผไฝฟ็จ็ฆๆญขใ"""
|
| 562 |
-
},
|
| 563 |
-
'zh': {
|
| 564 |
-
AgentRole.SUPERVISOR: """[ไธป็ฎก-็ปๆ่ฎพ่ฎก]
|
| 565 |
-
็ซๅณๅๆ๏ผๆ ธๅฟๆๅพ+ๆ้ไฟกๆฏ+็ญๆก็ปๆ
|
| 566 |
-
่พๅบ๏ผ5ไธชๆ ธๅฟ่ฆ็น๏ผๆฏไธช1ๅฅ๏ผ
|
| 567 |
-
ๆจ็ไฝ็ณปๆ็กฎ""",
|
| 568 |
-
|
| 569 |
-
AgentRole.CREATIVE: """[ๅๆ็ๆๅจ]
|
| 570 |
-
ๆ็ปๆๅ้ ๆงๆฉๅฑ
|
| 571 |
-
ๅฎ็จ็คบไพ+ๅๆฐๆนๆณ+ๅ
ทไฝๅปบ่ฎฎ
|
| 572 |
-
ๅ ้คไธๅฟ
่ฆ็่งฃ้""",
|
| 573 |
-
|
| 574 |
-
AgentRole.CRITIC: """[่ฏ่ฎบๅฎถ-้ช่ฏ]
|
| 575 |
-
ๅฟซ้ๅฎกๆฅ๏ผๅ็กฎๆง/้ป่พๆง/ๅฎ็จๆง
|
| 576 |
-
ไป
3ไธชๆน่ฟ็น
|
| 577 |
-
ๆฏไธชๆๅค2ๅฅ""",
|
| 578 |
-
|
| 579 |
-
AgentRole.FINALIZER: """[ๆ็ปๆดๅ]
|
| 580 |
-
็ปผๅๆๆๆ่งโๆไฝณ็ญๆก
|
| 581 |
-
ๆธ
ๆฐ็ปๆ+ๅฎ็จไฟกๆฏ+ๅๆๅนณ่กก
|
| 582 |
-
็ดๆฅไปๆ ธๅฟๅ
ๅฎนๅผๅงใๆ ้ไธๅฟ
่ฆ็ๆ ้ขๆๆ ่ฎฐใ็ฆๆญขไฝฟ็จMarkdownๆ ้ข๏ผ#ใ##ใ###๏ผใ"""
|
| 583 |
-
}
|
| 584 |
-
}
|
| 585 |
-
|
| 586 |
-
return prompts.get(lang, prompts['en'])
|
| 587 |
-
|
| 588 |
-
async def parallel_process_agents(
|
| 589 |
-
self,
|
| 590 |
-
query: str,
|
| 591 |
-
search_results: List[Dict],
|
| 592 |
-
show_progress: bool = True,
|
| 593 |
-
lang: str = None
|
| 594 |
-
) -> AsyncGenerator[Tuple[str, str], None]:
|
| 595 |
-
"""๋ณ๋ ฌ ์ฒ๋ฆฌ ํ์ดํ๋ผ์ธ (์บ์ฑ ์์)"""
|
| 596 |
-
|
| 597 |
-
start_time = time.time()
|
| 598 |
-
|
| 599 |
-
# ์ธ์ด ์๋ ๊ฐ์ง
|
| 600 |
-
if lang is None:
|
| 601 |
-
lang = self.language_detector.detect_language(query)
|
| 602 |
-
|
| 603 |
-
# ์ธ์ด๋ณ ํ๋กฌํํธ ์ค์
|
| 604 |
-
self.compact_prompts = self._init_compact_prompts(lang)
|
| 605 |
-
|
| 606 |
-
search_context = self._format_search_results(search_results)
|
| 607 |
-
accumulated_response = ""
|
| 608 |
-
agent_thoughts = ""
|
| 609 |
-
|
| 610 |
-
# ์ถ๋ก ํจํด ๊ฒฐ์
|
| 611 |
-
reasoning_pattern = self.reasoning.get_reasoning_pattern(query, lang)
|
| 612 |
-
|
| 613 |
-
try:
|
| 614 |
-
# === 1๋จ๊ณ: ๊ฐ๋
์ + ๊ฒ์ ๋ณ๋ ฌ ์คํ ===
|
| 615 |
-
if show_progress:
|
| 616 |
-
progress_msg = {
|
| 617 |
-
'ko': "๐ ๋ณ๋ ฌ ์ฒ๋ฆฌ ์์\n๐ ๊ฐ๋
์ ๋ถ์ + ๐ ์ถ๊ฐ ๊ฒ์ ๋์ ์งํ...\n\n",
|
| 618 |
-
'en': "๐ Starting parallel processing\n๐ Supervisor analysis + ๐ Additional search in progress...\n\n",
|
| 619 |
-
'ja': "๐ ไธฆๅๅฆ็้ๅง\n๐ ็ฃ็ฃ่
ๅๆ + ๐ ่ฟฝๅ ๆค็ดขๅๆ้ฒ่กไธญ...\n\n",
|
| 620 |
-
'zh': "๐ ๅผๅงๅนถ่กๅค็\n๐ ไธป็ฎกๅๆ + ๐ ้ๅ ๆ็ดขๅๆถ่ฟ่ก...\n\n"
|
| 621 |
-
}
|
| 622 |
-
agent_thoughts = progress_msg.get(lang, progress_msg['en'])
|
| 623 |
-
yield accumulated_response, agent_thoughts
|
| 624 |
-
|
| 625 |
-
# ๊ฐ๋
์ ํ๋กฌํํธ (์ธ์ด๋ณ)
|
| 626 |
-
supervisor_prompt_templates = {
|
| 627 |
-
'ko': f"""
|
| 628 |
-
์ง๋ฌธ: {query}
|
| 629 |
-
๊ฒ์๊ฒฐ๊ณผ: {search_context}
|
| 630 |
-
์ถ๋ก ํจํด: {reasoning_pattern}
|
| 631 |
-
์ฆ์ ํต์ฌ๊ตฌ์กฐ 5๊ฐ ์ ์""",
|
| 632 |
-
'en': f"""
|
| 633 |
-
Question: {query}
|
| 634 |
-
Search results: {search_context}
|
| 635 |
-
Reasoning pattern: {reasoning_pattern}
|
| 636 |
-
Immediately provide 5 key structures""",
|
| 637 |
-
'ja': f"""
|
| 638 |
-
่ณชๅ: {query}
|
| 639 |
-
ๆค็ดข็ตๆ: {search_context}
|
| 640 |
-
ๆจ่ซใใฟใผใณ: {reasoning_pattern}
|
| 641 |
-
ๅณๅบงใซ5ใคใฎๆ ธๅฟๆง้ ใๆ็คบ""",
|
| 642 |
-
'zh': f"""
|
| 643 |
-
้ฎ้ข: {query}
|
| 644 |
-
ๆ็ดข็ปๆ: {search_context}
|
| 645 |
-
ๆจ็ๆจกๅผ: {reasoning_pattern}
|
| 646 |
-
็ซๅณๆไพ5ไธชๆ ธๅฟ็ปๆ"""
|
| 647 |
-
}
|
| 648 |
-
|
| 649 |
-
supervisor_prompt = supervisor_prompt_templates.get(lang, supervisor_prompt_templates['en'])
|
| 650 |
-
|
| 651 |
-
supervisor_response = ""
|
| 652 |
-
supervisor_task = self.llm.chat_stream_async(
|
| 653 |
-
messages=[
|
| 654 |
-
{"role": "system", "content": self.compact_prompts[AgentRole.SUPERVISOR]},
|
| 655 |
-
{"role": "user", "content": supervisor_prompt}
|
| 656 |
-
],
|
| 657 |
-
temperature=0.3,
|
| 658 |
-
max_tokens=500
|
| 659 |
-
)
|
| 660 |
-
|
| 661 |
-
# ๊ฐ๋
์ ์คํธ๋ฆฌ๋ฐ (๋ฒํผ๋ง)
|
| 662 |
-
async for chunk in self.streaming.buffer_and_yield(supervisor_task):
|
| 663 |
-
supervisor_response += chunk
|
| 664 |
-
if show_progress and len(supervisor_response) < 300:
|
| 665 |
-
supervisor_label = {
|
| 666 |
-
'ko': "๐ ๊ฐ๋
์ ๋ถ์",
|
| 667 |
-
'en': "๐ Supervisor Analysis",
|
| 668 |
-
'ja': "๐ ็ฃ็ฃ่
ๅๆ",
|
| 669 |
-
'zh': "๐ ไธป็ฎกๅๆ"
|
| 670 |
-
}
|
| 671 |
-
agent_thoughts = f"{supervisor_label.get(lang, supervisor_label['en'])}\n{supervisor_response[:300]}...\n\n"
|
| 672 |
-
yield accumulated_response, agent_thoughts
|
| 673 |
-
|
| 674 |
-
# === 2๋จ๊ณ: ์ฐฝ์์ฑ + ๋นํ ์ค๋น ๋ณ๋ ฌ ===
|
| 675 |
-
if show_progress:
|
| 676 |
-
creative_msg = {
|
| 677 |
-
'ko': "๐จ ์ฐฝ์์ฑ ์์ฑ์ + ๐ ๋นํ์ ์ค๋น...\n\n",
|
| 678 |
-
'en': "๐จ Creative Generator + ๐ Critic preparing...\n\n",
|
| 679 |
-
'ja': "๐จ ๅต้ ๆง็ๆ่
+ ๐ ๆน่ฉ่
ๆบๅไธญ...\n\n",
|
| 680 |
-
'zh': "๐จ ๅๆ็ๆๅจ + ๐ ่ฏ่ฎบๅฎถๅๅคไธญ...\n\n"
|
| 681 |
-
}
|
| 682 |
-
agent_thoughts += creative_msg.get(lang, creative_msg['en'])
|
| 683 |
-
yield accumulated_response, agent_thoughts
|
| 684 |
-
|
| 685 |
-
# ์ฐฝ์์ฑ ์์ฑ ์์ (์ธ์ด๋ณ)
|
| 686 |
-
creative_prompt_templates = {
|
| 687 |
-
'ko': f"""
|
| 688 |
-
์ง๋ฌธ: {query}
|
| 689 |
-
๊ฐ๋
์๊ตฌ์กฐ: {supervisor_response}
|
| 690 |
-
๊ฒ์๊ฒฐ๊ณผ: {search_context}
|
| 691 |
-
์ฐฝ์์ +์ค์ฉ์ ๋ต๋ณ ์ฆ์์์ฑ""",
|
| 692 |
-
'en': f"""
|
| 693 |
-
Question: {query}
|
| 694 |
-
Supervisor structure: {supervisor_response}
|
| 695 |
-
Search results: {search_context}
|
| 696 |
-
Generate creative+practical answer immediately""",
|
| 697 |
-
'ja': f"""
|
| 698 |
-
่ณชๅ: {query}
|
| 699 |
-
็ฃ็ฃ่
ๆง้ : {supervisor_response}
|
| 700 |
-
ๆค็ดข็ตๆ: {search_context}
|
| 701 |
-
ๅต้ ็+ๅฎ็จ็ๅ็ญๅณๅบง็ๆ""",
|
| 702 |
-
'zh': f"""
|
| 703 |
-
้ฎ้ข: {query}
|
| 704 |
-
ไธป็ฎก็ปๆ: {supervisor_response}
|
| 705 |
-
ๆ็ดข็ปๆ: {search_context}
|
| 706 |
-
็ซๅณ็ๆๅๆ+ๅฎ็จ็ญๆก"""
|
| 707 |
-
}
|
| 708 |
-
|
| 709 |
-
creative_prompt = creative_prompt_templates.get(lang, creative_prompt_templates['en'])
|
| 710 |
-
|
| 711 |
-
creative_response = ""
|
| 712 |
-
creative_partial = ""
|
| 713 |
-
critic_started = False
|
| 714 |
-
critic_response = ""
|
| 715 |
-
|
| 716 |
-
creative_task = self.llm.chat_stream_async(
|
| 717 |
-
messages=[
|
| 718 |
-
{"role": "system", "content": self.compact_prompts[AgentRole.CREATIVE]},
|
| 719 |
-
{"role": "user", "content": creative_prompt}
|
| 720 |
-
],
|
| 721 |
-
temperature=0.8,
|
| 722 |
-
max_tokens=1500
|
| 723 |
-
)
|
| 724 |
-
|
| 725 |
-
# ์ฐฝ์์ฑ ์คํธ๋ฆฌ๋ฐ + ๋นํ์ ์กฐ๊ธฐ ์์
|
| 726 |
-
async for chunk in self.streaming.buffer_and_yield(creative_task):
|
| 727 |
-
creative_response += chunk
|
| 728 |
-
creative_partial += chunk
|
| 729 |
-
|
| 730 |
-
# ์ฐฝ์์ฑ ์๋ต์ด 500์ ๋์ผ๋ฉด ๋นํ์ ์์
|
| 731 |
-
if len(creative_partial) > 500 and not critic_started:
|
| 732 |
-
critic_started = True
|
| 733 |
-
|
| 734 |
-
# ๋นํ์ ๋น๋๊ธฐ ์์ (์ธ์ด๋ณ)
|
| 735 |
-
critic_prompt_templates = {
|
| 736 |
-
'ko': f"""
|
| 737 |
-
์๋ณธ์ง๋ฌธ: {query}
|
| 738 |
-
์ฐฝ์์ฑ๋ต๋ณ(์ผ๋ถ): {creative_partial}
|
| 739 |
-
์ ์๊ฒํ โ๊ฐ์ ์ 3๊ฐ""",
|
| 740 |
-
'en': f"""
|
| 741 |
-
Original question: {query}
|
| 742 |
-
Creative answer (partial): {creative_partial}
|
| 743 |
-
Quick reviewโ3 improvements""",
|
| 744 |
-
'ja': f"""
|
| 745 |
-
ๅ
ใฎ่ณชๅ: {query}
|
| 746 |
-
ๅต้ ็ๅ็ญ๏ผไธ้จ๏ผ: {creative_partial}
|
| 747 |
-
่ฟ
้ใฌใใฅใผโๆนๅ็น3ใค""",
|
| 748 |
-
'zh': f"""
|
| 749 |
-
ๅๅง้ฎ้ข: {query}
|
| 750 |
-
ๅๆ็ญๆก๏ผ้จๅ๏ผ: {creative_partial}
|
| 751 |
-
ๅฟซ้ๅฎกๆฅโ3ไธชๆน่ฟ็น"""
|
| 752 |
-
}
|
| 753 |
-
|
| 754 |
-
critic_prompt = critic_prompt_templates.get(lang, critic_prompt_templates['en'])
|
| 755 |
-
|
| 756 |
-
critic_task = asyncio.create_task(
|
| 757 |
-
self._run_critic_async(critic_prompt)
|
| 758 |
-
)
|
| 759 |
-
|
| 760 |
-
if show_progress:
|
| 761 |
-
display_creative = creative_response[:400] + "..." if len(creative_response) > 400 else creative_response
|
| 762 |
-
creative_label = {
|
| 763 |
-
'ko': "๐จ ์ฐฝ์์ฑ ์์ฑ์",
|
| 764 |
-
'en': "๐จ Creative Generator",
|
| 765 |
-
'ja': "๐จ ๅต้ ๆง็ๆ่
",
|
| 766 |
-
'zh': "๐จ ๅๆ็ๆๅจ"
|
| 767 |
-
}
|
| 768 |
-
agent_thoughts = f"{creative_label.get(lang, creative_label['en'])}\n{display_creative}\n\n"
|
| 769 |
-
yield accumulated_response, agent_thoughts
|
| 770 |
-
|
| 771 |
-
# ๋นํ์ ๊ฒฐ๊ณผ ๋๊ธฐ
|
| 772 |
-
if critic_started:
|
| 773 |
-
critic_response = await critic_task
|
| 774 |
-
|
| 775 |
-
if show_progress:
|
| 776 |
-
critic_label = {
|
| 777 |
-
'ko': "๐ ๋นํ์ ๊ฒํ ",
|
| 778 |
-
'en': "๐ Critic Review",
|
| 779 |
-
'ja': "๐ ๆน่ฉ่
ใฌใใฅใผ",
|
| 780 |
-
'zh': "๐ ่ฏ่ฎบๅฎถๅฎกๆฅ"
|
| 781 |
-
}
|
| 782 |
-
agent_thoughts += f"{critic_label.get(lang, critic_label['en'])}\n{critic_response[:200]}...\n\n"
|
| 783 |
-
yield accumulated_response, agent_thoughts
|
| 784 |
-
|
| 785 |
-
# === 3๋จ๊ณ: ํ์ง ์ฒดํฌ ๋ฐ ์กฐ๊ธฐ ์ข
๋ฃ ===
|
| 786 |
-
quality_score, need_more = self.quality_checker.evaluate_response(
|
| 787 |
-
creative_response, query, lang
|
| 788 |
-
)
|
| 789 |
-
|
| 790 |
-
if not need_more and quality_score > 0.85:
|
| 791 |
-
# ํ์ง์ด ์ถฉ๋ถํ ๋์ผ๋ฉด ๋ฐ๋ก ๋ฐํ
|
| 792 |
-
accumulated_response = self.response_cleaner.clean_response(creative_response)
|
| 793 |
-
|
| 794 |
-
if show_progress:
|
| 795 |
-
quality_msg = {
|
| 796 |
-
'ko': f"โ
ํ์ง ์ถฉ์กฑ (์ ์: {quality_score:.2f})\n์กฐ๊ธฐ ์๋ฃ!\n",
|
| 797 |
-
'en': f"โ
Quality met (score: {quality_score:.2f})\nEarly completion!\n",
|
| 798 |
-
'ja': f"โ
ๅ่ณชๆบ่ถณ (ในใณใข: {quality_score:.2f})\nๆฉๆๅฎไบ!\n",
|
| 799 |
-
'zh': f"โ
่ดจ้ๆปก่ถณ (ๅๆฐ: {quality_score:.2f})\nๆๅๅฎๆ!\n"
|
| 800 |
-
}
|
| 801 |
-
agent_thoughts += quality_msg.get(lang, quality_msg['en'])
|
| 802 |
-
|
| 803 |
-
yield accumulated_response, agent_thoughts
|
| 804 |
-
return
|
| 805 |
-
|
| 806 |
-
# === 4๋จ๊ณ: ์ต์ข
ํตํฉ (์คํธ๋ฆฌ๋ฐ) ===
|
| 807 |
-
if show_progress:
|
| 808 |
-
final_msg = {
|
| 809 |
-
'ko': "โ
์ต์ข
ํตํฉ ์ค...\n\n",
|
| 810 |
-
'en': "โ
Final integration in progress...\n\n",
|
| 811 |
-
'ja': "โ
ๆ็ต็ตฑๅไธญ...\n\n",
|
| 812 |
-
'zh': "โ
ๆ็ปๆดๅไธญ...\n\n"
|
| 813 |
-
}
|
| 814 |
-
agent_thoughts += final_msg.get(lang, final_msg['en'])
|
| 815 |
-
yield accumulated_response, agent_thoughts
|
| 816 |
-
|
| 817 |
-
# ์ต์ข
ํ๋กฌํํธ (์ธ์ด๋ณ)
|
| 818 |
-
final_prompt_templates = {
|
| 819 |
-
'ko': f"""
|
| 820 |
-
์ง๋ฌธ: {query}
|
| 821 |
-
์ฐฝ์์ฑ๋ต๋ณ: {creative_response}
|
| 822 |
-
๋นํํผ๋๋ฐฑ: {critic_response}
|
| 823 |
-
๊ฐ๋
์๊ตฌ์กฐ: {supervisor_response}
|
| 824 |
-
์ต์ข
ํตํฉโ์๋ฒฝ๋ต๋ณ. ๋งํฌ๋ค์ด ํค๋(#, ##, ###) ์ฌ์ฉ ๊ธ์ง.""",
|
| 825 |
-
'en': f"""
|
| 826 |
-
Question: {query}
|
| 827 |
-
Creative answer: {creative_response}
|
| 828 |
-
Critic feedback: {critic_response}
|
| 829 |
-
Supervisor structure: {supervisor_response}
|
| 830 |
-
Final integrationโperfect answer. No markdown headers (#, ##, ###).""",
|
| 831 |
-
'ja': f"""
|
| 832 |
-
่ณชๅ: {query}
|
| 833 |
-
ๅต้ ็ๅ็ญ: {creative_response}
|
| 834 |
-
ๆน่ฉใใฃใผใใใใฏ: {critic_response}
|
| 835 |
-
็ฃ็ฃ่
ๆง้ : {supervisor_response}
|
| 836 |
-
ๆ็ต็ตฑๅโๅฎ็งใชๅ็ญใใใผใฏใใฆใณใใใใผ๏ผ#ใ##ใ###๏ผไฝฟ็จ็ฆๆญขใ""",
|
| 837 |
-
'zh': f"""
|
| 838 |
-
้ฎ้ข: {query}
|
| 839 |
-
ๅๆ็ญๆก: {creative_response}
|
| 840 |
-
่ฏ่ฎบๅ้ฆ: {critic_response}
|
| 841 |
-
ไธป็ฎก็ปๆ: {supervisor_response}
|
| 842 |
-
ๆ็ปๆดๅโๅฎ็พ็ญๆกใ็ฆๆญขไฝฟ็จMarkdownๆ ้ข๏ผ#ใ##ใ###๏ผใ"""
|
| 843 |
-
}
|
| 844 |
-
|
| 845 |
-
final_prompt = final_prompt_templates.get(lang, final_prompt_templates['en'])
|
| 846 |
-
|
| 847 |
-
final_task = self.llm.chat_stream_async(
|
| 848 |
-
messages=[
|
| 849 |
-
{"role": "system", "content": self.compact_prompts[AgentRole.FINALIZER]},
|
| 850 |
-
{"role": "user", "content": final_prompt}
|
| 851 |
-
],
|
| 852 |
-
temperature=0.5,
|
| 853 |
-
max_tokens=2500
|
| 854 |
-
)
|
| 855 |
-
|
| 856 |
-
# ์ต์ข
๋ต๋ณ ์คํธ๋ฆฌ๋ฐ
|
| 857 |
-
accumulated_response = ""
|
| 858 |
-
|
| 859 |
-
async for chunk in final_task:
|
| 860 |
-
accumulated_response += chunk
|
| 861 |
-
# ์ค์๊ฐ ์ ๋ฆฌ
|
| 862 |
-
cleaned_response = self.response_cleaner.clean_response(accumulated_response)
|
| 863 |
-
yield cleaned_response, agent_thoughts
|
| 864 |
-
|
| 865 |
-
# ์ต์ข
์ ๋ฆฌ
|
| 866 |
-
accumulated_response = self.response_cleaner.clean_response(accumulated_response)
|
| 867 |
-
|
| 868 |
-
# ์ฒ๋ฆฌ ์๊ฐ ์ถ๊ฐ (์ธ์ด๋ณ)
|
| 869 |
-
processing_time = time.time() - start_time
|
| 870 |
-
time_msg = {
|
| 871 |
-
'ko': f"\n\n---\nโก ์ฒ๋ฆฌ ์๊ฐ: {processing_time:.1f}์ด",
|
| 872 |
-
'en': f"\n\n---\nโก Processing time: {processing_time:.1f} seconds",
|
| 873 |
-
'ja': f"\n\n---\nโก ๅฆ็ๆ้: {processing_time:.1f}็ง",
|
| 874 |
-
'zh': f"\n\n---\nโก ๅค็ๆถ้ด: {processing_time:.1f}็ง"
|
| 875 |
-
}
|
| 876 |
-
accumulated_response += time_msg.get(lang, time_msg['en'])
|
| 877 |
-
|
| 878 |
-
yield accumulated_response, agent_thoughts
|
| 879 |
-
|
| 880 |
-
except Exception as e:
|
| 881 |
-
error_msg = {
|
| 882 |
-
'ko': f"โ ์ค๋ฅ ๋ฐ์: {str(e)}",
|
| 883 |
-
'en': f"โ Error occurred: {str(e)}",
|
| 884 |
-
'ja': f"โ ใจใฉใผ็บ็: {str(e)}",
|
| 885 |
-
'zh': f"โ ๅ็้่ฏฏ: {str(e)}"
|
| 886 |
-
}
|
| 887 |
-
yield error_msg.get(lang, error_msg['en']), agent_thoughts
|
| 888 |
-
|
| 889 |
-
async def _run_critic_async(self, prompt: str) -> str:
|
| 890 |
-
"""๋นํ์ ๋น๋๊ธฐ ์คํ with error handling"""
|
| 891 |
-
try:
|
| 892 |
-
response = ""
|
| 893 |
-
async for chunk in self.llm.chat_stream_async(
|
| 894 |
-
messages=[
|
| 895 |
-
{"role": "system", "content": self.compact_prompts[AgentRole.CRITIC]},
|
| 896 |
-
{"role": "user", "content": prompt}
|
| 897 |
-
],
|
| 898 |
-
temperature=0.2,
|
| 899 |
-
max_tokens=500
|
| 900 |
-
):
|
| 901 |
-
response += chunk
|
| 902 |
-
return response
|
| 903 |
-
except Exception as e:
|
| 904 |
-
# ์ธ์ด ๊ฐ์ง
|
| 905 |
-
lang = 'ko' if '์ง๋ฌธ' in prompt else 'en'
|
| 906 |
-
error_msg = {
|
| 907 |
-
'ko': "๋นํ ์ฒ๋ฆฌ ์ค ์ค๋ฅ",
|
| 908 |
-
'en': "Error during critic processing",
|
| 909 |
-
'ja': "ๆน่ฉๅฆ็ไธญใฎใจใฉใผ",
|
| 910 |
-
'zh': "่ฏ่ฎบๅค็ไธญๅบ้"
|
| 911 |
-
}
|
| 912 |
-
return error_msg.get(lang, error_msg['en'])
|
| 913 |
-
|
| 914 |
-
def _format_search_results(self, results: List[Dict]) -> str:
|
| 915 |
-
"""๊ฒ์ ๊ฒฐ๊ณผ ์์ถ ํฌ๋งท"""
|
| 916 |
-
if not results:
|
| 917 |
-
return "No search results"
|
| 918 |
-
|
| 919 |
-
formatted = []
|
| 920 |
-
for i, r in enumerate(results[:3], 1):
|
| 921 |
-
title = r.get('title', '')[:50]
|
| 922 |
-
desc = r.get('description', '')[:100]
|
| 923 |
-
formatted.append(f"[{i}]{title}:{desc}")
|
| 924 |
-
|
| 925 |
-
return " | ".join(formatted)
|
| 926 |
-
|
| 927 |
-
|
| 928 |
-
# ============================================================================
|
| 929 |
-
# Gradio UI (์ต์ ํ ๋ฒ์ - ์บ์ฑ ์ ๊ฑฐ)
|
| 930 |
-
# ============================================================================
|
| 931 |
-
|
| 932 |
-
def create_optimized_gradio_interface():
|
| 933 |
-
"""์ต์ ํ๋ Gradio ์ธํฐํ์ด์ค (์บ์ฑ ์์)"""
|
| 934 |
-
|
| 935 |
-
# ์์คํ
์ด๊ธฐํ
|
| 936 |
-
system = SpeedOptimizedMultiAgentSystem()
|
| 937 |
-
|
| 938 |
-
def process_query_optimized(
|
| 939 |
-
message: str,
|
| 940 |
-
history: List[Dict],
|
| 941 |
-
use_search: bool,
|
| 942 |
-
show_agent_thoughts: bool,
|
| 943 |
-
search_count: int,
|
| 944 |
-
language_mode: str
|
| 945 |
-
):
|
| 946 |
-
"""์ต์ ํ๋ ์ฟผ๋ฆฌ ์ฒ๋ฆฌ - ์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ ๋ฒ์ """
|
| 947 |
-
|
| 948 |
-
if not message:
|
| 949 |
-
yield history, "", ""
|
| 950 |
-
return
|
| 951 |
-
|
| 952 |
-
# ์ธ์ด ์ค์
|
| 953 |
-
if language_mode == "Auto":
|
| 954 |
-
lang = None # ์๋ ๊ฐ์ง
|
| 955 |
-
else:
|
| 956 |
-
lang_map = {"Korean": "ko", "English": "en", "Japanese": "ja", "Chinese": "zh"}
|
| 957 |
-
lang = lang_map.get(language_mode, None)
|
| 958 |
-
|
| 959 |
-
# ๋น๋๊ธฐ ํจ์๋ฅผ ๋๊ธฐ์ ์ผ๋ก ์คํ
|
| 960 |
-
try:
|
| 961 |
-
import nest_asyncio
|
| 962 |
-
nest_asyncio.apply()
|
| 963 |
-
except ImportError:
|
| 964 |
-
pass
|
| 965 |
-
|
| 966 |
-
try:
|
| 967 |
-
# ๊ฒ์ ์ํ (๋๊ธฐํ)
|
| 968 |
-
search_results = []
|
| 969 |
-
search_display = ""
|
| 970 |
-
|
| 971 |
-
# ์ธ์ด ์๋ ๊ฐ์ง (ํ์ํ ๊ฒฝ์ฐ)
|
| 972 |
-
detected_lang = lang or system.language_detector.detect_language(message)
|
| 973 |
-
|
| 974 |
-
if use_search:
|
| 975 |
-
# ๊ฒ์ ์ํ ํ์
|
| 976 |
-
processing_msg = {
|
| 977 |
-
'ko': "โก ๊ณ ์ ์ฒ๋ฆฌ ์ค...",
|
| 978 |
-
'en': "โก High-speed processing...",
|
| 979 |
-
'ja': "โก ้ซ้ๅฆ็ไธญ...",
|
| 980 |
-
'zh': "โก ้ซ้ๅค็ไธญ..."
|
| 981 |
-
}
|
| 982 |
-
history_with_message = history + [
|
| 983 |
-
{"role": "user", "content": message},
|
| 984 |
-
{"role": "assistant", "content": processing_msg.get(detected_lang, processing_msg['en'])}
|
| 985 |
-
]
|
| 986 |
-
yield history_with_message, "", ""
|
| 987 |
-
|
| 988 |
-
# ๋น๋๊ธฐ ๊ฒ์์ ๋๊ธฐ์ ์ผ๋ก ์คํ
|
| 989 |
-
async def search_wrapper():
|
| 990 |
-
return await system.search.search_async(message, count=search_count, lang=detected_lang)
|
| 991 |
-
|
| 992 |
-
loop = asyncio.new_event_loop()
|
| 993 |
-
asyncio.set_event_loop(loop)
|
| 994 |
-
search_results = loop.run_until_complete(search_wrapper())
|
| 995 |
-
|
| 996 |
-
if search_results:
|
| 997 |
-
ref_label = {
|
| 998 |
-
'ko': "๐ ์ฐธ๊ณ ์๋ฃ",
|
| 999 |
-
'en': "๐ References",
|
| 1000 |
-
'ja': "๐ ๅ่่ณๆ",
|
| 1001 |
-
'zh': "๐ ๅ่่ตๆ"
|
| 1002 |
-
}
|
| 1003 |
-
search_display = f"{ref_label.get(detected_lang, ref_label['en'])}\n\n"
|
| 1004 |
-
for i, result in enumerate(search_results[:3], 1):
|
| 1005 |
-
search_display += f"**{i}. [{result['title'][:50]}]({result['url']})**\n"
|
| 1006 |
-
search_display += f" {result['description'][:100]}...\n\n"
|
| 1007 |
-
|
| 1008 |
-
# ์ฌ์ฉ์ ๋ฉ์์ง ์ถ๊ฐ
|
| 1009 |
-
current_history = history + [{"role": "user", "content": message}]
|
| 1010 |
-
|
| 1011 |
-
# ์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ์ ์ํ ๋น๋๊ธฐ ์ฒ๋ฆฌ
|
| 1012 |
-
async def stream_responses():
|
| 1013 |
-
"""์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ ์ ๋๋ ์ดํฐ"""
|
| 1014 |
-
async for response, thoughts in system.parallel_process_agents(
|
| 1015 |
-
query=message,
|
| 1016 |
-
search_results=search_results,
|
| 1017 |
-
show_progress=show_agent_thoughts,
|
| 1018 |
-
lang=detected_lang
|
| 1019 |
-
):
|
| 1020 |
-
yield response, thoughts
|
| 1021 |
-
|
| 1022 |
-
# ์ ์ด๋ฒคํธ ๋ฃจํ์์ ์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ
|
| 1023 |
-
loop = asyncio.new_event_loop()
|
| 1024 |
-
asyncio.set_event_loop(loop)
|
| 1025 |
-
|
| 1026 |
-
# ๋น๋๊ธฐ ์ ๋๋ ์ดํฐ๋ฅผ ๋๊ธฐ์ ์ผ๋ก ์ํ
|
| 1027 |
-
gen = stream_responses()
|
| 1028 |
-
|
| 1029 |
-
while True:
|
| 1030 |
-
try:
|
| 1031 |
-
# ๋ค์ ํญ๋ชฉ ๊ฐ์ ธ์ค๊ธฐ
|
| 1032 |
-
task = asyncio.ensure_future(gen.__anext__(), loop=loop)
|
| 1033 |
-
response, thoughts = loop.run_until_complete(task)
|
| 1034 |
-
|
| 1035 |
-
# ์ค์๊ฐ ์
๋ฐ์ดํธ
|
| 1036 |
-
updated_history = current_history + [
|
| 1037 |
-
{"role": "assistant", "content": response}
|
| 1038 |
-
]
|
| 1039 |
-
yield updated_history, thoughts, search_display
|
| 1040 |
-
|
| 1041 |
-
except StopAsyncIteration:
|
| 1042 |
-
break
|
| 1043 |
-
|
| 1044 |
-
except Exception as e:
|
| 1045 |
-
error_history = history + [
|
| 1046 |
-
{"role": "user", "content": message},
|
| 1047 |
-
{"role": "assistant", "content": f"โ Error: {str(e)}"}
|
| 1048 |
-
]
|
| 1049 |
-
yield error_history, "", ""
|
| 1050 |
-
finally:
|
| 1051 |
-
# ๋ฃจํ ์ ๋ฆฌ
|
| 1052 |
-
try:
|
| 1053 |
-
loop.close()
|
| 1054 |
-
except:
|
| 1055 |
-
pass
|
| 1056 |
-
|
| 1057 |
-
# Gradio ์ธํฐํ์ด์ค
|
| 1058 |
-
with gr.Blocks(
|
| 1059 |
-
title="โก Speed-Optimized Multi-Agent System (No Cache)",
|
| 1060 |
-
theme=gr.themes.Soft(),
|
| 1061 |
-
css="""
|
| 1062 |
-
.gradio-container {
|
| 1063 |
-
max-width: 1400px !important;
|
| 1064 |
-
margin: auto !important;
|
| 1065 |
-
}
|
| 1066 |
-
"""
|
| 1067 |
-
) as demo:
|
| 1068 |
-
gr.Markdown("""
|
| 1069 |
-
# โก Enhanced Multi-Agent RAG System (์บ์ฑ ์ ๊ฑฐ ๋ฒ์ )
|
| 1070 |
-
**Complex questions processed within 5-8 seconds | Multi-language support**
|
| 1071 |
-
|
| 1072 |
-
**Optimization Features:**
|
| 1073 |
-
- ๐ Parallel Processing: Concurrent agent execution
|
| 1074 |
-
- โก Stream Buffering: Network optimization
|
| 1075 |
-
- ๐ฏ Early Termination: Complete immediately when quality is met
|
| 1076 |
-
- ๐ Multi-language: Auto-detect Korean/English/Japanese/Chinese
|
| 1077 |
-
- โ **Caching Disabled**: ์บ์ฑ ๊ธฐ๋ฅ ์ ๊ฑฐ๋จ
|
| 1078 |
-
""")
|
| 1079 |
-
|
| 1080 |
-
with gr.Row():
|
| 1081 |
-
with gr.Column(scale=3):
|
| 1082 |
-
chatbot = gr.Chatbot(
|
| 1083 |
-
height=500,
|
| 1084 |
-
label="๐ฌ Chat",
|
| 1085 |
-
type="messages"
|
| 1086 |
-
)
|
| 1087 |
-
|
| 1088 |
-
msg = gr.Textbox(
|
| 1089 |
-
label="Enter complex question",
|
| 1090 |
-
placeholder="Enter complex questions requiring analysis, strategy, or creative solutions...",
|
| 1091 |
-
lines=3
|
| 1092 |
-
)
|
| 1093 |
-
|
| 1094 |
-
with gr.Row():
|
| 1095 |
-
submit = gr.Button("โก High-Speed Process", variant="primary")
|
| 1096 |
-
clear = gr.Button("๐ Reset")
|
| 1097 |
-
|
| 1098 |
-
with gr.Accordion("๐ค Agent Processing", open=False):
|
| 1099 |
-
agent_thoughts = gr.Markdown()
|
| 1100 |
-
|
| 1101 |
-
with gr.Accordion("๐ Search Sources", open=False):
|
| 1102 |
-
search_sources = gr.Markdown()
|
| 1103 |
-
|
| 1104 |
-
with gr.Column(scale=1):
|
| 1105 |
-
gr.Markdown("**โ๏ธ Settings**")
|
| 1106 |
-
|
| 1107 |
-
language_mode = gr.Radio(
|
| 1108 |
-
choices=["Auto", "Korean", "English", "Japanese", "Chinese"],
|
| 1109 |
-
value="Auto",
|
| 1110 |
-
label="๐ Language Mode"
|
| 1111 |
-
)
|
| 1112 |
-
|
| 1113 |
-
use_search = gr.Checkbox(
|
| 1114 |
-
label="๐ Use Web Search",
|
| 1115 |
-
value=True
|
| 1116 |
-
)
|
| 1117 |
-
|
| 1118 |
-
show_agent_thoughts = gr.Checkbox(
|
| 1119 |
-
label="๐ง Show Processing",
|
| 1120 |
-
value=True
|
| 1121 |
-
)
|
| 1122 |
-
|
| 1123 |
-
search_count = gr.Slider(
|
| 1124 |
-
minimum=3,
|
| 1125 |
-
maximum=10,
|
| 1126 |
-
value=5,
|
| 1127 |
-
step=1,
|
| 1128 |
-
label="Search Results Count"
|
| 1129 |
-
)
|
| 1130 |
-
|
| 1131 |
-
gr.Markdown("""
|
| 1132 |
-
**โก Optimization Status**
|
| 1133 |
-
|
| 1134 |
-
**Active Optimizations:**
|
| 1135 |
-
- โ
Parallel Processing
|
| 1136 |
-
- โ ~~Smart Caching~~ (์ ๊ฑฐ๋จ)
|
| 1137 |
-
- โ
Buffer Streaming
|
| 1138 |
-
- โ
Early Termination
|
| 1139 |
-
- โ
Compressed Prompts
|
| 1140 |
-
- โ
Multi-language Support
|
| 1141 |
-
- โ
Error Recovery
|
| 1142 |
-
|
| 1143 |
-
**Expected Processing Time:**
|
| 1144 |
-
- Simple Query: 3-5 seconds
|
| 1145 |
-
- Complex Query: 5-8 seconds
|
| 1146 |
-
- Very Complex: 8-12 seconds
|
| 1147 |
-
""")
|
| 1148 |
-
|
| 1149 |
-
# ๋ณต์กํ ์ง๋ฌธ ์์ (๋ค๊ตญ์ด)
|
| 1150 |
-
gr.Examples(
|
| 1151 |
-
examples=[
|
| 1152 |
-
# Korean
|
| 1153 |
-
"AI ๊ธฐ์ ์ด ํฅํ 10๋
๊ฐ ํ๊ตญ ๊ฒฝ์ ์ ๋ฏธ์น ์ํฅ์ ๋ค๊ฐ๋๋ก ๋ถ์ํ๊ณ ๋์ ์ ๋ต์ ์ ์ํด์ค",
|
| 1154 |
-
"์คํํธ์
์ด ๋๊ธฐ์
๊ณผ ๊ฒฝ์ํ๊ธฐ ์ํ ํ์ ์ ์ธ ์ ๋ต์ ๋จ๊ณ๋ณ๋ก ์๋ฆฝํด์ค",
|
| 1155 |
-
# English
|
| 1156 |
-
"Analyze the multifaceted impact of quantum computing on current encryption systems and propose alternatives",
|
| 1157 |
-
"Design 5 innovative business models for climate change mitigation with practical implementation details",
|
| 1158 |
-
# Japanese
|
| 1159 |
-
"ใกใฟใใผในๆไปฃใฎๆ่ฒ้ฉๆฐๆนๆกใๅฎ่ฃ
ๅฏ่ฝใชใฌใใซใงๆๆกใใฆใใ ใใ",
|
| 1160 |
-
# Chinese
|
| 1161 |
-
"ๅๆไบบๅทฅๆบ่ฝๅฏนๆชๆฅๅๅนดๅ
จ็็ปๆต็ๅฝฑๅๅนถๆๅบๅบๅฏน็ญ็ฅ"
|
| 1162 |
-
],
|
| 1163 |
-
inputs=msg
|
| 1164 |
-
)
|
| 1165 |
-
|
| 1166 |
-
# ์ด๋ฒคํธ ๋ฐ์ธ๋ฉ
|
| 1167 |
-
submit.click(
|
| 1168 |
-
process_query_optimized,
|
| 1169 |
-
inputs=[msg, chatbot, use_search, show_agent_thoughts, search_count, language_mode],
|
| 1170 |
-
outputs=[chatbot, agent_thoughts, search_sources]
|
| 1171 |
-
).then(
|
| 1172 |
-
lambda: "",
|
| 1173 |
-
None,
|
| 1174 |
-
msg
|
| 1175 |
-
)
|
| 1176 |
-
|
| 1177 |
-
msg.submit(
|
| 1178 |
-
process_query_optimized,
|
| 1179 |
-
inputs=[msg, chatbot, use_search, show_agent_thoughts, search_count, language_mode],
|
| 1180 |
-
outputs=[chatbot, agent_thoughts, search_sources]
|
| 1181 |
-
).then(
|
| 1182 |
-
lambda: "",
|
| 1183 |
-
None,
|
| 1184 |
-
msg
|
| 1185 |
-
)
|
| 1186 |
-
|
| 1187 |
-
clear.click(
|
| 1188 |
-
lambda: ([], "", ""),
|
| 1189 |
-
None,
|
| 1190 |
-
[chatbot, agent_thoughts, search_sources]
|
| 1191 |
-
)
|
| 1192 |
-
|
| 1193 |
-
return demo
|
| 1194 |
-
|
| 1195 |
-
|
| 1196 |
-
# ============================================================================
|
| 1197 |
-
# ๋ฉ์ธ ์คํ
|
| 1198 |
-
# ============================================================================
|
| 1199 |
-
|
| 1200 |
-
if __name__ == "__main__":
|
| 1201 |
-
print("""
|
| 1202 |
-
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 1203 |
-
โ โก Speed-Optimized Multi-Agent System (No Cache) โก โ
|
| 1204 |
-
โ โ
|
| 1205 |
-
โ High-speed AI system processing complex questions โ
|
| 1206 |
-
โ โ
|
| 1207 |
-
โ Features: โ
|
| 1208 |
-
โ โข Multi-language support (KO/EN/JA/ZH) โ
|
| 1209 |
-
โ โข Improved error recovery โ
|
| 1210 |
-
โ โข NO CACHING (์บ์ฑ ๊ธฐ๋ฅ ์ ๊ฑฐ๋จ) โ
|
| 1211 |
-
โ โข Adaptive stream buffering โ
|
| 1212 |
-
โ โข Response cleaning & formatting โ
|
| 1213 |
-
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 1214 |
-
""")
|
| 1215 |
-
|
| 1216 |
-
# API ํค ํ์ธ
|
| 1217 |
-
if not os.getenv("FIREWORKS_API_KEY"):
|
| 1218 |
-
print("\nโ ๏ธ FIREWORKS_API_KEY is not set.")
|
| 1219 |
-
|
| 1220 |
-
if not os.getenv("BRAVE_SEARCH_API_KEY"):
|
| 1221 |
-
print("\nโ ๏ธ BRAVE_SEARCH_API_KEY is not set.")
|
| 1222 |
-
|
| 1223 |
-
# Gradio ์ฑ ์คํ
|
| 1224 |
-
demo = create_optimized_gradio_interface()
|
| 1225 |
-
|
| 1226 |
-
is_hf_spaces = os.getenv("SPACE_ID") is not None
|
| 1227 |
-
|
| 1228 |
-
if is_hf_spaces:
|
| 1229 |
-
print("\n๐ค Running in optimized mode on Hugging Face Spaces (No Cache)...")
|
| 1230 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 1231 |
-
else:
|
| 1232 |
-
print("\n๐ป Running in optimized mode on local environment (No Cache)...")
|
| 1233 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, share=False), '', response, flags=re.MULTILINE)
|
| 1234 |
-
|
| 1235 |
-
# ํ
์ด๋ธ ๊ตฌ๋ถ์ ์ ๊ฑฐ (|---|, |:---|, |---:|, |:---:| ๋ฑ)
|
| 1236 |
-
response = re.sub(r'^\|[\s\-:]+\|.*
|
| 1237 |
-
|
| 1238 |
-
|
| 1239 |
-
# ============================================================================
|
| 1240 |
-
# ํตํฉ ์ต์ ํ ๋ฉํฐ ์์ด์ ํธ ์์คํ
(์บ์ฑ ์ ๊ฑฐ ๋ฒ์ )
|
| 1241 |
-
# ============================================================================
|
| 1242 |
-
|
| 1243 |
-
class SpeedOptimizedMultiAgentSystem:
|
| 1244 |
-
"""์๋ ์ต์ ํ๋ ๋ฉํฐ ์์ด์ ํธ ์์คํ
(์บ์ฑ ์์)"""
|
| 1245 |
-
|
| 1246 |
-
def __init__(self):
|
| 1247 |
-
self.llm = OptimizedFireworksClient()
|
| 1248 |
-
self.search = AsyncBraveSearch()
|
| 1249 |
-
self.reasoning = LightweightReasoningChain()
|
| 1250 |
-
self.quality_checker = QualityChecker()
|
| 1251 |
-
self.streaming = OptimizedStreaming()
|
| 1252 |
-
self.language_detector = LanguageDetector()
|
| 1253 |
-
self.response_cleaner = ResponseCleaner()
|
| 1254 |
-
|
| 1255 |
-
# ๋ณ๋ ฌ ์ฒ๋ฆฌ ํ
|
| 1256 |
-
self.executor = ThreadPoolExecutor(max_workers=4)
|
| 1257 |
-
|
| 1258 |
-
def _init_compact_prompts(self, lang: str = 'ko') -> Dict:
|
| 1259 |
-
"""์์ถ๋ ๊ณ ํจ์จ ํ๋กฌํํธ (์ธ์ด๋ณ)"""
|
| 1260 |
-
prompts = {
|
| 1261 |
-
'ko': {
|
| 1262 |
-
AgentRole.SUPERVISOR: """[๊ฐ๋
์-๊ตฌ์กฐ์ค๊ณ]
|
| 1263 |
-
์ฆ์๋ถ์: ํต์ฌ์๋+ํ์์ ๋ณด+๋ต๋ณ๊ตฌ์กฐ
|
| 1264 |
-
์ถ๋ ฅ: 5๊ฐ ํต์ฌํฌ์ธํธ(๊ฐ 1๋ฌธ์ฅ)
|
| 1265 |
-
์ถ๋ก ์ฒด๊ณ ๋ช
์""",
|
| 1266 |
-
|
| 1267 |
-
AgentRole.CREATIVE: """[์ฐฝ์์ฑ์์ฑ์]
|
| 1268 |
-
์
๋ ฅ๊ตฌ์กฐ ๋ฐ๋ผ ์ฐฝ์์ ํ์ฅ
|
| 1269 |
-
์ค์ฉ์์+ํ์ ์ ๊ทผ+๊ตฌ์ฒด์กฐ์ธ
|
| 1270 |
-
๋ถํ์์ค๋ช
์ ๊ฑฐ""",
|
| 1271 |
-
|
| 1272 |
-
AgentRole.CRITIC: """[๋นํ์-๊ฒ์ฆ]
|
| 1273 |
-
์ ์๊ฒํ : ์ ํ์ฑ/๋
ผ๋ฆฌ์ฑ/์ค์ฉ์ฑ
|
| 1274 |
-
๊ฐ์ ํฌ์ธํธ 3๊ฐ๋ง
|
| 1275 |
-
๊ฐ 2๋ฌธ์ฅ ์ด๋ด""",
|
| 1276 |
-
|
| 1277 |
-
AgentRole.FINALIZER: """[์ต์ข
ํตํฉ]
|
| 1278 |
-
๋ชจ๋ ์๊ฒฌ ์ข
ํฉโ์ต์ ๋ต๋ณ
|
| 1279 |
-
๋ช
ํ๊ตฌ์กฐ+์ค์ฉ์ ๋ณด+์ฐฝ์๊ท ํ
|
| 1280 |
-
๋ฐ๋ก ํต์ฌ ๋ด์ฉ๋ถํฐ ์์. ๋ถํ์ํ ํค๋๋ ๋งํฌ์
์์ด. ๋งํฌ๋ค์ด ํค๋(#, ##, ###) ์ฌ์ฉ ๊ธ์ง."""
|
| 1281 |
-
},
|
| 1282 |
-
'en': {
|
| 1283 |
-
AgentRole.SUPERVISOR: """[Supervisor-Structure]
|
| 1284 |
-
Immediate analysis: core intent+required info+answer structure
|
| 1285 |
-
Output: 5 key points (1 sentence each)
|
| 1286 |
-
Clear reasoning framework""",
|
| 1287 |
-
|
| 1288 |
-
AgentRole.CREATIVE: """[Creative Generator]
|
| 1289 |
-
Follow structure, expand creatively
|
| 1290 |
-
Practical examples+innovative approach+specific advice
|
| 1291 |
-
Remove unnecessary explanations""",
|
| 1292 |
-
|
| 1293 |
-
AgentRole.CRITIC: """[Critic-Verification]
|
| 1294 |
-
Quick review: accuracy/logic/practicality
|
| 1295 |
-
Only 3 improvement points
|
| 1296 |
-
Max 2 sentences each""",
|
| 1297 |
-
|
| 1298 |
-
AgentRole.FINALIZER: """[Final Integration]
|
| 1299 |
-
Synthesize all inputsโoptimal answer
|
| 1300 |
-
Clear structure+practical info+creative balance
|
| 1301 |
-
Start with core content directly. No unnecessary headers or markup. No markdown headers (#, ##, ###)."""
|
| 1302 |
-
},
|
| 1303 |
-
'ja': {
|
| 1304 |
-
AgentRole.SUPERVISOR: """[็ฃ็ฃ่
-ๆง้ ่จญ่จ]
|
| 1305 |
-
ๅณๆๅๆ๏ผๆ ธๅฟๆๅณ+ๅฟ
่ฆๆ
ๅ ฑ+ๅ็ญๆง้
|
| 1306 |
-
ๅบๅ๏ผ5ใคใฎๆ ธๅฟใใคใณใ๏ผๅ1ๆ๏ผ
|
| 1307 |
-
ๆจ่ซไฝ็ณปๆ็คบ""",
|
| 1308 |
-
|
| 1309 |
-
AgentRole.CREATIVE: """[ๅต้ ๆง็ๆ่
]
|
| 1310 |
-
ๅ
ฅๅๆง้ ใซๅพใฃใฆๅต้ ็ๆกๅผต
|
| 1311 |
-
ๅฎ็จไพ+้ฉๆฐ็ใขใใญใผใ+ๅ
ทไฝ็ใขใใใคใน
|
| 1312 |
-
ไธ่ฆใช่ชฌๆๅ้ค""",
|
| 1313 |
-
|
| 1314 |
-
AgentRole.CRITIC: """[ๆน่ฉ่
-ๆค่จผ]
|
| 1315 |
-
่ฟ
้ใฌใใฅใผ๏ผๆญฃ็ขบๆง/่ซ็ๆง/ๅฎ็จๆง
|
| 1316 |
-
ๆนๅใใคใณใ3ใคใฎใฟ
|
| 1317 |
-
ๅ2ๆไปฅๅ
""",
|
| 1318 |
-
|
| 1319 |
-
AgentRole.FINALIZER: """[ๆ็ต็ตฑๅ]
|
| 1320 |
-
ๅ
จๆ่ฆ็ตฑๅโๆ้ฉๅ็ญ
|
| 1321 |
-
ๆ็ขบๆง้ +ๅฎ็จๆ
ๅ ฑ+ๅต้ ๆง๏ฟฝ๏ฟฝ๏ฟฝใฉใณใน
|
| 1322 |
-
ๆ ธๅฟๅ
ๅฎนใใ็ดๆฅ้ๅงใไธ่ฆใชใใใใผใใใผใฏใขใใใชใใใใผใฏใใฆใณใใใใผ๏ผ#ใ##ใ###๏ผไฝฟ็จ็ฆๆญขใ"""
|
| 1323 |
-
},
|
| 1324 |
-
'zh': {
|
| 1325 |
-
AgentRole.SUPERVISOR: """[ไธป็ฎก-็ปๆ่ฎพ่ฎก]
|
| 1326 |
-
็ซๅณๅๆ๏ผๆ ธๅฟๆๅพ+ๆ้ไฟกๆฏ+็ญๆก็ปๆ
|
| 1327 |
-
่พๅบ๏ผ5ไธชๆ ธๅฟ่ฆ็น๏ผๆฏไธช1ๅฅ๏ผ
|
| 1328 |
-
ๆจ็ไฝ็ณปๆ็กฎ""",
|
| 1329 |
-
|
| 1330 |
-
AgentRole.CREATIVE: """[ๅๆ็ๆๅจ]
|
| 1331 |
-
ๆ็ปๆๅ้ ๆงๆฉๅฑ
|
| 1332 |
-
ๅฎ็จ็คบไพ+ๅๆฐๆนๆณ+ๅ
ทไฝๅปบ่ฎฎ
|
| 1333 |
-
ๅ ้คไธๅฟ
่ฆ็่งฃ้""",
|
| 1334 |
-
|
| 1335 |
-
AgentRole.CRITIC: """[่ฏ่ฎบๅฎถ-้ช่ฏ]
|
| 1336 |
-
ๅฟซ้ๅฎกๆฅ๏ผๅ็กฎๆง/้ป่พๆง/ๅฎ็จๆง
|
| 1337 |
-
ไป
3ไธชๆน่ฟ็น
|
| 1338 |
-
ๆฏไธชๆๅค2ๅฅ""",
|
| 1339 |
-
|
| 1340 |
-
AgentRole.FINALIZER: """[ๆ็ปๆดๅ]
|
| 1341 |
-
็ปผๅๆๆๆ่งโๆไฝณ็ญๆก
|
| 1342 |
-
ๆธ
ๆฐ็ปๆ+ๅฎ็จไฟกๆฏ+ๅๆๅนณ่กก
|
| 1343 |
-
็ดๆฅไปๆ ธๅฟๅ
ๅฎนๅผๅงใๆ ้ไธๅฟ
่ฆ็ๆ ้ขๆๆ ่ฎฐใ็ฆๆญขไฝฟ็จMarkdownๆ ้ข๏ผ#ใ##ใ###๏ผใ"""
|
| 1344 |
-
}
|
| 1345 |
-
}
|
| 1346 |
-
|
| 1347 |
-
return prompts.get(lang, prompts['en'])
|
| 1348 |
-
|
| 1349 |
-
async def parallel_process_agents(
|
| 1350 |
-
self,
|
| 1351 |
-
query: str,
|
| 1352 |
-
search_results: List[Dict],
|
| 1353 |
-
show_progress: bool = True,
|
| 1354 |
-
lang: str = None
|
| 1355 |
-
) -> AsyncGenerator[Tuple[str, str], None]:
|
| 1356 |
-
"""๋ณ๋ ฌ ์ฒ๋ฆฌ ํ์ดํ๋ผ์ธ (์บ์ฑ ์์)"""
|
| 1357 |
-
|
| 1358 |
-
start_time = time.time()
|
| 1359 |
-
|
| 1360 |
-
# ์ธ์ด ์๋ ๊ฐ์ง
|
| 1361 |
-
if lang is None:
|
| 1362 |
-
lang = self.language_detector.detect_language(query)
|
| 1363 |
-
|
| 1364 |
-
# ์ธ์ด๋ณ ํ๋กฌํํธ ์ค์
|
| 1365 |
-
self.compact_prompts = self._init_compact_prompts(lang)
|
| 1366 |
-
|
| 1367 |
-
search_context = self._format_search_results(search_results)
|
| 1368 |
-
accumulated_response = ""
|
| 1369 |
-
agent_thoughts = ""
|
| 1370 |
-
|
| 1371 |
-
# ์ถ๋ก ํจํด ๊ฒฐ์
|
| 1372 |
-
reasoning_pattern = self.reasoning.get_reasoning_pattern(query, lang)
|
| 1373 |
-
|
| 1374 |
-
try:
|
| 1375 |
-
# === 1๋จ๊ณ: ๊ฐ๋
์ + ๊ฒ์ ๋ณ๋ ฌ ์คํ ===
|
| 1376 |
-
if show_progress:
|
| 1377 |
-
progress_msg = {
|
| 1378 |
-
'ko': "๐ ๋ณ๋ ฌ ์ฒ๋ฆฌ ์์\n๐ ๊ฐ๋
์ ๋ถ์ + ๐ ์ถ๊ฐ ๊ฒ์ ๋์ ์งํ...\n\n",
|
| 1379 |
-
'en': "๐ Starting parallel processing\n๐ Supervisor analysis + ๐ Additional search in progress...\n\n",
|
| 1380 |
-
'ja': "๐ ไธฆๅๅฆ็้ๅง\n๐ ็ฃ็ฃ่
ๅๆ + ๐ ่ฟฝๅ ๆค็ดขๅๆ้ฒ่กไธญ...\n\n",
|
| 1381 |
-
'zh': "๐ ๅผๅงๅนถ่กๅค็\n๐ ไธป็ฎกๅๆ + ๐ ้ๅ ๆ็ดขๅๆถ่ฟ่ก...\n\n"
|
| 1382 |
-
}
|
| 1383 |
-
agent_thoughts = progress_msg.get(lang, progress_msg['en'])
|
| 1384 |
-
yield accumulated_response, agent_thoughts
|
| 1385 |
-
|
| 1386 |
-
# ๊ฐ๋
์ ํ๋กฌํํธ (์ธ์ด๋ณ)
|
| 1387 |
-
supervisor_prompt_templates = {
|
| 1388 |
-
'ko': f"""
|
| 1389 |
-
์ง๋ฌธ: {query}
|
| 1390 |
-
๊ฒ์๊ฒฐ๊ณผ: {search_context}
|
| 1391 |
-
์ถ๋ก ํจํด: {reasoning_pattern}
|
| 1392 |
-
์ฆ์ ํต์ฌ๊ตฌ์กฐ 5๊ฐ ์ ์""",
|
| 1393 |
-
'en': f"""
|
| 1394 |
-
Question: {query}
|
| 1395 |
-
Search results: {search_context}
|
| 1396 |
-
Reasoning pattern: {reasoning_pattern}
|
| 1397 |
-
Immediately provide 5 key structures""",
|
| 1398 |
-
'ja': f"""
|
| 1399 |
-
่ณชๅ: {query}
|
| 1400 |
-
ๆค็ดข็ตๆ: {search_context}
|
| 1401 |
-
ๆจ่ซใใฟใผใณ: {reasoning_pattern}
|
| 1402 |
-
ๅณๅบงใซ5ใคใฎๆ ธๅฟๆง้ ใๆ็คบ""",
|
| 1403 |
-
'zh': f"""
|
| 1404 |
-
้ฎ้ข: {query}
|
| 1405 |
-
ๆ็ดข็ปๆ: {search_context}
|
| 1406 |
-
ๆจ็ๆจกๅผ: {reasoning_pattern}
|
| 1407 |
-
็ซๅณๆไพ5ไธชๆ ธๅฟ็ปๆ"""
|
| 1408 |
-
}
|
| 1409 |
-
|
| 1410 |
-
supervisor_prompt = supervisor_prompt_templates.get(lang, supervisor_prompt_templates['en'])
|
| 1411 |
-
|
| 1412 |
-
supervisor_response = ""
|
| 1413 |
-
supervisor_task = self.llm.chat_stream_async(
|
| 1414 |
-
messages=[
|
| 1415 |
-
{"role": "system", "content": self.compact_prompts[AgentRole.SUPERVISOR]},
|
| 1416 |
-
{"role": "user", "content": supervisor_prompt}
|
| 1417 |
-
],
|
| 1418 |
-
temperature=0.3,
|
| 1419 |
-
max_tokens=500
|
| 1420 |
-
)
|
| 1421 |
-
|
| 1422 |
-
# ๊ฐ๋
์ ์คํธ๋ฆฌ๋ฐ (๋ฒํผ๋ง)
|
| 1423 |
-
async for chunk in self.streaming.buffer_and_yield(supervisor_task):
|
| 1424 |
-
supervisor_response += chunk
|
| 1425 |
-
if show_progress and len(supervisor_response) < 300:
|
| 1426 |
-
supervisor_label = {
|
| 1427 |
-
'ko': "๐ ๊ฐ๋
์ ๋ถ์",
|
| 1428 |
-
'en': "๐ Supervisor Analysis",
|
| 1429 |
-
'ja': "๐ ็ฃ็ฃ่
ๅๆ",
|
| 1430 |
-
'zh': "๐ ไธป็ฎกๅๆ"
|
| 1431 |
-
}
|
| 1432 |
-
agent_thoughts = f"{supervisor_label.get(lang, supervisor_label['en'])}\n{supervisor_response[:300]}...\n\n"
|
| 1433 |
-
yield accumulated_response, agent_thoughts
|
| 1434 |
-
|
| 1435 |
-
# === 2๋จ๊ณ: ์ฐฝ์์ฑ + ๋นํ ์ค๋น ๋ณ๋ ฌ ===
|
| 1436 |
-
if show_progress:
|
| 1437 |
-
creative_msg = {
|
| 1438 |
-
'ko': "๐จ ์ฐฝ์์ฑ ์์ฑ์ + ๐ ๋นํ์ ์ค๋น...\n\n",
|
| 1439 |
-
'en': "๐จ Creative Generator + ๐ Critic preparing...\n\n",
|
| 1440 |
-
'ja': "๐จ ๅต้ ๆง็ๆ่
+ ๐ ๆน่ฉ่
ๆบๅไธญ...\n\n",
|
| 1441 |
-
'zh': "๐จ ๅๆ็ๆๅจ + ๐ ่ฏ่ฎบๅฎถๅๅคไธญ...\n\n"
|
| 1442 |
-
}
|
| 1443 |
-
agent_thoughts += creative_msg.get(lang, creative_msg['en'])
|
| 1444 |
-
yield accumulated_response, agent_thoughts
|
| 1445 |
-
|
| 1446 |
-
# ์ฐฝ์์ฑ ์์ฑ ์์ (์ธ์ด๋ณ)
|
| 1447 |
-
creative_prompt_templates = {
|
| 1448 |
-
'ko': f"""
|
| 1449 |
-
์ง๋ฌธ: {query}
|
| 1450 |
-
๊ฐ๋
์๊ตฌ์กฐ: {supervisor_response}
|
| 1451 |
-
๊ฒ์๊ฒฐ๊ณผ: {search_context}
|
| 1452 |
-
์ฐฝ์์ +์ค์ฉ์ ๋ต๋ณ ์ฆ์์์ฑ""",
|
| 1453 |
-
'en': f"""
|
| 1454 |
-
Question: {query}
|
| 1455 |
-
Supervisor structure: {supervisor_response}
|
| 1456 |
-
Search results: {search_context}
|
| 1457 |
-
Generate creative+practical answer immediately""",
|
| 1458 |
-
'ja': f"""
|
| 1459 |
-
่ณชๅ: {query}
|
| 1460 |
-
็ฃ็ฃ่
ๆง้ : {supervisor_response}
|
| 1461 |
-
ๆค็ดข็ตๆ: {search_context}
|
| 1462 |
-
ๅต้ ็+ๅฎ็จ็ๅ็ญๅณๅบง็ๆ""",
|
| 1463 |
-
'zh': f"""
|
| 1464 |
-
้ฎ้ข: {query}
|
| 1465 |
-
ไธป็ฎก็ปๆ: {supervisor_response}
|
| 1466 |
-
ๆ็ดข็ปๆ: {search_context}
|
| 1467 |
-
็ซๅณ็ๆๅๆ+ๅฎ็จ็ญๆก"""
|
| 1468 |
-
}
|
| 1469 |
-
|
| 1470 |
-
creative_prompt = creative_prompt_templates.get(lang, creative_prompt_templates['en'])
|
| 1471 |
-
|
| 1472 |
-
creative_response = ""
|
| 1473 |
-
creative_partial = ""
|
| 1474 |
-
critic_started = False
|
| 1475 |
-
critic_response = ""
|
| 1476 |
-
|
| 1477 |
-
creative_task = self.llm.chat_stream_async(
|
| 1478 |
-
messages=[
|
| 1479 |
-
{"role": "system", "content": self.compact_prompts[AgentRole.CREATIVE]},
|
| 1480 |
-
{"role": "user", "content": creative_prompt}
|
| 1481 |
-
],
|
| 1482 |
-
temperature=0.8,
|
| 1483 |
-
max_tokens=1500
|
| 1484 |
-
)
|
| 1485 |
-
|
| 1486 |
-
# ์ฐฝ์์ฑ ์คํธ๋ฆฌ๋ฐ + ๋นํ์ ์กฐ๊ธฐ ์์
|
| 1487 |
-
async for chunk in self.streaming.buffer_and_yield(creative_task):
|
| 1488 |
-
creative_response += chunk
|
| 1489 |
-
creative_partial += chunk
|
| 1490 |
-
|
| 1491 |
-
# ์ฐฝ์์ฑ ์๋ต์ด 500์ ๋์ผ๋ฉด ๋นํ์ ์์
|
| 1492 |
-
if len(creative_partial) > 500 and not critic_started:
|
| 1493 |
-
critic_started = True
|
| 1494 |
-
|
| 1495 |
-
# ๋นํ์ ๋น๋๊ธฐ ์์ (์ธ์ด๋ณ)
|
| 1496 |
-
critic_prompt_templates = {
|
| 1497 |
-
'ko': f"""
|
| 1498 |
-
์๋ณธ์ง๋ฌธ: {query}
|
| 1499 |
-
์ฐฝ์์ฑ๋ต๋ณ(์ผ๋ถ): {creative_partial}
|
| 1500 |
-
์ ์๊ฒํ โ๊ฐ์ ์ 3๊ฐ""",
|
| 1501 |
-
'en': f"""
|
| 1502 |
-
Original question: {query}
|
| 1503 |
-
Creative answer (partial): {creative_partial}
|
| 1504 |
-
Quick reviewโ3 improvements""",
|
| 1505 |
-
'ja': f"""
|
| 1506 |
-
ๅ
ใฎ่ณชๅ: {query}
|
| 1507 |
-
ๅต้ ็ๅ็ญ๏ผไธ้จ๏ผ: {creative_partial}
|
| 1508 |
-
่ฟ
้ใฌใใฅใผโๆนๅ็น3ใค""",
|
| 1509 |
-
'zh': f"""
|
| 1510 |
-
ๅๅง้ฎ้ข: {query}
|
| 1511 |
-
ๅๆ็ญๆก๏ผ้จๅ๏ผ: {creative_partial}
|
| 1512 |
-
ๅฟซ้ๅฎกๆฅโ3ไธชๆน่ฟ็น"""
|
| 1513 |
-
}
|
| 1514 |
-
|
| 1515 |
-
critic_prompt = critic_prompt_templates.get(lang, critic_prompt_templates['en'])
|
| 1516 |
-
|
| 1517 |
-
critic_task = asyncio.create_task(
|
| 1518 |
-
self._run_critic_async(critic_prompt)
|
| 1519 |
-
)
|
| 1520 |
-
|
| 1521 |
-
if show_progress:
|
| 1522 |
-
display_creative = creative_response[:400] + "..." if len(creative_response) > 400 else creative_response
|
| 1523 |
-
creative_label = {
|
| 1524 |
-
'ko': "๐จ ์ฐฝ์์ฑ ์์ฑ์",
|
| 1525 |
-
'en': "๐จ Creative Generator",
|
| 1526 |
-
'ja': "๐จ ๅต้ ๆง็ๆ่
",
|
| 1527 |
-
'zh': "๐จ ๅๆ็ๆๅจ"
|
| 1528 |
-
}
|
| 1529 |
-
agent_thoughts = f"{creative_label.get(lang, creative_label['en'])}\n{display_creative}\n\n"
|
| 1530 |
-
yield accumulated_response, agent_thoughts
|
| 1531 |
-
|
| 1532 |
-
# ๋นํ์ ๊ฒฐ๊ณผ ๋๊ธฐ
|
| 1533 |
-
if critic_started:
|
| 1534 |
-
critic_response = await critic_task
|
| 1535 |
-
|
| 1536 |
-
if show_progress:
|
| 1537 |
-
critic_label = {
|
| 1538 |
-
'ko': "๐ ๋นํ์ ๊ฒํ ",
|
| 1539 |
-
'en': "๐ Critic Review",
|
| 1540 |
-
'ja': "๐ ๆน่ฉ่
ใฌใใฅใผ",
|
| 1541 |
-
'zh': "๐ ่ฏ่ฎบๅฎถๅฎกๆฅ"
|
| 1542 |
-
}
|
| 1543 |
-
agent_thoughts += f"{critic_label.get(lang, critic_label['en'])}\n{critic_response[:200]}...\n\n"
|
| 1544 |
-
yield accumulated_response, agent_thoughts
|
| 1545 |
-
|
| 1546 |
-
# === 3๋จ๊ณ: ํ์ง ์ฒดํฌ ๋ฐ ์กฐ๊ธฐ ์ข
๋ฃ ===
|
| 1547 |
-
quality_score, need_more = self.quality_checker.evaluate_response(
|
| 1548 |
-
creative_response, query, lang
|
| 1549 |
-
)
|
| 1550 |
-
|
| 1551 |
-
if not need_more and quality_score > 0.85:
|
| 1552 |
-
# ํ์ง์ด ์ถฉ๋ถํ ๋์ผ๋ฉด ๋ฐ๋ก ๋ฐํ
|
| 1553 |
-
accumulated_response = self.response_cleaner.clean_response(creative_response)
|
| 1554 |
-
|
| 1555 |
-
if show_progress:
|
| 1556 |
-
quality_msg = {
|
| 1557 |
-
'ko': f"โ
ํ์ง ์ถฉ์กฑ (์ ์: {quality_score:.2f})\n์กฐ๊ธฐ ์๋ฃ!\n",
|
| 1558 |
-
'en': f"โ
Quality met (score: {quality_score:.2f})\nEarly completion!\n",
|
| 1559 |
-
'ja': f"โ
ๅ่ณชๆบ่ถณ (ในใณใข: {quality_score:.2f})\nๆฉๆๅฎไบ!\n",
|
| 1560 |
-
'zh': f"โ
่ดจ้ๆปก่ถณ (ๅๆฐ: {quality_score:.2f})\nๆๅๅฎๆ!\n"
|
| 1561 |
-
}
|
| 1562 |
-
agent_thoughts += quality_msg.get(lang, quality_msg['en'])
|
| 1563 |
-
|
| 1564 |
-
yield accumulated_response, agent_thoughts
|
| 1565 |
-
return
|
| 1566 |
-
|
| 1567 |
-
# === 4๋จ๊ณ: ์ต์ข
ํตํฉ (์คํธ๋ฆฌ๋ฐ) ===
|
| 1568 |
-
if show_progress:
|
| 1569 |
-
final_msg = {
|
| 1570 |
-
'ko': "โ
์ต์ข
ํตํฉ ์ค...\n\n",
|
| 1571 |
-
'en': "โ
Final integration in progress...\n\n",
|
| 1572 |
-
'ja': "โ
ๆ็ต็ตฑๅไธญ...\n\n",
|
| 1573 |
-
'zh': "โ
ๆ็ปๆดๅไธญ...\n\n"
|
| 1574 |
-
}
|
| 1575 |
-
agent_thoughts += final_msg.get(lang, final_msg['en'])
|
| 1576 |
-
yield accumulated_response, agent_thoughts
|
| 1577 |
-
|
| 1578 |
-
# ์ต์ข
ํ๋กฌํํธ (์ธ์ด๋ณ)
|
| 1579 |
-
final_prompt_templates = {
|
| 1580 |
-
'ko': f"""
|
| 1581 |
-
์ง๋ฌธ: {query}
|
| 1582 |
-
์ฐฝ์์ฑ๋ต๋ณ: {creative_response}
|
| 1583 |
-
๋นํํผ๋๋ฐฑ: {critic_response}
|
| 1584 |
-
๊ฐ๋
์๊ตฌ์กฐ: {supervisor_response}
|
| 1585 |
-
์ต์ข
ํตํฉโ์๋ฒฝ๋ต๋ณ. ๋งํฌ๋ค์ด ํค๋(#, ##, ###) ์ฌ์ฉ ๊ธ์ง.""",
|
| 1586 |
-
'en': f"""
|
| 1587 |
-
Question: {query}
|
| 1588 |
-
Creative answer: {creative_response}
|
| 1589 |
-
Critic feedback: {critic_response}
|
| 1590 |
-
Supervisor structure: {supervisor_response}
|
| 1591 |
-
Final integrationโperfect answer. No markdown headers (#, ##, ###).""",
|
| 1592 |
-
'ja': f"""
|
| 1593 |
-
่ณชๅ: {query}
|
| 1594 |
-
ๅต้ ็ๅ็ญ: {creative_response}
|
| 1595 |
-
ๆน่ฉใใฃใผใใใใฏ: {critic_response}
|
| 1596 |
-
็ฃ็ฃ่
ๆง้ : {supervisor_response}
|
| 1597 |
-
ๆ็ต็ตฑๅโๅฎ็งใชๅ็ญใใใผใฏใใฆใณใใใใผ๏ผ#ใ##ใ###๏ผไฝฟ็จ็ฆๆญขใ""",
|
| 1598 |
-
'zh': f"""
|
| 1599 |
-
้ฎ้ข: {query}
|
| 1600 |
-
ๅๆ็ญๆก: {creative_response}
|
| 1601 |
-
่ฏ่ฎบๅ้ฆ: {critic_response}
|
| 1602 |
-
ไธป็ฎก็ปๆ: {supervisor_response}
|
| 1603 |
-
ๆ็ปๆดๅโๅฎ็พ็ญๆกใ็ฆๆญขไฝฟ็จMarkdownๆ ้ข๏ผ#ใ##ใ###๏ผใ"""
|
| 1604 |
-
}
|
| 1605 |
-
|
| 1606 |
-
final_prompt = final_prompt_templates.get(lang, final_prompt_templates['en'])
|
| 1607 |
-
|
| 1608 |
-
final_task = self.llm.chat_stream_async(
|
| 1609 |
-
messages=[
|
| 1610 |
-
{"role": "system", "content": self.compact_prompts[AgentRole.FINALIZER]},
|
| 1611 |
-
{"role": "user", "content": final_prompt}
|
| 1612 |
-
],
|
| 1613 |
-
temperature=0.5,
|
| 1614 |
-
max_tokens=2500
|
| 1615 |
-
)
|
| 1616 |
-
|
| 1617 |
-
# ์ต์ข
๋ต๋ณ ์คํธ๋ฆฌ๋ฐ
|
| 1618 |
-
accumulated_response = ""
|
| 1619 |
-
|
| 1620 |
-
async for chunk in final_task:
|
| 1621 |
-
accumulated_response += chunk
|
| 1622 |
-
# ์ค์๊ฐ ์ ๋ฆฌ
|
| 1623 |
-
cleaned_response = self.response_cleaner.clean_response(accumulated_response)
|
| 1624 |
-
yield cleaned_response, agent_thoughts
|
| 1625 |
-
|
| 1626 |
-
# ์ต์ข
์ ๋ฆฌ
|
| 1627 |
-
accumulated_response = self.response_cleaner.clean_response(accumulated_response)
|
| 1628 |
-
|
| 1629 |
-
# ์ฒ๋ฆฌ ์๊ฐ ์ถ๊ฐ (์ธ์ด๋ณ)
|
| 1630 |
-
processing_time = time.time() - start_time
|
| 1631 |
-
time_msg = {
|
| 1632 |
-
'ko': f"\n\n---\nโก ์ฒ๋ฆฌ ์๊ฐ: {processing_time:.1f}์ด",
|
| 1633 |
-
'en': f"\n\n---\nโก Processing time: {processing_time:.1f} seconds",
|
| 1634 |
-
'ja': f"\n\n---\nโก ๅฆ็ๆ้: {processing_time:.1f}็ง",
|
| 1635 |
-
'zh': f"\n\n---\nโก ๅค็ๆถ้ด: {processing_time:.1f}็ง"
|
| 1636 |
-
}
|
| 1637 |
-
accumulated_response += time_msg.get(lang, time_msg['en'])
|
| 1638 |
-
|
| 1639 |
-
yield accumulated_response, agent_thoughts
|
| 1640 |
-
|
| 1641 |
-
except Exception as e:
|
| 1642 |
-
error_msg = {
|
| 1643 |
-
'ko': f"โ ์ค๋ฅ ๋ฐ์: {str(e)}",
|
| 1644 |
-
'en': f"โ Error occurred: {str(e)}",
|
| 1645 |
-
'ja': f"โ ใจใฉใผ็บ็: {str(e)}",
|
| 1646 |
-
'zh': f"โ ๅ็้่ฏฏ: {str(e)}"
|
| 1647 |
-
}
|
| 1648 |
-
yield error_msg.get(lang, error_msg['en']), agent_thoughts
|
| 1649 |
-
|
| 1650 |
-
async def _run_critic_async(self, prompt: str) -> str:
|
| 1651 |
-
"""๋นํ์ ๋น๋๊ธฐ ์คํ with error handling"""
|
| 1652 |
-
try:
|
| 1653 |
-
response = ""
|
| 1654 |
-
async for chunk in self.llm.chat_stream_async(
|
| 1655 |
-
messages=[
|
| 1656 |
-
{"role": "system", "content": self.compact_prompts[AgentRole.CRITIC]},
|
| 1657 |
-
{"role": "user", "content": prompt}
|
| 1658 |
-
],
|
| 1659 |
-
temperature=0.2,
|
| 1660 |
-
max_tokens=500
|
| 1661 |
-
):
|
| 1662 |
-
response += chunk
|
| 1663 |
-
return response
|
| 1664 |
-
except Exception as e:
|
| 1665 |
-
# ์ธ์ด ๊ฐ์ง
|
| 1666 |
-
lang = 'ko' if '์ง๋ฌธ' in prompt else 'en'
|
| 1667 |
-
error_msg = {
|
| 1668 |
-
'ko': "๋นํ ์ฒ๋ฆฌ ์ค ์ค๋ฅ",
|
| 1669 |
-
'en': "Error during critic processing",
|
| 1670 |
-
'ja': "ๆน่ฉๅฆ็ไธญใฎใจใฉใผ",
|
| 1671 |
-
'zh': "่ฏ่ฎบๅค็ไธญๅบ้"
|
| 1672 |
-
}
|
| 1673 |
-
return error_msg.get(lang, error_msg['en'])
|
| 1674 |
-
|
| 1675 |
-
def _format_search_results(self, results: List[Dict]) -> str:
|
| 1676 |
-
"""๊ฒ์ ๊ฒฐ๊ณผ ์์ถ ํฌ๋งท"""
|
| 1677 |
-
if not results:
|
| 1678 |
-
return "No search results"
|
| 1679 |
-
|
| 1680 |
-
formatted = []
|
| 1681 |
-
for i, r in enumerate(results[:3], 1):
|
| 1682 |
-
title = r.get('title', '')[:50]
|
| 1683 |
-
desc = r.get('description', '')[:100]
|
| 1684 |
-
formatted.append(f"[{i}]{title}:{desc}")
|
| 1685 |
-
|
| 1686 |
-
return " | ".join(formatted)
|
| 1687 |
-
|
| 1688 |
-
|
| 1689 |
-
# ============================================================================
|
| 1690 |
-
# Gradio UI (์ต์ ํ ๋ฒ์ - ์บ์ฑ ์ ๊ฑฐ)
|
| 1691 |
-
# ============================================================================
|
| 1692 |
-
|
| 1693 |
-
def create_optimized_gradio_interface():
|
| 1694 |
-
"""์ต์ ํ๋ Gradio ์ธํฐํ์ด์ค (์บ์ฑ ์์)"""
|
| 1695 |
-
|
| 1696 |
-
# ์์คํ
์ด๊ธฐํ
|
| 1697 |
-
system = SpeedOptimizedMultiAgentSystem()
|
| 1698 |
-
|
| 1699 |
-
def process_query_optimized(
|
| 1700 |
-
message: str,
|
| 1701 |
-
history: List[Dict],
|
| 1702 |
-
use_search: bool,
|
| 1703 |
-
show_agent_thoughts: bool,
|
| 1704 |
-
search_count: int,
|
| 1705 |
-
language_mode: str
|
| 1706 |
-
):
|
| 1707 |
-
"""์ต์ ํ๋ ์ฟผ๋ฆฌ ์ฒ๋ฆฌ - ์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ ๋ฒ์ """
|
| 1708 |
-
|
| 1709 |
-
if not message:
|
| 1710 |
-
yield history, "", ""
|
| 1711 |
-
return
|
| 1712 |
-
|
| 1713 |
-
# ์ธ์ด ์ค์
|
| 1714 |
-
if language_mode == "Auto":
|
| 1715 |
-
lang = None # ์๋ ๊ฐ์ง
|
| 1716 |
-
else:
|
| 1717 |
-
lang_map = {"Korean": "ko", "English": "en", "Japanese": "ja", "Chinese": "zh"}
|
| 1718 |
-
lang = lang_map.get(language_mode, None)
|
| 1719 |
-
|
| 1720 |
-
# ๋น๋๊ธฐ ํจ์๋ฅผ ๋๊ธฐ์ ์ผ๋ก ์คํ
|
| 1721 |
-
try:
|
| 1722 |
-
import nest_asyncio
|
| 1723 |
-
nest_asyncio.apply()
|
| 1724 |
-
except ImportError:
|
| 1725 |
-
pass
|
| 1726 |
-
|
| 1727 |
-
try:
|
| 1728 |
-
# ๊ฒ์ ์ํ (๋๊ธฐํ)
|
| 1729 |
-
search_results = []
|
| 1730 |
-
search_display = ""
|
| 1731 |
-
|
| 1732 |
-
# ์ธ์ด ์๋ ๊ฐ์ง (ํ์ํ ๊ฒฝ์ฐ)
|
| 1733 |
-
detected_lang = lang or system.language_detector.detect_language(message)
|
| 1734 |
-
|
| 1735 |
-
if use_search:
|
| 1736 |
-
# ๊ฒ์ ์ํ ํ์
|
| 1737 |
-
processing_msg = {
|
| 1738 |
-
'ko': "โก ๊ณ ์ ์ฒ๋ฆฌ ์ค...",
|
| 1739 |
-
'en': "โก High-speed processing...",
|
| 1740 |
-
'ja': "โก ้ซ้ๅฆ็ไธญ...",
|
| 1741 |
-
'zh': "โก ้ซ้ๅค็ไธญ..."
|
| 1742 |
-
}
|
| 1743 |
-
history_with_message = history + [
|
| 1744 |
-
{"role": "user", "content": message},
|
| 1745 |
-
{"role": "assistant", "content": processing_msg.get(detected_lang, processing_msg['en'])}
|
| 1746 |
-
]
|
| 1747 |
-
yield history_with_message, "", ""
|
| 1748 |
-
|
| 1749 |
-
# ๋น๋๊ธฐ ๊ฒ์์ ๋๊ธฐ์ ์ผ๋ก ์คํ
|
| 1750 |
-
async def search_wrapper():
|
| 1751 |
-
return await system.search.search_async(message, count=search_count, lang=detected_lang)
|
| 1752 |
-
|
| 1753 |
-
loop = asyncio.new_event_loop()
|
| 1754 |
-
asyncio.set_event_loop(loop)
|
| 1755 |
-
search_results = loop.run_until_complete(search_wrapper())
|
| 1756 |
-
|
| 1757 |
-
if search_results:
|
| 1758 |
-
ref_label = {
|
| 1759 |
-
'ko': "๐ ์ฐธ๊ณ ์๋ฃ",
|
| 1760 |
-
'en': "๐ References",
|
| 1761 |
-
'ja': "๐ ๅ่่ณๆ",
|
| 1762 |
-
'zh': "๐ ๅ่่ตๆ"
|
| 1763 |
-
}
|
| 1764 |
-
search_display = f"{ref_label.get(detected_lang, ref_label['en'])}\n\n"
|
| 1765 |
-
for i, result in enumerate(search_results[:3], 1):
|
| 1766 |
-
search_display += f"**{i}. [{result['title'][:50]}]({result['url']})**\n"
|
| 1767 |
-
search_display += f" {result['description'][:100]}...\n\n"
|
| 1768 |
-
|
| 1769 |
-
# ์ฌ์ฉ์ ๋ฉ์์ง ์ถ๊ฐ
|
| 1770 |
-
current_history = history + [{"role": "user", "content": message}]
|
| 1771 |
-
|
| 1772 |
-
# ์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ์ ์ํ ๋น๋๊ธฐ ์ฒ๋ฆฌ
|
| 1773 |
-
async def stream_responses():
|
| 1774 |
-
"""์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ ์ ๋๋ ์ดํฐ"""
|
| 1775 |
-
async for response, thoughts in system.parallel_process_agents(
|
| 1776 |
-
query=message,
|
| 1777 |
-
search_results=search_results,
|
| 1778 |
-
show_progress=show_agent_thoughts,
|
| 1779 |
-
lang=detected_lang
|
| 1780 |
-
):
|
| 1781 |
-
yield response, thoughts
|
| 1782 |
-
|
| 1783 |
-
# ์ ์ด๋ฒคํธ ๋ฃจํ์์ ์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ
|
| 1784 |
-
loop = asyncio.new_event_loop()
|
| 1785 |
-
asyncio.set_event_loop(loop)
|
| 1786 |
-
|
| 1787 |
-
# ๋น๋๊ธฐ ์ ๋๋ ์ดํฐ๋ฅผ ๋๊ธฐ์ ์ผ๋ก ์ํ
|
| 1788 |
-
gen = stream_responses()
|
| 1789 |
-
|
| 1790 |
-
while True:
|
| 1791 |
-
try:
|
| 1792 |
-
# ๋ค์ ํญ๋ชฉ ๊ฐ์ ธ์ค๊ธฐ
|
| 1793 |
-
task = asyncio.ensure_future(gen.__anext__(), loop=loop)
|
| 1794 |
-
response, thoughts = loop.run_until_complete(task)
|
| 1795 |
-
|
| 1796 |
-
# ์ค์๊ฐ ์
๋ฐ์ดํธ
|
| 1797 |
-
updated_history = current_history + [
|
| 1798 |
-
{"role": "assistant", "content": response}
|
| 1799 |
-
]
|
| 1800 |
-
yield updated_history, thoughts, search_display
|
| 1801 |
-
|
| 1802 |
-
except StopAsyncIteration:
|
| 1803 |
-
break
|
| 1804 |
-
|
| 1805 |
-
except Exception as e:
|
| 1806 |
-
error_history = history + [
|
| 1807 |
-
{"role": "user", "content": message},
|
| 1808 |
-
{"role": "assistant", "content": f"โ Error: {str(e)}"}
|
| 1809 |
-
]
|
| 1810 |
-
yield error_history, "", ""
|
| 1811 |
-
finally:
|
| 1812 |
-
# ๋ฃจํ ์ ๋ฆฌ
|
| 1813 |
-
try:
|
| 1814 |
-
loop.close()
|
| 1815 |
-
except:
|
| 1816 |
-
pass
|
| 1817 |
-
|
| 1818 |
-
# Gradio ์ธํฐํ์ด์ค
|
| 1819 |
-
with gr.Blocks(
|
| 1820 |
-
title="โก Speed-Optimized Multi-Agent System (No Cache)",
|
| 1821 |
-
theme=gr.themes.Soft(),
|
| 1822 |
-
css="""
|
| 1823 |
-
.gradio-container {
|
| 1824 |
-
max-width: 1400px !important;
|
| 1825 |
-
margin: auto !important;
|
| 1826 |
-
}
|
| 1827 |
-
"""
|
| 1828 |
-
) as demo:
|
| 1829 |
-
gr.Markdown("""
|
| 1830 |
-
# โก Enhanced Multi-Agent RAG System (์บ์ฑ ์ ๊ฑฐ ๋ฒ์ )
|
| 1831 |
-
**Complex questions processed within 5-8 seconds | Multi-language support**
|
| 1832 |
-
|
| 1833 |
-
**Optimization Features:**
|
| 1834 |
-
- ๐ Parallel Processing: Concurrent agent execution
|
| 1835 |
-
- โก Stream Buffering: Network optimization
|
| 1836 |
-
- ๐ฏ Early Termination: Complete immediately when quality is met
|
| 1837 |
-
- ๐ Multi-language: Auto-detect Korean/English/Japanese/Chinese
|
| 1838 |
-
- โ **Caching Disabled**: ์บ์ฑ ๊ธฐ๋ฅ ์ ๊ฑฐ๋จ
|
| 1839 |
-
""")
|
| 1840 |
-
|
| 1841 |
-
with gr.Row():
|
| 1842 |
-
with gr.Column(scale=3):
|
| 1843 |
-
chatbot = gr.Chatbot(
|
| 1844 |
-
height=500,
|
| 1845 |
-
label="๐ฌ Chat",
|
| 1846 |
-
type="messages"
|
| 1847 |
-
)
|
| 1848 |
-
|
| 1849 |
-
msg = gr.Textbox(
|
| 1850 |
-
label="Enter complex question",
|
| 1851 |
-
placeholder="Enter complex questions requiring analysis, strategy, or creative solutions...",
|
| 1852 |
-
lines=3
|
| 1853 |
-
)
|
| 1854 |
-
|
| 1855 |
-
with gr.Row():
|
| 1856 |
-
submit = gr.Button("โก High-Speed Process", variant="primary")
|
| 1857 |
-
clear = gr.Button("๐ Reset")
|
| 1858 |
-
|
| 1859 |
-
with gr.Accordion("๐ค Agent Processing", open=False):
|
| 1860 |
-
agent_thoughts = gr.Markdown()
|
| 1861 |
-
|
| 1862 |
-
with gr.Accordion("๐ Search Sources", open=False):
|
| 1863 |
-
search_sources = gr.Markdown()
|
| 1864 |
-
|
| 1865 |
-
with gr.Column(scale=1):
|
| 1866 |
-
gr.Markdown("**โ๏ธ Settings**")
|
| 1867 |
-
|
| 1868 |
-
language_mode = gr.Radio(
|
| 1869 |
-
choices=["Auto", "Korean", "English", "Japanese", "Chinese"],
|
| 1870 |
-
value="Auto",
|
| 1871 |
-
label="๐ Language Mode"
|
| 1872 |
-
)
|
| 1873 |
-
|
| 1874 |
-
use_search = gr.Checkbox(
|
| 1875 |
-
label="๐ Use Web Search",
|
| 1876 |
-
value=True
|
| 1877 |
-
)
|
| 1878 |
-
|
| 1879 |
-
show_agent_thoughts = gr.Checkbox(
|
| 1880 |
-
label="๐ง Show Processing",
|
| 1881 |
-
value=True
|
| 1882 |
-
)
|
| 1883 |
-
|
| 1884 |
-
search_count = gr.Slider(
|
| 1885 |
-
minimum=3,
|
| 1886 |
-
maximum=10,
|
| 1887 |
-
value=5,
|
| 1888 |
-
step=1,
|
| 1889 |
-
label="Search Results Count"
|
| 1890 |
-
)
|
| 1891 |
-
|
| 1892 |
-
gr.Markdown("""
|
| 1893 |
-
**โก Optimization Status**
|
| 1894 |
-
|
| 1895 |
-
**Active Optimizations:**
|
| 1896 |
-
- โ
Parallel Processing
|
| 1897 |
-
- โ ~~Smart Caching~~ (์ ๊ฑฐ๋จ)
|
| 1898 |
-
- โ
Buffer Streaming
|
| 1899 |
-
- โ
Early Termination
|
| 1900 |
-
- โ
Compressed Prompts
|
| 1901 |
-
- โ
Multi-language Support
|
| 1902 |
-
- โ
Error Recovery
|
| 1903 |
-
|
| 1904 |
-
**Expected Processing Time:**
|
| 1905 |
-
- Simple Query: 3-5 seconds
|
| 1906 |
-
- Complex Query: 5-8 seconds
|
| 1907 |
-
- Very Complex: 8-12 seconds
|
| 1908 |
-
""")
|
| 1909 |
-
|
| 1910 |
-
# ๋ณต์กํ ์ง๋ฌธ ์์ (๋ค๊ตญ์ด)
|
| 1911 |
-
gr.Examples(
|
| 1912 |
-
examples=[
|
| 1913 |
-
# Korean
|
| 1914 |
-
"AI ๊ธฐ์ ์ด ํฅํ 10๋
๊ฐ ํ๊ตญ ๊ฒฝ์ ์ ๋ฏธ์น ์ํฅ์ ๋ค๊ฐ๋๋ก ๋ถ์ํ๊ณ ๋์ ์ ๋ต์ ์ ์ํด์ค",
|
| 1915 |
-
"์คํํธ์
์ด ๋๊ธฐ์
๊ณผ ๊ฒฝ์ํ๊ธฐ ์ํ ํ์ ์ ์ธ ์ ๋ต์ ๋จ๊ณ๋ณ๋ก ์๋ฆฝํด์ค",
|
| 1916 |
-
# English
|
| 1917 |
-
"Analyze the multifaceted impact of quantum computing on current encryption systems and propose alternatives",
|
| 1918 |
-
"Design 5 innovative business models for climate change mitigation with practical implementation details",
|
| 1919 |
-
# Japanese
|
| 1920 |
-
"ใกใฟใใผในๆไปฃใฎๆ่ฒ้ฉๆฐๆนๆกใๅฎ่ฃ
ๅฏ่ฝใชใฌใใซใงๆๆกใใฆใใ ใใ",
|
| 1921 |
-
# Chinese
|
| 1922 |
-
"ๅๆไบบๅทฅๆบ่ฝๅฏนๆชๆฅๅๅนดๅ
จ็็ปๆต็ๅฝฑๅๅนถๆๅบๅบๅฏน็ญ็ฅ"
|
| 1923 |
-
],
|
| 1924 |
-
inputs=msg
|
| 1925 |
-
)
|
| 1926 |
-
|
| 1927 |
-
# ์ด๋ฒคํธ ๋ฐ์ธ๋ฉ
|
| 1928 |
-
submit.click(
|
| 1929 |
-
process_query_optimized,
|
| 1930 |
-
inputs=[msg, chatbot, use_search, show_agent_thoughts, search_count, language_mode],
|
| 1931 |
-
outputs=[chatbot, agent_thoughts, search_sources]
|
| 1932 |
-
).then(
|
| 1933 |
-
lambda: "",
|
| 1934 |
-
None,
|
| 1935 |
-
msg
|
| 1936 |
-
)
|
| 1937 |
-
|
| 1938 |
-
msg.submit(
|
| 1939 |
-
process_query_optimized,
|
| 1940 |
-
inputs=[msg, chatbot, use_search, show_agent_thoughts, search_count, language_mode],
|
| 1941 |
-
outputs=[chatbot, agent_thoughts, search_sources]
|
| 1942 |
-
).then(
|
| 1943 |
-
lambda: "",
|
| 1944 |
-
None,
|
| 1945 |
-
msg
|
| 1946 |
-
)
|
| 1947 |
-
|
| 1948 |
-
clear.click(
|
| 1949 |
-
lambda: ([], "", ""),
|
| 1950 |
-
None,
|
| 1951 |
-
[chatbot, agent_thoughts, search_sources]
|
| 1952 |
-
)
|
| 1953 |
-
|
| 1954 |
-
return demo
|
| 1955 |
-
|
| 1956 |
-
|
| 1957 |
-
# ============================================================================
|
| 1958 |
-
# ๋ฉ์ธ ์คํ
|
| 1959 |
-
# ============================================================================
|
| 1960 |
-
|
| 1961 |
-
if __name__ == "__main__":
|
| 1962 |
-
print("""
|
| 1963 |
-
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 1964 |
-
โ โก Speed-Optimized Multi-Agent System (No Cache) โก โ
|
| 1965 |
-
โ โ
|
| 1966 |
-
โ High-speed AI system processing complex questions โ
|
| 1967 |
-
โ โ
|
| 1968 |
-
โ Features: โ
|
| 1969 |
-
โ โข Multi-language support (KO/EN/JA/ZH) โ
|
| 1970 |
-
โ โข Improved error recovery โ
|
| 1971 |
-
โ โข NO CACHING (์บ์ฑ ๊ธฐ๋ฅ ์ ๊ฑฐ๋จ) โ
|
| 1972 |
-
โ โข Adaptive stream buffering โ
|
| 1973 |
-
โ โข Response cleaning & formatting โ
|
| 1974 |
-
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 1975 |
-
""")
|
| 1976 |
-
|
| 1977 |
-
# API ํค ํ์ธ
|
| 1978 |
-
if not os.getenv("FIREWORKS_API_KEY"):
|
| 1979 |
-
print("\nโ ๏ธ FIREWORKS_API_KEY is not set.")
|
| 1980 |
-
|
| 1981 |
-
if not os.getenv("BRAVE_SEARCH_API_KEY"):
|
| 1982 |
-
print("\nโ ๏ธ BRAVE_SEARCH_API_KEY is not set.")
|
| 1983 |
-
|
| 1984 |
-
# Gradio ์ฑ ์คํ
|
| 1985 |
-
demo = create_optimized_gradio_interface()
|
| 1986 |
-
|
| 1987 |
-
is_hf_spaces = os.getenv("SPACE_ID") is not None
|
| 1988 |
-
|
| 1989 |
-
if is_hf_spaces:
|
| 1990 |
-
print("\n๐ค Running in optimized mode on Hugging Face Spaces (No Cache)...")
|
| 1991 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 1992 |
-
else:
|
| 1993 |
-
print("\n๐ป Running in optimized mode on local environment (No Cache)...")
|
| 1994 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, share=False), '', response, flags=re.MULTILINE)
|
| 1995 |
-
|
| 1996 |
-
# ๋งํฌ๋ค์ด ํค๋ ์ ๊ฑฐ
|
| 1997 |
-
response = re.sub(r'^#{1,6}\s+', '', response, flags=re.MULTILINE)
|
| 1998 |
-
|
| 1999 |
-
# ๋ณผ๋ ํ
์คํธ ์ ๊ฑฐ (**text** ๋๋ __text__)
|
| 2000 |
-
response = re.sub(r'\*\*(.*?)\*\*', r'\1', response)
|
| 2001 |
-
response = re.sub(r'__(.*?)__', r'\1', response)
|
| 2002 |
-
|
| 2003 |
-
# ์ดํค๋ฆญ ํ
์คํธ ์ ๊ฑฐ (*text* ๋๋ _text_)
|
| 2004 |
-
response = re.sub(r'\*(.*?)\*', r'\1', response)
|
| 2005 |
-
response = re.sub(r'_(.*?)_', r'\1', response)
|
| 2006 |
-
|
| 2007 |
-
# ์ฝ๋ ๋ธ๋ก ์ ๊ฑฐ (```code```)
|
| 2008 |
-
response = re.sub(r'```[\s\S]*?```', '', response)
|
| 2009 |
-
|
| 2010 |
-
# ์ธ๋ผ์ธ ์ฝ๋ ์ ๊ฑฐ (`code`)
|
| 2011 |
-
response = re.sub(r'`([^`]*)`', r'\1', response)
|
| 2012 |
-
|
| 2013 |
-
# ๋ถํ์ํ ๊ตฌ๋ถ์ ์ ๊ฑฐ (---, ***, ___)
|
| 2014 |
-
response = re.sub(r'^[\*\-_]{3,}
|
| 2015 |
-
|
| 2016 |
-
|
| 2017 |
-
# ============================================================================
|
| 2018 |
-
# ํตํฉ ์ต์ ํ ๋ฉํฐ ์์ด์ ํธ ์์คํ
(์บ์ฑ ์ ๊ฑฐ ๋ฒ์ )
|
| 2019 |
-
# ============================================================================
|
| 2020 |
-
|
| 2021 |
-
class SpeedOptimizedMultiAgentSystem:
|
| 2022 |
-
"""์๋ ์ต์ ํ๋ ๋ฉํฐ ์์ด์ ํธ ์์คํ
(์บ์ฑ ์์)"""
|
| 2023 |
-
|
| 2024 |
-
def __init__(self):
|
| 2025 |
-
self.llm = OptimizedFireworksClient()
|
| 2026 |
-
self.search = AsyncBraveSearch()
|
| 2027 |
-
self.reasoning = LightweightReasoningChain()
|
| 2028 |
-
self.quality_checker = QualityChecker()
|
| 2029 |
-
self.streaming = OptimizedStreaming()
|
| 2030 |
-
self.language_detector = LanguageDetector()
|
| 2031 |
-
self.response_cleaner = ResponseCleaner()
|
| 2032 |
-
|
| 2033 |
-
# ๋ณ๋ ฌ ์ฒ๋ฆฌ ํ
|
| 2034 |
-
self.executor = ThreadPoolExecutor(max_workers=4)
|
| 2035 |
-
|
| 2036 |
-
def _init_compact_prompts(self, lang: str = 'ko') -> Dict:
|
| 2037 |
-
"""์์ถ๋ ๊ณ ํจ์จ ํ๋กฌํํธ (์ธ์ด๋ณ)"""
|
| 2038 |
-
prompts = {
|
| 2039 |
-
'ko': {
|
| 2040 |
-
AgentRole.SUPERVISOR: """[๊ฐ๋
์-๊ตฌ์กฐ์ค๊ณ]
|
| 2041 |
-
์ฆ์๋ถ์: ํต์ฌ์๋+ํ์์ ๋ณด+๋ต๋ณ๊ตฌ์กฐ
|
| 2042 |
-
์ถ๋ ฅ: 5๊ฐ ํต์ฌํฌ์ธํธ(๊ฐ 1๋ฌธ์ฅ)
|
| 2043 |
-
์ถ๋ก ์ฒด๊ณ ๋ช
์""",
|
| 2044 |
-
|
| 2045 |
-
AgentRole.CREATIVE: """[์ฐฝ์์ฑ์์ฑ์]
|
| 2046 |
-
์
๋ ฅ๊ตฌ์กฐ ๋ฐ๋ผ ์ฐฝ์์ ํ์ฅ
|
| 2047 |
-
์ค์ฉ์์+ํ์ ์ ๊ทผ+๊ตฌ์ฒด์กฐ์ธ
|
| 2048 |
-
๋ถํ์์ค๋ช
์ ๊ฑฐ""",
|
| 2049 |
-
|
| 2050 |
-
AgentRole.CRITIC: """[๋นํ์-๊ฒ์ฆ]
|
| 2051 |
-
์ ์๊ฒํ : ์ ํ์ฑ/๋
ผ๋ฆฌ์ฑ/์ค์ฉ์ฑ
|
| 2052 |
-
๊ฐ์ ํฌ์ธํธ 3๊ฐ๋ง
|
| 2053 |
-
๊ฐ 2๋ฌธ์ฅ ์ด๋ด""",
|
| 2054 |
-
|
| 2055 |
-
AgentRole.FINALIZER: """[์ต์ข
ํตํฉ]
|
| 2056 |
-
๋ชจ๋ ์๊ฒฌ ์ข
ํฉโ์ต์ ๋ต๋ณ
|
| 2057 |
-
๋ช
ํ๊ตฌ์กฐ+์ค์ฉ์ ๋ณด+์ฐฝ์๊ท ํ
|
| 2058 |
-
๋ฐ๋ก ํต์ฌ ๋ด์ฉ๋ถํฐ ์์. ๋ถํ์ํ ํค๋๋ ๋งํฌ์
์์ด. ๋งํฌ๋ค์ด ํค๋(#, ##, ###) ์ฌ์ฉ ๊ธ์ง."""
|
| 2059 |
-
},
|
| 2060 |
-
'en': {
|
| 2061 |
-
AgentRole.SUPERVISOR: """[Supervisor-Structure]
|
| 2062 |
-
Immediate analysis: core intent+required info+answer structure
|
| 2063 |
-
Output: 5 key points (1 sentence each)
|
| 2064 |
-
Clear reasoning framework""",
|
| 2065 |
-
|
| 2066 |
-
AgentRole.CREATIVE: """[Creative Generator]
|
| 2067 |
-
Follow structure, expand creatively
|
| 2068 |
-
Practical examples+innovative approach+specific advice
|
| 2069 |
-
Remove unnecessary explanations""",
|
| 2070 |
-
|
| 2071 |
-
AgentRole.CRITIC: """[Critic-Verification]
|
| 2072 |
-
Quick review: accuracy/logic/practicality
|
| 2073 |
-
Only 3 improvement points
|
| 2074 |
-
Max 2 sentences each""",
|
| 2075 |
-
|
| 2076 |
-
AgentRole.FINALIZER: """[Final Integration]
|
| 2077 |
-
Synthesize all inputsโoptimal answer
|
| 2078 |
-
Clear structure+practical info+creative balance
|
| 2079 |
-
Start with core content directly. No unnecessary headers or markup. No markdown headers (#, ##, ###)."""
|
| 2080 |
-
},
|
| 2081 |
-
'ja': {
|
| 2082 |
-
AgentRole.SUPERVISOR: """[็ฃ็ฃ่
-ๆง้ ่จญ่จ]
|
| 2083 |
-
ๅณๆๅๆ๏ผๆ ธๅฟๆๅณ+ๅฟ
่ฆๆ
ๅ ฑ+ๅ็ญๆง้
|
| 2084 |
-
ๅบๅ๏ผ5ใคใฎๆ ธๅฟใใคใณใ๏ผๅ1ๆ๏ผ
|
| 2085 |
-
ๆจ่ซไฝ็ณปๆ็คบ""",
|
| 2086 |
-
|
| 2087 |
-
AgentRole.CREATIVE: """[ๅต้ ๆง็ๆ่
]
|
| 2088 |
-
ๅ
ฅๅๆง้ ใซๅพใฃใฆๅต้ ็ๆกๅผต
|
| 2089 |
-
ๅฎ็จไพ+้ฉๆฐ็ใขใใญใผใ+ๅ
ทไฝ็ใขใใใคใน
|
| 2090 |
-
ไธ่ฆใช่ชฌๆๅ้ค""",
|
| 2091 |
-
|
| 2092 |
-
AgentRole.CRITIC: """[ๆน่ฉ่
-ๆค่จผ]
|
| 2093 |
-
่ฟ
้ใฌใใฅใผ๏ผๆญฃ็ขบๆง/่ซ็ๆง/ๅฎ็จๆง
|
| 2094 |
-
ๆนๅใใคใณใ3ใคใฎใฟ
|
| 2095 |
-
ๅ2ๆไปฅๅ
""",
|
| 2096 |
-
|
| 2097 |
-
AgentRole.FINALIZER: """[ๆ็ต็ตฑๅ]
|
| 2098 |
-
ๅ
จๆ่ฆ็ตฑๅโๆ้ฉๅ็ญ
|
| 2099 |
-
ๆ็ขบๆง้ +ๅฎ็จๆ
ๅ ฑ+ๅต้ ๆงใใฉใณใน
|
| 2100 |
-
ๆ ธๅฟๅ
ๅฎนใใ็ดๆฅ้ๅงใไธ่ฆใชใใใใผใใใผใฏใขใใใชใใใใผใฏใใฆใณใใใใผ๏ผ#ใ##ใ###๏ผไฝฟ็จ็ฆๆญขใ"""
|
| 2101 |
-
},
|
| 2102 |
-
'zh': {
|
| 2103 |
-
AgentRole.SUPERVISOR: """[ไธป็ฎก-็ปๆ่ฎพ่ฎก]
|
| 2104 |
-
็ซๅณๅๆ๏ผๆ ธๅฟๆๅพ+ๆ้ไฟกๆฏ+็ญๆก็ปๆ
|
| 2105 |
-
่พๅบ๏ผ5ไธชๆ ธๅฟ่ฆ็น๏ผๆฏไธช1ๅฅ๏ผ
|
| 2106 |
-
ๆจ็ไฝ็ณปๆ็กฎ""",
|
| 2107 |
-
|
| 2108 |
-
AgentRole.CREATIVE: """[ๅๆ็ๆๅจ]
|
| 2109 |
-
ๆ็ปๆๅ้ ๆงๆฉๅฑ
|
| 2110 |
-
ๅฎ็จ็คบไพ+ๅๆฐๆนๆณ+ๅ
ทไฝๅปบ่ฎฎ
|
| 2111 |
-
ๅ ้คไธๅฟ
่ฆ็่งฃ้""",
|
| 2112 |
-
|
| 2113 |
-
AgentRole.CRITIC: """[่ฏ่ฎบๅฎถ-้ช่ฏ]
|
| 2114 |
-
ๅฟซ้ๅฎกๆฅ๏ผๅ็กฎๆง/้ป่พๆง/ๅฎ็จๆง
|
| 2115 |
-
ไป
3ไธชๆน่ฟ็น
|
| 2116 |
-
ๆฏไธชๆๅค2ๅฅ""",
|
| 2117 |
-
|
| 2118 |
-
AgentRole.FINALIZER: """[ๆ็ปๆดๅ]
|
| 2119 |
-
็ปผๅๆๆๆ่งโๆไฝณ็ญๆก
|
| 2120 |
-
ๆธ
ๆฐ็ปๆ+ๅฎ็จไฟกๆฏ+ๅๆๅนณ่กก
|
| 2121 |
-
็ดๆฅไปๆ ธๅฟๅ
ๅฎนๅผๅงใๆ ้ไธๅฟ
่ฆ็ๆ ้ขๆๆ ่ฎฐใ็ฆๆญขไฝฟ็จMarkdownๆ ้ข๏ผ#ใ##ใ###๏ผใ"""
|
| 2122 |
-
}
|
| 2123 |
-
}
|
| 2124 |
-
|
| 2125 |
-
return prompts.get(lang, prompts['en'])
|
| 2126 |
-
|
| 2127 |
-
async def parallel_process_agents(
|
| 2128 |
-
self,
|
| 2129 |
-
query: str,
|
| 2130 |
-
search_results: List[Dict],
|
| 2131 |
-
show_progress: bool = True,
|
| 2132 |
-
lang: str = None
|
| 2133 |
-
) -> AsyncGenerator[Tuple[str, str], None]:
|
| 2134 |
-
"""๋ณ๋ ฌ ์ฒ๋ฆฌ ํ์ดํ๋ผ์ธ (์บ์ฑ ์์)"""
|
| 2135 |
-
|
| 2136 |
-
start_time = time.time()
|
| 2137 |
-
|
| 2138 |
-
# ์ธ์ด ์๋ ๊ฐ์ง
|
| 2139 |
-
if lang is None:
|
| 2140 |
-
lang = self.language_detector.detect_language(query)
|
| 2141 |
-
|
| 2142 |
-
# ์ธ์ด๋ณ ํ๋กฌํํธ ์ค์
|
| 2143 |
-
self.compact_prompts = self._init_compact_prompts(lang)
|
| 2144 |
-
|
| 2145 |
-
search_context = self._format_search_results(search_results)
|
| 2146 |
-
accumulated_response = ""
|
| 2147 |
-
agent_thoughts = ""
|
| 2148 |
-
|
| 2149 |
-
# ์ถ๋ก ํจํด ๊ฒฐ์
|
| 2150 |
-
reasoning_pattern = self.reasoning.get_reasoning_pattern(query, lang)
|
| 2151 |
-
|
| 2152 |
-
try:
|
| 2153 |
-
# === 1๋จ๊ณ: ๊ฐ๋
์ + ๊ฒ์ ๋ณ๋ ฌ ์คํ ===
|
| 2154 |
-
if show_progress:
|
| 2155 |
-
progress_msg = {
|
| 2156 |
-
'ko': "๐ ๋ณ๋ ฌ ์ฒ๋ฆฌ ์์\n๐ ๊ฐ๋
์ ๋ถ์ + ๐ ์ถ๊ฐ ๊ฒ์ ๋์ ์งํ...\n\n",
|
| 2157 |
-
'en': "๐ Starting parallel processing\n๐ Supervisor analysis + ๐ Additional search in progress...\n\n",
|
| 2158 |
-
'ja': "๐ ไธฆๅๅฆ็้ๅง\n๐ ็ฃ็ฃ่
ๅๆ + ๐ ่ฟฝๅ ๆค็ดขๅๆ้ฒ่กไธญ...\n\n",
|
| 2159 |
-
'zh': "๐ ๅผๅงๅนถ่กๅค็\n๐ ไธป็ฎกๅๆ + ๐ ้ๅ ๆ็ดขๅๆถ่ฟ่ก...\n\n"
|
| 2160 |
-
}
|
| 2161 |
-
agent_thoughts = progress_msg.get(lang, progress_msg['en'])
|
| 2162 |
-
yield accumulated_response, agent_thoughts
|
| 2163 |
-
|
| 2164 |
-
# ๊ฐ๋
์ ํ๋กฌํํธ (์ธ์ด๋ณ)
|
| 2165 |
-
supervisor_prompt_templates = {
|
| 2166 |
-
'ko': f"""
|
| 2167 |
-
์ง๋ฌธ: {query}
|
| 2168 |
-
๊ฒ์๊ฒฐ๊ณผ: {search_context}
|
| 2169 |
-
์ถ๋ก ํจํด: {reasoning_pattern}
|
| 2170 |
-
์ฆ์ ํต์ฌ๊ตฌ์กฐ 5๊ฐ ์ ์""",
|
| 2171 |
-
'en': f"""
|
| 2172 |
-
Question: {query}
|
| 2173 |
-
Search results: {search_context}
|
| 2174 |
-
Reasoning pattern: {reasoning_pattern}
|
| 2175 |
-
Immediately provide 5 key structures""",
|
| 2176 |
-
'ja': f"""
|
| 2177 |
-
่ณชๅ: {query}
|
| 2178 |
-
ๆค็ดข็ตๆ: {search_context}
|
| 2179 |
-
ๆจ่ซใใฟใผใณ: {reasoning_pattern}
|
| 2180 |
-
ๅณๅบงใซ5ใคใฎๆ ธๅฟๆง้ ใๆ็คบ""",
|
| 2181 |
-
'zh': f"""
|
| 2182 |
-
้ฎ้ข: {query}
|
| 2183 |
-
ๆ็ดข็ปๆ: {search_context}
|
| 2184 |
-
ๆจ็ๆจกๅผ: {reasoning_pattern}
|
| 2185 |
-
็ซๅณๆไพ5ไธชๆ ธๅฟ็ปๆ"""
|
| 2186 |
-
}
|
| 2187 |
-
|
| 2188 |
-
supervisor_prompt = supervisor_prompt_templates.get(lang, supervisor_prompt_templates['en'])
|
| 2189 |
-
|
| 2190 |
-
supervisor_response = ""
|
| 2191 |
-
supervisor_task = self.llm.chat_stream_async(
|
| 2192 |
-
messages=[
|
| 2193 |
-
{"role": "system", "content": self.compact_prompts[AgentRole.SUPERVISOR]},
|
| 2194 |
-
{"role": "user", "content": supervisor_prompt}
|
| 2195 |
-
],
|
| 2196 |
-
temperature=0.3,
|
| 2197 |
-
max_tokens=500
|
| 2198 |
-
)
|
| 2199 |
-
|
| 2200 |
-
# ๊ฐ๋
์ ์คํธ๋ฆฌ๋ฐ (๋ฒํผ๋ง)
|
| 2201 |
-
async for chunk in self.streaming.buffer_and_yield(supervisor_task):
|
| 2202 |
-
supervisor_response += chunk
|
| 2203 |
-
if show_progress and len(supervisor_response) < 300:
|
| 2204 |
-
supervisor_label = {
|
| 2205 |
-
'ko': "๐ ๊ฐ๋
์ ๋ถ์",
|
| 2206 |
-
'en': "๐ Supervisor Analysis",
|
| 2207 |
-
'ja': "๐ ็ฃ็ฃ่
ๅๆ",
|
| 2208 |
-
'zh': "๐ ไธป็ฎกๅๆ"
|
| 2209 |
-
}
|
| 2210 |
-
agent_thoughts = f"{supervisor_label.get(lang, supervisor_label['en'])}\n{supervisor_response[:300]}...\n\n"
|
| 2211 |
-
yield accumulated_response, agent_thoughts
|
| 2212 |
-
|
| 2213 |
-
# === 2๋จ๊ณ: ์ฐฝ์์ฑ + ๋นํ ์ค๋น ๋ณ๋ ฌ ===
|
| 2214 |
-
if show_progress:
|
| 2215 |
-
creative_msg = {
|
| 2216 |
-
'ko': "๐จ ์ฐฝ์์ฑ ์์ฑ์ + ๐ ๋นํ์ ์ค๋น...\n\n",
|
| 2217 |
-
'en': "๐จ Creative Generator + ๐ Critic preparing...\n\n",
|
| 2218 |
-
'ja': "๐จ ๅต้ ๆง็ๆ่
+ ๐ ๆน่ฉ่
ๆบๅไธญ...\n\n",
|
| 2219 |
-
'zh': "๐จ ๅๆ็ๆๅจ + ๐ ่ฏ่ฎบๅฎถๅๅคไธญ...\n\n"
|
| 2220 |
-
}
|
| 2221 |
-
agent_thoughts += creative_msg.get(lang, creative_msg['en'])
|
| 2222 |
-
yield accumulated_response, agent_thoughts
|
| 2223 |
-
|
| 2224 |
-
# ์ฐฝ์์ฑ ์์ฑ ์์ (์ธ์ด๋ณ)
|
| 2225 |
-
creative_prompt_templates = {
|
| 2226 |
-
'ko': f"""
|
| 2227 |
-
์ง๋ฌธ: {query}
|
| 2228 |
-
๊ฐ๋
์๊ตฌ์กฐ: {supervisor_response}
|
| 2229 |
-
๊ฒ์๊ฒฐ๊ณผ: {search_context}
|
| 2230 |
-
์ฐฝ์์ +์ค์ฉ์ ๋ต๋ณ ์ฆ์์์ฑ""",
|
| 2231 |
-
'en': f"""
|
| 2232 |
-
Question: {query}
|
| 2233 |
-
Supervisor structure: {supervisor_response}
|
| 2234 |
-
Search results: {search_context}
|
| 2235 |
-
Generate creative+practical answer immediately""",
|
| 2236 |
-
'ja': f"""
|
| 2237 |
-
่ณชๅ: {query}
|
| 2238 |
-
็ฃ็ฃ่
ๆง้ : {supervisor_response}
|
| 2239 |
-
ๆค็ดข็ตๆ: {search_context}
|
| 2240 |
-
ๅต้ ็+ๅฎ็จ็ๅ็ญๅณๅบง็ๆ""",
|
| 2241 |
-
'zh': f"""
|
| 2242 |
-
้ฎ้ข: {query}
|
| 2243 |
-
ไธป็ฎก็ปๆ: {supervisor_response}
|
| 2244 |
-
ๆ็ดข็ปๆ: {search_context}
|
| 2245 |
-
็ซๅณ็ๆๅๆ+ๅฎ็จ็ญๆก"""
|
| 2246 |
-
}
|
| 2247 |
-
|
| 2248 |
-
creative_prompt = creative_prompt_templates.get(lang, creative_prompt_templates['en'])
|
| 2249 |
-
|
| 2250 |
-
creative_response = ""
|
| 2251 |
-
creative_partial = ""
|
| 2252 |
-
critic_started = False
|
| 2253 |
-
critic_response = ""
|
| 2254 |
-
|
| 2255 |
-
creative_task = self.llm.chat_stream_async(
|
| 2256 |
-
messages=[
|
| 2257 |
-
{"role": "system", "content": self.compact_prompts[AgentRole.CREATIVE]},
|
| 2258 |
-
{"role": "user", "content": creative_prompt}
|
| 2259 |
-
],
|
| 2260 |
-
temperature=0.8,
|
| 2261 |
-
max_tokens=1500
|
| 2262 |
-
)
|
| 2263 |
-
|
| 2264 |
-
# ์ฐฝ์์ฑ ์คํธ๋ฆฌ๋ฐ + ๋นํ์ ์กฐ๊ธฐ ์์
|
| 2265 |
-
async for chunk in self.streaming.buffer_and_yield(creative_task):
|
| 2266 |
-
creative_response += chunk
|
| 2267 |
-
creative_partial += chunk
|
| 2268 |
-
|
| 2269 |
-
# ์ฐฝ์์ฑ ์๋ต์ด 500์ ๋์ผ๋ฉด ๋นํ์ ์์
|
| 2270 |
-
if len(creative_partial) > 500 and not critic_started:
|
| 2271 |
-
critic_started = True
|
| 2272 |
-
|
| 2273 |
-
# ๋นํ์ ๋น๋๊ธฐ ์์ (์ธ์ด๋ณ)
|
| 2274 |
-
critic_prompt_templates = {
|
| 2275 |
-
'ko': f"""
|
| 2276 |
-
์๋ณธ์ง๋ฌธ: {query}
|
| 2277 |
-
์ฐฝ์์ฑ๋ต๋ณ(์ผ๋ถ): {creative_partial}
|
| 2278 |
-
์ ์๊ฒํ โ๊ฐ์ ์ 3๊ฐ""",
|
| 2279 |
-
'en': f"""
|
| 2280 |
-
Original question: {query}
|
| 2281 |
-
Creative answer (partial): {creative_partial}
|
| 2282 |
-
Quick reviewโ3 improvements""",
|
| 2283 |
-
'ja': f"""
|
| 2284 |
-
ๅ
ใฎ่ณชๅ: {query}
|
| 2285 |
-
ๅต้ ็ๅ็ญ๏ผไธ้จ๏ผ: {creative_partial}
|
| 2286 |
-
่ฟ
้ใฌใใฅใผโๆนๅ็น3ใค""",
|
| 2287 |
-
'zh': f"""
|
| 2288 |
-
ๅๅง้ฎ้ข: {query}
|
| 2289 |
-
ๅๆ็ญๆก๏ผ้จๅ๏ผ: {creative_partial}
|
| 2290 |
-
ๅฟซ้ๅฎกๆฅโ3ไธชๆน่ฟ็น"""
|
| 2291 |
-
}
|
| 2292 |
-
|
| 2293 |
-
critic_prompt = critic_prompt_templates.get(lang, critic_prompt_templates['en'])
|
| 2294 |
-
|
| 2295 |
-
critic_task = asyncio.create_task(
|
| 2296 |
-
self._run_critic_async(critic_prompt)
|
| 2297 |
-
)
|
| 2298 |
-
|
| 2299 |
-
if show_progress:
|
| 2300 |
-
display_creative = creative_response[:400] + "..." if len(creative_response) > 400 else creative_response
|
| 2301 |
-
creative_label = {
|
| 2302 |
-
'ko': "๐จ ์ฐฝ์์ฑ ์์ฑ์",
|
| 2303 |
-
'en': "๐จ Creative Generator",
|
| 2304 |
-
'ja': "๐จ ๅต้ ๆง็ๆ่
",
|
| 2305 |
-
'zh': "๐จ ๅๆ็ๆๅจ"
|
| 2306 |
-
}
|
| 2307 |
-
agent_thoughts = f"{creative_label.get(lang, creative_label['en'])}\n{display_creative}\n\n"
|
| 2308 |
-
yield accumulated_response, agent_thoughts
|
| 2309 |
-
|
| 2310 |
-
# ๋นํ์ ๊ฒฐ๊ณผ ๋๊ธฐ
|
| 2311 |
-
if critic_started:
|
| 2312 |
-
critic_response = await critic_task
|
| 2313 |
-
|
| 2314 |
-
if show_progress:
|
| 2315 |
-
critic_label = {
|
| 2316 |
-
'ko': "๐ ๋นํ์ ๊ฒํ ",
|
| 2317 |
-
'en': "๐ Critic Review",
|
| 2318 |
-
'ja': "๐ ๆน่ฉ่
ใฌใใฅใผ",
|
| 2319 |
-
'zh': "๐ ่ฏ่ฎบๅฎถๅฎกๆฅ"
|
| 2320 |
-
}
|
| 2321 |
-
agent_thoughts += f"{critic_label.get(lang, critic_label['en'])}\n{critic_response[:200]}...\n\n"
|
| 2322 |
-
yield accumulated_response, agent_thoughts
|
| 2323 |
-
|
| 2324 |
-
# === 3๋จ๊ณ: ํ์ง ์ฒดํฌ ๋ฐ ์กฐ๊ธฐ ์ข
๋ฃ ===
|
| 2325 |
-
quality_score, need_more = self.quality_checker.evaluate_response(
|
| 2326 |
-
creative_response, query, lang
|
| 2327 |
-
)
|
| 2328 |
-
|
| 2329 |
-
if not need_more and quality_score > 0.85:
|
| 2330 |
-
# ํ์ง์ด ์ถฉ๋ถํ ๋์ผ๋ฉด ๋ฐ๋ก ๋ฐํ
|
| 2331 |
-
accumulated_response = self.response_cleaner.clean_response(creative_response)
|
| 2332 |
-
|
| 2333 |
-
if show_progress:
|
| 2334 |
-
quality_msg = {
|
| 2335 |
-
'ko': f"โ
ํ์ง ์ถฉ์กฑ (์ ์: {quality_score:.2f})\n์กฐ๊ธฐ ์๋ฃ!\n",
|
| 2336 |
-
'en': f"โ
Quality met (score: {quality_score:.2f})\nEarly completion!\n",
|
| 2337 |
-
'ja': f"โ
ๅ่ณชๆบ่ถณ (ในใณใข: {quality_score:.2f})\nๆฉๆๅฎไบ!\n",
|
| 2338 |
-
'zh': f"โ
่ดจ้ๆปก่ถณ (ๅๆฐ: {quality_score:.2f})\nๆๅๅฎๆ!\n"
|
| 2339 |
-
}
|
| 2340 |
-
agent_thoughts += quality_msg.get(lang, quality_msg['en'])
|
| 2341 |
-
|
| 2342 |
-
yield accumulated_response, agent_thoughts
|
| 2343 |
-
return
|
| 2344 |
-
|
| 2345 |
-
# === 4๋จ๊ณ: ์ต์ข
ํตํฉ (์คํธ๋ฆฌ๋ฐ) ===
|
| 2346 |
-
if show_progress:
|
| 2347 |
-
final_msg = {
|
| 2348 |
-
'ko': "โ
์ต์ข
ํตํฉ ์ค...\n\n",
|
| 2349 |
-
'en': "โ
Final integration in progress...\n\n",
|
| 2350 |
-
'ja': "โ
ๆ็ต็ตฑๅไธญ...\n\n",
|
| 2351 |
-
'zh': "โ
ๆ็ปๆดๅไธญ...\n\n"
|
| 2352 |
-
}
|
| 2353 |
-
agent_thoughts += final_msg.get(lang, final_msg['en'])
|
| 2354 |
-
yield accumulated_response, agent_thoughts
|
| 2355 |
-
|
| 2356 |
-
# ์ต์ข
ํ๋กฌํํธ (์ธ์ด๋ณ)
|
| 2357 |
-
final_prompt_templates = {
|
| 2358 |
-
'ko': f"""
|
| 2359 |
-
์ง๋ฌธ: {query}
|
| 2360 |
-
์ฐฝ์์ฑ๋ต๋ณ: {creative_response}
|
| 2361 |
-
๋นํํผ๋๋ฐฑ: {critic_response}
|
| 2362 |
-
๊ฐ๋
์๊ตฌ์กฐ: {supervisor_response}
|
| 2363 |
-
์ต์ข
ํตํฉโ์๋ฒฝ๋ต๋ณ. ๋งํฌ๋ค์ด ํค๋(#, ##, ###) ์ฌ์ฉ ๊ธ์ง.""",
|
| 2364 |
-
'en': f"""
|
| 2365 |
-
Question: {query}
|
| 2366 |
-
Creative answer: {creative_response}
|
| 2367 |
-
Critic feedback: {critic_response}
|
| 2368 |
-
Supervisor structure: {supervisor_response}
|
| 2369 |
-
Final integrationโperfect answer. No markdown headers (#, ##, ###).""",
|
| 2370 |
-
'ja': f"""
|
| 2371 |
-
่ณชๅ: {query}
|
| 2372 |
-
ๅต้ ็ๅ็ญ: {creative_response}
|
| 2373 |
-
ๆน่ฉใใฃใผใใใใฏ: {critic_response}
|
| 2374 |
-
็ฃ็ฃ่
ๆง้ : {supervisor_response}
|
| 2375 |
-
ๆ็ต็ตฑๅโๅฎ็งใชๅ็ญใใใผใฏใใฆใณใใใใผ๏ผ#ใ##ใ###๏ผไฝฟ็จ็ฆๆญขใ""",
|
| 2376 |
-
'zh': f"""
|
| 2377 |
-
้ฎ้ข: {query}
|
| 2378 |
-
ๅๆ็ญๆก: {creative_response}
|
| 2379 |
-
่ฏ่ฎบๅ้ฆ: {critic_response}
|
| 2380 |
-
ไธป็ฎก็ปๆ: {supervisor_response}
|
| 2381 |
-
ๆ็ปๆดๅโๅฎ็พ็ญๆกใ็ฆๆญขไฝฟ็จMarkdownๆ ้ข๏ผ#ใ##ใ###๏ผใ"""
|
| 2382 |
-
}
|
| 2383 |
-
|
| 2384 |
-
final_prompt = final_prompt_templates.get(lang, final_prompt_templates['en'])
|
| 2385 |
-
|
| 2386 |
-
final_task = self.llm.chat_stream_async(
|
| 2387 |
-
messages=[
|
| 2388 |
-
{"role": "system", "content": self.compact_prompts[AgentRole.FINALIZER]},
|
| 2389 |
-
{"role": "user", "content": final_prompt}
|
| 2390 |
-
],
|
| 2391 |
-
temperature=0.5,
|
| 2392 |
-
max_tokens=2500
|
| 2393 |
-
)
|
| 2394 |
-
|
| 2395 |
-
# ์ต์ข
๋ต๋ณ ์คํธ๋ฆฌ๋ฐ
|
| 2396 |
-
accumulated_response = ""
|
| 2397 |
-
|
| 2398 |
-
async for chunk in final_task:
|
| 2399 |
-
accumulated_response += chunk
|
| 2400 |
-
# ์ค์๊ฐ ์ ๋ฆฌ
|
| 2401 |
-
cleaned_response = self.response_cleaner.clean_response(accumulated_response)
|
| 2402 |
-
yield cleaned_response, agent_thoughts
|
| 2403 |
-
|
| 2404 |
-
# ์ต์ข
์ ๋ฆฌ
|
| 2405 |
-
accumulated_response = self.response_cleaner.clean_response(accumulated_response)
|
| 2406 |
-
|
| 2407 |
-
# ์ฒ๋ฆฌ ์๊ฐ ์ถ๊ฐ (์ธ์ด๋ณ)
|
| 2408 |
-
processing_time = time.time() - start_time
|
| 2409 |
-
time_msg = {
|
| 2410 |
-
'ko': f"\n\n---\nโก ์ฒ๋ฆฌ ์๊ฐ: {processing_time:.1f}์ด",
|
| 2411 |
-
'en': f"\n\n---\nโก Processing time: {processing_time:.1f} seconds",
|
| 2412 |
-
'ja': f"\n\n---\nโก ๅฆ็ๆ้: {processing_time:.1f}็ง",
|
| 2413 |
-
'zh': f"\n\n---\nโก ๅค็ๆถ้ด: {processing_time:.1f}็ง"
|
| 2414 |
-
}
|
| 2415 |
-
accumulated_response += time_msg.get(lang, time_msg['en'])
|
| 2416 |
-
|
| 2417 |
-
yield accumulated_response, agent_thoughts
|
| 2418 |
-
|
| 2419 |
-
except Exception as e:
|
| 2420 |
-
error_msg = {
|
| 2421 |
-
'ko': f"โ ์ค๋ฅ ๋ฐ์: {str(e)}",
|
| 2422 |
-
'en': f"โ Error occurred: {str(e)}",
|
| 2423 |
-
'ja': f"โ ใจใฉใผ็บ็: {str(e)}",
|
| 2424 |
-
'zh': f"โ ๅ็้่ฏฏ: {str(e)}"
|
| 2425 |
-
}
|
| 2426 |
-
yield error_msg.get(lang, error_msg['en']), agent_thoughts
|
| 2427 |
-
|
| 2428 |
-
async def _run_critic_async(self, prompt: str) -> str:
|
| 2429 |
-
"""๋นํ์ ๋น๋๊ธฐ ์คํ with error handling"""
|
| 2430 |
-
try:
|
| 2431 |
-
response = ""
|
| 2432 |
-
async for chunk in self.llm.chat_stream_async(
|
| 2433 |
-
messages=[
|
| 2434 |
-
{"role": "system", "content": self.compact_prompts[AgentRole.CRITIC]},
|
| 2435 |
-
{"role": "user", "content": prompt}
|
| 2436 |
-
],
|
| 2437 |
-
temperature=0.2,
|
| 2438 |
-
max_tokens=500
|
| 2439 |
-
):
|
| 2440 |
-
response += chunk
|
| 2441 |
-
return response
|
| 2442 |
-
except Exception as e:
|
| 2443 |
-
# ์ธ์ด ๊ฐ์ง
|
| 2444 |
-
lang = 'ko' if '์ง๋ฌธ' in prompt else 'en'
|
| 2445 |
-
error_msg = {
|
| 2446 |
-
'ko': "๋นํ ์ฒ๋ฆฌ ์ค ์ค๋ฅ",
|
| 2447 |
-
'en': "Error during critic processing",
|
| 2448 |
-
'ja': "ๆน่ฉๅฆ็ไธญใฎใจใฉใผ",
|
| 2449 |
-
'zh': "่ฏ่ฎบๅค็ไธญๅบ้"
|
| 2450 |
-
}
|
| 2451 |
-
return error_msg.get(lang, error_msg['en'])
|
| 2452 |
-
|
| 2453 |
-
def _format_search_results(self, results: List[Dict]) -> str:
|
| 2454 |
-
"""๊ฒ์ ๊ฒฐ๊ณผ ์์ถ ํฌ๋งท"""
|
| 2455 |
-
if not results:
|
| 2456 |
-
return "No search results"
|
| 2457 |
-
|
| 2458 |
-
formatted = []
|
| 2459 |
-
for i, r in enumerate(results[:3], 1):
|
| 2460 |
-
title = r.get('title', '')[:50]
|
| 2461 |
-
desc = r.get('description', '')[:100]
|
| 2462 |
-
formatted.append(f"[{i}]{title}:{desc}")
|
| 2463 |
-
|
| 2464 |
-
return " | ".join(formatted)
|
| 2465 |
-
|
| 2466 |
-
|
| 2467 |
-
# ============================================================================
|
| 2468 |
-
# Gradio UI (์ต์ ํ ๋ฒ์ - ์บ์ฑ ์ ๊ฑฐ)
|
| 2469 |
-
# ============================================================================
|
| 2470 |
-
|
| 2471 |
-
def create_optimized_gradio_interface():
|
| 2472 |
-
"""์ต์ ํ๋ Gradio ์ธํฐํ์ด์ค (์บ์ฑ ์์)"""
|
| 2473 |
-
|
| 2474 |
-
# ์์คํ
์ด๊ธฐํ
|
| 2475 |
-
system = SpeedOptimizedMultiAgentSystem()
|
| 2476 |
-
|
| 2477 |
-
def process_query_optimized(
|
| 2478 |
-
message: str,
|
| 2479 |
-
history: List[Dict],
|
| 2480 |
-
use_search: bool,
|
| 2481 |
-
show_agent_thoughts: bool,
|
| 2482 |
-
search_count: int,
|
| 2483 |
-
language_mode: str
|
| 2484 |
-
):
|
| 2485 |
-
"""์ต์ ํ๋ ์ฟผ๋ฆฌ ์ฒ๋ฆฌ - ์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ ๋ฒ์ """
|
| 2486 |
-
|
| 2487 |
-
if not message:
|
| 2488 |
-
yield history, "", ""
|
| 2489 |
-
return
|
| 2490 |
-
|
| 2491 |
-
# ์ธ์ด ์ค์
|
| 2492 |
-
if language_mode == "Auto":
|
| 2493 |
-
lang = None # ์๋ ๊ฐ์ง
|
| 2494 |
-
else:
|
| 2495 |
-
lang_map = {"Korean": "ko", "English": "en", "Japanese": "ja", "Chinese": "zh"}
|
| 2496 |
-
lang = lang_map.get(language_mode, None)
|
| 2497 |
-
|
| 2498 |
-
# ๋น๋๊ธฐ ํจ์๋ฅผ ๋๊ธฐ์ ์ผ๋ก ์คํ
|
| 2499 |
-
try:
|
| 2500 |
-
import nest_asyncio
|
| 2501 |
-
nest_asyncio.apply()
|
| 2502 |
-
except ImportError:
|
| 2503 |
-
pass
|
| 2504 |
-
|
| 2505 |
-
try:
|
| 2506 |
-
# ๊ฒ์ ์ํ (๋๊ธฐํ)
|
| 2507 |
-
search_results = []
|
| 2508 |
-
search_display = ""
|
| 2509 |
-
|
| 2510 |
-
# ์ธ์ด ์๋ ๊ฐ์ง (ํ์ํ ๊ฒฝ์ฐ)
|
| 2511 |
-
detected_lang = lang or system.language_detector.detect_language(message)
|
| 2512 |
-
|
| 2513 |
-
if use_search:
|
| 2514 |
-
# ๊ฒ์ ์ํ ํ์
|
| 2515 |
-
processing_msg = {
|
| 2516 |
-
'ko': "โก ๊ณ ์ ์ฒ๋ฆฌ ์ค...",
|
| 2517 |
-
'en': "โก High-speed processing...",
|
| 2518 |
-
'ja': "โก ้ซ้ๅฆ็ไธญ...",
|
| 2519 |
-
'zh': "โก ้ซ้ๅค็ไธญ..."
|
| 2520 |
-
}
|
| 2521 |
-
history_with_message = history + [
|
| 2522 |
-
{"role": "user", "content": message},
|
| 2523 |
-
{"role": "assistant", "content": processing_msg.get(detected_lang, processing_msg['en'])}
|
| 2524 |
-
]
|
| 2525 |
-
yield history_with_message, "", ""
|
| 2526 |
-
|
| 2527 |
-
# ๋น๋๊ธฐ ๊ฒ์์ ๋๊ธฐ์ ์ผ๋ก ์คํ
|
| 2528 |
-
async def search_wrapper():
|
| 2529 |
-
return await system.search.search_async(message, count=search_count, lang=detected_lang)
|
| 2530 |
-
|
| 2531 |
-
loop = asyncio.new_event_loop()
|
| 2532 |
-
asyncio.set_event_loop(loop)
|
| 2533 |
-
search_results = loop.run_until_complete(search_wrapper())
|
| 2534 |
-
|
| 2535 |
-
if search_results:
|
| 2536 |
-
ref_label = {
|
| 2537 |
-
'ko': "๐ ์ฐธ๊ณ ์๋ฃ",
|
| 2538 |
-
'en': "๐ References",
|
| 2539 |
-
'ja': "๐ ๅ่่ณๆ",
|
| 2540 |
-
'zh': "๐ ๅ่่ตๆ"
|
| 2541 |
-
}
|
| 2542 |
-
search_display = f"{ref_label.get(detected_lang, ref_label['en'])}\n\n"
|
| 2543 |
-
for i, result in enumerate(search_results[:3], 1):
|
| 2544 |
-
search_display += f"**{i}. [{result['title'][:50]}]({result['url']})**\n"
|
| 2545 |
-
search_display += f" {result['description'][:100]}...\n\n"
|
| 2546 |
-
|
| 2547 |
-
# ์ฌ์ฉ์ ๋ฉ์์ง ์ถ๊ฐ
|
| 2548 |
-
current_history = history + [{"role": "user", "content": message}]
|
| 2549 |
-
|
| 2550 |
-
# ์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ์ ์ํ ๋น๋๊ธฐ ์ฒ๋ฆฌ
|
| 2551 |
-
async def stream_responses():
|
| 2552 |
-
"""์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ ์ ๋๋ ์ดํฐ"""
|
| 2553 |
-
async for response, thoughts in system.parallel_process_agents(
|
| 2554 |
-
query=message,
|
| 2555 |
-
search_results=search_results,
|
| 2556 |
-
show_progress=show_agent_thoughts,
|
| 2557 |
-
lang=detected_lang
|
| 2558 |
-
):
|
| 2559 |
-
yield response, thoughts
|
| 2560 |
-
|
| 2561 |
-
# ์ ์ด๋ฒคํธ ๋ฃจํ์์ ์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ
|
| 2562 |
-
loop = asyncio.new_event_loop()
|
| 2563 |
-
asyncio.set_event_loop(loop)
|
| 2564 |
-
|
| 2565 |
-
# ๋น๋๊ธฐ ์ ๋๋ ์ดํฐ๋ฅผ ๋๊ธฐ์ ์ผ๋ก ์ํ
|
| 2566 |
-
gen = stream_responses()
|
| 2567 |
-
|
| 2568 |
-
while True:
|
| 2569 |
-
try:
|
| 2570 |
-
# ๋ค์ ํญ๋ชฉ ๊ฐ์ ธ์ค๊ธฐ
|
| 2571 |
-
task = asyncio.ensure_future(gen.__anext__(), loop=loop)
|
| 2572 |
-
response, thoughts = loop.run_until_complete(task)
|
| 2573 |
-
|
| 2574 |
-
# ์ค์๊ฐ ์
๋ฐ์ดํธ
|
| 2575 |
-
updated_history = current_history + [
|
| 2576 |
-
{"role": "assistant", "content": response}
|
| 2577 |
-
]
|
| 2578 |
-
yield updated_history, thoughts, search_display
|
| 2579 |
-
|
| 2580 |
-
except StopAsyncIteration:
|
| 2581 |
-
break
|
| 2582 |
-
|
| 2583 |
-
except Exception as e:
|
| 2584 |
-
error_history = history + [
|
| 2585 |
-
{"role": "user", "content": message},
|
| 2586 |
-
{"role": "assistant", "content": f"โ Error: {str(e)}"}
|
| 2587 |
-
]
|
| 2588 |
-
yield error_history, "", ""
|
| 2589 |
-
finally:
|
| 2590 |
-
# ๋ฃจํ ์ ๋ฆฌ
|
| 2591 |
-
try:
|
| 2592 |
-
loop.close()
|
| 2593 |
-
except:
|
| 2594 |
-
pass
|
| 2595 |
-
|
| 2596 |
-
# Gradio ์ธํฐํ์ด์ค
|
| 2597 |
-
with gr.Blocks(
|
| 2598 |
-
title="โก Speed-Optimized Multi-Agent System (No Cache)",
|
| 2599 |
-
theme=gr.themes.Soft(),
|
| 2600 |
-
css="""
|
| 2601 |
-
.gradio-container {
|
| 2602 |
-
max-width: 1400px !important;
|
| 2603 |
-
margin: auto !important;
|
| 2604 |
-
}
|
| 2605 |
-
"""
|
| 2606 |
-
) as demo:
|
| 2607 |
-
gr.Markdown("""
|
| 2608 |
-
# โก Enhanced Multi-Agent RAG System (์บ์ฑ ์ ๊ฑฐ ๋ฒ์ )
|
| 2609 |
-
**Complex questions processed within 5-8 seconds | Multi-language support**
|
| 2610 |
-
|
| 2611 |
-
**Optimization Features:**
|
| 2612 |
-
- ๐ Parallel Processing: Concurrent agent execution
|
| 2613 |
-
- โก Stream Buffering: Network optimization
|
| 2614 |
-
- ๐ฏ Early Termination: Complete immediately when quality is met
|
| 2615 |
-
- ๐ Multi-language: Auto-detect Korean/English/Japanese/Chinese
|
| 2616 |
-
- โ **Caching Disabled**: ์บ์ฑ ๊ธฐ๋ฅ ์ ๊ฑฐ๋จ
|
| 2617 |
-
""")
|
| 2618 |
-
|
| 2619 |
-
with gr.Row():
|
| 2620 |
-
with gr.Column(scale=3):
|
| 2621 |
-
chatbot = gr.Chatbot(
|
| 2622 |
-
height=500,
|
| 2623 |
-
label="๐ฌ Chat",
|
| 2624 |
-
type="messages"
|
| 2625 |
-
)
|
| 2626 |
-
|
| 2627 |
-
msg = gr.Textbox(
|
| 2628 |
-
label="Enter complex question",
|
| 2629 |
-
placeholder="Enter complex questions requiring analysis, strategy, or creative solutions...",
|
| 2630 |
-
lines=3
|
| 2631 |
-
)
|
| 2632 |
-
|
| 2633 |
-
with gr.Row():
|
| 2634 |
-
submit = gr.Button("โก High-Speed Process", variant="primary")
|
| 2635 |
-
clear = gr.Button("๐ Reset")
|
| 2636 |
-
|
| 2637 |
-
with gr.Accordion("๐ค Agent Processing", open=False):
|
| 2638 |
-
agent_thoughts = gr.Markdown()
|
| 2639 |
-
|
| 2640 |
-
with gr.Accordion("๐ Search Sources", open=False):
|
| 2641 |
-
search_sources = gr.Markdown()
|
| 2642 |
-
|
| 2643 |
-
with gr.Column(scale=1):
|
| 2644 |
-
gr.Markdown("**โ๏ธ Settings**")
|
| 2645 |
-
|
| 2646 |
-
language_mode = gr.Radio(
|
| 2647 |
-
choices=["Auto", "Korean", "English", "Japanese", "Chinese"],
|
| 2648 |
-
value="Auto",
|
| 2649 |
-
label="๐ Language Mode"
|
| 2650 |
-
)
|
| 2651 |
-
|
| 2652 |
-
use_search = gr.Checkbox(
|
| 2653 |
-
label="๐ Use Web Search",
|
| 2654 |
-
value=True
|
| 2655 |
-
)
|
| 2656 |
-
|
| 2657 |
-
show_agent_thoughts = gr.Checkbox(
|
| 2658 |
-
label="๐ง Show Processing",
|
| 2659 |
-
value=True
|
| 2660 |
-
)
|
| 2661 |
-
|
| 2662 |
-
search_count = gr.Slider(
|
| 2663 |
-
minimum=3,
|
| 2664 |
-
maximum=10,
|
| 2665 |
-
value=5,
|
| 2666 |
-
step=1,
|
| 2667 |
-
label="Search Results Count"
|
| 2668 |
-
)
|
| 2669 |
-
|
| 2670 |
-
gr.Markdown("""
|
| 2671 |
-
**โก Optimization Status**
|
| 2672 |
-
|
| 2673 |
-
**Active Optimizations:**
|
| 2674 |
-
- โ
Parallel Processing
|
| 2675 |
-
- โ ~~Smart Caching~~ (์ ๊ฑฐ๋จ)
|
| 2676 |
-
- โ
Buffer Streaming
|
| 2677 |
-
- โ
Early Termination
|
| 2678 |
-
- โ
Compressed Prompts
|
| 2679 |
-
- โ
Multi-language Support
|
| 2680 |
-
- โ
Error Recovery
|
| 2681 |
-
|
| 2682 |
-
**Expected Processing Time:**
|
| 2683 |
-
- Simple Query: 3-5 seconds
|
| 2684 |
-
- Complex Query: 5-8 seconds
|
| 2685 |
-
- Very Complex: 8-12 seconds
|
| 2686 |
-
""")
|
| 2687 |
-
|
| 2688 |
-
# ๋ณต์กํ ์ง๋ฌธ ์์ (๋ค๊ตญ์ด)
|
| 2689 |
-
gr.Examples(
|
| 2690 |
-
examples=[
|
| 2691 |
-
# Korean
|
| 2692 |
-
"AI ๊ธฐ์ ์ด ํฅํ 10๋
๊ฐ ํ๊ตญ ๊ฒฝ์ ์ ๋ฏธ์น ์ํฅ์ ๋ค๊ฐ๋๋ก ๋ถ์ํ๊ณ ๋์ ์ ๋ต์ ์ ์ํด์ค",
|
| 2693 |
-
"์คํํธ์
์ด ๋๊ธฐ์
๊ณผ ๊ฒฝ์ํ๊ธฐ ์ํ ํ์ ์ ์ธ ์ ๋ต์ ๋จ๊ณ๋ณ๋ก ์๋ฆฝํด์ค",
|
| 2694 |
-
# English
|
| 2695 |
-
"Analyze the multifaceted impact of quantum computing on current encryption systems and propose alternatives",
|
| 2696 |
-
"Design 5 innovative business models for climate change mitigation with practical implementation details",
|
| 2697 |
-
# Japanese
|
| 2698 |
-
"ใกใฟใใผในๆไปฃใฎๆ่ฒ้ฉๆฐๆนๆกใๅฎ่ฃ
ๅฏ่ฝใชใฌใใซใงๆๆกใใฆใใ ใใ",
|
| 2699 |
-
# Chinese
|
| 2700 |
-
"ๅๆไบบๅทฅๆบ่ฝๅฏนๆชๆฅๅๅนดๅ
จ็็ปๆต็ๅฝฑๅๅนถๆๅบๅบๅฏน็ญ็ฅ"
|
| 2701 |
-
],
|
| 2702 |
-
inputs=msg
|
| 2703 |
-
)
|
| 2704 |
-
|
| 2705 |
-
# ์ด๋ฒคํธ ๋ฐ์ธ๋ฉ
|
| 2706 |
-
submit.click(
|
| 2707 |
-
process_query_optimized,
|
| 2708 |
-
inputs=[msg, chatbot, use_search, show_agent_thoughts, search_count, language_mode],
|
| 2709 |
-
outputs=[chatbot, agent_thoughts, search_sources]
|
| 2710 |
-
).then(
|
| 2711 |
-
lambda: "",
|
| 2712 |
-
None,
|
| 2713 |
-
msg
|
| 2714 |
-
)
|
| 2715 |
-
|
| 2716 |
-
msg.submit(
|
| 2717 |
-
process_query_optimized,
|
| 2718 |
-
inputs=[msg, chatbot, use_search, show_agent_thoughts, search_count, language_mode],
|
| 2719 |
-
outputs=[chatbot, agent_thoughts, search_sources]
|
| 2720 |
-
).then(
|
| 2721 |
-
lambda: "",
|
| 2722 |
-
None,
|
| 2723 |
-
msg
|
| 2724 |
-
)
|
| 2725 |
-
|
| 2726 |
-
clear.click(
|
| 2727 |
-
lambda: ([], "", ""),
|
| 2728 |
-
None,
|
| 2729 |
-
[chatbot, agent_thoughts, search_sources]
|
| 2730 |
-
)
|
| 2731 |
-
|
| 2732 |
-
return demo
|
| 2733 |
-
|
| 2734 |
-
|
| 2735 |
-
# ============================================================================
|
| 2736 |
-
# ๋ฉ์ธ ์คํ
|
| 2737 |
-
# ============================================================================
|
| 2738 |
-
|
| 2739 |
-
if __name__ == "__main__":
|
| 2740 |
-
print("""
|
| 2741 |
-
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 2742 |
-
โ โก Speed-Optimized Multi-Agent System (No Cache) โก โ
|
| 2743 |
-
โ โ
|
| 2744 |
-
โ High-speed AI system processing complex questions โ
|
| 2745 |
-
โ โ
|
| 2746 |
-
โ Features: โ
|
| 2747 |
-
โ โข Multi-language support (KO/EN/JA/ZH) โ
|
| 2748 |
-
โ โข Improved error recovery โ
|
| 2749 |
-
โ โข NO CACHING (์บ์ฑ ๊ธฐ๋ฅ ์ ๊ฑฐ๋จ) โ
|
| 2750 |
-
โ โข Adaptive stream buffering โ
|
| 2751 |
-
โ โข Response cleaning & formatting โ
|
| 2752 |
-
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 2753 |
-
""")
|
| 2754 |
-
|
| 2755 |
-
# API ํค ํ์ธ
|
| 2756 |
-
if not os.getenv("FIREWORKS_API_KEY"):
|
| 2757 |
-
print("\nโ ๏ธ FIREWORKS_API_KEY is not set.")
|
| 2758 |
-
|
| 2759 |
-
if not os.getenv("BRAVE_SEARCH_API_KEY"):
|
| 2760 |
-
print("\nโ ๏ธ BRAVE_SEARCH_API_KEY is not set.")
|
| 2761 |
-
|
| 2762 |
-
# Gradio ์ฑ ์คํ
|
| 2763 |
-
demo = create_optimized_gradio_interface()
|
| 2764 |
-
|
| 2765 |
-
is_hf_spaces = os.getenv("SPACE_ID") is not None
|
| 2766 |
-
|
| 2767 |
-
if is_hf_spaces:
|
| 2768 |
-
print("\n๐ค Running in optimized mode on Hugging Face Spaces (No Cache)...")
|
| 2769 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 2770 |
-
else:
|
| 2771 |
-
print("\n๐ป Running in optimized mode on local environment (No Cache)...")
|
| 2772 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, share=False), '', response, flags=re.MULTILINE)
|
| 2773 |
-
|
| 2774 |
-
# ๋ฆฌ์คํธ ๋ง์ปค๋ฅผ ์ผ๋ฐ ํ
์คํธ๋ก ๋ณ๊ฒฝ
|
| 2775 |
-
response = re.sub(r'^\s*[\*\-\+]\s+', 'โข ', response, flags=re.MULTILINE)
|
| 2776 |
-
response = re.sub(r'^\s*\d+\.\s+', '', response, flags=re.MULTILINE)
|
| 2777 |
-
|
| 2778 |
-
# ๋งํฌ ํ
์คํธ๋ง ๋จ๊ธฐ๊ธฐ [text](url) -> text
|
| 2779 |
-
response = re.sub(r'\[([^\]]+)\]\([^\)]+\)', r'\1', response)
|
| 2780 |
|
| 2781 |
-
#
|
| 2782 |
-
response = re.sub(r'
|
| 2783 |
|
| 2784 |
# ์ค๋ณต ๊ณต๋ฐฑ ์ ๊ฑฐ
|
| 2785 |
response = re.sub(r'\n{3,}', '\n\n', response)
|
| 2786 |
-
response = re.sub(r' {2,}', ' ', response)
|
| 2787 |
|
| 2788 |
# ํน์ ํจํด ์ ๊ฑฐ
|
| 2789 |
unwanted_patterns = [
|
|
@@ -2792,18 +486,12 @@ if __name__ == "__main__":
|
|
| 2792 |
r'^\s*\*\*\[.*?\]\*\*\s*', # [ํ๊ทธ] ํ์ ์ ๊ฑฐ
|
| 2793 |
r'^\s*###\s*', # ### ์ ๊ฑฐ
|
| 2794 |
r'^\s*##\s*', # ## ์ ๊ฑฐ
|
| 2795 |
-
r'^\s*#\s*'
|
| 2796 |
-
r'^\s*>\s+', # ์ธ์ฉ๋ฌธ ๋ง์ปค ์ ๊ฑฐ
|
| 2797 |
]
|
| 2798 |
|
| 2799 |
for pattern in unwanted_patterns:
|
| 2800 |
response = re.sub(pattern, '', response, flags=re.MULTILINE)
|
| 2801 |
|
| 2802 |
-
# ๋น ์ค ์ ๋ฆฌ
|
| 2803 |
-
lines = [line.strip() for line in response.split('\n')]
|
| 2804 |
-
lines = [line for line in lines if line]
|
| 2805 |
-
response = '\n\n'.join(lines)
|
| 2806 |
-
|
| 2807 |
return response.strip()
|
| 2808 |
|
| 2809 |
|
|
@@ -3562,5 +1250,4 @@ if __name__ == "__main__":
|
|
| 3562 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 3563 |
else:
|
| 3564 |
print("\n๐ป Running in optimized mode on local environment (No Cache)...")
|
| 3565 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
|
| 3566 |
-
|
|
|
|
| 470 |
@staticmethod
|
| 471 |
def clean_response(response: str) -> str:
|
| 472 |
"""๋ถํ์ํ ๋งํฌ์
์ ๊ฑฐ ๊ฐํ"""
|
| 473 |
+
# ๋งํฌ๋ค์ด ํค๋ ์ ๊ฑฐ
|
| 474 |
+
response = re.sub(r'^#{1,6}\s+', '', response, flags=re.MULTILINE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 475 |
|
| 476 |
+
# ๋ถํ์ํ ๊ตฌ๋ถ์ ์ ๊ฑฐ
|
| 477 |
+
response = re.sub(r'\*{2,}|_{2,}|-{3,}', '', response)
|
| 478 |
|
| 479 |
# ์ค๋ณต ๊ณต๋ฐฑ ์ ๊ฑฐ
|
| 480 |
response = re.sub(r'\n{3,}', '\n\n', response)
|
|
|
|
| 481 |
|
| 482 |
# ํน์ ํจํด ์ ๊ฑฐ
|
| 483 |
unwanted_patterns = [
|
|
|
|
| 486 |
r'^\s*\*\*\[.*?\]\*\*\s*', # [ํ๊ทธ] ํ์ ์ ๊ฑฐ
|
| 487 |
r'^\s*###\s*', # ### ์ ๊ฑฐ
|
| 488 |
r'^\s*##\s*', # ## ์ ๊ฑฐ
|
| 489 |
+
r'^\s*#\s*' # # ์ ๊ฑฐ
|
|
|
|
| 490 |
]
|
| 491 |
|
| 492 |
for pattern in unwanted_patterns:
|
| 493 |
response = re.sub(pattern, '', response, flags=re.MULTILINE)
|
| 494 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 495 |
return response.strip()
|
| 496 |
|
| 497 |
|
|
|
|
| 1250 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 1251 |
else:
|
| 1252 |
print("\n๐ป Running in optimized mode on local environment (No Cache)...")
|
| 1253 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
|
|
|