Spaces:
Sleeping
Sleeping
| import json | |
| import logging | |
| # ๋ก๊น ์ค์ | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| def generate_question_recommendation(client, prompts, job_title, company_name, experience_level): | |
| """ | |
| ๋ฉด์ ์ง๋ฌธ ์ถ์ฒ์ ์์ฑํ๋ ํจ์ | |
| Args: | |
| client: OpenAI ํด๋ผ์ด์ธํธ | |
| prompts: ํ๋กฌํํธ ๋์ ๋๋ฆฌ | |
| job_title: ์ง๋ฌด๋ช | |
| company_name: ํ์ฌ๋ช | |
| experience_level: ๊ฒฝ๋ ฅ ์์ค | |
| Returns: | |
| str: LLM ์๋ต ๊ฒฐ๊ณผ | |
| """ | |
| try: | |
| # ์ฌ์ฉ์ ํ๋กฌํํธ ํฌ๋งทํ | |
| user_prompt = prompts['user_prompt'].format( | |
| job_title=job_title, | |
| company_name=company_name, | |
| experience_level=experience_level | |
| ) | |
| logger.info(f"๋ฉด์ ์ง๋ฌธ ์ถ์ฒ ์์ฒญ - ์ง๋ฌด: {job_title}, ํ์ฌ: {company_name}, ๊ฒฝ๋ ฅ: {experience_level}") | |
| # OpenAI Responses API ํธ์ถ (์น ๊ฒ์ ํ์ฑํ) | |
| response = client.responses.create( | |
| model="gpt-4o-mini", | |
| tools=[{ | |
| "type": "web_search_preview", | |
| "search_context_size": "high", | |
| }], | |
| input=f"{prompts['system_prompt']}\n\n{user_prompt}" | |
| ) | |
| print(response) | |
| result = response.output_text | |
| logger.info("๋ฉด์ ์ง๋ฌธ ์ถ์ฒ ์์ฑ ์๋ฃ") | |
| return result | |
| except Exception as e: | |
| logger.error(f"๋ฉด์ ์ง๋ฌธ ์ถ์ฒ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}") | |
| raise e | |
| def parse_question_recommendation(response_text): | |
| """ | |
| LLM ์๋ต์์ ๋ฉด์ ์ง๋ฌธ ์ถ์ฒ ๊ฒฐ๊ณผ๋ฅผ ํ์ฑํ๋ ํจ์ | |
| Args: | |
| response_text: LLM ์๋ต ํ ์คํธ | |
| Returns: | |
| dict: ํ์ฑ๋ ๋ฉด์ ์ง๋ฌธ ์ถ์ฒ ๊ฒฐ๊ณผ | |
| """ | |
| try: | |
| # JSON ํํ๋ก ํ์ฑ ์๋ | |
| if response_text.strip().startswith('{') and response_text.strip().endswith('}'): | |
| parsed_result = json.loads(response_text) | |
| logger.info("JSON ํํ๋ก ํ์ฑ ์ฑ๊ณต") | |
| return parsed_result | |
| # JSON ๋ธ๋ก ์ถ์ถ ์๋ | |
| if '```json' in response_text: | |
| start_idx = response_text.find('```json') + 7 | |
| end_idx = response_text.find('```', start_idx) | |
| json_content = response_text[start_idx:end_idx].strip() | |
| parsed_result = json.loads(json_content) | |
| logger.info("JSON ๋ธ๋ก์์ ํ์ฑ ์ฑ๊ณต") | |
| return parsed_result | |
| # ์ค๊ดํธ๋ก ๋๋ฌ์ธ์ธ JSON ์ถ์ถ ์๋ | |
| start_idx = response_text.find('{') | |
| end_idx = response_text.rfind('}') + 1 | |
| if start_idx != -1 and end_idx != 0: | |
| json_content = response_text[start_idx:end_idx] | |
| parsed_result = json.loads(json_content) | |
| logger.info("์ค๊ดํธ JSON์์ ํ์ฑ ์ฑ๊ณต") | |
| return parsed_result | |
| # JSON ํ์ฑ ์คํจ ์ ํ ์คํธ์์ ์ง๋ฌธ ์ถ์ถ | |
| logger.warning("JSON ํ์ฑ ์คํจ, ํ ์คํธ์์ ์ง๋ฌธ ์ถ์ถ ์๋") | |
| # ๋ฐ์ดํ๋ก ๋๋ฌ์ธ์ธ ์ง๋ฌธ ์ฐพ๊ธฐ | |
| lines = response_text.split('\n') | |
| for line in lines: | |
| line = line.strip() | |
| if line and not line.startswith('#') and not line.startswith('-'): | |
| # ๋ฐ์ดํ ์ ๊ฑฐ | |
| question = line.strip('"').strip("'").strip() | |
| if len(question) > 10: # ์ต์ ๊ธธ์ด ์ฒดํฌ | |
| return {"recommended_question": question} | |
| # ์ ์ฒด ํ ์คํธ๋ฅผ ์ง๋ฌธ์ผ๋ก ์ฌ์ฉ (์ตํ์ ์๋จ) | |
| clean_text = response_text.strip().strip('"').strip("'") | |
| if len(clean_text) > 10: | |
| return {"recommended_question": clean_text} | |
| logger.error("๋ฉด์ ์ง๋ฌธ ์ถ์ถ ์คํจ") | |
| return {"recommended_question": "์ง๋ฌธ ์์ฑ์ ์คํจํ์ต๋๋ค."} | |
| except json.JSONDecodeError as e: | |
| logger.error(f"JSON ํ์ฑ ์ค๋ฅ: {str(e)}") | |
| # JSON ํ์ฑ ์คํจ ์ ํ ์คํธ์์ ์ง์ ์ถ์ถ | |
| clean_text = response_text.strip().strip('"').strip("'") | |
| return {"recommended_question": clean_text if clean_text else "์ง๋ฌธ ์์ฑ์ ์คํจํ์ต๋๋ค."} | |
| except Exception as e: | |
| logger.error(f"๋ฉด์ ์ง๋ฌธ ํ์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}") | |
| return {"recommended_question": "์ง๋ฌธ ํ์ฑ์ ์คํจํ์ต๋๋ค."} |