gllm-inference-binary 0.5.9b1__cp311-cp311-macosx_10_9_universal2.macosx_13_0_x86_64.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 +137 -0
- gllm_inference/builder/build_lm_invoker.pyi +161 -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 +10 -0
- gllm_inference/em_invoker/azure_openai_em_invoker.pyi +88 -0
- gllm_inference/em_invoker/bedrock_em_invoker.pyi +106 -0
- gllm_inference/em_invoker/em_invoker.pyi +90 -0
- gllm_inference/em_invoker/google_em_invoker.pyi +129 -0
- gllm_inference/em_invoker/langchain/__init__.pyi +3 -0
- gllm_inference/em_invoker/langchain/em_invoker_embeddings.pyi +84 -0
- gllm_inference/em_invoker/langchain_em_invoker.pyi +46 -0
- gllm_inference/em_invoker/openai_compatible_em_invoker.pyi +96 -0
- gllm_inference/em_invoker/openai_em_invoker.pyi +90 -0
- gllm_inference/em_invoker/schema/__init__.pyi +0 -0
- gllm_inference/em_invoker/schema/bedrock.pyi +22 -0
- gllm_inference/em_invoker/schema/google.pyi +9 -0
- gllm_inference/em_invoker/schema/langchain.pyi +5 -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 +101 -0
- gllm_inference/em_invoker/voyage_em_invoker.pyi +104 -0
- gllm_inference/exceptions/__init__.pyi +4 -0
- gllm_inference/exceptions/error_parser.pyi +41 -0
- gllm_inference/exceptions/exceptions.pyi +132 -0
- gllm_inference/exceptions/provider_error_map.pyi +23 -0
- gllm_inference/lm_invoker/__init__.pyi +12 -0
- gllm_inference/lm_invoker/anthropic_lm_invoker.pyi +275 -0
- gllm_inference/lm_invoker/azure_openai_lm_invoker.pyi +252 -0
- gllm_inference/lm_invoker/bedrock_lm_invoker.pyi +234 -0
- gllm_inference/lm_invoker/datasaur_lm_invoker.pyi +166 -0
- gllm_inference/lm_invoker/google_lm_invoker.pyi +317 -0
- gllm_inference/lm_invoker/langchain_lm_invoker.pyi +260 -0
- gllm_inference/lm_invoker/litellm_lm_invoker.pyi +248 -0
- gllm_inference/lm_invoker/lm_invoker.pyi +152 -0
- gllm_inference/lm_invoker/openai_compatible_lm_invoker.pyi +265 -0
- gllm_inference/lm_invoker/openai_lm_invoker.pyi +362 -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 +53 -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 +23 -0
- gllm_inference/lm_invoker/schema/openai.pyi +91 -0
- gllm_inference/lm_invoker/schema/openai_compatible.pyi +60 -0
- gllm_inference/lm_invoker/schema/xai.pyi +31 -0
- gllm_inference/lm_invoker/xai_lm_invoker.pyi +305 -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 +101 -0
- gllm_inference/request_processor/uses_lm_mixin.pyi +130 -0
- gllm_inference/schema/__init__.pyi +14 -0
- gllm_inference/schema/attachment.pyi +88 -0
- gllm_inference/schema/code_exec_result.pyi +14 -0
- gllm_inference/schema/config.pyi +15 -0
- gllm_inference/schema/enums.pyi +29 -0
- gllm_inference/schema/lm_output.pyi +36 -0
- gllm_inference/schema/message.pyi +52 -0
- gllm_inference/schema/model_id.pyi +147 -0
- gllm_inference/schema/reasoning.pyi +15 -0
- gllm_inference/schema/token_usage.pyi +75 -0
- gllm_inference/schema/tool_call.pyi +14 -0
- gllm_inference/schema/tool_result.pyi +11 -0
- gllm_inference/schema/type_alias.pyi +11 -0
- gllm_inference/utils/__init__.pyi +5 -0
- gllm_inference/utils/io_utils.pyi +26 -0
- gllm_inference/utils/langchain.pyi +30 -0
- gllm_inference/utils/validation.pyi +12 -0
- gllm_inference.build/.gitignore +1 -0
- gllm_inference.cpython-311-darwin.so +0 -0
- gllm_inference.pyi +123 -0
- gllm_inference_binary-0.5.9b1.dist-info/METADATA +71 -0
- gllm_inference_binary-0.5.9b1.dist-info/RECORD +105 -0
- gllm_inference_binary-0.5.9b1.dist-info/WHEEL +6 -0
- gllm_inference_binary-0.5.9b1.dist-info/top_level.txt +1 -0
|
@@ -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
|
+
"""
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
from _typeshed import Incomplete
|
|
2
|
+
from gllm_core.event import EventEmitter as EventEmitter
|
|
3
|
+
from gllm_inference.lm_invoker.lm_invoker import BaseLMInvoker as BaseLMInvoker
|
|
4
|
+
from gllm_inference.output_parser.output_parser import BaseOutputParser as BaseOutputParser
|
|
5
|
+
from gllm_inference.prompt_builder.prompt_builder import PromptBuilder as PromptBuilder
|
|
6
|
+
from gllm_inference.schema import LMOutput as LMOutput, Message as Message, MessageContent as MessageContent, ResponseSchema as ResponseSchema, ToolCall as ToolCall, ToolResult as ToolResult
|
|
7
|
+
from langchain_core.tools import Tool as Tool
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
class LMRequestProcessor:
|
|
11
|
+
"""A request processor to perform language models inference.
|
|
12
|
+
|
|
13
|
+
The `LMRequestProcessor` class handles the process of building a prompt, invoking a language model, and optionally
|
|
14
|
+
parsing the output. It combines a prompt builder, language model invoker, and an optional output parser to manage
|
|
15
|
+
the inference process in Gen AI applications.
|
|
16
|
+
|
|
17
|
+
Attributes:
|
|
18
|
+
prompt_builder (PromptBuilder): The prompt builder used to format the prompt.
|
|
19
|
+
lm_invoker (BaseLMInvoker): The language model invoker that handles the model inference.
|
|
20
|
+
output_parser (BaseOutputParser | None): The optional parser to process the model's output, if any.
|
|
21
|
+
tool_dict (dict[str, Tool]): A dictionary of tools provided to the language model to enable tool calling,
|
|
22
|
+
if any. The dictionary maps the tool name to the tools themselves.
|
|
23
|
+
"""
|
|
24
|
+
prompt_builder: Incomplete
|
|
25
|
+
lm_invoker: Incomplete
|
|
26
|
+
output_parser: Incomplete
|
|
27
|
+
tool_dict: Incomplete
|
|
28
|
+
def __init__(self, prompt_builder: PromptBuilder, lm_invoker: BaseLMInvoker, output_parser: BaseOutputParser | None = None) -> None:
|
|
29
|
+
"""Initializes a new instance of the LMRequestProcessor class.
|
|
30
|
+
|
|
31
|
+
Args:
|
|
32
|
+
prompt_builder (PromptBuilder): The prompt builder used to format the prompt.
|
|
33
|
+
lm_invoker (BaseLMInvoker): The language model invoker that handles the model inference.
|
|
34
|
+
output_parser (BaseOutputParser, optional): An optional parser to process the model's output.
|
|
35
|
+
Defaults to None.
|
|
36
|
+
"""
|
|
37
|
+
def set_tools(self, tools: list[Tool]) -> None:
|
|
38
|
+
"""Sets the tools for the LM invoker.
|
|
39
|
+
|
|
40
|
+
This method sets the tools for the LM invoker. Any existing tools will be replaced.
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
tools (list[Tool]): The list of tools to be used.
|
|
44
|
+
"""
|
|
45
|
+
def clear_tools(self) -> None:
|
|
46
|
+
"""Clears the tools for the LM invoker.
|
|
47
|
+
|
|
48
|
+
This method clears the tools for the LM invoker.
|
|
49
|
+
"""
|
|
50
|
+
def set_response_schema(self, response_schema: ResponseSchema | None) -> None:
|
|
51
|
+
"""Sets the response schema for the LM invoker.
|
|
52
|
+
|
|
53
|
+
This method sets the response schema for the LM invoker. Any existing response schema will be replaced.
|
|
54
|
+
|
|
55
|
+
Args:
|
|
56
|
+
response_schema (ResponseSchema | None): The response schema to be used.
|
|
57
|
+
"""
|
|
58
|
+
def clear_response_schema(self) -> None:
|
|
59
|
+
"""Clears the response schema for the LM invoker.
|
|
60
|
+
|
|
61
|
+
This method clears the response schema for the LM invoker.
|
|
62
|
+
"""
|
|
63
|
+
async def process(self, prompt_kwargs: dict[str, Any] | None = None, history: list[Message] | None = None, extra_contents: list[MessageContent] | None = None, hyperparameters: dict[str, Any] | None = None, event_emitter: EventEmitter | None = None, auto_execute_tools: bool = True, max_lm_calls: int = 5, **kwargs: Any) -> Any:
|
|
64
|
+
"""Processes a language model inference request.
|
|
65
|
+
|
|
66
|
+
This method processes the language model inference request as follows:
|
|
67
|
+
1. Assembling the prompt using the provided keyword arguments.
|
|
68
|
+
2. Invoking the language model with the assembled prompt and optional hyperparameters.
|
|
69
|
+
3. If `auto_execute_tools` is True, the method will automatically execute tools if the LM output includes
|
|
70
|
+
tool calls.
|
|
71
|
+
4. Optionally parsing the model's output using the output parser if provided. If the model output is an
|
|
72
|
+
LMOutput object, the output parser will process the `response` attribute of the LMOutput object.
|
|
73
|
+
|
|
74
|
+
Args:
|
|
75
|
+
prompt_kwargs (dict[str, Any], optional): Deprecated parameter for passing prompt kwargs.
|
|
76
|
+
Replaced by **kwargs. Defaults to None
|
|
77
|
+
history (list[Message] | None, optional): A list of conversation history to be included in the prompt.
|
|
78
|
+
Defaults to None.
|
|
79
|
+
extra_contents (list[MessageContent] | None, optional): A list of extra contents to be included in the
|
|
80
|
+
user message. Defaults to None.
|
|
81
|
+
hyperparameters (dict[str, Any] | None, optional): A dictionary of hyperparameters for the model invocation.
|
|
82
|
+
Defaults to None.
|
|
83
|
+
event_emitter (EventEmitter | None, optional): An event emitter for streaming model outputs.
|
|
84
|
+
Defaults to None.
|
|
85
|
+
auto_execute_tools (bool, optional): Whether to automatically execute tools if the LM invokes output
|
|
86
|
+
tool calls. Defaults to True.
|
|
87
|
+
max_lm_calls (int, optional): The maximum number of times the language model can be invoked
|
|
88
|
+
when `auto_execute_tools` is True. Defaults to 5.
|
|
89
|
+
**kwargs (Any): Keyword arguments that will be passed to format the prompt builder.
|
|
90
|
+
Values must be either a string or an object that can be serialized to a string.
|
|
91
|
+
Reserved keyword arguments that cannot be passed to the prompt builder include:
|
|
92
|
+
1. `history`
|
|
93
|
+
2. `extra_contents`
|
|
94
|
+
3. `hyperparameters`
|
|
95
|
+
4. `event_emitter`
|
|
96
|
+
5. `auto_execute_tools`
|
|
97
|
+
6. `max_lm_calls`
|
|
98
|
+
|
|
99
|
+
Returns:
|
|
100
|
+
Any: The result of the language model invocation, optionally parsed by the output parser.
|
|
101
|
+
"""
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
from gllm_inference.builder.build_lm_invoker import build_lm_invoker as build_lm_invoker
|
|
2
|
+
from gllm_inference.lm_invoker.lm_invoker import BaseLMInvoker as BaseLMInvoker
|
|
3
|
+
from gllm_inference.output_parser.output_parser import BaseOutputParser as BaseOutputParser
|
|
4
|
+
from gllm_inference.prompt_builder.prompt_builder import PromptBuilder as PromptBuilder
|
|
5
|
+
from gllm_inference.request_processor.lm_request_processor import LMRequestProcessor as LMRequestProcessor
|
|
6
|
+
from gllm_inference.schema import LMOutput as LMOutput
|
|
7
|
+
from pydantic import BaseModel as BaseModel
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
class UsesLM:
|
|
11
|
+
'''A mixin to be extended by components that use LMRequestProcessor.
|
|
12
|
+
|
|
13
|
+
This mixin should be extended by components that use LMRequestProcessor. Components that extend this mixin
|
|
14
|
+
must have a constructor that accepts the LMRequestProcessor instance as its first argument.
|
|
15
|
+
|
|
16
|
+
LM based components can be categorized into two types:
|
|
17
|
+
1. Components that do not utilize structured output.
|
|
18
|
+
2. Components that utilize structured output.
|
|
19
|
+
|
|
20
|
+
Building a component without structured output:
|
|
21
|
+
As defined above, the component must accepts an LMRequestProcessor instance as its first argument, e.g.:
|
|
22
|
+
```python
|
|
23
|
+
class LMBasedComponent(Component, UsesLM):
|
|
24
|
+
def __init__(self, lm_request_processor: LMRequestProcessor, custom_kwarg: str):
|
|
25
|
+
self.lm_request_processor = lm_request_processor
|
|
26
|
+
self.custom_kwarg = custom_kwarg
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Using the `from_lm_components` method provided by this mixin, the component can be instantiated as follows:
|
|
30
|
+
```python
|
|
31
|
+
component = LMBasedComponent.from_lm_components(
|
|
32
|
+
prompt_builder,
|
|
33
|
+
lm_invoker,
|
|
34
|
+
output_parser,
|
|
35
|
+
custom_kwarg="custom_value",
|
|
36
|
+
)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Building a component with structured output:
|
|
40
|
+
When the component utilizes structured output, the `_parse_structured_output` method can be used
|
|
41
|
+
to simplify the process of extracting the structured output in the component\'s runtime methods, e.g.:
|
|
42
|
+
```python
|
|
43
|
+
class LMBasedComponent(Component, UsesLM):
|
|
44
|
+
def __init__(self, lm_request_processor: LMRequestProcessor, custom_kwarg: str):
|
|
45
|
+
self.lm_request_processor = lm_request_processor
|
|
46
|
+
self.custom_kwarg = custom_kwarg
|
|
47
|
+
|
|
48
|
+
def runtime_method(self, param1: str, param2: str) -> str:
|
|
49
|
+
lm_output = self.lm_request_processor.process(param1=param1, param2=param2)
|
|
50
|
+
return self._parse_structured_output(lm_output, "target_key", "fallback_output")
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Notice that in the above example, the LMRequestProcessor is configured to take `param1` and `param2`
|
|
54
|
+
as keyword arguments and output a structured output that contains the `target_key` key. Hence,
|
|
55
|
+
these conditions must be fulfilled when instantiating the component.
|
|
56
|
+
|
|
57
|
+
This mixin also provides the `with_structured_output` method to simplify the process of instantiating
|
|
58
|
+
the component with structured output. Let\'s take a look at an example that meets the above conditions:
|
|
59
|
+
```python
|
|
60
|
+
class Schema(BaseModel):
|
|
61
|
+
target_key: str
|
|
62
|
+
|
|
63
|
+
component = LMBasedComponent.with_structured_output(
|
|
64
|
+
model_id="openai/gpt-4.1-mini",
|
|
65
|
+
response_schema=Schema,
|
|
66
|
+
system_template="system_template {param1}",
|
|
67
|
+
user_template="user_template {param2}",
|
|
68
|
+
custom_kwarg="custom_value",
|
|
69
|
+
)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Building a structured output preset:
|
|
73
|
+
If desired, the component can also define a quick preset. This can be done by providing default prompts
|
|
74
|
+
as response schema. Here\'s an example:
|
|
75
|
+
```python
|
|
76
|
+
class Schema(BaseModel):
|
|
77
|
+
target_key: str
|
|
78
|
+
|
|
79
|
+
@classmethod
|
|
80
|
+
def from_preset(cls, model_id: str, custom_kwarg: str) -> "LMBasedComponent":
|
|
81
|
+
return cls.with_structured_output(
|
|
82
|
+
model_id=model_id,
|
|
83
|
+
response_schema=Schema,
|
|
84
|
+
system_template=PRESET_SYSTEM_TEMPLATE,
|
|
85
|
+
user_template=PRESET_USER_TEMPLATE,
|
|
86
|
+
custom_kwarg=custom_kwarg,
|
|
87
|
+
)
|
|
88
|
+
)
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Then, the preset can be instantiated as follows:
|
|
92
|
+
```python
|
|
93
|
+
component = LMBasedComponent.from_preset(
|
|
94
|
+
model_id="openai/gpt-4.1-mini",
|
|
95
|
+
custom_kwarg="custom_value",
|
|
96
|
+
)
|
|
97
|
+
```
|
|
98
|
+
'''
|
|
99
|
+
@classmethod
|
|
100
|
+
def from_lm_components(cls, prompt_builder: PromptBuilder, lm_invoker: BaseLMInvoker, output_parser: BaseOutputParser | None = None, **kwargs: Any) -> UsesLM:
|
|
101
|
+
"""Creates an instance from LMRequestProcessor components directly.
|
|
102
|
+
|
|
103
|
+
This method is a shortcut to initialize the class by providing the LMRequestProcessor components directly.
|
|
104
|
+
|
|
105
|
+
Args:
|
|
106
|
+
prompt_builder (PromptBuilder): The prompt builder used to format the prompt.
|
|
107
|
+
lm_invoker (BaseLMInvoker): The language model invoker that handles the model inference.
|
|
108
|
+
output_parser (BaseOutputParser, optional): An optional parser to process the model's output.
|
|
109
|
+
Defaults to None.
|
|
110
|
+
**kwargs (Any): Additional keyword arguments to be passed to the class constructor.
|
|
111
|
+
|
|
112
|
+
Returns:
|
|
113
|
+
UsesLM: An instance of the class that mixes in this mixin.
|
|
114
|
+
"""
|
|
115
|
+
@classmethod
|
|
116
|
+
def with_structured_output(cls, model_id: str, response_schema: type[BaseModel], system_template: str = '', user_template: str = '', **kwargs: Any) -> UsesLM:
|
|
117
|
+
"""Creates an instance with structured output configuration.
|
|
118
|
+
|
|
119
|
+
This method is a shortcut to initialize the class with structured output configuration.
|
|
120
|
+
|
|
121
|
+
Args:
|
|
122
|
+
model_id (str): The model ID of the language model.
|
|
123
|
+
response_schema (type[BaseModel]): The response schema of the language model.
|
|
124
|
+
system_template (str, optional): The system template of the language model. Defaults to an empty string.
|
|
125
|
+
user_template (str, optional): The user template of the language model. Defaults to an empty string.
|
|
126
|
+
**kwargs (Any): Additional keyword arguments to be passed to the class constructor.
|
|
127
|
+
|
|
128
|
+
Returns:
|
|
129
|
+
UsesLM: An instance of the class that mixes in this mixin with structured output configuration.
|
|
130
|
+
"""
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from gllm_inference.schema.attachment import Attachment as Attachment
|
|
2
|
+
from gllm_inference.schema.code_exec_result import CodeExecResult as CodeExecResult
|
|
3
|
+
from gllm_inference.schema.config import TruncationConfig as TruncationConfig
|
|
4
|
+
from gllm_inference.schema.enums import AttachmentType as AttachmentType, EmitDataType as EmitDataType, MessageRole as MessageRole, TruncateSide as TruncateSide
|
|
5
|
+
from gllm_inference.schema.lm_output import LMOutput as LMOutput
|
|
6
|
+
from gllm_inference.schema.message import Message as Message
|
|
7
|
+
from gllm_inference.schema.model_id import ModelId as ModelId, ModelProvider as ModelProvider
|
|
8
|
+
from gllm_inference.schema.reasoning import Reasoning as Reasoning
|
|
9
|
+
from gllm_inference.schema.token_usage import InputTokenDetails as InputTokenDetails, OutputTokenDetails as OutputTokenDetails, TokenUsage as TokenUsage
|
|
10
|
+
from gllm_inference.schema.tool_call import ToolCall as ToolCall
|
|
11
|
+
from gllm_inference.schema.tool_result import ToolResult as ToolResult
|
|
12
|
+
from gllm_inference.schema.type_alias import EMContent as EMContent, MessageContent as MessageContent, ResponseSchema as ResponseSchema, Vector as Vector
|
|
13
|
+
|
|
14
|
+
__all__ = ['Attachment', 'AttachmentType', 'CodeExecResult', 'EMContent', 'EmitDataType', 'InputTokenDetails', 'MessageContent', 'LMOutput', 'ModelId', 'ModelProvider', 'Message', 'MessageRole', 'OutputTokenDetails', 'Reasoning', 'ResponseSchema', 'TokenUsage', 'ToolCall', 'ToolResult', 'TruncateSide', 'TruncationConfig', 'Vector']
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
from _typeshed import Incomplete
|
|
2
|
+
from gllm_inference.constants import HEX_REPR_LENGTH as HEX_REPR_LENGTH
|
|
3
|
+
from pydantic import BaseModel
|
|
4
|
+
|
|
5
|
+
logger: Incomplete
|
|
6
|
+
|
|
7
|
+
class Attachment(BaseModel):
|
|
8
|
+
"""Defines a file attachment schema.
|
|
9
|
+
|
|
10
|
+
Attributes:
|
|
11
|
+
data (bytes): The content data of the file attachment.
|
|
12
|
+
filename (str): The filename of the file attachment.
|
|
13
|
+
mime_type (str): The mime type of the file attachment.
|
|
14
|
+
extension (str): The extension of the file attachment.
|
|
15
|
+
url (str | None): The URL of the file attachment. Defaults to None.
|
|
16
|
+
"""
|
|
17
|
+
data: bytes
|
|
18
|
+
filename: str
|
|
19
|
+
mime_type: str
|
|
20
|
+
extension: str
|
|
21
|
+
url: str | None
|
|
22
|
+
@classmethod
|
|
23
|
+
def from_bytes(cls, bytes: bytes, filename: str | None = None) -> Attachment:
|
|
24
|
+
"""Creates an Attachment from bytes.
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
bytes (bytes): The bytes of the file.
|
|
28
|
+
filename (str | None, optional): The filename of the file. Defaults to None,
|
|
29
|
+
in which case the filename will be derived from the extension.
|
|
30
|
+
|
|
31
|
+
Returns:
|
|
32
|
+
Attachment: The instantiated Attachment.
|
|
33
|
+
"""
|
|
34
|
+
@classmethod
|
|
35
|
+
def from_base64(cls, base64_data: str, filename: str | None = None) -> Attachment:
|
|
36
|
+
"""Creates an Attachment from a base64 string.
|
|
37
|
+
|
|
38
|
+
Args:
|
|
39
|
+
base64_data (str): The base64 string of the file.
|
|
40
|
+
filename (str | None, optional): The filename of the file. Defaults to None,
|
|
41
|
+
in which case the filename will be derived from the mime type.
|
|
42
|
+
|
|
43
|
+
Returns:
|
|
44
|
+
Attachment: The instantiated Attachment.
|
|
45
|
+
"""
|
|
46
|
+
@classmethod
|
|
47
|
+
def from_data_url(cls, data_url: str, filename: str | None = None) -> Attachment:
|
|
48
|
+
"""Creates an Attachment from a data URL (data:[mime/type];base64,[bytes]).
|
|
49
|
+
|
|
50
|
+
Args:
|
|
51
|
+
data_url (str): The data URL of the file.
|
|
52
|
+
filename (str | None, optional): The filename of the file. Defaults to None,
|
|
53
|
+
in which case the filename will be derived from the mime type.
|
|
54
|
+
|
|
55
|
+
Returns:
|
|
56
|
+
Attachment: The instantiated Attachment.
|
|
57
|
+
"""
|
|
58
|
+
@classmethod
|
|
59
|
+
def from_url(cls, url: str, filename: str | None = None) -> Attachment:
|
|
60
|
+
"""Creates an Attachment from a URL.
|
|
61
|
+
|
|
62
|
+
Args:
|
|
63
|
+
url (str): The URL of the file.
|
|
64
|
+
filename (str | None, optional): The filename of the file. Defaults to None,
|
|
65
|
+
in which case the filename will be derived from the URL.
|
|
66
|
+
|
|
67
|
+
Returns:
|
|
68
|
+
Attachment: The instantiated Attachment.
|
|
69
|
+
"""
|
|
70
|
+
@classmethod
|
|
71
|
+
def from_path(cls, path: str, filename: str | None = None) -> Attachment:
|
|
72
|
+
"""Creates an Attachment from a path.
|
|
73
|
+
|
|
74
|
+
Args:
|
|
75
|
+
path (str): The path to the file.
|
|
76
|
+
filename (str | None, optional): The filename of the file. Defaults to None,
|
|
77
|
+
in which case the filename will be derived from the path.
|
|
78
|
+
|
|
79
|
+
Returns:
|
|
80
|
+
Attachment: The instantiated Attachment.
|
|
81
|
+
"""
|
|
82
|
+
def write_to_file(self, path: str | None = None) -> None:
|
|
83
|
+
"""Writes the Attachment to a file.
|
|
84
|
+
|
|
85
|
+
Args:
|
|
86
|
+
path (str | None, optional): The path to the file. Defaults to None,
|
|
87
|
+
in which case the filename will be used as the path.
|
|
88
|
+
"""
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from gllm_inference.schema.attachment import Attachment as Attachment
|
|
2
|
+
from pydantic import BaseModel
|
|
3
|
+
|
|
4
|
+
class CodeExecResult(BaseModel):
|
|
5
|
+
"""Defines a code execution result when a language model is configured to execute code.
|
|
6
|
+
|
|
7
|
+
Attributes:
|
|
8
|
+
id (str): The ID of the code execution. Defaults to an empty string.
|
|
9
|
+
code (str): The executed code. Defaults to an empty string.
|
|
10
|
+
output (list[str | Attachment]): The output of the executed code. Defaults to an empty list.
|
|
11
|
+
"""
|
|
12
|
+
id: str
|
|
13
|
+
code: str
|
|
14
|
+
output: list[str | Attachment]
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from gllm_inference.schema.enums import TruncateSide as TruncateSide
|
|
2
|
+
from pydantic import BaseModel
|
|
3
|
+
|
|
4
|
+
class TruncationConfig(BaseModel):
|
|
5
|
+
"""Configuration for text truncation behavior.
|
|
6
|
+
|
|
7
|
+
Attributes:
|
|
8
|
+
max_length (int): Maximum length of text content. Required.
|
|
9
|
+
truncate_side (TruncateSide | None): Side to truncate from when max_length is exceeded.
|
|
10
|
+
1. TruncateSide.RIGHT: Keep the beginning of the text, truncate from the end (default)
|
|
11
|
+
2. TruncateSide.LEFT: Keep the end of the text, truncate from the beginning
|
|
12
|
+
If None, defaults to TruncateSide.RIGHT
|
|
13
|
+
"""
|
|
14
|
+
max_length: int
|
|
15
|
+
truncate_side: TruncateSide | None
|