Skip to content

pydantic : replace uses of __annotations__ with get_type_hints #8474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 14, 2024

Conversation

compilade
Copy link
Collaborator

This should fix #8471.

The problem was triggered in #8341 by adding this specific line:

https://github.com/ggerganov/llama.cpp/blob/17eb6aa8a992cda37ee65cf848d9289bd6cad860/examples/pydantic_models_to_grammar_examples.py#L2

This uncovered the existing problem in examples/pydantic_models_to_grammar.py of directly using obj.__annotations__ instead of using typing.get_type_hints as suggested in PEP563.

I've tested the script with Qwen2-0.5B-Instruct.
$ python3 pydantic_models_to_grammar_examples.py 
root ::= function
function ::= (" "| "\n") "{" ws "\"function\""  ":" ws grammar-models
grammar-models ::= send-message-to-user-grammar-model | calculator-grammar-model
send-message-to-user-grammar-model ::= "\"SendMessageToUser\"" "," ws "\"function_parameters\"" ":" ws send-message-to-user
calculator-grammar-model ::= "\"Calculator\"" "," ws "\"function_parameters\"" ":" ws calculator
send-message-to-user ::= "{" "\n"  ws "\"chain_of_thought\"" ":" ws string "," "\n"  ws "\"message\"" ":" ws string "\n" ws "}""\n" ws "}"
calculator ::= "{" "\n"  ws "\"number_one\"" ":" ws calculator-number-one-union "," "\n"  ws "\"operation\"" ":" ws calculator-operation "," "\n"  ws "\"number_two\"" ":" ws calculator-number-two-union "\n" ws "}""\n" ws "}"
calculator-number-one-union ::= integer | float
calculator-operation ::= "\"add\"" | "\"subtract\"" | "\"multiply\"" | "\"divide\""
calculator-number-two-union ::= integer | float
boolean ::= "true" | "false"
null ::= "null"
string ::= "\"" (
        [^"\\] |
        "\\" (["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F])
      )* "\"" ws
ws ::= ([ \t\n] ws)?
float ::= ("-"? ([0] | [1-9] [0-9]*)) ("." [0-9]+)? ([eE] [-+]? [0-9]+)? ws
integer ::= [0-9]+
Function: SendMessageToUser
  Description: Send a message to the User.
  Parameters:
    chain_of_thought (str):
        Description: Your chain of thought while sending the message.
    message (str):
        Description: Message you want to send to the user.

Function: Calculator
  Description: Perform a math operation on two numbers.
  Parameters:
    number_one (int or float):
        Description: First number.
    operation (math-operation):
        Description: Math operation to perform.
    number_two (int or float):
        Description: Second number.



{
  "function": "Calculator",
  "function_parameters": {
    "number_one": 42,
    "operation": "multiply",
    "number_two": 42
  }
}

{
  "title": "Feynman Lectures on Physics",
  "author": "Richard Feynman",
  "published_year": 1961,
  "keywords": ["physics", "laboratory", "caltech", "lectures"],
  "category": "Fiction",
  "summary": "A physics textbook based on some lectures by Richard Feynman, a Nobel laureate who has sometimes been called " 
        }
title='Feynman Lectures on Physics' author='Richard Feynman' published_year=1961 keywords=['physics', 'laboratory', 'caltech', 'lectures'] category=<Category.Fiction: 'Fiction'> summary='A physics textbook based on some lectures by Richard Feynman, a Nobel laureate who has sometimes been called '

[
    {
        "function": "get_current_datetime",
        "params": {
            "output_format": "%Y-%m-%d %H:%M:%S"
        }
    },
    {
        "function": "get_current_weather",
        "params": {
            "location": "London",
            "unit": "celsius"
        }
    },
    {
        "function": "Calculator",
        "params": {
            "number_one": 42,
            "operation": "multiply",
            "number_two": 42
        }
    },
    {
        "function": "Calculator",
        "params": {
            "number_one": 42,
            "operation": "divide",
            "number_two": 42
        }
    },
    {
        "function": "Calculator",
        "params": {
            "number_one": 42,
            "operation": "add",
            "number_two": 42
        }
    },
    {
        "function": "Calculator",
        "params": {
            "number_one": 42,
            "operation": "subtract",
            "number_two": 42
        }
    }
]
[{'function': 'get_current_datetime', 'params': {'output_format': '%Y-%m-%d %H:%M:%S'}}, {'function': 'get_current_weather', 'params': {'location': 'London', 'unit': 'celsius'}}, {'function': 'Calculator', 'params': {'number_one': 42, 'operation': 'multiply', 'number_two': 42}}, {'function': 'Calculator', 'params': {'number_one': 42, 'operation': 'divide', 'number_two': 42}}, {'function': 'Calculator', 'params': {'number_one': 42, 'operation': 'add', 'number_two': 42}}, {'function': 'Calculator', 'params': {'number_one': 42, 'operation': 'subtract', 'number_two': 42}}]
2024-07-13 17:07:35
{"location": "London", "temperature": "42", "unit": "celsius"}
1764
1.0
84
0

There is no longer an error.


@compilade compilade added bugfix fixes an issue or bug Review Complexity : Low Trivial changes to code that most beginner devs (or those who want a break) can tackle. e.g. UI fix python python script changes labels Jul 13, 2024
@maruel
Copy link
Contributor

maruel commented Jul 13, 2024

Thanks! Confirmed pydantic_models_to_grammar_examples.py runs on python3.12 on macOS.

On Ubuntu 22.04.4 with python 3.10, it still fails but I don't know if it's worth fixing.

Traceback (most recent call last):                 
  File "$HOME/llama.cpp/examples/pydantic_models_to_grammar_examples.py", line 129, in <module>                                                                                                  
    gbnf_grammar, documentation = generate_gbnf_grammar_and_documentation([Book])
  File "$HOME/llama.cpp/examples/pydantic_models_to_grammar.py", line 1080, in generate_gbnf_grammar_and_documentation
    documentation = generate_markdown_documentation(                                         
  File "$HOME/llama.cpp/examples/pydantic_models_to_grammar.py", line 721, in generate_markdown_documentation
    documentation += generate_field_markdown(
  File "$HOME/llama.cpp/examples/pydantic_models_to_grammar.py", line 795, in generate_field_markdown                                        
    if isclass(field_type) and issubclass(field_type, BaseModel):
  File "/usr/lib/python3.10/abc.py", line 123, in __subclasscheck__                                     
    return _abc_subclasscheck(cls, subclass)
TypeError: issubclass() arg 1 must be a class

I added a print and it happens on list[str]

@compilade
Copy link
Collaborator Author

compilade commented Jul 13, 2024

TypeError: issubclass() arg 1 must be a class

Weird, since isclass is used before passing the argument to issubclass.

I've used Python 3.11.9 on NixOS in my tests.

On Ubuntu 22.04.4 with python 3.10, it still fails but I don't know if it's worth fixing.

I can confirm I get the same error with Python 3.9.2 in a Debian 11 container.

So this means it works for Python 3.11 and 3.12, but fails for Python 3.9 and 3.10. (I wonder if it also failed previously, e.g. on commit a8db2a9, right before #8341 was merged (EDIT: Yes, it DID fail before too, at least with Python 3.9))

I'll try to investigate the cause.

@maruel
Copy link
Contributor

maruel commented Jul 14, 2024

Confirmed on python 3.10.12. Thanks a lot!

Copy link
Contributor

@maruel maruel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non owner approval

@compilade compilade merged commit 090fca7 into master Jul 14, 2024
12 checks passed
arthw pushed a commit to arthw/llama.cpp that referenced this pull request Jul 27, 2024
…org#8474)

* pydantic : replace uses of __annotations__ with get_type_hints

* pydantic : fix Python 3.9 and 3.10 support
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix fixes an issue or bug examples python python script changes Review Complexity : Low Trivial changes to code that most beginner devs (or those who want a break) can tackle. e.g. UI fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: pydantic_models_to_grammar_examples.py is broken
3 participants