Categorize Geographical Locations Under Common Types

I’m making an app where a user can enter different types of locations and specify what kind of location it is (mountain, forest, city, farm, etc). Also, the user can use GPT to have it generate locations for them, which will return any random kind of location. There’s a page in the app where the user can filter on the different locations, and right now I’m just listing every different kind as its own filter.

If the user or GPT creates a bunch of locations with different types, there will be a ton of filters. Moreover, GPT sometimes creates similar locations with different types (mountain vs mountain range). I want to prompt GPT to take all the current location types and group them together under similar categories. If I do this through ChatGPT, I get pretty good results (see below example). However, if I do it through my app with the API, I get very varied and unreliable results. Both are using GPT-4o, so I’m not sure what I need to do to make it work. See below for an examples of what the API is returning.

Prompt

Given the following list of specific locations, return a mapping of common locations to them. For instance, if the list contains both "mountain" and "mountain range", put those both under "mountain". However, don't make the mapping too generalized. For instance, don't put "ocean" and "river" under a single "water" category. Return just mapping as a JSON array in the following format: 

{ 
    "broad_type": [ "specific_type", ... ],
     ...
}

List of specific locations: Canyon, Castle, Cave, Cave System, City, Desert, Farm, Forest, Island, Lake, Mountain, Ocean, River, Swamp, Temple, Tower, Town, Valley, Village, Volcano

ChatGPT Response

{
    "canyon": ["Canyon"],
    "castle": ["Castle"],
    "cave": ["Cave", "Cave System"],
    "city": ["City"],
    "desert": ["Desert"],
    "farm": ["Farm"],
    "forest": ["Forest"],
    "island": ["Island"],
    "lake": ["Lake"],
    "mountain": ["Mountain"],
    "ocean": ["Ocean"],
    "river": ["River"],
    "swamp": ["Swamp"],
    "temple": ["Temple"],
    "tower": ["Tower"],
    "town": ["Town"],
    "valley": ["Valley"],
    "village": ["Village"],
    "volcano": ["Volcano"]
}

API Response 1 (no good)

{
  "natural feature": [
    "Canyon",
    "Cave",
    "Cave System",
    "Desert",
    "Forest",
    "Island",
    "Lake",
    "Mountain",
    "Ocean",
    "River",
    "Swamp",
    "Valley",
    "Volcano"
  ],
  "man-made structure": [
    "Castle",
    "Farm",
    "Temple",
    "Tower"
  ],
  "settlement": [
    "City",
    "Town",
    "Village"
  ]
}

Response 2 (acceptable)

{
  "canyon": ["Canyon"],
  "castle": ["Castle"],
  "cave": ["Cave", "Cave System"],
  "city": ["City"],
  "desert": ["Desert"],
  "farm": ["Farm"],
  "forest": ["Forest"],
  "island": ["Island"],
  "lake": ["Lake"],
  "mountain": ["Mountain"],
  "ocean": ["Ocean"],
  "river": ["River"],
  "swamp": ["Swamp"],
  "temple": ["Temple"],
  "tower": ["Tower"],
  "town": ["Town"],
  "valley": ["Valley"],
  "village": ["Village"],
  "volcano": ["Volcano"]
}

How can I modify the prompt to give me consistently reliable results like the second response?

Hello Pramathpancholi,

It seems like the issue you’re encountering with the API might be related to how the prompt is structured or the parameters set for the generation. When using GPT for categorization tasks like yours, the specificity of your prompt is crucial. To achieve consistent and reliable results similar to your second response, consider refining your prompt to guide the model more explicitly towards the desired format and categories.

Here’s a suggestion on how to adjust your prompt for the API:

Blockquote
Please categorize the following specific locations into closely related groups, avoiding overly broad categories. Use distinct classifications for natural features, man-made structures, and settlements. Format the response as a JSON array with each category listing its related locations. Here’s the list of specific locations for categorization: Canyon, Castle, Cave, Cave System, City, Desert, Farm, Forest, Island, Lake, Mountain, Ocean, River, Swamp, Temple, Tower, Town, Valley, Village, Volcano.

Blockquote

Please categorize the following specific locations into closely related groups, avoiding overly broad categories. Use distinct classifications for natural features, man-made structures, and settlements. Format the response as a JSON array with each category listing its related locations. Here’s the list of specific locations for categorization: Canyon, Castle, Cave, Cave System, City, Desert, Farm, Forest, Island, Lake, Mountain, Ocean, River, Swamp, Temple, Tower, Town, Valley, Village, Volcano.


This prompt guides the model by explicitly requesting distinct classifications for different types of locations and asks for a specific output format. You may also want to experiment with different temperatures or max token settings if your API allows tweaking these parameters, as they can influence the creativity and coherence of the responses.

Additionally, if you or your users are incorporating location-based functionalities in your app and need real-time location tracking or verification, using a tool like [What Is My Location](https://whatismylocation.org/) can be quite helpful. This tool can provide users with their current geographical coordinates, which could enhance the app’s location-aware features.

Best of luck with your app development!