Spaces:
Sleeping
Sleeping
File size: 10,215 Bytes
594e237 |
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 |
import os
import json
import re
import yaml
from openai import OpenAI
# OpenAI ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# ํ๋กฌํํธ ํ
ํ๋ฆฟ ๋ก๋
import os
current_dir = os.path.dirname(os.path.abspath(__file__))
prompt_path = os.path.join(current_dir, 'prompt.yaml')
with open(prompt_path, 'r', encoding='utf-8') as f:
prompt_data = yaml.safe_load(f)
prompt_template = prompt_data['prompt']
def parse_context_report(content):
"""
AI ์๋ต์์ ์ปจํ
์คํธ ๋ฆฌํฌํธ JSON์ ํ์ฑํ๋ ํจ์
"""
try:
print(f"ํ์ฑํ ์ปจํ
์ธ ๊ธธ์ด: {len(content)}")
print(f"ํ์ฑํ ์ปจํ
์ธ ์ฒซ 200์: {repr(content[:200])}")
# ํ
์คํธ ์ ์ฒ๋ฆฌ
cleaned_content = content.strip()
# 1. JSON ์ฝ๋ ๋ธ๋ก ์ฐพ๊ธฐ (```json ... ``` ํ์)
json_patterns = [
r'```json\s*(\{.*?\})\s*```',
r'```\s*(\{.*?\})\s*```',
r'```json\s*(.*?)\s*```',
r'```\s*(.*?)\s*```'
]
for pattern in json_patterns:
json_match = re.search(pattern, cleaned_content, re.DOTALL)
if json_match:
json_str = json_match.group(1).strip()
print(f"JSON ๋ธ๋ก ๋ฐ๊ฒฌ: {repr(json_str[:100])}")
# JSON ๋ฌธ์์ด ์ ๋ฆฌ
json_str = re.sub(r'\n\s*', ' ', json_str)
json_str = re.sub(r',\s*}', '}', json_str)
json_str = re.sub(r',\s*]', ']', json_str)
try:
parsed_json = json.loads(json_str)
if isinstance(parsed_json, dict) and 'company_profile' in parsed_json:
return parsed_json
except json.JSONDecodeError as e:
print(f"JSON ๋ธ๋ก ํ์ฑ ์คํจ: {e}")
# 2. ์ค๊ดํธ๋ก ๋๋ฌ์ธ์ธ JSON ์ฐพ๊ธฐ
brace_patterns = [
r'\{.*?\}'
]
for pattern in brace_patterns:
brace_match = re.search(pattern, cleaned_content, re.DOTALL)
if brace_match:
json_str = brace_match.group(0).strip()
print(f"์ค๊ดํธ ๋ธ๋ก ๋ฐ๊ฒฌ: {repr(json_str[:100])}")
# JSON ๋ฌธ์์ด ์ ๋ฆฌ
json_str = re.sub(r'\n\s*', ' ', json_str)
json_str = re.sub(r',\s*}', '}', json_str)
json_str = re.sub(r',\s*]', ']', json_str)
try:
parsed_json = json.loads(json_str)
if isinstance(parsed_json, dict) and 'company_profile' in parsed_json:
return parsed_json
except json.JSONDecodeError as e:
print(f"์ค๊ดํธ ๋ธ๋ก ํ์ฑ ์คํจ: {e}")
# 3. ์ ์ฒด ํ
์คํธ๋ฅผ JSON์ผ๋ก ํ์ฑ ์๋
try:
# ์ฝ๋ ๋ธ๋ก ๋ง์ปค ์ ๊ฑฐ
if cleaned_content.startswith('```'):
lines = cleaned_content.split('\n')
start_idx = 1 if lines[0].startswith('```') else 0
end_idx = len(lines)
for i in range(len(lines)-1, -1, -1):
if lines[i].strip() == '```':
end_idx = i
break
cleaned_content = '\n'.join(lines[start_idx:end_idx])
cleaned_content = cleaned_content.strip()
parsed_json = json.loads(cleaned_content)
if isinstance(parsed_json, dict) and 'company_profile' in parsed_json:
return parsed_json
except json.JSONDecodeError as e:
print(f"์ ์ฒด JSON ํ์ฑ ์คํจ: {e}")
# 4. ๊ธฐ๋ณธ ๊ตฌ์กฐ ๋ฐํ (ํ์ฑ ์คํจ ์)
print("JSON ํ์ฑ ์คํจ, ๊ธฐ๋ณธ ๊ตฌ์กฐ ๋ฐํ")
return {
"company_profile": {
"name": "ํ์ฑ ์คํจ",
"vision_mission": "์ ๋ณด๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.",
"core_values": ["์ ๋ณด ์์"],
"talent_philosophy": "์ ๋ณด๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.",
"recent_news_summary": "์ ๋ณด๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.",
"main_products_services": ["์ ๋ณด ์์"]
},
"position_analysis": {
"role_summary": "์ ๋ณด๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.",
"required_skills": {
"hard": ["์ ๋ณด ์์"],
"soft": ["์ ๋ณด ์์"]
},
"keywords": ["์ ๋ณด ์์"]
},
"industry_context": {
"trends": ["์ ๋ณด ์์"],
"competitors": ["์ ๋ณด ์์"]
}
}
except Exception as e:
print(f"์ปจํ
์คํธ ๋ฆฌํฌํธ ํ์ฑ ์ ์ฒด ์ค๋ฅ: {e}")
print(f"ํ์ฑ ์คํจํ ์ปจํ
์ธ : {repr(content)}")
return {
"company_profile": {
"name": "์ค๋ฅ ๋ฐ์",
"vision_mission": f"ํ์ฑ ์ค๋ฅ: {str(e)}",
"core_values": ["์ค๋ฅ"],
"talent_philosophy": f"ํ์ฑ ์ค๋ฅ: {str(e)}",
"recent_news_summary": f"ํ์ฑ ์ค๋ฅ: {str(e)}",
"main_products_services": ["์ค๋ฅ"]
},
"position_analysis": {
"role_summary": f"ํ์ฑ ์ค๋ฅ: {str(e)}",
"required_skills": {
"hard": ["์ค๋ฅ"],
"soft": ["์ค๋ฅ"]
},
"keywords": ["์ค๋ฅ"]
},
"industry_context": {
"trends": ["์ค๋ฅ"],
"competitors": ["์ค๋ฅ"]
}
}
def generate_context_report(job_title, company_name, experience_level):
"""
OpenAI API๋ฅผ ์ฌ์ฉํ์ฌ ์์์ ์ปจํ
์คํธ ๋ฆฌํฌํธ๋ฅผ ์์ฑํ๋ ํจ์
"""
try:
if not job_title or not company_name or not experience_level:
return "์ง๋ฌด, ํ์ฌ๋ช
, ๊ฒฝ๋ ฅ ์์ค์ ๋ชจ๋ ์
๋ ฅํด์ฃผ์ธ์.", {}
# ํ๋กฌํํธ ์์ฑ
prompt = prompt_template.format(
job_title=job_title,
company_name=company_name,
experience_level=experience_level
)
# OpenAI Responses API ํธ์ถ (Web Search Preview ์ฌ์ฉ)
response = client.responses.create(
model="gpt-4o-mini",
tools=[{
"type": "web_search_preview",
"search_context_size": "high",
}],
input=f"๋น์ ์ ์๊ธฐ์๊ฐ์ ์์ฑ์ ์ํ ๊ธฐ์
๋ฐ ์ง๋ฌด ๋ถ์ ์ ๋ฌธ๊ฐ์
๋๋ค. ์น ๊ฒ์์ ํตํด ์ต์ ๊ธฐ์
์ ๋ณด์ ์ฐ์
๋ํฅ์ ํ์ธํ๊ณ ์ ํํ JSON ํ์์ผ๋ก ๊ตฌ์กฐํ๋ ์ ๋ณด๋ฅผ ์ ๊ณตํด์ฃผ์ธ์.\n\n{prompt}"
)
content = response.output_text
print(f"=== AI ์๋ต ์๋ณธ ===")
print(content)
print(f"=== ์ ์ฒด ์๋ต ๊ฐ์ฒด ===")
print(response)
# ์น ๊ฒ์ ์ฐธ๊ณ ๋งํฌ ์ถ๋ ฅ
if hasattr(response, 'web_search_results') and response.web_search_results:
print(f"=== ์ฐธ๊ณ ํ ์น ๊ฒ์ ๋งํฌ ===")
for i, result in enumerate(response.web_search_results, 1):
if hasattr(result, 'url'):
print(f"{i}. {result.url}")
elif hasattr(result, 'link'):
print(f"{i}. {result.link}")
print(f"=== AI ์๋ต ๋ ===")
report_data = parse_context_report(content)
if not report_data or 'company_profile' not in report_data:
return "์ปจํ
์คํธ ๋ฆฌํฌํธ ์์ฑ์ ์คํจํ์ต๋๋ค. ๋ค์ ์๋ํด์ฃผ์ธ์.", {}
# ๊ฒฐ๊ณผ ํฌ๋งทํ
result = f"""## ๐ {company_name} - {job_title} ์ปจํ
์คํธ ๋ฆฌํฌํธ
### ๐ข **๊ธฐ์
ํ๋กํ**
**๐ฏ ๋น์ & ๋ฏธ์
**
{report_data['company_profile']['vision_mission']}
**๐ ํต์ฌ ๊ฐ์น**
"""
for i, value in enumerate(report_data['company_profile']['core_values'], 1):
result += f"**{i}.** {value}\n"
result += f"""
**๐ฅ ์ธ์ฌ์**
{report_data['company_profile']['talent_philosophy']}
**๐ฐ ์ต๊ทผ ๋ํฅ**
{report_data['company_profile']['recent_news_summary']}
**๐๏ธ ์ฃผ์ ์ ํ/์๋น์ค**
"""
for i, service in enumerate(report_data['company_profile']['main_products_services'], 1):
result += f"**{i}.** {service}\n"
result += f"""
### ๐ผ **์ง๋ฌด ๋ถ์**
**๐ ์ญํ ์์ฝ**
{report_data['position_analysis']['role_summary']}
**๐ง ํ์ ์คํฌ**
*ํ๋ ์คํฌ:*
"""
for skill in report_data['position_analysis']['required_skills']['hard']:
result += f"โข {skill}\n"
result += "\n*์ํํธ ์คํฌ:*\n"
for skill in report_data['position_analysis']['required_skills']['soft']:
result += f"โข {skill}\n"
result += f"""
**๐ท๏ธ ํต์ฌ ํค์๋**
"""
for keyword in report_data['position_analysis']['keywords']:
result += f"`{keyword}` "
result += f"""
### ๐ **์ฐ์
๋งฅ๋ฝ**
**๐ ์ฃผ์ ํธ๋ ๋**
"""
for i, trend in enumerate(report_data['industry_context']['trends'], 1):
result += f"**{i}.** {trend}\n"
result += f"""
**๐ ์ฃผ์ ๊ฒฝ์์ฌ**
"""
for i, competitor in enumerate(report_data['industry_context']['competitors'], 1):
result += f"**{i}.** {competitor}\n"
result += f"""
---
**๐ ์
๋ ฅ ์ ๋ณด:**
- ํ์ฌ: {company_name}
- ์ง๋ฌด: {job_title}
- ๊ฒฝ๋ ฅ: {experience_level}
*๋ณธ ๋ฆฌํฌํธ๋ AI๊ฐ ์์ฑํ ๊ฒ์ผ๋ก, ์ค์ ์ ๋ณด์ ๋ค๋ฅผ ์ ์์ต๋๋ค. ์์์ ์์ฑ ์ ์ฐธ๊ณ ์ฉ์ผ๋ก ํ์ฉํ์ธ์.*
"""
return result, report_data
except Exception as e:
error_msg = f"""## โ ์ค๋ฅ ๋ฐ์
์ปจํ
์คํธ ๋ฆฌํฌํธ ์์ฑ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.
**์ค๋ฅ ๋ด์ฉ:** {str(e)}
๋ค์ ์๋ํด์ฃผ์ธ์.
"""
return error_msg, {} |