# 프롬프트 생성기 개발 V4: Python으로 '의도 추론' API 연동 구현 > 2025년 12월 3일 ## 개발 목표 V3에서 설계한 메타 프롬프트와 Python 환경 설정을 바탕으로, `main.py`에 실제 코드를 작성하여 의도 추론 기능을 구현합니다. 이 스크립트는 하드코딩된 테스트 문장을 API에 보내 의도가 올바르게 추론되는지 확인하는 것을 목표로 합니다. ## 사전 준비 코드를 실행하기 전에, `requirements.txt`에 명시된 라이브러리를 설치해야 합니다. ```bash pip install -r requirements.txt ``` 또한, 프로젝트 루트(`20251203-prompt-generator`)에 `.env.example` 파일을 `.env` 파일로 복사한 후, 파일 안에 `GOOGLE_API_KEY='YOUR_API_KEY'` 형식으로 자신의 Google AI Studio API 키를 추가해야 합니다. ## `main.py` 코드 아래는 사용자의 의도를 추론하는 첫 번째 버전의 Python 스크립트입니다. ```python import os import sys import google.generativeai as genai from dotenv import load_dotenv def main(): """ Main function to run the intent inference. """ # Load environment variables from .env file load_dotenv() # Get API key from environment api_key = os.getenv("GOOGLE_API_KEY") if not api_key: print("Error: GOOGLE_API_KEY not found in .env file.") sys.exit(1) # Configure the generative AI client genai.configure(api_key=api_key) model = genai.GenerativeModel('gemini-1.5-pro-latest') # Meta-prompt for intent classification meta_prompt = """You are an expert at understanding user intent. Analyze the following user request and classify it into one of these categories: [Image Generation], [Text Summarization], [Code Generation], [General Question]. Respond with only the category name in brackets. User Request: "{{user_input}}" """ # --- Test with a sample user input --- user_input = "A cat traveling in space on a starry night, painted by Van Gogh" # Combine meta-prompt with user input prompt_for_api = meta_prompt.replace("{{user_input}}", user_input) print(f"Sending request to API with input: '{user_input}'") # Call the API try: response = model.generate_content(prompt_for_api) # Print the cleaned-up response text print(f"API Response (Intent): {response.text.strip()}") except Exception as e: print(f"An error occurred: {e}") if __name__ == "__main__": main() ``` ## 실행 방법 및 예상 결과 터미널에서 다음 명령어로 스크립트를 실행합니다. ```bash python main.py ``` `user_input`이 "A cat traveling in space on a starry night, painted by Van Gogh"이므로, API가 의도를 정확히 추론했다면 다음과 같은 결과가 출력될 것입니다. ``` Sending request to API with input: 'A cat traveling in space on a starry night, painted by Van Gogh' API Response (Intent): [Image Generation] ``` ## 다음 단계 다음 단계(`V5`)에서는 이 스크립트를 좀 더 사용자 친화적으로 만들어, 하드코딩된 입력 대신 실제 사용자로부터 터미널을 통해 문장을 입력받도록 개선할 것입니다.