Spaces:
Sleeping
Sleeping
Download src/openai_client.py from GGGIS/GGGISfolio: direct link, hf CLI and curl.
- Browser
- Download file 10.6 kB
-
https://huggingface.co/spaces/GGGIS/GGGISfolio/resolve/main/src/openai_client.py
- Command line
-
hf download hf://spaces/GGGIS/GGGISfolio/src/openai_client.py
-
curl -L -o openai_client.py https://huggingface.co/spaces/GGGIS/GGGISfolio/resolve/main/src/openai_client.py
10.6 kB
| from openai import OpenAI | |
| import os | |
| from typing import List, Dict, Any | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| class OpenAIClient: | |
| def __init__(self, api_key: str = None): | |
| """ | |
| OpenAI ํด๋ผ์ด์ธํธ ์ด๊ธฐํ | |
| api_key๊ฐ None์ด๋ฉด ํ๊ฒฝ๋ณ์ OPENAI_API_KEY์์ ๊ฐ์ ธ์ด | |
| """ | |
| self.api_key = api_key or os.getenv("OPENAI_API_KEY") | |
| if not self.api_key: | |
| raise ValueError("OpenAI API key๊ฐ ํ์ํฉ๋๋ค.") | |
| self.client = OpenAI(api_key=self.api_key) | |
| def generate_idea( | |
| self, contest_info: Dict[str, str], nodes_data: List[Dict[str, Any]] | |
| ) -> Dict[str, str]: | |
| """ | |
| ๊ณต๋ชจ์ ์ ๋ณด์ ๋ ธ๋ ๋ฐ์ดํฐ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์์ด๋์ด ์์ฑ | |
| """ | |
| try: | |
| # ๋ ธ๋ ์์ฝ | |
| nodes_summary = self._format_nodes_for_prompt(nodes_data) | |
| prompt = self._create_prompt(contest_info, nodes_summary) | |
| # OpenAI GPT ํธ์ถ | |
| response = self.client.chat.completions.create( | |
| model="gpt-4o", # ๋๋ "gpt-4", "gpt-3.5-turbo" | |
| messages=[ | |
| { | |
| "role": "system", | |
| "content": """ | |
| ๋น์ ์ ์์ด๋์์ด์ ์ ๋ฌธ๊ฐ์ด์ ํฌ๋ฆฌ์์ดํฐ๋ธ ์ปจ์คํดํธ์ ๋๋ค. ์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ง๋ ๋ค์ ๋ค ๊ฐ์ง ์์๋ฅผ ๊ฒฐํฉํด, ๊ตฌ์ฒด์ ์ด๊ณ ์คํ ๊ฐ๋ฅํ ํ์ ์์ด๋์ด๋ฅผ ์ ์ํด์ผ ํฉ๋๋ค. | |
| 1. ๋๋ฉ์ธ(Domain): ์์ด๋์์ด์ ์ ์ถ๋ฐ์ ์ด ๋๋ ๋ถ์ผ (์: ๋์ , ์๋ฃ, ๋ฒ, ์์ ๋ฑ) | |
| 2. ์ปจํ ์คํธ(Context): ํด๋น ๊ณผ์ ๋ฅผ ์ํํด์ผ ํ๋ ์ด์ ๋ ๋ฐฐ๊ฒฝ ์ค๋ช (์: ๊ณต๋ชจ์ ์ฃผ์ , ์ฃผ์ต๊ธฐ๊ด์ ๋ชฉํ, ์์ฅ ๋ํฅ) | |
| 3. ์ด๊ทธ๋์ดํฐ(Igniter): ์์ด๋์ด์ ํต์ฌ ๋ฐฉํฅ์ฑ์ ๊ฒฐ์ ํ๋ ํค์๋๋ ์ง๋ฌธ (์: ์ง์๊ฐ๋ฅ์ฑ ๊ทน๋ํ, ๋ฐ์ดํฐ ๋ฏผ์ฃผํ, ์ฌ์ฉ์ ์ฐธ์ฌ ๊ฐํ ๋ฑ) | |
| 4. ๋ ธ๋(Nodes): ์ฌ์ฉ์์ ๊ฒฝํ, ํ๋ก์ ํธ ์ฌ๋ก, ๊ธฐ์ ์คํ ๋ฑ Connecting the Dots๋ฅผ ์ํ ์์ฐ ์ปฌ๋ ์ | |
| """, | |
| }, | |
| {"role": "user", "content": prompt}, | |
| ], | |
| temperature=0.7, | |
| max_tokens=5000, | |
| ) | |
| generated_text = response.choices[0].message.content | |
| idea = self._parse_generated_idea(generated_text, contest_info) | |
| return idea | |
| except Exception as e: | |
| return { | |
| "error": f"์์ด๋์ด ์์ฑ ์ค ์ค๋ฅ: {str(e)}", | |
| "ai_name": "ChatGPT", | |
| "title": "์ค๋ฅ", | |
| "overview": "์์ด๋์ด ์์ฑ ์คํจ", | |
| "problem": "", | |
| "solution": "", | |
| "implementation": "", | |
| "expected_effect": "", | |
| "rationale": "", | |
| } | |
| def _format_nodes_for_prompt(self, nodes_data: List[Dict[str, Any]]) -> str: | |
| """๋ ธ๋ ๋ฐ์ดํฐ๋ฅผ ๋ฌธ์์ด๋ก ํฌ๋งทํ """ | |
| if not nodes_data: | |
| return "๊ธฐ์กด ํ๋ก์ ํธ ์ ๋ณด ์์." | |
| formatted = [] | |
| for i, node in enumerate(nodes_data, 1): | |
| formatted.append( | |
| f""" | |
| ํ๋ก์ ํธ {i}: | |
| - ์ ๋ชฉ: {node.get('title', '์ ๋ชฉ ์์')} | |
| - ์ค๋ช : {node.get('description', '์ค๋ช ์์')} | |
| - ํ ๋ํธ: {node.get('tenant', '๋ฏธ์ง์ ')} | |
| - ํ๊ทธ: {', '.join(node.get('tags', []))} | |
| """ | |
| ) | |
| return "\n".join(formatted) | |
| def _create_prompt(self, contest_info: Dict[str, str], nodes_summary: str) -> str: | |
| """ChatGPT์ฉ ํ๋กฌํํธ ๊ตฌ์ฑ""" | |
| return f""" | |
| ๋ค์ ์ ๋ ฅ๊ฐ์ ์ฐธ๊ณ ํด์ ๊ฐ์ฅ ์ต๊ณ ์ ์์ด๋์ด๋ฅผ **1๊ฐ์ง** ์ ์ํด์ฃผ์ธ์. | |
| ๊ฐ ์์ด๋์ด๋ ๋๋ฉ์ธ ์ค์ฌ์ผ๋ก ์ปจํ ์คํธยท์ด๊ทธ๋์ดํฐยท๋ ธ๋๋ฅผ ๊ฒฐํฉํ์ฌ ์์ฑํฉ๋๋ค. | |
| ใํ๊ฒ ๊ณต๋ชจ์ ์ ๋ณดใ | |
| - ๊ณต๋ชจ์ ์ ๋ชฉ: {contest_info.get('title', '')} | |
| - ๋๋ฉ์ธ: {contest_info.get('theme', '')} | |
| - ์ปจํ ์คํธ: {contest_info.get('description', '')} | |
| - ์ด๊ทธ๋์ดํฐ: {contest_info.get('context', '')} | |
| ใconnecting the dots์ ์ํ ๋ ธ๋ ์ ๋ณดใ | |
| - ๋ ธ๋: {nodes_summary} | |
| ์์) | |
| - ๋๋ฉ์ธ: ์ค๋งํธ ํ | |
| - ์ปจํ ์คํธ: ๋๋ฆผ๋ถ ์ฃผ์ต โ์นํ๊ฒฝ ์ค๋งํธ ๋์ ๊ณต๋ชจ์ โ, ์ ํ์ ๋ฐฐ์ถ ์ฐ์์ฌ๋ก ๋ฐ๊ตด | |
| - ์ด๊ทธ๋์ดํฐ: โAI๋ก ํ ์ ๊ฑด๊ฐ ์ค์๊ฐ ๋ชจ๋ํฐ๋งโ | |
| - ๋ ธ๋: OpenCV ๊ธฐ๋ฐ ์ด๋ฏธ์ง ๋ถ์, AWS RDS ๋์๋ณด๋ ๊ฐ๋ฐ ๊ฒฝํ, IoT ์ผ์ ๋คํธ์ํฌ ๊ตฌ์ถ ๊ฒฝํ | |
| ์๋ ํ์์ ๋ง์ถฐ ์๋ตํด์ฃผ์ธ์: | |
| ์ ๋ชฉ: [์์ด๋์ด ์ ๋ชฉ] | |
| ๊ฐ์: [๊ฐ๋จํ ์๊ฐ] | |
| ๋ฌธ์ ์์: [ํด๊ฒฐํ๊ณ ์ ํ๋ ๋ฌธ์ ] | |
| ์๋ฃจ์ : [๊ตฌ์ฒด์ ์ธ ํด๊ฒฐ ๋ฐฉ์] | |
| ๊ตฌํ๋ฐฉ์: [๊ธฐ์ ์ ๊ตฌํ ๋๋ ์คํ ๊ณํ์ ๋จ๊ณ๋ณ๋ก ๋์ด (1. 2. 3. ํํ ๋๋ - ํํ๋ก)] | |
| ๊ธฐ๋ํจ๊ณผ: [์์ ์ฑ๊ณผ ๋๋ ํจ๊ณผ๋ฅผ ํญ๋ชฉ๋ณ๋ก ๋์ด (- ํํ๋ก ์์ฑ)] | |
| ๊ทผ๊ฑฐ: [์์ ๊ณต๋ชจ์ ์ ๋ณด์ ๋ ธ๋๋ค์ด ์ด๋ป๊ฒ ์ฐ๊ฒฐ๋์ด ์ด ์์ด๋์ด๊ฐ ๋์ถ๋์๋์ง๋ฅผ connecting the dots ๊ด์ ์์ ๋ ผ๋ฆฌ์ ๋จ๊ณ๋ณ๋ก ์ค๋ช (- ํํ๋ก ์์ฑ)] | |
| """ | |
| def _parse_generated_idea( | |
| self, generated_text: str, contest_info: Dict[str, str] | |
| ) -> Dict[str, str]: | |
| """AI ์์ฑ ๊ฒฐ๊ณผ๋ฅผ ๊ตฌ์กฐํ๋ ํํ๋ก ํ์ฑ""" | |
| idea = { | |
| "ai_name": "ChatGPT", | |
| "title": "", | |
| "overview": "", | |
| "problem": "", | |
| "solution": "", | |
| "implementation": "", | |
| "expected_effect": "", | |
| "rationale": "", | |
| "contest_info": contest_info, | |
| "raw_response": generated_text, | |
| } | |
| try: | |
| # ๋ ๊ฐ๊ฑดํ ํ์ฑ์ ์ํ ํค์๋ ๋งคํ | |
| keyword_mappings = { | |
| "์ ๋ชฉ": "title", | |
| "๊ฐ์": "overview", | |
| "๋ฌธ์ ์์": "problem", | |
| "์๋ฃจ์ ": "solution", | |
| "๊ตฌํ๋ฐฉ์": "implementation", | |
| "๊ธฐ๋ํจ๊ณผ": "expected_effect", | |
| "๊ทผ๊ฑฐ": "rationale", | |
| } | |
| lines = generated_text.split("\n") | |
| current_key = None | |
| for line in lines: | |
| line = line.strip() | |
| if not line: # ๋น ์ค ๊ฑด๋๋ฐ๊ธฐ | |
| continue | |
| # ํค์๋ ๊ฒ์ (์ฝ๋ก ํฌํจ) | |
| found_key = False | |
| for keyword, key in keyword_mappings.items(): | |
| if ( | |
| line.startswith(f"{keyword}:") | |
| or line.startswith(f"**{keyword}:**") | |
| or line.startswith(f"#{keyword}") | |
| ): | |
| current_key = key | |
| # ์ฝ๋ก ์ดํ ๋ด์ฉ ์ถ์ถ | |
| content = line.split(":", 1)[1].strip() if ":" in line else "" | |
| idea[current_key] = content | |
| found_key = True | |
| break | |
| # ํค์๋๊ฐ ๋ฐ๊ฒฌ๋์ง ์์๊ณ ํ์ฌ ํค๊ฐ ์์ผ๋ฉด ๋ด์ฉ ์ถ๊ฐ | |
| if not found_key and current_key and line: | |
| if idea[current_key]: | |
| idea[current_key] += " " + line | |
| else: | |
| idea[current_key] = line | |
| except Exception as e: | |
| print(f"[ํ์ฑ์ค๋ฅ] {e}") | |
| print(f"[์๋ณธ์๋ต] {generated_text}") | |
| idea["title"] = "ํ์ฑ ์คํจ" | |
| idea["overview"] = f"๊ฒฐ๊ณผ ํ์ฑ์ ์คํจํ์ต๋๋ค. ์ค๋ฅ: {str(e)}" | |
| # ํ์ฑ ํ ๋น ํ๋๊ฐ ์๋์ง ํ์ธ ๋ฐ ๋๋ฒ๊น | |
| empty_fields = [ | |
| key | |
| for key, value in idea.items() | |
| if key not in ["contest_info", "raw_response", "ai_name"] and not value | |
| ] | |
| if empty_fields: | |
| print(f"[๊ฒฝ๊ณ ] ๋น ํ๋ ๋ฐ๊ฒฌ: {empty_fields}") | |
| print(f"[์๋ณธ์๋ต ์ผ๋ถ] {generated_text[:500]}...") | |
| # ์ต์ํ์ ์ ๋ชฉ์ ํ๋ณด | |
| if not idea.get("title"): | |
| idea["title"] = "์ ๋ชฉ ์์" | |
| # ๊ตฌํ๋ฐฉ์, ๊ธฐ๋ํจ๊ณผ, ๊ทผ๊ฑฐ ํ๋ ๊ฐ๋ ์ฑ ๊ฐ์ (ํญ๋ชฉ๋ณ ๊ฐํ ์ถ๊ฐ) | |
| if idea.get("implementation"): | |
| idea["implementation"] = format_list_text(idea["implementation"]) | |
| if idea.get("expected_effect"): | |
| idea["expected_effect"] = format_list_text(idea["expected_effect"]) | |
| if idea.get("rationale"): | |
| idea["rationale"] = format_list_text(idea["rationale"]) | |
| return idea | |
| def format_list_text(text: str) -> str: | |
| """ํ ์คํธ์ ๊ฐ๋ ์ฑ์ ๊ฐ์ ํ์ฌ ๊ฐ ํญ๋ชฉ๋ณ๋ก ๊ฐํ ์ถ๊ฐ""" | |
| if not text: | |
| return text | |
| import re | |
| # ๋จผ์ ํ ์คํธ๋ฅผ ์ ๋ฆฌ | |
| text = text.strip() | |
| # "- " ํจํด์ด ์์ผ๋ฉด ์ฐ์ ์ฒ๋ฆฌ | |
| if "- " in text and not text.startswith("- "): | |
| # "- " ์์ ๊ฐํ ์ถ๊ฐ (์ฒซ ๋ฒ์งธ ํญ๋ชฉ ์ ์ธ) | |
| formatted_text = re.sub(r"([^.\n])\s*(-\s)", r"\1\n\2", text) | |
| else: | |
| # "- " ํจํด์ด ์๊ฑฐ๋ ์ด๋ฏธ ์ฒซ ๋ฒ์งธ๊ฐ "- "๋ก ์์ํ๋ฉด ์ซ์ ํจํด๋ ์ฒ๋ฆฌ | |
| formatted_text = text | |
| # "1. ", "2. " ๋ฑ์ ํจํด ์์ ๊ฐํ ์ถ๊ฐ (์ฒซ ๋ฒ์งธ ํญ๋ชฉ ์ ์ธ) | |
| formatted_text = re.sub(r"([^.\n])(\d+\.\s)", r"\1\n\2", formatted_text) | |
| # "- " ๋ก ์์ํ๋ ํญ๋ชฉ๋ค ์ฒ๋ฆฌ | |
| formatted_text = re.sub(r"([^.\n])\s*(-\s)", r"\1\n\2", formatted_text) | |
| # "โข " ๋ก ์์ํ๋ ํญ๋ชฉ๋ค ์ฒ๋ฆฌ | |
| formatted_text = re.sub(r"([^.\n])\s*(โข\s)", r"\1\n\2", formatted_text) | |
| # ์ฒ์์ ๊ฐํ์ด ์ถ๊ฐ๋ ๊ฒฝ์ฐ ์ ๊ฑฐ | |
| if formatted_text.startswith("\n"): | |
| formatted_text = formatted_text[1:] | |
| return formatted_text | |
| def format_implementation_text(text: str) -> str: | |
| """๊ตฌํ๋ฐฉ์ ํ ์คํธ์ ๊ฐ๋ ์ฑ์ ๊ฐ์ ํ์ฌ ๊ฐ ํญ๋ชฉ๋ณ๋ก ๊ฐํ ์ถ๊ฐ (ํ์ ํธํ์ฑ)""" | |
| return format_list_text(text) | |
| # ์ฌ์ฉ ์์ | |
| def create_openai_client(api_key: str = None) -> OpenAIClient: | |
| return OpenAIClient(api_key) | |