openai로 내용 보강법

 

데이터를 가져다가 쓸 때 가끔 누락값이 있는 경우가 있다.

 

그럴때 GPT를 활용해서 빈 데이터를 채우거나 내용을 보강하는 것이 가능하다.

 

예를 들어:

  1. 빈 데이터 채우기:
import openai
from dotenv import load_dotenv
import os

load_dotenv()
openai.api_key = os.getenv('OPENAI_API_KEY')

def fill_missing_data(recipe_name, missing_field):
    prompt = f"""레시피 '{recipe_name}'의 빠진 {missing_field}를 생성해주세요.
    형식은 다음과 같이 해주세요:
    - 조리시간이 빠진 경우: '30분'과 같은 형식
    - 재료가 빠진 경우: '재료: 감자 2개, 양파 1개...' 형식
    - 조리방법이 빠진 경우: '1. 감자를 썬다 2. 양파를 볶는다...' 형식"""

    response = openai.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {"role": "system", "content": "당신은 요리 전문가입니다."},
            {"role": "user", "content": prompt}
        ]
    )

    return response.choices[0].message.content

  1. 내용 보강하기:
def enhance_recipe_content(recipe_data):
    prompt = f"""다음 레시피 정보를 더 자세하게 보강해주세요:
    레시피명: {recipe_data['CKG_NM']}
    현재 설명: {recipe_data['CKG_IPDC']}

    다음 내용을 추가해주세요:
    1. 영양 정보
    2. 조리 팁
    3. 맛있게 먹는 방법"""

    response = openai.chat.completions.create(
        model="gpt-4-0125-preview",
        messages=[
            {"role": "system", "content": "당신은 요리 전문가입니다."},
            {"role": "user", "content": prompt}
        ]
    )

    return response.choices[0].message.content

사용 예시:

# CSV 데이터 처리 중
for index, row in df.iterrows():
    # 조리시간이 없는 경우
    if pd.isna(row['CKG_TIME_NM']):
        cooking_time = fill_missing_data(row['CKG_NM'], '조리시간')
        df.at[index, 'CKG_TIME_NM'] = cooking_time

    # 내용 보강
    enhanced_content = enhance_recipe_content(row)
    # 보강된 내용 저장

 

주의사항:

  1. API 비용 고려
  2. 응답의 일관성 확인 필요
  3. 생성된 데이터 검증 로직 필요