pythonopenai-apichatgpt-api

ChatGPT API - creating longer JSON response bigger than gpt-3.5-turbo token limit


I have some use case for ChatGPT API which I don't know how to handle.

I'm creating Python app and I have method which creates request with some instructions and some data to rewrite for ChatGPT. It looks like this (instructions and data are just some samples in this example):

openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    temperature=0.6,
    messages=[
        {
            "role": "system",
            "content": """
                You are journalist who creates title and article content based on 
                the provided data. You also choose category from list: World, 
                Technology, Health and create 3 tags for article. 
                Your response is always just JSON which looks like this example 
                structure:
                {
                    "title": {{insert created title}},
                    "category": {{insert category}}
                    "content": {{insert article content}}
                    "tags": {{insert tags as list of strings}}
                }
            """
        },
        {
            "role": "user",
            "content": """
                Title and article content to rewrite:
                title: {}
                content: {}
            """.format(title, content)
        }
    ]
)

Provided article content can be really long and if it is and model limit is being reached then my response sometimes is fine JSON with very short created content and sometimes it is just broken JSON because content has not been finished due to token limit.

I've tried to pass response to another request but limit is still reached.


Solution

  • Try giving GPT a minified JSON example, i.e.,

    {"role":"user","content":""} 
    

    instead of

    {
    "role": "user",
    "content": ""
    }
    

    This way, GPT won't eat up tokens by giving you unnecessary spaces and line breaks.

    Otherwise, break up the JSON by its field properties and make separate completion requests. Once you get back each property separately, you can form the proper JSON object yourself.

    Last option: Go with GPT-4