gllm-inference-binary 0.5.9__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of gllm-inference-binary might be problematic. Click here for more details.
- gllm_inference/__init__.pyi +0 -0
- gllm_inference/builder/__init__.pyi +6 -0
- gllm_inference/builder/build_em_invoker.pyi +122 -0
- gllm_inference/builder/build_lm_invoker.pyi +150 -0
- gllm_inference/builder/build_lm_request_processor.pyi +93 -0
- gllm_inference/builder/build_output_parser.pyi +29 -0
- gllm_inference/catalog/__init__.pyi +4 -0
- gllm_inference/catalog/catalog.pyi +121 -0
- gllm_inference/catalog/lm_request_processor_catalog.pyi +112 -0
- gllm_inference/catalog/prompt_builder_catalog.pyi +82 -0
- gllm_inference/constants.pyi +10 -0
- gllm_inference/em_invoker/__init__.pyi +9 -0
- gllm_inference/em_invoker/azure_openai_em_invoker.pyi +86 -0
- gllm_inference/em_invoker/em_invoker.pyi +83 -0
- gllm_inference/em_invoker/google_em_invoker.pyi +124 -0
- gllm_inference/em_invoker/langchain/__init__.pyi +3 -0
- gllm_inference/em_invoker/langchain/em_invoker_embeddings.pyi +62 -0
- gllm_inference/em_invoker/langchain_em_invoker.pyi +41 -0
- gllm_inference/em_invoker/openai_compatible_em_invoker.pyi +92 -0
- gllm_inference/em_invoker/openai_em_invoker.pyi +86 -0
- gllm_inference/em_invoker/schema/__init__.pyi +0 -0
- gllm_inference/em_invoker/schema/google.pyi +7 -0
- gllm_inference/em_invoker/schema/langchain.pyi +4 -0
- gllm_inference/em_invoker/schema/openai.pyi +7 -0
- gllm_inference/em_invoker/schema/openai_compatible.pyi +7 -0
- gllm_inference/em_invoker/schema/twelvelabs.pyi +17 -0
- gllm_inference/em_invoker/schema/voyage.pyi +15 -0
- gllm_inference/em_invoker/twelevelabs_em_invoker.pyi +99 -0
- gllm_inference/em_invoker/voyage_em_invoker.pyi +101 -0
- gllm_inference/exceptions/__init__.pyi +4 -0
- gllm_inference/exceptions/error_parser.pyi +48 -0
- gllm_inference/exceptions/exceptions.pyi +120 -0
- gllm_inference/lm_invoker/__init__.pyi +11 -0
- gllm_inference/lm_invoker/anthropic_lm_invoker.pyi +275 -0
- gllm_inference/lm_invoker/azure_openai_lm_invoker.pyi +253 -0
- gllm_inference/lm_invoker/bedrock_lm_invoker.pyi +232 -0
- gllm_inference/lm_invoker/datasaur_lm_invoker.pyi +166 -0
- gllm_inference/lm_invoker/google_lm_invoker.pyi +310 -0
- gllm_inference/lm_invoker/langchain_lm_invoker.pyi +258 -0
- gllm_inference/lm_invoker/litellm_lm_invoker.pyi +248 -0
- gllm_inference/lm_invoker/lm_invoker.pyi +151 -0
- gllm_inference/lm_invoker/openai_compatible_lm_invoker.pyi +265 -0
- gllm_inference/lm_invoker/openai_lm_invoker.pyi +361 -0
- gllm_inference/lm_invoker/schema/__init__.pyi +0 -0
- gllm_inference/lm_invoker/schema/anthropic.pyi +50 -0
- gllm_inference/lm_invoker/schema/bedrock.pyi +48 -0
- gllm_inference/lm_invoker/schema/datasaur.pyi +12 -0
- gllm_inference/lm_invoker/schema/google.pyi +24 -0
- gllm_inference/lm_invoker/schema/langchain.pyi +22 -0
- gllm_inference/lm_invoker/schema/openai.pyi +90 -0
- gllm_inference/lm_invoker/schema/openai_compatible.pyi +56 -0
- gllm_inference/model/__init__.pyi +9 -0
- gllm_inference/model/em/__init__.pyi +0 -0
- gllm_inference/model/em/google_em.pyi +16 -0
- gllm_inference/model/em/openai_em.pyi +15 -0
- gllm_inference/model/em/twelvelabs_em.pyi +13 -0
- gllm_inference/model/em/voyage_em.pyi +20 -0
- gllm_inference/model/lm/__init__.pyi +0 -0
- gllm_inference/model/lm/anthropic_lm.pyi +20 -0
- gllm_inference/model/lm/google_lm.pyi +17 -0
- gllm_inference/model/lm/openai_lm.pyi +27 -0
- gllm_inference/output_parser/__init__.pyi +3 -0
- gllm_inference/output_parser/json_output_parser.pyi +60 -0
- gllm_inference/output_parser/output_parser.pyi +27 -0
- gllm_inference/prompt_builder/__init__.pyi +3 -0
- gllm_inference/prompt_builder/prompt_builder.pyi +56 -0
- gllm_inference/prompt_formatter/__init__.pyi +7 -0
- gllm_inference/prompt_formatter/agnostic_prompt_formatter.pyi +49 -0
- gllm_inference/prompt_formatter/huggingface_prompt_formatter.pyi +55 -0
- gllm_inference/prompt_formatter/llama_prompt_formatter.pyi +59 -0
- gllm_inference/prompt_formatter/mistral_prompt_formatter.pyi +53 -0
- gllm_inference/prompt_formatter/openai_prompt_formatter.pyi +35 -0
- gllm_inference/prompt_formatter/prompt_formatter.pyi +30 -0
- gllm_inference/request_processor/__init__.pyi +4 -0
- gllm_inference/request_processor/lm_request_processor.pyi +92 -0
- gllm_inference/request_processor/uses_lm_mixin.pyi +50 -0
- gllm_inference/schema/__init__.pyi +13 -0
- gllm_inference/schema/attachment.pyi +88 -0
- gllm_inference/schema/code_exec_result.pyi +14 -0
- gllm_inference/schema/enums.pyi +24 -0
- gllm_inference/schema/lm_output.pyi +36 -0
- gllm_inference/schema/message.pyi +52 -0
- gllm_inference/schema/model_id.pyi +138 -0
- gllm_inference/schema/reasoning.pyi +15 -0
- gllm_inference/schema/token_usage.pyi +11 -0
- gllm_inference/schema/tool_call.pyi +14 -0
- gllm_inference/schema/tool_result.pyi +11 -0
- gllm_inference/schema/type_alias.pyi +15 -0
- gllm_inference/utils/__init__.pyi +4 -0
- gllm_inference/utils/langchain.pyi +30 -0
- gllm_inference/utils/validation.pyi +12 -0
- gllm_inference_binary-0.5.9.dist-info/METADATA +108 -0
- gllm_inference_binary-0.5.9.dist-info/RECORD +94 -0
- gllm_inference_binary-0.5.9.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
class GoogleEM:
|
|
2
|
+
'''Defines Google embedding model names constants.
|
|
3
|
+
|
|
4
|
+
Usage example:
|
|
5
|
+
```python
|
|
6
|
+
from gllm_inference.model import GoogleEM
|
|
7
|
+
from gllm_inference.em_invoker import GoogleEMInvoker
|
|
8
|
+
|
|
9
|
+
em_invoker = GoogleEMInvoker(GoogleEM.GEMINI_EMBEDDING_001)
|
|
10
|
+
result = await em_invoker.invoke("Hello, world!")
|
|
11
|
+
```
|
|
12
|
+
'''
|
|
13
|
+
GEMINI_EMBEDDING_001: str
|
|
14
|
+
TEXT_EMBEDDING_004: str
|
|
15
|
+
TEXT_EMBEDDING_005: str
|
|
16
|
+
TEXT_MULTILINGUAL_EMBEDDING_002: str
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
class OpenAIEM:
|
|
2
|
+
'''Defines OpenAI embedding model names constants.
|
|
3
|
+
|
|
4
|
+
Usage example:
|
|
5
|
+
```python
|
|
6
|
+
from gllm_inference.model import OpenAIEM
|
|
7
|
+
from gllm_inference.em_invoker import OpenAIEMInvoker
|
|
8
|
+
|
|
9
|
+
em_invoker = OpenAIEMInvoker(OpenAIEM.TEXT_EMBEDDING_3_SMALL)
|
|
10
|
+
result = await em_invoker.invoke("Hello, world!")
|
|
11
|
+
```
|
|
12
|
+
'''
|
|
13
|
+
TEXT_EMBEDDING_3_SMALL: str
|
|
14
|
+
TEXT_EMBEDDING_3_LARGE: str
|
|
15
|
+
TEXT_EMBEDDING_ADA_002: str
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
class TwelveLabsEM:
|
|
2
|
+
'''Defines TwelveLabs embedding model names constants.
|
|
3
|
+
|
|
4
|
+
Usage example:
|
|
5
|
+
```python
|
|
6
|
+
from gllm_inference.model import TwelveLabsEM
|
|
7
|
+
from gllm_inference.em_invoker import TwelveLabsEMInvoker
|
|
8
|
+
|
|
9
|
+
em_invoker = TwelveLabsEMInvoker(TwelveLabsEM.MARENGO_RETRIEVAL_2_7)
|
|
10
|
+
result = await em_invoker.invoke("Hello, world!")
|
|
11
|
+
```
|
|
12
|
+
'''
|
|
13
|
+
MARENGO_RETRIEVAL_2_7: str
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
class VoyageEM:
|
|
2
|
+
'''Defines Voyage embedding model names constants.
|
|
3
|
+
|
|
4
|
+
Usage example:
|
|
5
|
+
```python
|
|
6
|
+
from gllm_inference.model import VoyageEM
|
|
7
|
+
from gllm_inference.em_invoker import VoyageEMInvoker
|
|
8
|
+
|
|
9
|
+
em_invoker = VoyageEMInvoker(VoyageEM.VOYAGE_3_5_LITE)
|
|
10
|
+
result = await em_invoker.invoke("Hello, world!")
|
|
11
|
+
```
|
|
12
|
+
'''
|
|
13
|
+
VOYAGE_3_5: str
|
|
14
|
+
VOYAGE_3_5_LITE: str
|
|
15
|
+
VOYAGE_3_LARGE: str
|
|
16
|
+
VOYAGE_CODE_3: str
|
|
17
|
+
VOYAGE_FINANCE_2: str
|
|
18
|
+
VOYAGE_LAW_2: str
|
|
19
|
+
VOYAGE_CODE_2: str
|
|
20
|
+
VOYAGE_MULTIMODAL_3: str
|
|
File without changes
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
class AnthropicLM:
|
|
2
|
+
'''Defines Anthropic language model names constants.
|
|
3
|
+
|
|
4
|
+
Usage example:
|
|
5
|
+
```python
|
|
6
|
+
from gllm_inference.model import AnthropicLM
|
|
7
|
+
from gllm_inference.lm_invoker import AnthropicLMInvoker
|
|
8
|
+
|
|
9
|
+
lm_invoker = AnthropicLMInvoker(AnthropicLM.CLAUDE_SONNET_4)
|
|
10
|
+
response = await lm_invoker.invoke("Hello, world!")
|
|
11
|
+
```
|
|
12
|
+
'''
|
|
13
|
+
CLAUDE_OPUS_4_1: str
|
|
14
|
+
CLAUDE_OPUS_4: str
|
|
15
|
+
CLAUDE_SONNET_4: str
|
|
16
|
+
CLAUDE_SONNET_3_7: str
|
|
17
|
+
CLAUDE_SONNET_3_5: str
|
|
18
|
+
CLAUDE_HAIKU_3_5: str
|
|
19
|
+
CLAUDE_OPUS_3: str
|
|
20
|
+
CLAUDE_HAIKU_3: str
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
class GoogleLM:
|
|
2
|
+
'''Defines Google language model names constants.
|
|
3
|
+
|
|
4
|
+
Usage example:
|
|
5
|
+
```python
|
|
6
|
+
from gllm_inference.model import GoogleLM
|
|
7
|
+
from gllm_inference.lm_invoker import GoogleLMInvoker
|
|
8
|
+
|
|
9
|
+
lm_invoker = GoogleLMInvoker(GoogleLM.GEMINI_2_5_FLASH)
|
|
10
|
+
response = await lm_invoker.invoke("Hello, world!")
|
|
11
|
+
```
|
|
12
|
+
'''
|
|
13
|
+
GEMINI_2_5_PRO: str
|
|
14
|
+
GEMINI_2_5_FLASH: str
|
|
15
|
+
GEMINI_2_5_FLASH_LITE: str
|
|
16
|
+
GEMINI_2_0_FLASH: str
|
|
17
|
+
GEMINI_2_0_FLASH_LITE: str
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
class OpenAILM:
|
|
2
|
+
'''Defines OpenAI language model names constants.
|
|
3
|
+
|
|
4
|
+
Usage example:
|
|
5
|
+
```python
|
|
6
|
+
from gllm_inference.model import OpenAILM
|
|
7
|
+
from gllm_inference.lm_invoker import OpenAILMInvoker
|
|
8
|
+
|
|
9
|
+
lm_invoker = OpenAILMInvoker(OpenAILM.GPT_5_NANO)
|
|
10
|
+
response = await lm_invoker.invoke("Hello, world!")
|
|
11
|
+
```
|
|
12
|
+
'''
|
|
13
|
+
GPT_5: str
|
|
14
|
+
GPT_5_MINI: str
|
|
15
|
+
GPT_5_NANO: str
|
|
16
|
+
GPT_4_1: str
|
|
17
|
+
GPT_4_1_MINI: str
|
|
18
|
+
GPT_4_1_NANO: str
|
|
19
|
+
GPT_4O: str
|
|
20
|
+
GPT_4O_MINI: str
|
|
21
|
+
O4_MINI: str
|
|
22
|
+
O4_MINI_DEEP_RESEARCH: str
|
|
23
|
+
O3: str
|
|
24
|
+
O3_PRO: str
|
|
25
|
+
O3_DEEP_RESEARCH: str
|
|
26
|
+
O1: str
|
|
27
|
+
O1_PRO: str
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
from gllm_inference.output_parser.output_parser import BaseOutputParser as BaseOutputParser
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
class JSONOutputParser(BaseOutputParser[dict[str, Any]]):
|
|
5
|
+
'''An output parser that parses a json object from the language model output.
|
|
6
|
+
|
|
7
|
+
The `JSONOutputParser` class searches for the first opening curly brace `{` and the last closing curly brace `}`
|
|
8
|
+
in the language model\'s output to identify and extract a JSON object. It then parses the extracted substring into
|
|
9
|
+
a Python dictionary. This method relies on finding a well-formed JSON structure enclosed by the first and last
|
|
10
|
+
curly braces in the string. If the result contains additional curly braces outside the JSON object, or if there
|
|
11
|
+
are multiple JSON objects, this parser will not function correctly and will raise a `ValueError`.
|
|
12
|
+
|
|
13
|
+
Example:
|
|
14
|
+
If the result is:
|
|
15
|
+
```
|
|
16
|
+
"Here is the data: {\\"key\\": \\"value\\"} and some other text."
|
|
17
|
+
```
|
|
18
|
+
The parser will extract the `{"key": "value"}` JSON object.
|
|
19
|
+
|
|
20
|
+
However, if the result contains multiple JSON objects or nested data, such as:
|
|
21
|
+
```
|
|
22
|
+
"Here are two JSONs: {\\"key1\\": \\"value1\\"} and {\\"key2\\": \\"value2\\"}"
|
|
23
|
+
```
|
|
24
|
+
The parser will not handle this correctly, as it only extracts the content between the first `{` and the last `}`.
|
|
25
|
+
'''
|
|
26
|
+
def parse(self, result: str) -> dict[str, Any]:
|
|
27
|
+
'''Parses the raw output string to extract and decode a JSON object.
|
|
28
|
+
|
|
29
|
+
This method searches the provided string for the first opening curly brace `{` and the last closing curly
|
|
30
|
+
brace `}` to identify a JSON object. It extracts the substring between these braces and attempts to parse it
|
|
31
|
+
as a JSON object. The method raises a `ValueError` if no valid JSON structure is found or if the JSON is
|
|
32
|
+
malformed.
|
|
33
|
+
|
|
34
|
+
Note:
|
|
35
|
+
This approach relies on the first `{` and the last `}` in the string. It will fail if:
|
|
36
|
+
- The result contains curly braces outside the intended JSON object.
|
|
37
|
+
- There are multiple JSON objects within the string, as it only processes the first and last braces.
|
|
38
|
+
|
|
39
|
+
Example:
|
|
40
|
+
If the result is:
|
|
41
|
+
```
|
|
42
|
+
"Here is the data: {\\"key\\": \\"value\\"} and some other text."
|
|
43
|
+
```
|
|
44
|
+
The parser will extract and return the `{"key": "value"}` object.
|
|
45
|
+
|
|
46
|
+
However, if the result is:
|
|
47
|
+
```
|
|
48
|
+
"Here are two JSONs: {\\"key1\\": \\"value1\\"} and {\\"key2\\": \\"value2\\"}"
|
|
49
|
+
```
|
|
50
|
+
The parser will incorrectly attempt to parse everything between the first `{` and the last `}`.
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
result (str): The raw output string from the language model.
|
|
54
|
+
|
|
55
|
+
Returns:
|
|
56
|
+
dict[str, Any]: The parsed JSON object as a Python dictionary.
|
|
57
|
+
|
|
58
|
+
Raises:
|
|
59
|
+
ValueError: If no valid JSON object is found or if the JSON string is invalid.
|
|
60
|
+
'''
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import abc
|
|
2
|
+
from abc import ABC, abstractmethod
|
|
3
|
+
from typing import Generic, TypeVar
|
|
4
|
+
|
|
5
|
+
T = TypeVar('T')
|
|
6
|
+
|
|
7
|
+
class BaseOutputParser(ABC, Generic[T], metaclass=abc.ABCMeta):
|
|
8
|
+
"""A base class for output parsers used in Gen AI applications.
|
|
9
|
+
|
|
10
|
+
The `BaseOutputParser` class defines the interface for parsing the output of language models.
|
|
11
|
+
Subclasses must implement the `parse` method to process and extract meaningful data from the raw output.
|
|
12
|
+
"""
|
|
13
|
+
@abstractmethod
|
|
14
|
+
def parse(self, result: str) -> T:
|
|
15
|
+
"""Parses the raw output string from the language model.
|
|
16
|
+
|
|
17
|
+
This abstract method must be implemented by subclasses to define how the result is parsed and processed.
|
|
18
|
+
|
|
19
|
+
Args:
|
|
20
|
+
result (str): The raw output string from the language model.
|
|
21
|
+
|
|
22
|
+
Returns:
|
|
23
|
+
T: The parsed result of type T.
|
|
24
|
+
|
|
25
|
+
Raises:
|
|
26
|
+
NotImplementedError: If the method is not implemented in a subclass.
|
|
27
|
+
"""
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
from _typeshed import Incomplete
|
|
2
|
+
from gllm_inference.schema import Message as Message, MessageContent as MessageContent, MessageRole as MessageRole
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
KEY_EXTRACTOR_REGEX: Incomplete
|
|
6
|
+
|
|
7
|
+
class PromptBuilder:
|
|
8
|
+
"""A prompt builder class used in Gen AI applications.
|
|
9
|
+
|
|
10
|
+
Attributes:
|
|
11
|
+
system_template (str): The system prompt template. May contain placeholders enclosed in curly braces `{}`.
|
|
12
|
+
user_template (str): The user prompt template. May contain placeholders enclosed in curly braces `{}`.
|
|
13
|
+
prompt_key_set (set[str]): A set of expected keys that must be present in the prompt templates.
|
|
14
|
+
key_defaults (dict[str, str]): Default values for the keys in the prompt templates.
|
|
15
|
+
"""
|
|
16
|
+
system_template: Incomplete
|
|
17
|
+
user_template: Incomplete
|
|
18
|
+
prompt_key_set: Incomplete
|
|
19
|
+
key_defaults: Incomplete
|
|
20
|
+
def __init__(self, system_template: str = '', user_template: str = '', key_defaults: dict[str, str] | None = None, ignore_extra_keys: bool | None = None) -> None:
|
|
21
|
+
"""Initializes a new instance of the PromptBuilder class.
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
system_template (str, optional): The system prompt template. May contain placeholders enclosed in curly
|
|
25
|
+
braces `{}`. Defaults to an empty string.
|
|
26
|
+
user_template (str, optional): The user prompt template. May contain placeholders enclosed in curly
|
|
27
|
+
braces `{}`. Defaults to an empty string.
|
|
28
|
+
key_defaults (dict[str, str] | None, optional): Default values for the keys in the prompt templates.
|
|
29
|
+
Applied when the corresponding keys are not provided in the runtime input.
|
|
30
|
+
Defaults to None, in which case no default values will be assigned to the keys.
|
|
31
|
+
ignore_extra_keys (bool | None, optional): Deprecated parameter. Will be removed in v0.6. Extra keys
|
|
32
|
+
will always raise a warning only instead of raising an error.
|
|
33
|
+
|
|
34
|
+
Raises:
|
|
35
|
+
ValueError: If both `system_template` and `user_template` are empty.
|
|
36
|
+
"""
|
|
37
|
+
def format(self, history: list[Message] | None = None, extra_contents: list[MessageContent] | None = None, **kwargs: Any) -> list[Message]:
|
|
38
|
+
"""Formats the prompt templates into a list of messages.
|
|
39
|
+
|
|
40
|
+
This method processes each prompt template, replacing the placeholders in the template content with the
|
|
41
|
+
corresponding values from `kwargs`. If any required key is missing from `kwargs`, it raises a `ValueError`.
|
|
42
|
+
It also handles the provided history and extra contents. It formats the prompt as a list of messages.
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
history (list[Message] | None, optional): The history to be included in the prompt. Defaults to None.
|
|
46
|
+
extra_contents (list[MessageContent] | None, optional): The extra contents to be included in the user
|
|
47
|
+
message. Defaults to None.
|
|
48
|
+
**kwargs (Any): A dictionary of placeholder values to be injected into the prompt templates.
|
|
49
|
+
Values must be either a string or an object that can be serialized to a string.
|
|
50
|
+
|
|
51
|
+
Returns:
|
|
52
|
+
list[Message]: A formatted list of messages.
|
|
53
|
+
|
|
54
|
+
Raises:
|
|
55
|
+
ValueError: If a required key for the prompt template is missing from `kwargs`.
|
|
56
|
+
"""
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
from gllm_inference.prompt_formatter.agnostic_prompt_formatter import AgnosticPromptFormatter as AgnosticPromptFormatter
|
|
2
|
+
from gllm_inference.prompt_formatter.huggingface_prompt_formatter import HuggingFacePromptFormatter as HuggingFacePromptFormatter
|
|
3
|
+
from gllm_inference.prompt_formatter.llama_prompt_formatter import LlamaPromptFormatter as LlamaPromptFormatter
|
|
4
|
+
from gllm_inference.prompt_formatter.mistral_prompt_formatter import MistralPromptFormatter as MistralPromptFormatter
|
|
5
|
+
from gllm_inference.prompt_formatter.openai_prompt_formatter import OpenAIPromptFormatter as OpenAIPromptFormatter
|
|
6
|
+
|
|
7
|
+
__all__ = ['AgnosticPromptFormatter', 'HuggingFacePromptFormatter', 'LlamaPromptFormatter', 'MistralPromptFormatter', 'OpenAIPromptFormatter']
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
from _typeshed import Incomplete
|
|
2
|
+
from gllm_inference.prompt_formatter.prompt_formatter import BasePromptFormatter as BasePromptFormatter
|
|
3
|
+
from gllm_inference.schema import MessageRole as MessageRole
|
|
4
|
+
|
|
5
|
+
class AgnosticPromptFormatter(BasePromptFormatter):
|
|
6
|
+
'''A prompt formatter that formats prompt without any specific model formatting.
|
|
7
|
+
|
|
8
|
+
The `AgnosticPromptFormatter` class formats a prompt by joining the content of the prompt templates using a
|
|
9
|
+
specified separator. It is designed to work independently of specific model types.
|
|
10
|
+
|
|
11
|
+
Attributes:
|
|
12
|
+
content_separator (str): A string used to separate each content in a message.
|
|
13
|
+
message_separator (str): A string used to separate each message.
|
|
14
|
+
|
|
15
|
+
Usage:
|
|
16
|
+
The `AgnosticPromptFormatter` can be used to format a prompt for any model.
|
|
17
|
+
The `content_separator` and `message_separator` can be customized to define the format of the prompt.
|
|
18
|
+
|
|
19
|
+
Usage example:
|
|
20
|
+
```python
|
|
21
|
+
prompt = [
|
|
22
|
+
(MessageRole.USER, ["Hello", "how are you?"]),
|
|
23
|
+
(MessageRole.ASSISTANT, ["I\'m fine", "thank you!"]),
|
|
24
|
+
(MessageRole.USER, ["What is the capital of France?"]),
|
|
25
|
+
]
|
|
26
|
+
prompt_formatter = AgnosticPromptFormatter(
|
|
27
|
+
message_separator="\\n###\\n",
|
|
28
|
+
content_separator="---"
|
|
29
|
+
)
|
|
30
|
+
print(prompt_formatter.format(prompt))
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Output example:
|
|
34
|
+
```
|
|
35
|
+
Hello---how are you?
|
|
36
|
+
###
|
|
37
|
+
I\'m fine---thank you!
|
|
38
|
+
###
|
|
39
|
+
What is the capital of France?
|
|
40
|
+
```
|
|
41
|
+
'''
|
|
42
|
+
message_separator: Incomplete
|
|
43
|
+
def __init__(self, message_separator: str = '\n', content_separator: str = '\n') -> None:
|
|
44
|
+
'''Initializes a new instance of the AgnosticPromptFormatter class.
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
message_separator (str, optional): A string used to separate each message. Defaults to "\\n".
|
|
48
|
+
content_separator (str, optional): A string used to separate each content in a message. Defaults to "\\n".
|
|
49
|
+
'''
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
from gllm_inference.prompt_formatter.prompt_formatter import BasePromptFormatter as BasePromptFormatter
|
|
2
|
+
from gllm_inference.schema import MessageRole as MessageRole
|
|
3
|
+
|
|
4
|
+
TOKENIZER_LOAD_ERROR_MESSAGE: str
|
|
5
|
+
|
|
6
|
+
class HuggingFacePromptFormatter(BasePromptFormatter):
|
|
7
|
+
'''A prompt formatter that formats prompt using HuggingFace model\'s specific formatting.
|
|
8
|
+
|
|
9
|
+
The `HuggingFacePromptFormatter` class is designed to format prompt using a HuggingFace model\'s specific formatting.
|
|
10
|
+
It does so by using the model\'s tokenizer\'s `apply_chat_template` method.
|
|
11
|
+
|
|
12
|
+
Attributes:
|
|
13
|
+
content_separator (str): A string used to separate each content in a message.
|
|
14
|
+
tokenizer (PreTrainedTokenizer): The HuggingFace model tokenizer used for chat templating.
|
|
15
|
+
|
|
16
|
+
Usage:
|
|
17
|
+
The `HuggingFacePromptFormatter` can be used to format a prompt using a HuggingFace model\'s specific formatting.
|
|
18
|
+
The `content_separator` and `model_name_or_path` can be customized to define the format of the prompt.
|
|
19
|
+
The `model_name_or_path` defines the name of the HuggingFace model whose tokenizer will be used to format
|
|
20
|
+
the prompt using the `apply_chat_template` method.
|
|
21
|
+
|
|
22
|
+
Usage example:
|
|
23
|
+
```python
|
|
24
|
+
prompt = [
|
|
25
|
+
(MessageRole.USER, ["Hello", "how are you?"]),
|
|
26
|
+
(MessageRole.ASSISTANT, ["I\'m fine", "thank you!"]),
|
|
27
|
+
(MessageRole.USER, ["What is the capital of France?"]),
|
|
28
|
+
]
|
|
29
|
+
prompt_formatter = HuggingFacePromptFormatter(
|
|
30
|
+
model_name_or_path="mistralai/Mistral-7B-Instruct-v0.1",
|
|
31
|
+
content_separator="---"
|
|
32
|
+
)
|
|
33
|
+
print(prompt_formatter.format(prompt))
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Output example:
|
|
37
|
+
```
|
|
38
|
+
<s>[INST] Hello---how are you? [/INST]I\'m fine---thank you!</s> [INST] What is the capital of France? [/INST]
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Using a gated model:
|
|
42
|
+
If you\'re trying to access the prompt builder template of a gated model, you\'d need to:
|
|
43
|
+
1. Request access to the gated repo using your HuggingFace account.
|
|
44
|
+
2. Login to HuggingFace in your system. This can be done as follows:
|
|
45
|
+
2.1. Install huggingface-hub: ```pip install huggingface-hub```
|
|
46
|
+
2.2. Login to HuggingFace: ```huggingface-cli login```
|
|
47
|
+
2.3. Enter your HuggingFace token.
|
|
48
|
+
'''
|
|
49
|
+
def __init__(self, model_name_or_path: str, content_separator: str = '\n') -> None:
|
|
50
|
+
'''Initializes a new instance of the HuggingFacePromptFormatter class.
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
model_name_or_path (str): The model name or path of the HuggingFace model tokenizer to be loaded.
|
|
54
|
+
content_separator (str, optional): A string used to separate each content in a message. Defaults to "\\n".
|
|
55
|
+
'''
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
from gllm_inference.prompt_formatter import HuggingFacePromptFormatter as HuggingFacePromptFormatter
|
|
2
|
+
|
|
3
|
+
class LlamaPromptFormatter(HuggingFacePromptFormatter):
|
|
4
|
+
'''A prompt formatter that formats prompt using Llama model\'s specific formatting.
|
|
5
|
+
|
|
6
|
+
The `LlamaPromptFormatter` class is designed to format prompt using a Llama model\'s specific formatting.
|
|
7
|
+
It does so by using the model\'s tokenizer\'s `apply_chat_template` method.
|
|
8
|
+
|
|
9
|
+
Attributes:
|
|
10
|
+
content_separator (str): A string used to separate each content in a message.
|
|
11
|
+
tokenizer (PreTrainedTokenizer): The HuggingFace model tokenizer used for chat templating.
|
|
12
|
+
|
|
13
|
+
Usage:
|
|
14
|
+
The `LlamaPromptFormatter` can be used to format a prompt using a Llama model\'s specific formatting.
|
|
15
|
+
The `content_separator` and `model_name` can be customized to define the format of the prompt.
|
|
16
|
+
The `model_name` defines the name of the HuggingFace model whose tokenizer will be used to format
|
|
17
|
+
the prompt using the `apply_chat_template` method.
|
|
18
|
+
|
|
19
|
+
Usage example:
|
|
20
|
+
```python
|
|
21
|
+
prompt = [
|
|
22
|
+
(MessageRole.USER, ["Hello", "how are you?"]),
|
|
23
|
+
(MessageRole.ASSISTANT, ["I\'m fine", "thank you!"]),
|
|
24
|
+
(MessageRole.USER, ["What is the capital of France?"]),
|
|
25
|
+
]
|
|
26
|
+
prompt_formatter = LlamaPromptFormatter(
|
|
27
|
+
model_name_or_path="meta-llama/Meta-Llama-3.1-8B-Instruct",
|
|
28
|
+
content_separator="---"
|
|
29
|
+
)
|
|
30
|
+
print(prompt_formatter.format(prompt))
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Output example:
|
|
34
|
+
```
|
|
35
|
+
<|begin_of_text|><|start_header_id|>user<|end_header_id|>
|
|
36
|
+
|
|
37
|
+
Hello---how are you?<|eot_id|><|start_header_id|>assistant<|end_header_id|>
|
|
38
|
+
|
|
39
|
+
I\'m fine---thank you!<|eot_id|><|start_header_id|>user<|end_header_id|>
|
|
40
|
+
|
|
41
|
+
What is the capital of France?<|eot_id|><|start_header_id|>assistant<|end_header_id|>
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Using a gated model:
|
|
45
|
+
If you\'re trying to access the prompt builder template of a gated model, you\'d need to:
|
|
46
|
+
1. Request access to the gated repo using your HuggingFace account.
|
|
47
|
+
2. Login to HuggingFace in your system. This can be done as follows:
|
|
48
|
+
2.1. Install huggingface-hub: ```pip install huggingface-hub```
|
|
49
|
+
2.2. Login to HuggingFace: ```huggingface-cli login```
|
|
50
|
+
2.3. Enter your HuggingFace token.
|
|
51
|
+
'''
|
|
52
|
+
def __init__(self, model_name: str = 'Meta-Llama-3.1-8B-Instruct', content_separator: str = '\n') -> None:
|
|
53
|
+
'''Initializes a new instance of the LlamaPromptFormatter class.
|
|
54
|
+
|
|
55
|
+
Args:
|
|
56
|
+
model_name (str, optional): The name of the Llama model tokenizer to be loaded. Defaults to
|
|
57
|
+
`Meta-Llama-3.1-8B-Instruct`.
|
|
58
|
+
content_separator (str, optional): A string used to separate each content in a message. Defaults to "\\n".
|
|
59
|
+
'''
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
from gllm_inference.prompt_formatter import HuggingFacePromptFormatter as HuggingFacePromptFormatter
|
|
2
|
+
|
|
3
|
+
class MistralPromptFormatter(HuggingFacePromptFormatter):
|
|
4
|
+
'''A prompt formatter that formats prompt using Mistral model\'s specific formatting.
|
|
5
|
+
|
|
6
|
+
The `MistralPromptFormatter` class is designed to format prompt using a Mistral model\'s specific formatting.
|
|
7
|
+
It does so by using the model\'s tokenizer\'s `apply_chat_template` method.
|
|
8
|
+
|
|
9
|
+
Attributes:
|
|
10
|
+
content_separator (str): A string used to separate each content in a message.
|
|
11
|
+
tokenizer (PreTrainedTokenizer): The HuggingFace model tokenizer used for chat templating.
|
|
12
|
+
|
|
13
|
+
Usage:
|
|
14
|
+
The `MistralPromptFormatter` can be used to format a prompt using a Mistral model\'s specific formatting.
|
|
15
|
+
The `content_separator` and `model_name` can be customized to define the format of the prompt.
|
|
16
|
+
The `model_name` defines the name of the HuggingFace model whose tokenizer will be used to format
|
|
17
|
+
the prompt using the `apply_chat_template` method.
|
|
18
|
+
|
|
19
|
+
Usage example:
|
|
20
|
+
```python
|
|
21
|
+
prompt = [
|
|
22
|
+
(MessageRole.USER, ["Hello", "how are you?"]),
|
|
23
|
+
(MessageRole.ASSISTANT, ["I\'m fine", "thank you!"]),
|
|
24
|
+
(MessageRole.USER, ["What is the capital of France?"]),
|
|
25
|
+
]
|
|
26
|
+
prompt_formatter = MistralPromptFormatter(
|
|
27
|
+
model_name_or_path="mistralai/Mistral-7B-Instruct-v0.1",
|
|
28
|
+
content_separator="---"
|
|
29
|
+
)
|
|
30
|
+
print(prompt_formatter.format(prompt))
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Output example:
|
|
34
|
+
```
|
|
35
|
+
<s>[INST] Hello---how are you? [/INST]I\'m fine---thank you!</s> [INST] What is the capital of France? [/INST]
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Using a gated model:
|
|
39
|
+
If you\'re trying to access the prompt builder template of a gated model, you\'d need to:
|
|
40
|
+
1. Request access to the gated repo using your HuggingFace account.
|
|
41
|
+
2. Login to HuggingFace in your system. This can be done as follows:
|
|
42
|
+
2.1. Install huggingface-hub: ```pip install huggingface-hub```
|
|
43
|
+
2.2. Login to HuggingFace: ```huggingface-cli login```
|
|
44
|
+
2.3. Enter your HuggingFace token.
|
|
45
|
+
'''
|
|
46
|
+
def __init__(self, model_name: str = 'Mistral-7B-Instruct-v0.3', content_separator: str = '\n') -> None:
|
|
47
|
+
'''Initializes a new instance of the MistralPromptFormatter class.
|
|
48
|
+
|
|
49
|
+
Args:
|
|
50
|
+
model_name (str, optional): The name of the Mistral model tokenizer to be loaded. Defaults to
|
|
51
|
+
`Mistral-7B-Instruct-v0.3`.
|
|
52
|
+
content_separator (str, optional): A string used to separate each content in a message. Defaults to "\\n".
|
|
53
|
+
'''
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from gllm_inference.prompt_formatter.prompt_formatter import BasePromptFormatter as BasePromptFormatter
|
|
2
|
+
from gllm_inference.schema import MessageRole as MessageRole
|
|
3
|
+
|
|
4
|
+
class OpenAIPromptFormatter(BasePromptFormatter):
|
|
5
|
+
'''A prompt formatter that formats prompt with OpenAI\'s specific formatting.
|
|
6
|
+
|
|
7
|
+
The `OpenAIPromptFormatter` class formats a prompt by utilizing OpenAI\'s specific formatting.
|
|
8
|
+
|
|
9
|
+
Attributes:
|
|
10
|
+
content_separator (str): A string used to separate each content in a message.
|
|
11
|
+
|
|
12
|
+
Usage:
|
|
13
|
+
The `OpenAIPromptFormatter` can be used to format a prompt for OpenAI\'s models.
|
|
14
|
+
The `content_separator` can be customized to define the format of the prompt.
|
|
15
|
+
|
|
16
|
+
Usage example:
|
|
17
|
+
```python
|
|
18
|
+
prompt = [
|
|
19
|
+
(MessageRole.USER, ["Hello", "how are you?"]),
|
|
20
|
+
(MessageRole.ASSISTANT, ["I\'m fine", "thank you!"]),
|
|
21
|
+
(MessageRole.USER, ["What is the capital of France?"]),
|
|
22
|
+
]
|
|
23
|
+
prompt_formatter = OpenAIPromptFormatter(
|
|
24
|
+
content_separator="---"
|
|
25
|
+
)
|
|
26
|
+
print(prompt_formatter.format(prompt))
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Output example:
|
|
30
|
+
```
|
|
31
|
+
User: Hello---how are you?
|
|
32
|
+
Assistant: I\'m fine---thank you!
|
|
33
|
+
User: What is the capital of France?
|
|
34
|
+
```
|
|
35
|
+
'''
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import abc
|
|
2
|
+
from _typeshed import Incomplete
|
|
3
|
+
from abc import ABC
|
|
4
|
+
from gllm_inference.schema import Message as Message, MessageContent as MessageContent, MessageRole as MessageRole
|
|
5
|
+
|
|
6
|
+
class BasePromptFormatter(ABC, metaclass=abc.ABCMeta):
|
|
7
|
+
"""A base class for prompt formatters used in Gen AI applications.
|
|
8
|
+
|
|
9
|
+
The prompt formatter class is used to format a prompt into a string with specific formatting.
|
|
10
|
+
|
|
11
|
+
Attributes:
|
|
12
|
+
content_separator (str): The separator to be used between the string in a single message.
|
|
13
|
+
"""
|
|
14
|
+
content_separator: Incomplete
|
|
15
|
+
def __init__(self, content_separator: str = '\n') -> None:
|
|
16
|
+
'''Initializes a new instance of the BasePromptFormatter class.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
content_separator (str, optional): The separator to be used between the string in a single message.
|
|
20
|
+
Defaults to "\\n".
|
|
21
|
+
'''
|
|
22
|
+
def format(self, messages: list[Message]) -> str:
|
|
23
|
+
"""Formats the prompt as a string.
|
|
24
|
+
|
|
25
|
+
Args:
|
|
26
|
+
messages (list[Message]): The messages to be formatted as a string.
|
|
27
|
+
|
|
28
|
+
Returns:
|
|
29
|
+
str: The formatted messages as a string.
|
|
30
|
+
"""
|