pythonopenai-apichatgpt-api

Chatgpt API keeps returning Error:404 Failed to get response from API


I am using ChatGPT's API for text classification task, which I upload a dataset and ask the ChatGPT to decide whether my text has something to do with real estate. My code is:

Basic Setup

import json
import requests
from os import getenv

# Load JSON data from file
with open('/policy_cleaned.json', 'r', encoding='utf-8') as file:
    data = json.load(file)

# API settings
api_url = "https://api.openai.com/v1/completions"
headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer sk-proj-...1u"
}

API Function

def classify_text(text):
    prompt = f"Classify the following text whether it is related to the Chinese real estate industry, and if it is specifically about Chinese real estate policy: {text}"
    payload = {
        "model": "gpt-4-turbo",  
        "prompt": prompt,
        "max_tokens": 64,
        "temperature": 0.1
    }
    response = requests.post(api_url, headers=headers, json=payload)
    if response.status_code == 200:
        result = response.json()
        return result
    else:
        return {"error": "Failed to get response from API", "status_code": response.status_code}

Run on my dataset and output response

results = []
for item in data:
    classification = classify_text(item['CleanedContent'])
    results.append({
        "PolicyID": item['PolicyID'],
        "Title": item['Title'],
        "Classification": classification
    })

with open('classified_data.json', 'w', encoding='utf-8') as file:
    json.dump(results, file, ensure_ascii=False)

The program keeps returning in the returning JSON file:

{"error": "Failed to get response from API", "status_code": 404}.

I am a rookie using this, so I am desperate for any help!


Solution

  • Here's a modified version of your classify_text method that should work. The changes are based on the examples used in OpenAI's API Reference.

    import json
    import requests
    
    def classify_text(text):
        system = "Classify the following text whether it is related to the Chinese real estate industry, and if it is specifically about Chinese real estate policy"
        payload = json.dumps({
            "model": "gpt-4-turbo",  
            "messages": [
                {"role": "system", "content": system},
                {"role": "user", "content": text}
            ],
            "max_tokens": 64,
            "temperature": 0.1
        })
        response = requests.post(api_url, headers=headers, data=payload)
    
        # Consider printing the response output to debug any issues
        print(response.json())
        
        if response.status_code == 200:
            result = response.json()
            return result
        else:
            return {"error": "Failed to get response from API", "status_code": response.status_code}