Skip to content

Factory

src.utils.factory.PromptBuilderFactory

Source code in src/utils/factory.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class PromptBuilderFactory:
    @staticmethod
    def get_prompt_builder(vendor: str) -> BasePromptBuilder:
        """Get a prompt builder instance for the specified vendor.

        Args:
            vendor (str): Vendor identifier (e.g., 'openai', 'anthropic', 'watsonx', 'mistral_ai')

        Returns:
            BasePromptBuilder: Appropriate prompt builder instance

        Raises:
            ValueError: If no prompt builder is available for the specified vendor
        """
        match vendor.lower():
            case "watsonx-granite":
                return WatsonXGranitePromptBuilder()
            case "watsonx-llama":
                return WatsonXLlamaPromptBuilder()
            case "watsonx-mistral":
                return WatsonXMistralPromptBuilder()
            case "openai":
                return OpenAIPromptBuilder()
            case "anthropic":
                return AnthropicPromptBuilder()
            case "mistral-ai":
                return MistralAIPromptBuilder()
            case "openai-compat-granite":
                return OpenAICompatGranitePromptBuilder()
            case "openai-compat-llama":
                return OpenAICompatLlamaPromptBuilder()
            case "xai":
                return XAIPromptBuilder()
            case _:
                raise ValueError(f"No prompt builder available for vendor: {vendor}")

get_prompt_builder(vendor) staticmethod

Get a prompt builder instance for the specified vendor.

Parameters:

Name Type Description Default
vendor str

Vendor identifier (e.g., 'openai', 'anthropic', 'watsonx', 'mistral_ai')

required

Returns:

Name Type Description
BasePromptBuilder BasePromptBuilder

Appropriate prompt builder instance

Raises:

Type Description
ValueError

If no prompt builder is available for the specified vendor

Source code in src/utils/factory.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
@staticmethod
def get_prompt_builder(vendor: str) -> BasePromptBuilder:
    """Get a prompt builder instance for the specified vendor.

    Args:
        vendor (str): Vendor identifier (e.g., 'openai', 'anthropic', 'watsonx', 'mistral_ai')

    Returns:
        BasePromptBuilder: Appropriate prompt builder instance

    Raises:
        ValueError: If no prompt builder is available for the specified vendor
    """
    match vendor.lower():
        case "watsonx-granite":
            return WatsonXGranitePromptBuilder()
        case "watsonx-llama":
            return WatsonXLlamaPromptBuilder()
        case "watsonx-mistral":
            return WatsonXMistralPromptBuilder()
        case "openai":
            return OpenAIPromptBuilder()
        case "anthropic":
            return AnthropicPromptBuilder()
        case "mistral-ai":
            return MistralAIPromptBuilder()
        case "openai-compat-granite":
            return OpenAICompatGranitePromptBuilder()
        case "openai-compat-llama":
            return OpenAICompatLlamaPromptBuilder()
        case "xai":
            return XAIPromptBuilder()
        case _:
            raise ValueError(f"No prompt builder available for vendor: {vendor}")

src.utils.factory.FormatType

Bases: Enum

Enumeration of supported tool call format types.

Attributes:

Name Type Description
JSON

For JSON-formatted tool calls.

NON_JSON

For non-JSON formatted tool calls.

Source code in src/utils/factory.py
66
67
68
69
70
71
72
73
74
class FormatType(Enum):
    """Enumeration of supported tool call format types.

    Attributes:
        JSON: For JSON-formatted tool calls.
        NON_JSON: For non-JSON formatted tool calls.
    """
    JSON = "json_format"
    NON_JSON = "non_json_format"

src.utils.factory.ToolCallParserFactory

Factory for creating tool call parser instances.

Manages the creation of parsers for different tool call formats, including JSON and non-JSON formats.

Attributes:

Name Type Description
registry Dict

Mapping of format types to their parser classes.

Example
parser = ToolCallParserFactory.get_parser(
    FormatType.JSON,
    config={"clean_tokens": []}
)
Source code in src/utils/factory.py
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
class ToolCallParserFactory:
    """Factory for creating tool call parser instances.

    Manages the creation of parsers for different tool call formats,
    including JSON and non-JSON formats.

    Attributes:
        registry (Dict): Mapping of format types to their parser classes.

    Example:
        ```python
        parser = ToolCallParserFactory.get_parser(
            FormatType.JSON,
            config={"clean_tokens": []}
        )
        ```
    """
    registry = {
        FormatType.JSON: JSONToolCallParser,
        FormatType.NON_JSON: NonJSONToolCallParser,
    }

    @staticmethod
    def get_parser(format_type: FormatType, config: Dict[str, Any]):
        """Get appropriate parser for the specified format type.

        Args:
            format_type (FormatType): Type of format to parse.
            config (Dict[str, Any]): Configuration for the parser.

        Returns:
            BaseToolCallParser: Instance of appropriate parser.

        Raises:
            ValueError: If format type is not supported.
        """
        if format_type in ToolCallParserFactory.registry:
            return ToolCallParserFactory.registry[format_type](config)
        raise ValueError(f"Unsupported format: {format_type}")

get_parser(format_type, config) staticmethod

Get appropriate parser for the specified format type.

Parameters:

Name Type Description Default
format_type FormatType

Type of format to parse.

required
config Dict[str, Any]

Configuration for the parser.

required

Returns:

Name Type Description
BaseToolCallParser

Instance of appropriate parser.

Raises:

Type Description
ValueError

If format type is not supported.

Source code in src/utils/factory.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
@staticmethod
def get_parser(format_type: FormatType, config: Dict[str, Any]):
    """Get appropriate parser for the specified format type.

    Args:
        format_type (FormatType): Type of format to parse.
        config (Dict[str, Any]): Configuration for the parser.

    Returns:
        BaseToolCallParser: Instance of appropriate parser.

    Raises:
        ValueError: If format type is not supported.
    """
    if format_type in ToolCallParserFactory.registry:
        return ToolCallParserFactory.registry[format_type](config)
    raise ValueError(f"Unsupported format: {format_type}")