File size: 37,909 Bytes
d93130e d3a088e 1fb86bb d3a088e 1fb86bb d3a088e 1fb86bb d3a088e 1fb86bb d3a088e 1fb86bb d3a088e 1fb86bb d3a088e 1fb86bb d3a088e 1fb86bb d3a088e 1fb86bb d3a088e 1fb86bb d3a088e 1fb86bb d3a088e 1fb86bb d3a088e 1fb86bb d3a088e 1fb86bb d3a088e 1fb86bb d3a088e 1fb86bb d3a088e 1fb86bb d3a088e 1fb86bb d3a088e 1fb86bb d93130e 1fb86bb d93130e 1fb86bb ffbaab8 1fb86bb ffbaab8 d93130e 1fb86bb d93130e 1fb86bb d93130e 1fb86bb d93130e 18786d2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 |
import gradio as gr
import os
import json
import requests
from datetime import datetime
import time
from typing import List, Dict, Any, Generator, Tuple
import logging
import re
# ๋ก๊น
์ค์
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# ํ๊ฒฝ ๋ณ์์์ ํ ํฐ ๊ฐ์ ธ์ค๊ธฐ
FRIENDLI_TOKEN = os.getenv("FRIENDLI_TOKEN", "YOUR_FRIENDLI_TOKEN")
BAPI_TOKEN = os.getenv("BAPI_TOKEN", "YOUR_BRAVE_API_TOKEN")
API_URL = "https://api.friendli.ai/dedicated/v1/chat/completions"
BRAVE_SEARCH_URL = "https://api.search.brave.com/res/v1/web/search"
MODEL_ID = "dep89a2fld32mcm"
TEST_MODE = os.getenv("TEST_MODE", "false").lower() == "true"
# ์ ์ญ ๋ณ์
conversation_history = []
class LLMCollaborativeSystem:
def __init__(self):
self.token = FRIENDLI_TOKEN
self.bapi_token = BAPI_TOKEN
self.api_url = API_URL
self.brave_url = BRAVE_SEARCH_URL
self.model_id = MODEL_ID
self.test_mode = TEST_MODE or (self.token == "YOUR_FRIENDLI_TOKEN")
if self.test_mode:
logger.warning("ํ
์คํธ ๋ชจ๋๋ก ์คํ๋ฉ๋๋ค.")
if self.bapi_token == "YOUR_BRAVE_API_TOKEN":
logger.warning("Brave API ํ ํฐ์ด ์ค์ ๋์ง ์์์ต๋๋ค.")
def create_headers(self):
"""API ํค๋ ์์ฑ"""
return {
"Authorization": f"Bearer {self.token}",
"Content-Type": "application/json"
}
def create_brave_headers(self):
"""Brave API ํค๋ ์์ฑ"""
return {
"Accept": "application/json",
"Accept-Encoding": "gzip",
"X-Subscription-Token": self.bapi_token
}
def create_supervisor_initial_prompt(self, user_query: str) -> str:
"""๊ฐ๋
์ AI ์ด๊ธฐ ํ๋กฌํํธ ์์ฑ"""
return f"""๋น์ ์ ๊ฑฐ์์ ๊ด์ ์์ ๋ถ์ํ๊ณ ์ง๋ํ๋ ๊ฐ๋
์ AI์
๋๋ค.
์ฌ์ฉ์ ์ง๋ฌธ: {user_query}
์ด ์ง๋ฌธ์ ๋ํด:
1. ์ ์ฒด์ ์ธ ์ ๊ทผ ๋ฐฉํฅ๊ณผ ํ๋ ์์ํฌ๋ฅผ ์ ์ํ์ธ์
2. ํต์ฌ ์์์ ๊ณ ๋ ค์ฌํญ์ ๊ตฌ์กฐํํ์ฌ ์ค๋ช
ํ์ธ์
3. ์ด ์ฃผ์ ์ ๋ํด ์กฐ์ฌ๊ฐ ํ์ํ 5-7๊ฐ์ ๊ตฌ์ฒด์ ์ธ ํค์๋๋ ๊ฒ์์ด๋ฅผ ์ ์ํ์ธ์
ํค์๋๋ ๋ค์ ํ์์ผ๋ก ์ ์ํ์ธ์:
[๊ฒ์ ํค์๋]: ํค์๋1, ํค์๋2, ํค์๋3, ํค์๋4, ํค์๋5"""
def create_researcher_prompt(self, user_query: str, supervisor_guidance: str, search_results: Dict[str, List[Dict]]) -> str:
"""์กฐ์ฌ์ AI ํ๋กฌํํธ ์์ฑ"""
search_summary = ""
for keyword, results in search_results.items():
search_summary += f"\n\n**{keyword}์ ๋ํ ๊ฒ์ ๊ฒฐ๊ณผ:**\n"
for i, result in enumerate(results[:3], 1):
search_summary += f"{i}. {result.get('title', 'N/A')}\n"
search_summary += f" - {result.get('description', 'N/A')}\n"
search_summary += f" - ์ถ์ฒ: {result.get('url', 'N/A')}\n"
return f"""๋น์ ์ ์ ๋ณด๋ฅผ ์กฐ์ฌํ๊ณ ์ ๋ฆฌํ๋ ์กฐ์ฌ์ AI์
๋๋ค.
์ฌ์ฉ์ ์ง๋ฌธ: {user_query}
๊ฐ๋
์ AI์ ์ง์นจ:
{supervisor_guidance}
๋ธ๋ ์ด๋ธ ๊ฒ์ ๊ฒฐ๊ณผ:
{search_summary}
์ ๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ์ผ๋ก:
1. ๊ฐ ํค์๋๋ณ๋ก ์ค์ํ ์ ๋ณด๋ฅผ ์ ๋ฆฌํ์ธ์
2. ์ ๋ขฐํ ์ ์๋ ์ถ์ฒ๋ฅผ ๋ช
์ํ์ธ์
3. ์คํ์ AI๊ฐ ํ์ฉํ ์ ์๋ ๊ตฌ์ฒด์ ์ธ ๋ฐ์ดํฐ์ ์ฌ์ค์ ์ถ์ถํ์ธ์
4. ์ต์ ํธ๋ ๋๋ ์ค์ํ ํต๊ณ๊ฐ ์๋ค๋ฉด ๊ฐ์กฐํ์ธ์"""
def create_supervisor_execution_prompt(self, user_query: str, research_summary: str) -> str:
"""๊ฐ๋
์ AI์ ์คํ ์ง์ ํ๋กฌํํธ"""
return f"""๋น์ ์ ๊ฑฐ์์ ๊ด์ ์์ ๋ถ์ํ๊ณ ์ง๋ํ๋ ๊ฐ๋
์ AI์
๋๋ค.
์ฌ์ฉ์ ์ง๋ฌธ: {user_query}
์กฐ์ฌ์ AI๊ฐ ์ ๋ฆฌํ ์กฐ์ฌ ๋ด์ฉ:
{research_summary}
์ ์กฐ์ฌ ๋ด์ฉ์ ๊ธฐ๋ฐ์ผ๋ก ์คํ์ AI์๊ฒ ์์ฃผ ๊ตฌ์ฒด์ ์ธ ์ง์๋ฅผ ๋ด๋ ค์ฃผ์ธ์:
1. ์กฐ์ฌ๋ ์ ๋ณด๋ฅผ ์ด๋ป๊ฒ ํ์ฉํ ์ง ๋ช
ํํ ์ง์ํ์ธ์
2. ์คํ ๊ฐ๋ฅํ ๋จ๊ณ๋ณ ์์
์ ๊ตฌ์ฒด์ ์ผ๋ก ์ ์ํ์ธ์
3. ๊ฐ ๋จ๊ณ์์ ์ฐธ๊ณ ํด์ผ ํ ์กฐ์ฌ ๋ด์ฉ์ ๋ช
์ํ์ธ์
4. ์์๋๋ ๊ฒฐ๊ณผ๋ฌผ์ ํํ๋ฅผ ๊ตฌ์ฒด์ ์ผ๋ก ์ค๋ช
ํ์ธ์"""
def create_executor_prompt(self, user_query: str, supervisor_guidance: str, research_summary: str) -> str:
"""์คํ์ AI ํ๋กฌํํธ ์์ฑ"""
return f"""๋น์ ์ ์ธ๋ถ์ ์ธ ๋ด์ฉ์ ๊ตฌํํ๋ ์คํ์ AI์
๋๋ค.
์ฌ์ฉ์ ์ง๋ฌธ: {user_query}
์กฐ์ฌ์ AI๊ฐ ์ ๋ฆฌํ ์กฐ์ฌ ๋ด์ฉ:
{research_summary}
๊ฐ๋
์ AI์ ๊ตฌ์ฒด์ ์ธ ์ง์:
{supervisor_guidance}
์ ์กฐ์ฌ ๋ด์ฉ๊ณผ ์ง์์ฌํญ์ ๋ฐํ์ผ๋ก:
1. ์กฐ์ฌ๋ ์ ๋ณด๋ฅผ ์ ๊ทน ํ์ฉํ์ฌ ๊ตฌ์ฒด์ ์ธ ์คํ ๊ณํ์ ์์ฑํ์ธ์
2. ๊ฐ ๋จ๊ณ๋ณ๋ก ์ฐธ๊ณ ํ ์กฐ์ฌ ๋ด์ฉ์ ๋ช
์ํ์ธ์
3. ์ค์ ๋ก ์ ์ฉ ๊ฐ๋ฅํ ๊ตฌ์ฒด์ ์ธ ๋ฐฉ๋ฒ๋ก ์ ์ ์ํ์ธ์
4. ์์๋๋ ์ฑ๊ณผ์ ์ธก์ ๋ฐฉ๋ฒ์ ํฌํจํ์ธ์"""
def create_executor_final_prompt(self, user_query: str, initial_response: str, supervisor_feedback: str, research_summary: str) -> str:
"""์คํ์ AI ์ต์ข
๋ณด๊ณ ์ ํ๋กฌํํธ"""
return f"""๋น์ ์ ์ธ๋ถ์ ์ธ ๋ด์ฉ์ ๊ตฌํํ๋ ์คํ์ AI์
๋๋ค.
์ฌ์ฉ์ ์ง๋ฌธ: {user_query}
์กฐ์ฌ์ AI์ ์กฐ์ฌ ๋ด์ฉ:
{research_summary}
๋น์ ์ ์ด๊ธฐ ๋ต๋ณ:
{initial_response}
๊ฐ๋
์ AI์ ํผ๋๋ฐฑ ๋ฐ ๊ฐ์ ์ฌํญ:
{supervisor_feedback}
์ ํผ๋๋ฐฑ์ ์์ ํ ๋ฐ์ํ์ฌ ์ต์ข
๋ณด๊ณ ์๋ฅผ ์์ฑํ์ธ์:
1. ๊ฐ๋
์์ ๋ชจ๋ ๊ฐ์ ์ฌํญ์ ๋ฐ์ํ์ธ์
2. ์กฐ์ฌ ๋ด์ฉ์ ๋์ฑ ๊ตฌ์ฒด์ ์ผ๋ก ํ์ฉํ์ธ์
3. ์คํ ๊ฐ๋ฅ์ฑ์ ๋์ด๋ ์ธ๋ถ ๊ณํ์ ํฌํจํ์ธ์
4. ๋ช
ํํ ๊ฒฐ๋ก ๊ณผ ๋ค์ ๋จ๊ณ๋ฅผ ์ ์ํ์ธ์
5. ์ ๋ฌธ์ ์ด๊ณ ์์ฑ๋ ๋์ ์ต์ข
๋ณด๊ณ ์ ํ์์ผ๋ก ์์ฑํ์ธ์"""
def extract_keywords(self, supervisor_response: str) -> List[str]:
"""๊ฐ๋
์ ์๋ต์์ ํค์๋ ์ถ์ถ"""
keywords = []
# [๊ฒ์ ํค์๋]: ํ์์ผ๋ก ํค์๋ ์ฐพ๊ธฐ
keyword_match = re.search(r'\[๊ฒ์ ํค์๋\]:\s*(.+)', supervisor_response, re.IGNORECASE)
if keyword_match:
keyword_str = keyword_match.group(1)
keywords = [k.strip() for k in keyword_str.split(',') if k.strip()]
# ํค์๋๊ฐ ์์ผ๋ฉด ๊ธฐ๋ณธ ํค์๋ ์์ฑ
if not keywords:
keywords = ["best practices", "implementation guide", "case studies", "latest trends", "success factors"]
return keywords[:7] # ์ต๋ 7๊ฐ๋ก ์ ํ
def brave_search(self, query: str) -> List[Dict]:
"""Brave Search API ํธ์ถ"""
if self.test_mode or self.bapi_token == "YOUR_BRAVE_API_TOKEN":
# ํ
์คํธ ๋ชจ๋์์๋ ์๋ฎฌ๋ ์ด์
๋ ๊ฒฐ๊ณผ ๋ฐํ
return [
{
"title": f"Best Practices for {query}",
"description": f"Comprehensive guide on implementing {query} with proven methodologies and real-world examples.",
"url": f"https://example.com/{query.replace(' ', '-')}"
},
{
"title": f"Latest Trends in {query}",
"description": f"Analysis of current trends and future directions in {query}, including market insights and expert opinions.",
"url": f"https://trends.example.com/{query.replace(' ', '-')}"
},
{
"title": f"{query}: Case Studies and Success Stories",
"description": f"Real-world implementations of {query} across various industries with measurable results.",
"url": f"https://casestudies.example.com/{query.replace(' ', '-')}"
}
]
try:
params = {
"q": query,
"count": 5,
"safesearch": "moderate",
"freshness": "pw" # Past week for recent results
}
response = requests.get(
self.brave_url,
headers=self.create_brave_headers(),
params=params,
timeout=10
)
if response.status_code == 200:
data = response.json()
results = []
for item in data.get("web", {}).get("results", [])[:5]:
results.append({
"title": item.get("title", ""),
"description": item.get("description", ""),
"url": item.get("url", "")
})
return results
else:
logger.error(f"Brave API ์ค๋ฅ: {response.status_code}")
return []
except Exception as e:
logger.error(f"Brave ๊ฒ์ ์ค ์ค๋ฅ: {str(e)}")
return []
def simulate_streaming(self, text: str, role: str) -> Generator[str, None, None]:
"""ํ
์คํธ ๋ชจ๋์์ ์คํธ๋ฆฌ๋ฐ ์๋ฎฌ๋ ์ด์
"""
words = text.split()
for i in range(0, len(words), 3):
chunk = " ".join(words[i:i+3])
yield chunk + " "
time.sleep(0.05)
def call_llm_streaming(self, messages: List[Dict[str, str]], role: str) -> Generator[str, None, None]:
"""์คํธ๋ฆฌ๋ฐ LLM API ํธ์ถ"""
# ํ
์คํธ ๋ชจ๋
if self.test_mode:
logger.info(f"ํ
์คํธ ๋ชจ๋ ์คํธ๋ฆฌ๋ฐ - Role: {role}")
test_responses = {
"supervisor_initial": """์ด ์ง๋ฌธ์ ๋ํ ๊ฑฐ์์ ๋ถ์์ ์ ์ํ๊ฒ ์ต๋๋ค.
1. **ํต์ฌ ๊ฐ๋
ํ์
**
- ์ง๋ฌธ์ ๋ณธ์ง์ ์์๋ฅผ ์ฌ์ธต ๋ถ์ํฉ๋๋ค
- ๊ด๋ จ๋ ์ฃผ์ ์ด๋ก ๊ณผ ์์น์ ๊ฒํ ํฉ๋๋ค
- ๋ค์ํ ๊ด์ ์์์ ์ ๊ทผ ๋ฐฉ๋ฒ์ ๊ณ ๋ คํฉ๋๋ค
2. **์ ๋ต์ ์ ๊ทผ ๋ฐฉํฅ**
- ์ฒด๊ณ์ ์ด๊ณ ๋จ๊ณ๋ณ ํด๊ฒฐ ๋ฐฉ์์ ์๋ฆฝํฉ๋๋ค
- ์ฅ๋จ๊ธฐ ๋ชฉํ๋ฅผ ๋ช
ํํ ์ค์ ํฉ๋๋ค
- ๋ฆฌ์คํฌ ์์ธ๊ณผ ๋์ ๋ฐฉ์์ ๋ง๋ จํฉ๋๋ค
3. **๊ธฐ๋ ํจ๊ณผ์ ๊ณผ์ **
- ์์๋๋ ๊ธ์ ์ ์ฑ๊ณผ๋ฅผ ๋ถ์ํฉ๋๋ค
- ์ ์ฌ์ ๋์ ๊ณผ์ ๋ฅผ ์๋ณํฉ๋๋ค
- ์ง์๊ฐ๋ฅํ ๋ฐ์ ๋ฐฉํฅ์ ์ ์ํฉ๋๋ค
[๊ฒ์ ํค์๋]: machine learning optimization, performance improvement strategies, model efficiency techniques, hyperparameter tuning best practices, latest ML trends 2024""",
"researcher": """์กฐ์ฌ ๊ฒฐ๊ณผ๋ฅผ ์ข
ํฉํ์ฌ ๋ค์๊ณผ ๊ฐ์ด ์ ๋ฆฌํ์ต๋๋ค.
**1. Machine Learning Optimization**
- ์ต์ ์ฐ๊ตฌ์ ๋ฐ๋ฅด๋ฉด ๋ชจ๋ธ ์ต์ ํ์ ํต์ฌ์ ์ํคํ
์ฒ ์ค๊ณ์ ํ๋ จ ์ ๋ต์ ๊ท ํ์
๋๋ค
- AutoML ๋๊ตฌ๋ค์ด ํ์ดํผํ๋ผ๋ฏธํฐ ํ๋์ ์๋ํํ์ฌ ํจ์จ์ฑ์ ํฌ๊ฒ ํฅ์์ํต๋๋ค
- ์ถ์ฒ: ML Conference 2024, Google Research
**2. Performance Improvement Strategies**
- ๋ฐ์ดํฐ ํ์ง ๊ฐ์ ์ด ๋ชจ๋ธ ์ฑ๋ฅ ํฅ์์ 80%๋ฅผ ์ฐจ์งํ๋ค๋ ์ฐ๊ตฌ ๊ฒฐ๊ณผ
- ์์๋ธ ๊ธฐ๋ฒ๊ณผ ์ ์ดํ์ต์ด ์ฃผ์ ์ฑ๋ฅ ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก ์
์ฆ๋จ
- ๋ฒค์น๋งํฌ: ImageNet์์ 95% ์ด์์ ์ ํ๋ ๋ฌ์ฑ ์ฌ๋ก
**3. Model Efficiency Techniques**
- ๋ชจ๋ธ ๊ฒฝ๋ํ(Pruning, Quantization)๋ก ์ถ๋ก ์๋ 10๋ฐฐ ํฅ์ ๊ฐ๋ฅ
- Knowledge Distillation์ผ๋ก ๋ชจ๋ธ ํฌ๊ธฐ 90% ๊ฐ์, ์ฑ๋ฅ ์ ์ง
- ์ต์ ํธ๋ ๋: Efficient Transformers, Neural Architecture Search
**4. ์ค์ ์ ์ฉ ์ฌ๋ก**
- Netflix: ์ถ์ฒ ์์คํ
๊ฐ์ ์ผ๋ก ์ฌ์ฉ์ ๋ง์กฑ๋ 35% ํฅ์
- Tesla: ์ค์๊ฐ ๊ฐ์ฒด ์ธ์ ์๋ 50% ๊ฐ์
- OpenAI: GPT ๋ชจ๋ธ ํจ์จ์ฑ ๊ฐ์ ์ผ๋ก ๋น์ฉ 70% ์ ๊ฐ""",
"supervisor_execution": """์กฐ์ฌ ๋ด์ฉ์ ๋ฐํ์ผ๋ก ์คํ์ AI์๊ฒ ๋ค์๊ณผ ๊ฐ์ด ๊ตฌ์ฒด์ ์ผ๋ก ์ง์ํฉ๋๋ค.
**1๋จ๊ณ: ํ์ฌ ๋ชจ๋ธ ์ง๋จ (1์ฃผ์ฐจ)**
- ์กฐ์ฌ๋ ๋ฒค์น๋งํฌ ๊ธฐ์ค์ผ๋ก ํ์ฌ ๋ชจ๋ธ ์ฑ๋ฅ ํ๊ฐ
- Netflix ์ฌ๋ก๋ฅผ ์ฐธ๊ณ ํ์ฌ ์ฃผ์ ๋ณ๋ชฉ ์ง์ ์๋ณ
- AutoML ๋๊ตฌ๋ฅผ ํ์ฉํ ์ด๊ธฐ ์ต์ ํ ๊ฐ๋ฅ์ฑ ํ์
**2๋จ๊ณ: ๋ฐ์ดํฐ ํ์ง ๊ฐ์ (2-3์ฃผ์ฐจ)**
- ์กฐ์ฌ ๊ฒฐ๊ณผ์ "80% ๊ท์น"์ ๋ฐ๋ผ ๋ฐ์ดํฐ ์ ์ ์ฐ์ ์คํ
- ๋ฐ์ดํฐ ์ฆ๊ฐ ๊ธฐ๋ฒ ์ ์ฉ (์กฐ์ฌ๋ ์ต์ ๊ธฐ๋ฒ ํ์ฉ)
- A/B ํ
์คํธ๋ก ๊ฐ์ ํจ๊ณผ ์ธก์
**3๋จ๊ณ: ๋ชจ๋ธ ์ต์ ํ ๊ตฌํ (4-6์ฃผ์ฐจ)**
- Knowledge Distillation ์ ์ฉํ์ฌ ๋ชจ๋ธ ๊ฒฝ๋ํ
- ์กฐ์ฌ๋ Pruning ๊ธฐ๋ฒ์ผ๋ก ์ถ๋ก ์๋ ๊ฐ์
- Tesla ์ฌ๋ก์ ์ค์๊ฐ ์ฒ๋ฆฌ ์ต์ ํ ๊ธฐ๋ฒ ๋ฒค์น๋งํน
**4๋จ๊ณ: ์ฑ๊ณผ ๊ฒ์ฆ ๋ฐ ๋ฐฐํฌ (7-8์ฃผ์ฐจ)**
- OpenAI ์ฌ๋ก์ ๋น์ฉ ์ ๊ฐ ์งํ ์ ์ฉ
- ์กฐ์ฌ๋ ์ฑ๋ฅ ์งํ๋ก ๊ฐ์ ์จ ์ธก์
- ๋จ๊ณ์ ๋ฐฐํฌ ์ ๋ต ์๋ฆฝ""",
"executor": """๊ฐ๋
์์ ์ง์์ ์กฐ์ฌ ๋ด์ฉ์ ๊ธฐ๋ฐ์ผ๋ก ๊ตฌ์ฒด์ ์ธ ์คํ ๊ณํ์ ์๋ฆฝํฉ๋๋ค.
**1๋จ๊ณ: ํ์ฌ ๋ชจ๋ธ ์ง๋จ (1์ฃผ์ฐจ)**
- ์์์ผ-ํ์์ผ: MLflow๋ฅผ ์ฌ์ฉํ ํ์ฌ ๋ชจ๋ธ ๋ฉํธ๋ฆญ ์์ง
* ์กฐ์ฌ ๊ฒฐ๊ณผ ์ฐธ๊ณ : Netflix๊ฐ ์ฌ์ฉํ ํต์ฌ ์งํ (์ ํ๋, ์ง์ฐ์๊ฐ, ์ฒ๋ฆฌ๋)
- ์์์ผ-๋ชฉ์์ผ: AutoML ๋๊ตฌ (Optuna, Ray Tune) ์ค์ ๋ฐ ์ด๊ธฐ ์คํ
* ์กฐ์ฌ๋ best practice์ ๋ฐ๋ผ search space ์ ์
- ๊ธ์์ผ: ์ง๋จ ๋ณด๊ณ ์ ์์ฑ ๋ฐ ๊ฐ์ ์ฐ์ ์์ ๊ฒฐ์
**2๋จ๊ณ: ๋ฐ์ดํฐ ํ์ง ๊ฐ์ (2-3์ฃผ์ฐจ)**
- ๋ฐ์ดํฐ ์ ์ ํ์ดํ๋ผ์ธ ๊ตฌ์ถ
* ์กฐ์ฌ ๊ฒฐ๊ณผ์ "80% ๊ท์น" ์ ์ฉ: ๋๋ฝ๊ฐ, ์ด์์น, ๋ ์ด๋ธ ์ค๋ฅ ์ฒ๋ฆฌ
* ์ฝ๋ ์์: `data_quality_pipeline.py` ๊ตฌํ
- ๋ฐ์ดํฐ ์ฆ๊ฐ ๊ตฌํ
* ์ต์ ๊ธฐ๋ฒ ์ ์ฉ: MixUp, CutMix, AutoAugment
* ๊ฒ์ฆ ๋ฐ์ดํฐ์
์ผ๋ก ํจ๊ณผ ์ธก์ (๋ชฉํ: 15% ์ฑ๋ฅ ํฅ์)
**3๋จ๊ณ: ๋ชจ๋ธ ์ต์ ํ ๊ตฌํ (4-6์ฃผ์ฐจ)**
- Knowledge Distillation ๊ตฌํ
* Teacher ๋ชจ๋ธ: ํ์ฌ ๋๊ท๋ชจ ๋ชจ๋ธ
* Student ๋ชจ๋ธ: 90% ์์ ํฌ๊ธฐ ๋ชฉํ (์กฐ์ฌ ๊ฒฐ๊ณผ ๊ธฐ๋ฐ)
* ๊ตฌํ ํ๋ ์์ํฌ: PyTorch/TensorFlow
- Pruning ๋ฐ Quantization ์ ์ฉ
* ๊ตฌ์กฐ์ pruning์ผ๋ก 50% ํ๋ผ๋ฏธํฐ ์ ๊ฑฐ
* INT8 quantization์ผ๋ก ์ถ๊ฐ 4๋ฐฐ ์๋ ํฅ์
* Tesla ์ฌ๋ก ์ฐธ๊ณ : TensorRT ์ต์ ํ ์ ์ฉ
**4๋จ๊ณ: ์ฑ๊ณผ ๊ฒ์ฆ ๋ฐ ๋ฐฐํฌ (7-8์ฃผ์ฐจ)**
- ์ฑ๊ณผ ์งํ ์ธก์
* ์ถ๋ก ์๋: ๋ชฉํ 10๋ฐฐ ํฅ์ (์กฐ์ฌ ๊ฒฐ๊ณผ ๊ธฐ๋ฐ)
* ์ ํ๋ ์์ค: ์ต๋ 2% ์ด๋ด ์ ์ง
* ๋น์ฉ ์ ๊ฐ: 70% ๋ชฉํ (OpenAI ์ฌ๋ก ์ฐธ๊ณ )
- ๋ฐฐํฌ ์ ๋ต
* A/B ํ
์คํธ: 10% ํธ๋ํฝ์ผ๋ก ์์
* ๋ชจ๋ํฐ๋ง: Prometheus + Grafana ๋์๋ณด๋
* ๋กค๋ฐฑ ๊ณํ: ์ฑ๋ฅ ์ ํ ์ ์๋ ๋กค๋ฐฑ
**์์ ๊ฒฐ๊ณผ๋ฌผ**
- ์ต์ ํ๋ ๋ชจ๋ธ (ํฌ๊ธฐ 90% ๊ฐ์, ์๋ 10๋ฐฐ ํฅ์)
- ์์ธ ์ฑ๋ฅ ๋ฒค์น๋งํฌ ๋ณด๊ณ ์
- ํ๋ก๋์
๋ฐฐํฌ ๊ฐ์ด๋ ๋ฐ ๋ชจ๋ํฐ๋ง ๋์๋ณด๋
- ์ฌํ ๊ฐ๋ฅํ ์ต์ ํ ํ์ดํ๋ผ์ธ ์ฝ๋""",
"supervisor_review": """์คํ์ AI์ ๊ณํ์ ๊ฒํ ํ ๊ฒฐ๊ณผ, ์กฐ์ฌ ๋ด์ฉ์ด ์ ๋ฐ์๋์์ต๋๋ค. ๋ค์๊ณผ ๊ฐ์ ๊ฐ์ ์ฌํญ์ ์ ์ํฉ๋๋ค.
**๊ฐ์ **
- ์กฐ์ฌ๋ ์ฌ๋ก๋ค(Netflix, Tesla, OpenAI)์ด ๊ฐ ๋จ๊ณ์ ์ ์ ํ ํ์ฉ๋จ
- ๊ตฌ์ฒด์ ์ธ ๋๊ตฌ์ ๊ธฐ๋ฒ์ด ๋ช
์๋์ด ์คํ ๊ฐ๋ฅ์ฑ์ด ๋์
- ์ธก์ ๊ฐ๋ฅํ ๋ชฉํ๊ฐ ์กฐ์ฌ ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ค์ ๋จ
**๊ฐ์ ํ์์ฌํญ**
1. **๋ฆฌ์คํฌ ๊ด๋ฆฌ ๊ฐํ**
- ๊ฐ ๋จ๊ณ๋ณ ์คํจ ์๋๋ฆฌ์ค์ ๋์ ๋ฐฉ์ ์ถ๊ฐ ํ์
- ๊ธฐ์ ์ ๋ฌธ์ ๋ฐ์ ์ ๋ฐฑ์
๊ณํ ์๋ฆฝ
2. **๋น์ฉ ๋ถ์ ๊ตฌ์ฒดํ**
- OpenAI ์ฌ๋ก์ 70% ์ ๊ฐ์ ์ํ ๊ตฌ์ฒด์ ์ธ ๋น์ฉ ๊ณ์ฐ
- ROI ๋ถ์ ๋ฐ ํฌ์ ๋๋น ํจ๊ณผ ์ธก์ ๋ฐฉ๋ฒ
3. **ํ ํ์
์ฒด๊ณํ**
- ๋ฐ์ดํฐ ๊ณผํ์, ML ์์ง๋์ด, DevOps ๊ฐ ์ญํ ๋ถ๋ด ๋ช
ํํ
- ์ฃผ๊ฐ ์งํ ์ํฉ ๊ณต์ ๋ฐ ์ด์ ํธ๋ํน ํ๋ก์ธ์ค
**์ถ๊ฐ ๊ถ์ฅ์ฌํญ**
- ์ต์ ์ฐ๊ตฌ ๋ํฅ ๋ชจ๋ํฐ๋ง ์ฒด๊ณ ๊ตฌ์ถ
- ๊ฒฝ์์ฌ ๋ฒค์น๋งํน์ ์ํ ์ ๊ธฐ์ ์ธ ์กฐ์ฌ ํ๋ก์ธ์ค
- ๋ด๋ถ ์ง์ ๊ณต์ ๋ฅผ ์ํ ๋ฌธ์ํ ๋ฐ ์ธ๋ฏธ๋ ๊ณํ
- ์คํจ ์ฌ๋ก์์ ๋ฐฐ์ด ๊ตํ์ ์ถ์ ํ๋ ์์คํ
๊ตฌ์ถ""",
"executor_final": """๊ฐ๋
์ AI์ ํผ๋๋ฐฑ์ ์์ ํ ๋ฐ์ํ์ฌ ์ต์ข
์คํ ๋ณด๊ณ ์๋ฅผ ์์ฑํฉ๋๋ค.
# ๐ฏ ๊ธฐ๊ณํ์ต ๋ชจ๋ธ ์ฑ๋ฅ ํฅ์ ์ต์ข
์คํ ๋ณด๊ณ ์
## ๐ Executive Summary
๋ณธ ๋ณด๊ณ ์๋ ์น ๊ฒ์์ ํตํด ์์ง๋ ์ต์ ์ฌ๋ก์ ๊ฐ๋
์ AI์ ์ ๋ต์ ์ง์นจ์ ๋ฐํ์ผ๋ก, 8์ฃผ๊ฐ์ ์ฒด๊ณ์ ์ธ ๋ชจ๋ธ ์ต์ ํ ํ๋ก์ ํธ๋ฅผ ์ ์ํฉ๋๋ค. ๋ชฉํ๋ ๋ชจ๋ธ ํฌ๊ธฐ 90% ๊ฐ์, ์ถ๋ก ์๋ 10๋ฐฐ ํฅ์, ์ด์ ๋น์ฉ 70% ์ ๊ฐ์
๋๋ค.
## ๐ 1๋จ๊ณ: ํ์ฌ ๋ชจ๋ธ ์ง๋จ ๋ฐ ๋ฒ ์ด์ค๋ผ์ธ ์ค์ (1์ฃผ์ฐจ)
### ์คํ ๊ณํ
**์-ํ์์ผ: ์ฑ๋ฅ ๋ฉํธ๋ฆญ ์์ง**
- MLflow๋ฅผ ํตํ ํ์ฌ ๋ชจ๋ธ ์ ์ฒด ๋ถ์
- Netflix ์ฌ๋ก ๊ธฐ๋ฐ ํต์ฌ ์งํ: ์ ํ๋(92%), ์ง์ฐ์๊ฐ(45ms), ์ฒ๋ฆฌ๋(1,000 req/s)
- ๋ฆฌ์์ค ์ฌ์ฉ๋: GPU ๋ฉ๋ชจ๋ฆฌ 8GB, ์ถ๋ก ์ CPU ์ฌ์ฉ๋ฅ 85%
**์-๋ชฉ์์ผ: AutoML ์ด๊ธฐ ํ์**
- Optuna๋ก ํ์ดํผํ๋ผ๋ฏธํฐ ์ต์ ํ (200ํ ์๋)
- Ray Tune์ผ๋ก ๋ถ์ฐ ํ์ต ํ๊ฒฝ ๊ตฌ์ถ
- ์ด๊ธฐ ๊ฐ์ ๊ฐ๋ฅ์ฑ: 15-20% ์ฑ๋ฅ ํฅ์ ์์
**๊ธ์์ผ: ์ง๋จ ๋ณด๊ณ ์ ๋ฐ ๋ฆฌ์คํฌ ๋ถ์**
- ์ฃผ์ ๋ณ๋ชฉ: ๋ชจ๋ธ ํฌ๊ธฐ(2.5GB), ๋ฐฐ์น ์ฒ๋ฆฌ ๋นํจ์จ์ฑ
- ๋ฆฌ์คํฌ: ๋ฐ์ดํฐ ๋๋ฆฌํํธ, ํ๋์จ์ด ์ ์ฝ
- ๋ฐฑ์
๊ณํ: ํด๋ผ์ฐ๋ GPU ์ธ์คํด์ค ํ๋ณด
### ์์ ์ฐ์ถ๋ฌผ
- ์์ธ ์ฑ๋ฅ ๋ฒ ์ด์ค๋ผ์ธ ๋ฌธ์
- ๊ฐ์ ๊ธฐํ ์ฐ์ ์์ ๋งคํธ๋ฆญ์ค
- ๋ฆฌ์คํฌ ๋ ์ง์คํฐ ๋ฐ ๋์ ๊ณํ
## ๐ 2๋จ๊ณ: ๋ฐ์ดํฐ ํ์ง ๊ฐ์ (2-3์ฃผ์ฐจ)
### ์คํ ๊ณํ
**2์ฃผ์ฐจ: ๋ฐ์ดํฐ ์ ์ ํ์ดํ๋ผ์ธ**
```python
# data_quality_pipeline.py ์ฃผ์ ๊ตฌ์ฑ
class DataQualityPipeline:
def __init__(self):
self.validators = [
MissingValueHandler(threshold=0.05),
OutlierDetector(method='isolation_forest'),
LabelConsistencyChecker(),
DataDriftMonitor()
]
def process(self, data):
# 80% ๊ท์น ์ ์ฉ: ๋ฐ์ดํฐ ํ์ง์ด ์ฑ๋ฅ์ 80% ๊ฒฐ์
for validator in self.validators:
data = validator.transform(data)
self.log_metrics(validator.get_stats())
return data
```
**3์ฃผ์ฐจ: ๊ณ ๊ธ ๋ฐ์ดํฐ ์ฆ๊ฐ**
- MixUp: 15% ์ ํ๋ ํฅ์ ์์
- CutMix: ๊ฒฝ๊ณ ๊ฒ์ถ ์ฑ๋ฅ 20% ๊ฐ์
- AutoAugment: ์๋ ์ต์ ์ฆ๊ฐ ์ ์ฑ
ํ์
- A/B ํ
์คํธ: ๊ฐ ๊ธฐ๋ฒ๋ณ ํจ๊ณผ ์ธก์
### ๋ฆฌ์คํฌ ๋์
- ๋ฐ์ดํฐ ํ์ง ์ ํ ์: ๋กค๋ฐฑ ๋ฉ์ปค๋์ฆ ๊ตฌํ
- ์ฆ๊ฐ ๊ณผ์ ํฉ ๋ฐฉ์ง: ๊ฒ์ฆ์
๋ถ๋ฆฌ ๋ฐ ๊ต์ฐจ ๊ฒ์ฆ
### ์์ ์ฐ์ถ๋ฌผ
- ์๋ํ๋ ๋ฐ์ดํฐ ํ์ง ํ์ดํ๋ผ์ธ
- ๋ฐ์ดํฐ ํ์ง ๋์๋ณด๋ (Grafana)
- 15% ์ด์ ์ฑ๋ฅ ํฅ์ ๊ฒ์ฆ ๋ณด๊ณ ์
## ๐ 3๋จ๊ณ: ๋ชจ๋ธ ์ต์ ํ ๊ตฌํ (4-6์ฃผ์ฐจ)
### ์คํ ๊ณํ
**4-5์ฃผ์ฐจ: Knowledge Distillation**
- Teacher ๋ชจ๋ธ: ํ์ฌ 2.5GB ๋ชจ๋ธ
- Student ๋ชจ๋ธ ์ํคํ
์ฒ:
* ํ๋ผ๋ฏธํฐ ์: 250M โ 25M (90% ๊ฐ์)
* ๋ ์ด์ด ์: 24 โ 6
* Hidden dimension: 1024 โ 256
- ํ๋ จ ์ ๋ต:
* Temperature: 5.0
* Alpha (KD loss weight): 0.7
* ํ๋ จ ์ํญ: 50
**6์ฃผ์ฐจ: Pruning & Quantization**
- ๊ตฌ์กฐ์ Pruning:
* Magnitude ๊ธฐ๋ฐ 50% ์ฑ๋ ์ ๊ฑฐ
* Fine-tuning: 10 ์ํญ
- INT8 Quantization:
* Post-training quantization
* Calibration dataset: 1,000 ์ํ
- TensorRT ์ต์ ํ (Tesla ์ฌ๋ก ์ ์ฉ):
* FP16 ์ถ๋ก ํ์ฑํ
* ๋์ ๋ฐฐ์น ์ต์ ํ
### ํ ํ์
์ฒด๊ณ
- ML ์์ง๋์ด: ๋ชจ๋ธ ์ํคํ
์ฒ ๋ฐ ํ๋ จ
- DevOps: ์ธํ๋ผ ๋ฐ ๋ฐฐํฌ ํ์ดํ๋ผ์ธ
- ๋ฐ์ดํฐ ๊ณผํ์: ์ฑ๋ฅ ๋ถ์ ๋ฐ ๊ฒ์ฆ
- ์ฃผ๊ฐ ์คํ ๋์
๋ฏธํ
๋ฐ Jira ์ด์ ํธ๋ํน
### ์์ ์ฐ์ถ๋ฌผ
- ์ต์ ํ๋ ๋ชจ๋ธ ์ฒดํฌํฌ์ธํธ
- ์ฑ๋ฅ ๋ฒค์น๋งํฌ ์์ธ ๋ณด๊ณ ์
- ๋ชจ๋ธ ๋ณํ ์๋ํ ์คํฌ๋ฆฝํธ
## ๐ 4๋จ๊ณ: ์ฑ๊ณผ ๊ฒ์ฆ ๋ฐ ํ๋ก๋์
๋ฐฐํฌ (7-8์ฃผ์ฐจ)
### ์คํ ๊ณํ
**7์ฃผ์ฐจ: ์ข
ํฉ ์ฑ๋ฅ ๊ฒ์ฆ**
- ์ฑ๋ฅ ์งํ ๋ฌ์ฑ๋:
* ์ถ๋ก ์๋: 45ms โ 4.5ms (10๋ฐฐ ํฅ์) โ
* ๋ชจ๋ธ ํฌ๊ธฐ: 2.5GB โ 250MB (90% ๊ฐ์) โ
* ์ ํ๋ ์์ค: 92% โ 90.5% (1.5% ์์ค) โ
- ๋น์ฉ ๋ถ์:
* GPU ์ธ์คํด์ค: $2,000/์ โ $600/์
* ์ฒ๋ฆฌ๋ ์ฆ๊ฐ๋ก ์ธํ ์๋ฒ ์ ๊ฐ์: 10๋ โ 3๋
* ์ด ๋น์ฉ ์ ๊ฐ: 70% ๋ฌ์ฑ โ
**8์ฃผ์ฐจ: ๋จ๊ณ์ ๋ฐฐํฌ**
- Canary ๋ฐฐํฌ:
* 1์ผ์ฐจ: 1% ํธ๋ํฝ
* 3์ผ์ฐจ: 10% ํธ๋ํฝ
* 7์ผ์ฐจ: 50% ํธ๋ํฝ
* 14์ผ์ฐจ: 100% ์ ํ
- ๋ชจ๋ํฐ๋ง ์ค์ :
* Prometheus + Grafana ๋์๋ณด๋
* ์๋ฆผ ์๊ณ๊ฐ: ์ง์ฐ์๊ฐ >10ms, ์ค๋ฅ์จ >0.1%
- ๋กค๋ฐฑ ๊ณํ:
* ์๋ ๋กค๋ฐฑ ํธ๋ฆฌ๊ฑฐ ์ค์
* Blue-Green ๋ฐฐํฌ๋ก ์ฆ์ ์ ํ ๊ฐ๋ฅ
### ROI ๋ถ์
- ์ด๊ธฐ ํฌ์: $50,000 (์ธ๊ฑด๋น + ์ธํ๋ผ)
- ์๊ฐ ์ ๊ฐ์ก: $14,000
- ํฌ์ ํ์ ๊ธฐ๊ฐ: 3.6๊ฐ์
- 1๋
์์ด์ต: $118,000
### ์์ ์ฐ์ถ๋ฌผ
- ํ๋ก๋์
๋ฐฐํฌ ์๋ฃ
- ์ค์๊ฐ ๋ชจ๋ํฐ๋ง ๋์๋ณด๋
- ROI ๋ถ์ ๋ณด๊ณ ์
- ์ด์ ๊ฐ์ด๋ ๋ฌธ์
## ๐ ์ง์์ ๊ฐ์ ๊ณํ
### ๋ชจ๋ํฐ๋ง ๋ฐ ์ ์ง๋ณด์
- ์๊ฐ ์ฑ๋ฅ ๋ฆฌ๋ทฐ ๋ฏธํ
- ๋ถ๊ธฐ๋ณ ์ฌํ๋ จ ๊ณํ
- ์ ๊ธฐ์ ๋์
๊ฒํ (Sparse Models, MoE)
### ์ง์ ๊ณต์
- ๋ด๋ถ ๊ธฐ์ ์ธ๋ฏธ๋ (์ 1ํ)
- ์ธ๋ถ ์ปจํผ๋ฐ์ค ๋ฐํ ์ค๋น
- ์คํ์์ค ๊ธฐ์ฌ ๊ณํ
### ์ฐจ๊ธฐ ํ๋ก์ ํธ
- ์ฃ์ง ๋๋ฐ์ด์ค ๋ฐฐํฌ ์ต์ ํ
- ์ฐํฉ ํ์ต(Federated Learning) ๋์
- AutoML ํ๋ซํผ ๊ตฌ์ถ
## ๐ ๊ฒฐ๋ก
๋ณธ ํ๋ก์ ํธ๋ ์ต์ ์ฐ๊ตฌ ๊ฒฐ๊ณผ์ ์
๊ณ ๋ฒ ์คํธ ํ๋ํฐ์ค๋ฅผ ์ ์ฉํ์ฌ, 8์ฃผ ๋ง์ ๋ชจ๋ธ ์ฑ๋ฅ์ ํ๊ธฐ์ ์ผ๋ก ๊ฐ์ ํ๊ณ ์ด์ ๋น์ฉ์ 70% ์ ๊ฐํ๋ ์ฑ๊ณผ๋ฅผ ๋ฌ์ฑํ ๊ฒ์ผ๋ก ์์๋ฉ๋๋ค. ์ฒด๊ณ์ ์ธ ์ ๊ทผ๊ณผ ๋ฆฌ์คํฌ ๊ด๋ฆฌ, ๊ทธ๋ฆฌ๊ณ ์ง์์ ์ธ ๊ฐ์ ๊ณํ์ ํตํด ์ฅ๊ธฐ์ ์ธ ๊ฒฝ์๋ ฅ์ ํ๋ณดํ ์ ์์ต๋๋ค.
---
*์์ฑ์ผ: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}*
*์์ฑ์: ํ๋ ฅ์ AI ์์คํ
(๊ฐ๋
์, ์กฐ์ฌ์, ์คํ์ AI)*"""
}
# ํ๋กฌํํธ ๋ด์ฉ์ ๋ฐ๋ผ ์ ์ ํ ์๋ต ์ ํ
if role == "supervisor" and "์กฐ์ฌ์ AI๊ฐ ์ ๋ฆฌํ" in messages[0]["content"]:
response = test_responses["supervisor_execution"]
elif role == "supervisor" and messages[0]["content"].find("์คํ์ AI์ ๋ต๋ณ") > -1:
response = test_responses["supervisor_review"]
elif role == "supervisor":
response = test_responses["supervisor_initial"]
elif role == "researcher":
response = test_responses["researcher"]
elif role == "executor" and "์ต์ข
๋ณด๊ณ ์" in messages[0]["content"]:
response = test_responses["executor_final"]
else:
response = test_responses["executor"]
yield from self.simulate_streaming(response, role)
return
# ์ค์ API ํธ์ถ
try:
system_prompts = {
"supervisor": "๋น์ ์ ๊ฑฐ์์ ๊ด์ ์์ ๋ถ์ํ๊ณ ์ง๋ํ๋ ๊ฐ๋
์ AI์
๋๋ค.",
"researcher": "๋น์ ์ ์ ๋ณด๋ฅผ ์กฐ์ฌํ๊ณ ์ฒด๊ณ์ ์ผ๋ก ์ ๋ฆฌํ๋ ์กฐ์ฌ์ AI์
๋๋ค.",
"executor": "๋น์ ์ ์ธ๋ถ์ ์ธ ๋ด์ฉ์ ๊ตฌํํ๋ ์คํ์ AI์
๋๋ค."
}
full_messages = [
{"role": "system", "content": system_prompts.get(role, "")},
*messages
]
payload = {
"model": self.model_id,
"messages": full_messages,
"max_tokens": 2048,
"temperature": 0.7,
"top_p": 0.8,
"stream": True,
"stream_options": {"include_usage": True}
}
logger.info(f"API ์คํธ๋ฆฌ๋ฐ ํธ์ถ ์์ - Role: {role}")
response = requests.post(
self.api_url,
headers=self.create_headers(),
json=payload,
stream=True,
timeout=10
)
if response.status_code != 200:
logger.error(f"API ์ค๋ฅ: {response.status_code}")
yield f"โ API ์ค๋ฅ ({response.status_code}): {response.text[:200]}"
return
for line in response.iter_lines():
if line:
line = line.decode('utf-8')
if line.startswith("data: "):
data = line[6:]
if data == "[DONE]":
break
try:
chunk = json.loads(data)
if "choices" in chunk and chunk["choices"]:
content = chunk["choices"][0].get("delta", {}).get("content", "")
if content:
yield content
except json.JSONDecodeError:
continue
except requests.exceptions.Timeout:
yield "โฑ๏ธ API ํธ์ถ ์๊ฐ์ด ์ด๊ณผ๋์์ต๋๋ค. ๋ค์ ์๋ํด์ฃผ์ธ์."
except requests.exceptions.ConnectionError:
yield "๐ API ์๋ฒ์ ์ฐ๊ฒฐํ ์ ์์ต๋๋ค. ์ธํฐ๋ท ์ฐ๊ฒฐ์ ํ์ธํด์ฃผ์ธ์."
except Exception as e:
logger.error(f"์คํธ๋ฆฌ๋ฐ ์ค ์ค๋ฅ: {str(e)}")
yield f"โ ์ค๋ฅ ๋ฐ์: {str(e)}"
# ์์คํ
์ธ์คํด์ค ์์ฑ
llm_system = LLMCollaborativeSystem()
# ๋ด๋ถ ํ์คํ ๋ฆฌ ๊ด๋ฆฌ (UI์๋ ํ์ํ์ง ์์)
internal_history = []
def process_query_streaming(user_query: str):
"""์คํธ๋ฆฌ๋ฐ์ ์ง์ํ๋ ์ฟผ๋ฆฌ ์ฒ๋ฆฌ"""
global internal_history
if not user_query:
return "", "", "", "", "โ ์ง๋ฌธ์ ์
๋ ฅํด์ฃผ์ธ์."
conversation_log = []
all_responses = {"supervisor": [], "researcher": [], "executor": []}
try:
# 1๋จ๊ณ: ๊ฐ๋
์ AI ์ด๊ธฐ ๋ถ์ ๋ฐ ํค์๋ ์ถ์ถ
supervisor_prompt = llm_system.create_supervisor_initial_prompt(user_query)
supervisor_initial_response = ""
supervisor_text = "[์ด๊ธฐ ๋ถ์] ๐ ์์ฑ ์ค...\n"
for chunk in llm_system.call_llm_streaming(
[{"role": "user", "content": supervisor_prompt}],
"supervisor"
):
supervisor_initial_response += chunk
supervisor_text = f"[์ด๊ธฐ ๋ถ์] - {datetime.now().strftime('%H:%M:%S')}\n{supervisor_initial_response}"
yield supervisor_text, "", "", "", "๐ ๊ฐ๋
์ AI๊ฐ ๋ถ์ ์ค..."
all_responses["supervisor"].append(supervisor_initial_response)
# ํค์๋ ์ถ์ถ
keywords = llm_system.extract_keywords(supervisor_initial_response)
logger.info(f"์ถ์ถ๋ ํค์๋: {keywords}")
# 2๋จ๊ณ: ๋ธ๋ ์ด๋ธ ๊ฒ์ ์ํ
researcher_text = "[์น ๊ฒ์] ๐ ๊ฒ์ ์ค...\n"
yield supervisor_text, researcher_text, "", "", "๐ ์น ๊ฒ์ ์ํ ์ค..."
search_results = {}
for keyword in keywords:
results = llm_system.brave_search(keyword)
if results:
search_results[keyword] = results
researcher_text += f"โ '{keyword}' ๊ฒ์ ์๋ฃ\n"
yield supervisor_text, researcher_text, "", "", f"๐ '{keyword}' ๊ฒ์ ์ค..."
# 3๋จ๊ณ: ์กฐ์ฌ์ AI๊ฐ ๊ฒ์ ๊ฒฐ๊ณผ ์ ๋ฆฌ
researcher_prompt = llm_system.create_researcher_prompt(user_query, supervisor_initial_response, search_results)
researcher_response = ""
researcher_text = "[์กฐ์ฌ ๊ฒฐ๊ณผ ์ ๋ฆฌ] ๐ ์์ฑ ์ค...\n"
for chunk in llm_system.call_llm_streaming(
[{"role": "user", "content": researcher_prompt}],
"researcher"
):
researcher_response += chunk
researcher_text = f"[์กฐ์ฌ ๊ฒฐ๊ณผ ์ ๋ฆฌ] - {datetime.now().strftime('%H:%M:%S')}\n{researcher_response}"
yield supervisor_text, researcher_text, "", "", "๐ ์กฐ์ฌ์ AI๊ฐ ์ ๋ฆฌ ์ค..."
all_responses["researcher"].append(researcher_response)
# 4๋จ๊ณ: ๊ฐ๋
์ AI๊ฐ ์กฐ์ฌ ๋ด์ฉ ๊ธฐ๋ฐ์ผ๋ก ์คํ ์ง์
supervisor_execution_prompt = llm_system.create_supervisor_execution_prompt(user_query, researcher_response)
supervisor_execution_response = ""
supervisor_text += "\n\n---\n\n[์คํ ์ง์] ๐ ์์ฑ ์ค...\n"
for chunk in llm_system.call_llm_streaming(
[{"role": "user", "content": supervisor_execution_prompt}],
"supervisor"
):
supervisor_execution_response += chunk
temp_text = f"{all_responses['supervisor'][0]}\n\n---\n\n[์คํ ์ง์] - {datetime.now().strftime('%H:%M:%S')}\n{supervisor_execution_response}"
supervisor_text = f"[์ด๊ธฐ ๋ถ์] - {datetime.now().strftime('%H:%M:%S')}\n{temp_text}"
yield supervisor_text, researcher_text, "", "", "๐ฏ ๊ฐ๋
์ AI๊ฐ ์ง์ ์ค..."
all_responses["supervisor"].append(supervisor_execution_response)
# 5๋จ๊ณ: ์คํ์ AI๊ฐ ์กฐ์ฌ ๋ด์ฉ๊ณผ ์ง์๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ด๊ธฐ ๊ตฌํ
executor_prompt = llm_system.create_executor_prompt(user_query, supervisor_execution_response, researcher_response)
executor_response = ""
executor_text = "[์ด๊ธฐ ๊ตฌํ] ๐ ์์ฑ ์ค...\n"
for chunk in llm_system.call_llm_streaming(
[{"role": "user", "content": executor_prompt}],
"executor"
):
executor_response += chunk
executor_text = f"[์ด๊ธฐ ๊ตฌํ] - {datetime.now().strftime('%H:%M:%S')}\n{executor_response}"
yield supervisor_text, researcher_text, executor_text, "", "๐ง ์คํ์ AI๊ฐ ๊ตฌํ ์ค..."
all_responses["executor"].append(executor_response)
# 6๋จ๊ณ: ๊ฐ๋
์ AI ๊ฒํ ๋ฐ ํผ๋๋ฐฑ
review_prompt = f"""๋น์ ์ ๊ฑฐ์์ ๊ด์ ์์ ๋ถ์ํ๊ณ ์ง๋ํ๋ ๊ฐ๋
์ AI์
๋๋ค.
์ฌ์ฉ์ ์ง๋ฌธ: {user_query}
์คํ์ AI์ ๋ต๋ณ:
{executor_response}
์ด ๋ต๋ณ์ ๊ฒํ ํ๊ณ ๊ฐ์ ์ ๊ณผ ์ถ๊ฐ ๊ณ ๋ ค์ฌํญ์ ์ ์ํด์ฃผ์ธ์. ๊ตฌ์ฒด์ ์ด๊ณ ์คํ ๊ฐ๋ฅํ ๊ฐ์ ๋ฐฉ์์ ์ ์ํ์ธ์."""
review_response = ""
supervisor_text = f"[์ด๊ธฐ ๋ถ์] - {datetime.now().strftime('%H:%M:%S')}\n{all_responses['supervisor'][0]}\n\n---\n\n[์คํ ์ง์] - {datetime.now().strftime('%H:%M:%S')}\n{all_responses['supervisor'][1]}\n\n---\n\n[๊ฒํ ๋ฐ ํผ๋๋ฐฑ] ๐ ์์ฑ ์ค...\n"
for chunk in llm_system.call_llm_streaming(
[{"role": "user", "content": review_prompt}],
"supervisor"
):
review_response += chunk
temp_text = f"{all_responses['supervisor'][0]}\n\n---\n\n[์คํ ์ง์] - {datetime.now().strftime('%H:%M:%S')}\n{all_responses['supervisor'][1]}\n\n---\n\n[๊ฒํ ๋ฐ ํผ๋๋ฐฑ] - {datetime.now().strftime('%H:%M:%S')}\n{review_response}"
supervisor_text = f"[์ด๊ธฐ ๋ถ์] - {datetime.now().strftime('%H:%M:%S')}\n{temp_text}"
yield supervisor_text, researcher_text, executor_text, "", "๐ ๊ฐ๋
์ AI๊ฐ ๊ฒํ ์ค..."
all_responses["supervisor"].append(review_response)
# 7๋จ๊ณ: ์คํ์ AI ์ต์ข
๋ณด๊ณ ์ (ํผ๋๋ฐฑ ๋ฐ์)
final_executor_prompt = llm_system.create_executor_final_prompt(
user_query,
executor_response,
review_response,
researcher_response
)
final_executor_response = ""
executor_text += "\n\n---\n\n[์ต์ข
๋ณด๊ณ ์] ๐ ์์ฑ ์ค...\n"
for chunk in llm_system.call_llm_streaming(
[{"role": "user", "content": final_executor_prompt}],
"executor"
):
final_executor_response += chunk
temp_text = f"[์ด๊ธฐ ๊ตฌํ] - {datetime.now().strftime('%H:%M:%S')}\n{all_responses['executor'][0]}\n\n---\n\n[์ต์ข
๋ณด๊ณ ์] - {datetime.now().strftime('%H:%M:%S')}\n{final_executor_response}"
executor_text = temp_text
yield supervisor_text, researcher_text, executor_text, "", "๐ ์ต์ข
๋ณด๊ณ ์ ์์ฑ ์ค..."
all_responses["executor"].append(final_executor_response)
# ์ต์ข
๊ฒฐ๊ณผ ์์ฑ (์ต์ข
๋ณด๊ณ ์๋ฅผ ๋ฉ์ธ์ผ๋ก)
final_summary = f"""## ๐ฏ ์ต์ข
์ข
ํฉ ๋ณด๊ณ ์
### ๐ ์ฌ์ฉ์ ์ง๋ฌธ
{user_query}
### ๐ ์ต์ข
๋ณด๊ณ ์ (์คํ์ AI - ํผ๋๋ฐฑ ๋ฐ์)
{final_executor_response}
---
<details>
<summary>๐ ์ ์ฒด ํ๋ ฅ ๊ณผ์ ๋ณด๊ธฐ</summary>
#### ๐ ๊ฑฐ์์ ๋ถ์ (๊ฐ๋
์ AI)
{all_responses['supervisor'][0]}
#### ๐ ์กฐ์ฌ ๊ฒฐ๊ณผ (์กฐ์ฌ์ AI)
{researcher_response}
#### ๐ฏ ์คํ ์ง์ (๊ฐ๋
์ AI)
{all_responses['supervisor'][1]}
#### ๐ก ์ด๊ธฐ ๊ตฌํ (์คํ์ AI)
{executor_response}
#### โจ ๊ฒํ ๋ฐ ๊ฐ์ ์ฌํญ (๊ฐ๋
์ AI)
{review_response}
</details>
---
*์ด ๋ณด๊ณ ์๋ ์น ๊ฒ์์ ํตํ ์ต์ ์ ๋ณด์ AI๋ค์ ํ๋ ฅ, ๊ทธ๋ฆฌ๊ณ ํผ๋๋ฐฑ ๋ฐ์์ ํตํด ์์ฑ๋์์ต๋๋ค.*"""
# ๋ด๋ถ ํ์คํ ๋ฆฌ ์
๋ฐ์ดํธ (UI์๋ ํ์ํ์ง ์์)
internal_history.append((user_query, final_summary))
yield supervisor_text, researcher_text, executor_text, final_summary, "โ
์ต์ข
๋ณด๊ณ ์ ์์ฑ!"
except Exception as e:
error_msg = f"โ ์ฒ๋ฆฌ ์ค ์ค๋ฅ: {str(e)}"
yield "", "", "", error_msg, error_msg
def clear_all():
"""๋ชจ๋ ๋ด์ฉ ์ด๊ธฐํ"""
global internal_history
internal_history = []
return "", "", "", "", "๐ ์ด๊ธฐํ๋์์ต๋๋ค."
# Gradio ์ธํฐํ์ด์ค
css = """
.gradio-container {
font-family: 'Arial', sans-serif;
}
.supervisor-box textarea {
border-left: 4px solid #667eea !important;
padding-left: 10px !important;
}
.researcher-box textarea {
border-left: 4px solid #10b981 !important;
padding-left: 10px !important;
}
.executor-box textarea {
border-left: 4px solid #764ba2 !important;
padding-left: 10px !important;
}
"""
with gr.Blocks(title="ํ๋ ฅ์ LLM ์์คํ
", theme=gr.themes.Soft(), css=css) as app:
# ์
๋ ฅ ์น์
with gr.Row():
with gr.Column():
user_input = gr.Textbox(
label="์ง๋ฌธ ์
๋ ฅ",
placeholder="์: ๊ธฐ๊ณํ์ต ๋ชจ๋ธ์ ์ฑ๋ฅ์ ํฅ์์ํค๋ ๋ฐฉ๋ฒ์?",
lines=3
)
with gr.Row():
submit_btn = gr.Button("๐ ๋ถ์ ์์", variant="primary", scale=2)
clear_btn = gr.Button("๐๏ธ ์ด๊ธฐํ", scale=1)
status_text = gr.Textbox(
label="์ํ",
interactive=False,
value="๋๊ธฐ ์ค...",
max_lines=1
)
# ์ต์ข
๊ฒฐ๊ณผ
with gr.Row():
with gr.Column():
with gr.Accordion("๐ ์ต์ข
์ข
ํฉ ๊ฒฐ๊ณผ", open=True):
final_output = gr.Markdown(
value="*์ง๋ฌธ์ ์
๋ ฅํ๋ฉด ๊ฒฐ๊ณผ๊ฐ ์ฌ๊ธฐ์ ํ์๋ฉ๋๋ค.*"
)
# AI ์ถ๋ ฅ๋ค - ํ ์ค์ ๋๋ํ ๋ฐฐ์น
with gr.Row():
# ๊ฐ๋
์ AI ์ถ๋ ฅ
with gr.Column():
gr.Markdown("### ๐ง ๊ฐ๋
์ AI (๊ฑฐ์์ ๋ถ์)")
supervisor_output = gr.Textbox(
label="",
lines=20,
max_lines=25,
interactive=False,
elem_classes=["supervisor-box"]
)
# ์กฐ์ฌ์ AI ์ถ๋ ฅ
with gr.Column():
gr.Markdown("### ๐ ์กฐ์ฌ์ AI (์น ๊ฒ์ & ์ ๋ฆฌ)")
researcher_output = gr.Textbox(
label="",
lines=20,
max_lines=25,
interactive=False,
elem_classes=["researcher-box"]
)
# ์คํ์ AI ์ถ๋ ฅ
with gr.Column():
gr.Markdown("### ๐๏ธ ์คํ์ AI (๋ฏธ์์ ๊ตฌํ)")
executor_output = gr.Textbox(
label="",
lines=20,
max_lines=25,
interactive=False,
elem_classes=["executor-box"]
)
# ์์
gr.Examples(
examples=[
"๊ธฐ๊ณํ์ต ๋ชจ๋ธ์ ์ฑ๋ฅ์ ํฅ์์ํค๋ ์ต์ ๋ฐฉ๋ฒ์?",
"2024๋
ํจ๊ณผ์ ์ธ ํ๋ก์ ํธ ๊ด๋ฆฌ ๋๊ตฌ์ ์ ๋ต์?",
"์ง์ ๊ฐ๋ฅํ ๋น์ฆ๋์ค ๋ชจ๋ธ์ ์ต์ ํธ๋ ๋๋?",
"์ต์ ๋ฐ์ดํฐ ์๊ฐํ ๋๊ตฌ์ ๊ธฐ๋ฒ์?",
"์๊ฒฉ ํ์ ์์ฐ์ฑ์ ๋์ด๋ ๊ฒ์ฆ๋ ๋ฐฉ๋ฒ์?"
],
inputs=user_input,
label="๐ก ์์ ์ง๋ฌธ"
)
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
submit_btn.click(
fn=process_query_streaming,
inputs=[user_input],
outputs=[supervisor_output, researcher_output, executor_output, final_output, status_text]
).then(
fn=lambda: "",
outputs=[user_input]
)
user_input.submit(
fn=process_query_streaming,
inputs=[user_input],
outputs=[supervisor_output, researcher_output, executor_output, final_output, status_text]
).then(
fn=lambda: "",
outputs=[user_input]
)
clear_btn.click(
fn=clear_all,
outputs=[supervisor_output, researcher_output, executor_output, final_output, status_text]
)
if __name__ == "__main__":
app.queue() # ์คํธ๋ฆฌ๋ฐ์ ์ํ ํ ํ์ฑํ
app.launch(
server_name="0.0.0.0",
server_port=7860,
share=True,
show_error=True
) |