Skip to content

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