ChatGPT app returns results; API does not

Hi all, new here. I have a list of organizations and am trying to extract information from them. I have an API key and am able to extract information using an API call (the same information I get when I use the chatGPT web app). However, for some organizations, the API call returns NULL. I enter the org into the web app and I do get results. What am I doing wrong? Code is below:

async def fetch_foundation_info(foundation, client):
    chat_completion = await client.chat.completions.create(
        messages=[
            {
                "role": "user",
                "content": f'Who is {foundation} and what do they do?',
            }
        ],
        model="gpt-4",

    )
    
    foundation_output = {}
    for choice in chat_completion.choices:
        text = choice.message.content
        # Assuming the text structure allows splitting by known headers
        sections = ["Areas of Focus:", "Recent Projects Funded:", "Application Acceptance:", "Contact Information:"]
        for section in sections:
            start_index = text.find(section)
            if start_index != -1:
                end_index = len(text)
                for next_section in sections:
                    if next_section != section and text.find(next_section) > start_index:
                        end_index = min(end_index, text.find(next_section))
                section_content = text[start_index:end_index].replace(section, '').strip()
                foundation_output[section.replace(":", "")] = section_content
    
    return foundation_output

Hey there and welcome to the community!

Have you by any chance looked at how it tried to call those APIs that returned null? What was the parameter that it fed to the API call?

These models can be a bit…finicky. It can have issues presenting the correct parameter to a function call at times.

I’m new at this so excuse me if I’m off here. The parameter (I assume you mean {foundation}) is a list of organizations that I read from a CSV file. In this case, the foundation, which is read properly because the API returns the name back to me but without results, is Always Enough Foundation. Other than that, I don’t pass any parameters to the API.

You are assuming the AI is going to write information within some categories, without any prompting that it is going to operate that way.

If the input text doesn’t contain one of the specified section names, the code simply skips over that section without adding any entry for it in the foundation_output dictionary. This means the output will only contain entries for sections that were actually found and processed in the input text.

If you want such categories filled in, I suggest you specify a json response format with the keys to the AI, and then you’ll get something back requiring little parsing and guessing.

1 Like

Thanks Jay, that was part of the problem. After I eliminated that requirement and the requirement to put things in json, I kept getting NULL results. I had to work on the prompt and be explicit about doing a web search and then I started getting results. Back to formatting the output in json now…