Skip to content

Adding Support for Literal types #89

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Dakantz
Copy link
Contributor

@Dakantz Dakantz commented Apr 1, 2025

I have added the support as a a simple rule extension, currently tested for

  • str
  • enum
  • int
    literals.
from llama_cpp_agent.gbnf_grammar_generator.gbnf_grammar_from_pydantic_models import (
    generate_gbnf_grammar_from_pydantic_models,
)
from pydantic import BaseModel, Field
from typing import List, Optional, Dict, Any, Literal
from enum import Enum

class TestEnum(str, Enum):
    TEST = "test"
    TARGET = "target"
class TestType(BaseModel):
    type: Literal["test"]
    name: str


class TargetType(BaseModel):
    type: Literal["target"]
    other: str


class CombinerType(BaseModel):
    combined_type: list[TestType | TargetType]
    enum_literal: Literal[TestEnum.TEST]
    int_literal: Literal[1,5]

grammar = generate_gbnf_grammar_from_pydantic_models(
    [CombinerType],
)
print(grammar)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant