community[patch]: Fix error in LlamaCpp community LLM with Configurable Fields, 'grammar' custom type not available (#16995)

- **Description:** Ensure the `LlamaGrammar` custom type is always
available when instantiating a `LlamaCpp` LLM
  - **Issue:** #16994 
  - **Dependencies:** None
  - **Twitter handle:** @fpaupier

---------

Co-authored-by: Bagatur <baskaryan@gmail.com>
This commit is contained in:
François Paupier 2024-02-06 02:56:58 +01:00 committed by GitHub
parent 563f325034
commit 929f071513
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,7 +2,7 @@ from __future__ import annotations
import logging import logging
from pathlib import Path from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, Iterator, List, Optional, Union from typing import Any, Dict, Iterator, List, Optional, Union
from langchain_core.callbacks import CallbackManagerForLLMRun from langchain_core.callbacks import CallbackManagerForLLMRun
from langchain_core.language_models.llms import LLM from langchain_core.language_models.llms import LLM
@ -11,9 +11,6 @@ from langchain_core.pydantic_v1 import Field, root_validator
from langchain_core.utils import get_pydantic_field_names from langchain_core.utils import get_pydantic_field_names
from langchain_core.utils.utils import build_extra_kwargs from langchain_core.utils.utils import build_extra_kwargs
if TYPE_CHECKING:
from llama_cpp import LlamaGrammar
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -126,7 +123,7 @@ class LlamaCpp(LLM):
to force the model to generate valid JSON or to speak exclusively in emojis. At most to force the model to generate valid JSON or to speak exclusively in emojis. At most
one of grammar_path and grammar should be passed in. one of grammar_path and grammar should be passed in.
""" """
grammar: Optional[Union[str, LlamaGrammar]] = None grammar: Optional[Union[str, Any]] = None
""" """
grammar: formal grammar for constraining model outputs. For instance, the grammar grammar: formal grammar for constraining model outputs. For instance, the grammar
can be used to force the model to generate valid JSON or to speak exclusively in can be used to force the model to generate valid JSON or to speak exclusively in