Selecting from a list in schema when using GPT Functions

Hello,

I am using GPT Functions to provide some structure to the completions API response. For one of the fields in my schema I would like a string value but only a string value that is an option from one of two lists, depending on the input. Is there a way to accomplish this when using functions / schema or does this need to go in the prompt? Here is an example schema:

schema = {
    "type": "object",
  "name": "get_new_titles",
  "properties": {
      "Titles": {
        "type": "array",
        "description" : "New titles for books",
        "items": {
          "type": "object", 
          "properties" : {
            "title" : {
                "type" : "string",
                "description" : "The name of the book"
            },
            "category" : {
                "type" : "string",
                "description" : "The category of the book from this list for fiction: Fantasy, Sci-Fi, Romance and this list for non-fiction: History, Biography, Auto-Biography"
            },

          }
        }
      },
  },
 "required": ["Titles"]
}

So in this example, I would like the completion response to use this schema but for the category attribute, only choose from either the fiction or non-fiction list depending on whether the input object has the attribute value of fiction or non-fiction for the attribute ‘book-type’. From initial testing this method doesn’t work and it seems like there should be a way to do this.

Thanks!

The type of object parameter that you are looking for that has a set list of possible options is “enum”. That allows you to specify the only options that can be returned.

Also you have an array, where the descriptions below the first nesting level are destroyed.

More specifically, about this data, you are asking for extraction of particular data fields from the input text. Since the AI can choose whether a particular parameter is one that should be used, I would make two separate parameters, each optional, like this:


function_list=[
    {
        "name": "book_lookup",
        "description": "Describe what the API actually does with the submission",
        "parameters": {
            "type": "object",
            "properties": {
                "book_name": {
                    "type": "string",
                    "description": "Book title"
                },
                "fiction_book_category": {
                    "type": "string",
                    "enum": ["Fantasy", "Sci-Fi", "Romance", "Other"],
                    "description": "Use parameter if extracted book type is fiction"
                    },
                "nonfiction_book_category": {
                    "type": "string",
                    "enum": ["History", "Biography", "Autobiography"],
                    "description": "Use if extracted book type is non-fiction"
                    },
            },
            "required": ["book_name"],
        },
    }
]
2 Likes

Appreciate this! Enum was what I was looking for. Do I need to arrange the input in any way to make sure that the model only chooses a fiction_book_category when the book_type is fiction and a nonfiction_book_category when the book_type is nonfiction? Would passing in a book_type parameter to the input object be sufficient?

With the function example I provided, I expect that the AI would use one of the two parameters, either fiction_book_category or nonfiction_book_category, choosing the appropriate one and its categories. (your software can treat them the same if you want)

This of course means the AI must know whether the book actually is fiction or non-fiction. It may make some poor guesses if the type of book it is not explicitly stated in the information you are providing to the AI in the user role with the other book info.

You didn’t tell us the type of book information you or your user are providing, or what the AI’s job is. Nor did you tell us what type of information the function API is supposed to be returning back to the AI. I really can’t even guess what operation you are trying to do, or if this is done within a chatbot or as part of some text processor, which is why I left a vague description of in my example function.

A function usually is for making the AI more informed so it can answer a question better. The AI decides when a function is going to be useful. Not just for me, but also the AI must understand what the function does and what it is going to get back as return data when it calls the function.

You don’t have to “pass a parameter”, exactly. The AI is the one passing these parameters to your function. The AI will extract the required data from the input you provide it. If you are giving it a book title, book author, book subject, book summary, ISBN, Dewey, etc, it might be able to guess if it is non-fiction, but it would be best to provide accurate supplemental information so it can call the function correctly.

Apologies let me provide some more context.

The goal of this is given a book object which contains the type of book, potentially prequels of the book, and similar_book_titles (inspiration titles for the book name), to generate a list of names for the book with the category of the book as well.
For example, I would provide the completions endpt with this data:

{'book_type': 'fiction',
 'prequels': ['prequel title a', 'prequel title b', 'prequel title c'],
 'similar_book_titles': ['title A', 'title b', 'title c']}

and expect this to be returned for whatever number of book titles I choose to generate, lets say 3:

[{"book_name" : "generated book name","book_category" : "Fantasy"},{...},...]

The point of me providing the book_type is so that it can generate a title and select a value from the enum that matches the book_type for the book_category (“fantasy”, “romance”, etc).

If I interpret this correctly, you want the AI to fabricate potential new book titles that sound similar to existing ones, or continue a series of prequels.

{'book_type': 'fiction',
 'prequels': ['Star Wars: A New Hope', 'Empire Strikes Back', 'Return of the Jedi'],
 'similar_book_titles': ['The Mandalorian', 'Clone Wars', 'Rogue One']}

And then AI goes to work:

[{"book_name": "Galactic Legacy: Dawn of Resurgence", "book_category" : "Sci-Fi"}]

So essentially, you are using “function calling” to try to format outputs. That can work, and I have made “function output only AI” before, but it somewhat works against the grain of what a function is for, needing both a very good function description and also system prompt in order for the AI to always call a non-existent function instead of answering a question, while also making a creative output.

The same can be accomplished with just a prompt and writing good instructions. You can use similar code-like instructions directed to the AI to teach it what kind of output you want - as I did to make the output above in ChatGPT with just instructions and pasting my movie-based input.

Yes I am using function calling to force the model to tie the creative output to an enum based on the object input. I have already tried with prompts and the consistency is not near what I would like to achieve which is why I turned to functions. I don’t see a solution being found with only a prompt.