class Chat_content

def initialize(api_key, gpt_content_prompt)

def initialize(api_key, gpt_content_prompt)
    @api_key = api_key
    @gpt_content_prompt = gpt_content_prompt
end

def message(content)

def message(content)
    puts '주의:GPT 특성상 원고 길이가 공백 포함 4천자를 넘기면 오류가 발생할 수 있습니다.'
    puts 'Sending request to GPT...(내용 변형 중...)'
    # "키워드 기반 글 생성 중..." 메시지를 별도 스레드로 처리

    thread = Thread.new do
        while true
            print "▶"
            sleep(3)
        end
    end

    url = 'https://api.openai.com/v1/chat/completions'
    headers = {
        'Content-Type' => 'application/json',
        'Authorization' => 'Bearer ' + @api_key
    }
    data = {
        'model' => 'gpt-4',
        'messages' => [{
            "role" => "system",
            "content" => "너는 매우 친절하고 성의 있게 답변하는 AI 어시스턴트야."
        },
        {
            "role" => "user",
            "content" => "#{@gpt_content_prompt}\n#{content}"
          
        }]
    }
    begin
        req = HTTP.headers(headers).post(url, json: data)
        
        response = JSON.parse(req.body.to_s)
        
        
        if req.status == 429
            return "API 요청 제한을 초과했습니다. 플랜 및 할당량을 확인하세요."
        end
        # 응답 데이터에서 안전하게 값 추출

        answer = response.dig('choices', 0, 'message', 'content')
        answer ||= (content) # 응답이 없을 경우 기본 메시지 설정

    rescue => e
        puts "Error: #{e.message}"
        answer = "오류가 발생했습니다."
    end
    # "생성 중..." 메시지 출력 종료

    thread.kill
    puts 'API return ==> '
    puts answer
    answer
end