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,92 @@
|
|
|
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) -> 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): A dictionary of arguments used to format the prompt.
|
|
76
|
+
Defaults to None, in which case no arguments will be passed to the prompt builder.
|
|
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
|
+
|
|
90
|
+
Returns:
|
|
91
|
+
Any: The result of the language model invocation, optionally parsed by the output parser.
|
|
92
|
+
"""
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
from gllm_inference.lm_invoker.lm_invoker import BaseLMInvoker as BaseLMInvoker
|
|
2
|
+
from gllm_inference.output_parser.output_parser import BaseOutputParser as BaseOutputParser
|
|
3
|
+
from gllm_inference.prompt_builder.prompt_builder import PromptBuilder as PromptBuilder
|
|
4
|
+
from gllm_inference.request_processor.lm_request_processor import LMRequestProcessor as LMRequestProcessor
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
class UsesLM:
|
|
8
|
+
'''A mixin to initialize classes that use LMRequestProcessor by providing the components directly.
|
|
9
|
+
|
|
10
|
+
This mixin should be extended by classes that use LMRequestProcessor. Extending this mixin allows the class to
|
|
11
|
+
create an instance of itself by providing the LMRequestProcessor components directly.
|
|
12
|
+
|
|
13
|
+
For example:
|
|
14
|
+
```python
|
|
15
|
+
class LMBasedComponent(BaseComponent, UsesLM):
|
|
16
|
+
def __init__(self, lm_request_processor: LMRequestProcessor, custom_kwarg: str):
|
|
17
|
+
self.lm_request_processor = lm_request_processor
|
|
18
|
+
self.custom_kwarg = custom_kwarg
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Then, the class can be instantiated with the following:
|
|
22
|
+
```python
|
|
23
|
+
component = LMBasedComponent.from_lm_components(
|
|
24
|
+
prompt_builder,
|
|
25
|
+
lm_invoker,
|
|
26
|
+
output_parser,
|
|
27
|
+
**{"custom_kwarg": "custom_value"},
|
|
28
|
+
)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Note:
|
|
32
|
+
Classes that extend this mixin must have a constructor that accepts the LMRequestProcessor instance as its
|
|
33
|
+
first argument.
|
|
34
|
+
'''
|
|
35
|
+
@classmethod
|
|
36
|
+
def from_lm_components(cls, prompt_builder: PromptBuilder, lm_invoker: BaseLMInvoker, output_parser: BaseOutputParser | None = None, **kwargs: Any):
|
|
37
|
+
"""Creates an instance by initializing LMRequestProcessor with given components.
|
|
38
|
+
|
|
39
|
+
This method is a shortcut to initialize the class by providing the LMRequestProcessor components directly.
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
prompt_builder (PromptBuilder): The prompt builder used to format the prompt.
|
|
43
|
+
lm_invoker (BaseLMInvoker): The language model invoker that handles the model inference.
|
|
44
|
+
output_parser (BaseOutputParser, optional): An optional parser to process the model's output.
|
|
45
|
+
Defaults to None.
|
|
46
|
+
**kwargs (Any): Additional keyword arguments to be passed to the class constructor.
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
An instance of the class that mixes in this mixin.
|
|
50
|
+
"""
|
|
@@ -0,0 +1,13 @@
|
|
|
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.enums import AttachmentType as AttachmentType, EmitDataType as EmitDataType, MessageRole as MessageRole
|
|
4
|
+
from gllm_inference.schema.lm_output import LMOutput as LMOutput
|
|
5
|
+
from gllm_inference.schema.message import Message as Message
|
|
6
|
+
from gllm_inference.schema.model_id import ModelId as ModelId, ModelProvider as ModelProvider
|
|
7
|
+
from gllm_inference.schema.reasoning import Reasoning as Reasoning
|
|
8
|
+
from gllm_inference.schema.token_usage import TokenUsage as TokenUsage
|
|
9
|
+
from gllm_inference.schema.tool_call import ToolCall as ToolCall
|
|
10
|
+
from gllm_inference.schema.tool_result import ToolResult as ToolResult
|
|
11
|
+
from gllm_inference.schema.type_alias import EMContent as EMContent, ErrorResponse as ErrorResponse, MessageContent as MessageContent, ResponseSchema as ResponseSchema, Vector as Vector
|
|
12
|
+
|
|
13
|
+
__all__ = ['Attachment', 'AttachmentType', 'CodeExecResult', 'EMContent', 'EmitDataType', 'ErrorResponse', 'MessageContent', 'LMOutput', 'ModelId', 'ModelProvider', 'Message', 'MessageRole', 'Reasoning', 'ResponseSchema', 'TokenUsage', 'ToolCall', 'ToolResult', '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,24 @@
|
|
|
1
|
+
from enum import StrEnum
|
|
2
|
+
|
|
3
|
+
class AttachmentType(StrEnum):
|
|
4
|
+
"""Defines valid attachment types."""
|
|
5
|
+
AUDIO = 'audio'
|
|
6
|
+
DOCUMENT = 'document'
|
|
7
|
+
IMAGE = 'image'
|
|
8
|
+
VIDEO = 'video'
|
|
9
|
+
|
|
10
|
+
class EmitDataType(StrEnum):
|
|
11
|
+
"""Defines valid data types for emitting events."""
|
|
12
|
+
ACTIVITY = 'activity'
|
|
13
|
+
CODE = 'code'
|
|
14
|
+
CODE_START = 'code_start'
|
|
15
|
+
CODE_END = 'code_end'
|
|
16
|
+
THINKING = 'thinking'
|
|
17
|
+
THINKING_START = 'thinking_start'
|
|
18
|
+
THINKING_END = 'thinking_end'
|
|
19
|
+
|
|
20
|
+
class MessageRole(StrEnum):
|
|
21
|
+
"""Defines valid message roles."""
|
|
22
|
+
SYSTEM = 'system'
|
|
23
|
+
USER = 'user'
|
|
24
|
+
ASSISTANT = 'assistant'
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
from gllm_core.schema import Chunk as Chunk
|
|
2
|
+
from gllm_inference.schema.code_exec_result import CodeExecResult as CodeExecResult
|
|
3
|
+
from gllm_inference.schema.reasoning import Reasoning as Reasoning
|
|
4
|
+
from gllm_inference.schema.token_usage import TokenUsage as TokenUsage
|
|
5
|
+
from gllm_inference.schema.tool_call import ToolCall as ToolCall
|
|
6
|
+
from pydantic import BaseModel
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
class LMOutput(BaseModel):
|
|
10
|
+
"""Defines the output of a language model.
|
|
11
|
+
|
|
12
|
+
Attributes:
|
|
13
|
+
response (str): The text response. Defaults to an empty string.
|
|
14
|
+
tool_calls (list[ToolCall]): The tool calls, if the language model decides to invoke tools.
|
|
15
|
+
Defaults to an empty list.
|
|
16
|
+
structured_output (dict[str, Any] | BaseModel | None): The structured output, if a response schema is defined
|
|
17
|
+
for the language model. Defaults to None.
|
|
18
|
+
token_usage (TokenUsage | None): The token usage analytics, if requested. Defaults to None.
|
|
19
|
+
duration (float | None): The duration of the invocation in seconds, if requested. Defaults to None.
|
|
20
|
+
finish_details (dict[str, Any]): The details about how the generation finished, if requested.
|
|
21
|
+
Defaults to an empty dictionary.
|
|
22
|
+
reasoning (list[Reasoning]): The reasoning, if the language model is configured to output reasoning.
|
|
23
|
+
Defaults to an empty list.
|
|
24
|
+
citations (list[Chunk]): The citations, if the language model outputs citations. Defaults to an empty list.
|
|
25
|
+
code_exec_results (list[CodeExecResult]): The code execution results, if the language model decides to
|
|
26
|
+
execute code. Defaults to an empty list.
|
|
27
|
+
"""
|
|
28
|
+
response: str
|
|
29
|
+
tool_calls: list[ToolCall]
|
|
30
|
+
structured_output: dict[str, Any] | BaseModel | None
|
|
31
|
+
token_usage: TokenUsage | None
|
|
32
|
+
duration: float | None
|
|
33
|
+
finish_details: dict[str, Any]
|
|
34
|
+
reasoning: list[Reasoning]
|
|
35
|
+
citations: list[Chunk]
|
|
36
|
+
code_exec_results: list[CodeExecResult]
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
from gllm_inference.schema.enums import MessageRole as MessageRole
|
|
2
|
+
from gllm_inference.schema.type_alias import MessageContent as MessageContent
|
|
3
|
+
from pydantic import BaseModel
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
class Message(BaseModel):
|
|
7
|
+
"""Defines a message schema to be used as inputs for a language model.
|
|
8
|
+
|
|
9
|
+
Attributes:
|
|
10
|
+
role (MessageRole): The role of the message.
|
|
11
|
+
contents (list[MessageContent]): The contents of the message.
|
|
12
|
+
metadata (dict[str, Any]): The metadata of the message.
|
|
13
|
+
"""
|
|
14
|
+
role: MessageRole
|
|
15
|
+
contents: list[MessageContent]
|
|
16
|
+
metadata: dict[str, Any]
|
|
17
|
+
@classmethod
|
|
18
|
+
def system(cls, contents: MessageContent | list[MessageContent], metadata: dict[str, Any] | None = None) -> Message:
|
|
19
|
+
"""Create a system message.
|
|
20
|
+
|
|
21
|
+
Args:
|
|
22
|
+
contents (MessageContent | list[MessageContent]): The message contents.
|
|
23
|
+
If a single content is provided, it will be wrapped in a list.
|
|
24
|
+
metadata (dict[str, Any], optional): Additional metadata for the message. Defaults to None.
|
|
25
|
+
|
|
26
|
+
Returns:
|
|
27
|
+
Message: A new message with SYSTEM role.
|
|
28
|
+
"""
|
|
29
|
+
@classmethod
|
|
30
|
+
def user(cls, contents: MessageContent | list[MessageContent], metadata: dict[str, Any] | None = None) -> Message:
|
|
31
|
+
"""Create a user message.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
contents (MessageContent | list[MessageContent]): The message contents.
|
|
35
|
+
If a single content is provided, it will be wrapped in a list.
|
|
36
|
+
metadata (dict[str, Any], optional): Additional metadata for the message. Defaults to None.
|
|
37
|
+
|
|
38
|
+
Returns:
|
|
39
|
+
Message: A new message with USER role.
|
|
40
|
+
"""
|
|
41
|
+
@classmethod
|
|
42
|
+
def assistant(cls, contents: MessageContent | list[MessageContent], metadata: dict[str, Any] | None = None) -> Message:
|
|
43
|
+
"""Create an assistant message.
|
|
44
|
+
|
|
45
|
+
Args:
|
|
46
|
+
contents (MessageContent | list[MessageContent]): The message contents.
|
|
47
|
+
If a single content is provided, it will be wrapped in a list.
|
|
48
|
+
metadata (dict[str, Any], optional): Additional metadata for the message. Defaults to None.
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
Message: A new message with ASSISTANT role.
|
|
52
|
+
"""
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
from enum import StrEnum
|
|
2
|
+
from gllm_inference.utils import validate_string_enum as validate_string_enum
|
|
3
|
+
from pydantic import BaseModel
|
|
4
|
+
|
|
5
|
+
PROVIDER_SEPARATOR: str
|
|
6
|
+
PATH_SEPARATOR: str
|
|
7
|
+
URL_NAME_REGEX_PATTERN: str
|
|
8
|
+
|
|
9
|
+
class ModelProvider(StrEnum):
|
|
10
|
+
"""Defines the supported model providers."""
|
|
11
|
+
ANTHROPIC = 'anthropic'
|
|
12
|
+
AZURE_OPENAI = 'azure-openai'
|
|
13
|
+
BEDROCK = 'bedrock'
|
|
14
|
+
DATASAUR = 'datasaur'
|
|
15
|
+
GOOGLE = 'google'
|
|
16
|
+
LANGCHAIN = 'langchain'
|
|
17
|
+
LITELLM = 'litellm'
|
|
18
|
+
OPENAI = 'openai'
|
|
19
|
+
OPENAI_COMPATIBLE = 'openai-compatible'
|
|
20
|
+
TWELVELABS = 'twelvelabs'
|
|
21
|
+
VOYAGE = 'voyage'
|
|
22
|
+
|
|
23
|
+
class ModelId(BaseModel):
|
|
24
|
+
'''Defines a representation of a valid model id.
|
|
25
|
+
|
|
26
|
+
Attributes:
|
|
27
|
+
provider (ModelProvider): The provider of the model.
|
|
28
|
+
name (str | None): The name of the model.
|
|
29
|
+
path (str | None): The path of the model.
|
|
30
|
+
|
|
31
|
+
Provider-specific examples:
|
|
32
|
+
# Using Anthropic
|
|
33
|
+
```python
|
|
34
|
+
model_id = ModelId.from_string("anthropic/claude-3-5-sonnet-latest")
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
# Using Bedrock
|
|
38
|
+
```python
|
|
39
|
+
model_id = ModelId.from_string("bedrock/us.anthropic.claude-sonnet-4-20250514-v1:0")
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
# Using Datasaur
|
|
43
|
+
```python
|
|
44
|
+
model_id = ModelId.from_string("datasaur/https://deployment.datasaur.ai/api/deployment/teamId/deploymentId/")
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
# Using Google
|
|
48
|
+
```python
|
|
49
|
+
model_id = ModelId.from_string("google/gemini-1.5-flash")
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
# Using OpenAI
|
|
53
|
+
```python
|
|
54
|
+
model_id = ModelId.from_string("openai/gpt-4o-mini")
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
# Using Azure OpenAI
|
|
58
|
+
```python
|
|
59
|
+
model_id = ModelId.from_string("azure-openai/https://my-resource.openai.azure.com:my-deployment")
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
# Using OpenAI compatible endpoints (e.g. Groq)
|
|
63
|
+
```python
|
|
64
|
+
model_id = ModelId.from_string("openai-compatible/https://api.groq.com/openai/v1:llama3-8b-8192")
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
# Using Voyage
|
|
68
|
+
```python
|
|
69
|
+
model_id = ModelId.from_string("voyage/voyage-3.5-lite")
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
# Using TwelveLabs
|
|
73
|
+
```python
|
|
74
|
+
model_id = ModelId.from_string("twelvelabs/Marengo-retrieval-2.7")
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
# Using LangChain
|
|
78
|
+
```python
|
|
79
|
+
model_id = ModelId.from_string("langchain/langchain_openai.ChatOpenAI:gpt-4o-mini")
|
|
80
|
+
```
|
|
81
|
+
For the list of supported providers, please refer to the following table:
|
|
82
|
+
https://python.langchain.com/docs/integrations/chat/#featured-providers
|
|
83
|
+
|
|
84
|
+
# Using LiteLLM
|
|
85
|
+
```python
|
|
86
|
+
model_id = ModelId.from_string("litellm/openai/gpt-4o-mini")
|
|
87
|
+
```
|
|
88
|
+
For the list of supported providers, please refer to the following page:
|
|
89
|
+
https://docs.litellm.ai/docs/providers/
|
|
90
|
+
|
|
91
|
+
Custom model name validation example:
|
|
92
|
+
```python
|
|
93
|
+
validation_map = {
|
|
94
|
+
ModelProvider.ANTHROPIC: {"claude-3-5-sonnet-latest"},
|
|
95
|
+
ModelProvider.GOOGLE: {"gemini-1.5-flash", "gemini-1.5-pro"},
|
|
96
|
+
ModelProvider.OPENAI: {"gpt-4o", "gpt-4o-mini"},
|
|
97
|
+
|
|
98
|
+
model_id = ModelId.from_string("...", validation_map)
|
|
99
|
+
```
|
|
100
|
+
'''
|
|
101
|
+
provider: ModelProvider
|
|
102
|
+
name: str | None
|
|
103
|
+
path: str | None
|
|
104
|
+
@classmethod
|
|
105
|
+
def from_string(cls, model_id: str, validation_map: dict[str, set[str]] | None = None) -> ModelId:
|
|
106
|
+
"""Parse a model id string into a ModelId object.
|
|
107
|
+
|
|
108
|
+
Args:
|
|
109
|
+
model_id (str): The model id to parse. Must be in the the following format:
|
|
110
|
+
1. For `azure-openai` provider: `azure-openai/azure-endpoint:azure-deployment`.
|
|
111
|
+
2. For `openai-compatible` provider: `openai-compatible/base-url:model-name`.
|
|
112
|
+
3. For `langchain` provider: `langchain/<package>.<class>:model-name`.
|
|
113
|
+
4. For `litellm` provider: `litellm/provider/model-name`.
|
|
114
|
+
5. For `datasaur` provider: `datasaur/base-url`.
|
|
115
|
+
6. For other providers: `provider/model-name`.
|
|
116
|
+
validation_map (dict[str, set[str]] | None, optional): An optional dictionary that maps provider names to
|
|
117
|
+
sets of valid model names. For the defined model providers, the model names will be validated against
|
|
118
|
+
the set of valid model names. For the undefined model providers, the model name will not be validated.
|
|
119
|
+
Defaults to None.
|
|
120
|
+
|
|
121
|
+
Returns:
|
|
122
|
+
ModelId: The parsed ModelId object.
|
|
123
|
+
|
|
124
|
+
Raises:
|
|
125
|
+
ValueError: If the provided model id is invalid or if the model name is not valid for the provider.
|
|
126
|
+
"""
|
|
127
|
+
def to_string(self) -> str:
|
|
128
|
+
"""Convert the ModelId object to a string.
|
|
129
|
+
|
|
130
|
+
Returns:
|
|
131
|
+
str: The string representation of the ModelId object. The format is as follows:
|
|
132
|
+
1. For `azure-openai` provider: `azure-openai/azure-endpoint:azure-deployment`.
|
|
133
|
+
2. For `openai-compatible` provider: `openai-compatible/base-url:model-name`.
|
|
134
|
+
3. For `langchain` provider: `langchain/<package>.<class>:model-name`.
|
|
135
|
+
4. For `litellm` provider: `litellm/provider/model-name`.
|
|
136
|
+
5. For `datasaur` provider: `datasaur/base-url`.
|
|
137
|
+
6. For other providers: `provider/model-name`.
|
|
138
|
+
"""
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from pydantic import BaseModel
|
|
2
|
+
|
|
3
|
+
class Reasoning(BaseModel):
|
|
4
|
+
"""Defines a reasoning output when a language model is configured to use reasoning.
|
|
5
|
+
|
|
6
|
+
Attributes:
|
|
7
|
+
id (str): The ID of the reasoning output. Defaults to an empty string.
|
|
8
|
+
reasoning (str): The reasoning text. Defaults to an empty string.
|
|
9
|
+
type (str): The type of the reasoning output. Defaults to an empty string.
|
|
10
|
+
data (str): The additional data of the reasoning output. Defaults to an empty string.
|
|
11
|
+
"""
|
|
12
|
+
id: str
|
|
13
|
+
reasoning: str
|
|
14
|
+
type: str
|
|
15
|
+
data: str
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from pydantic import BaseModel
|
|
2
|
+
|
|
3
|
+
class TokenUsage(BaseModel):
|
|
4
|
+
"""Defines the token usage data structure of a language model.
|
|
5
|
+
|
|
6
|
+
Attributes:
|
|
7
|
+
input_tokens (int): The number of input tokens.
|
|
8
|
+
output_tokens (int): The number of output tokens.
|
|
9
|
+
"""
|
|
10
|
+
input_tokens: int
|
|
11
|
+
output_tokens: int
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from pydantic import BaseModel
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
class ToolCall(BaseModel):
|
|
5
|
+
"""Defines a tool call request when a language model decides to invoke a tool.
|
|
6
|
+
|
|
7
|
+
Attributes:
|
|
8
|
+
id (str): The ID of the tool call.
|
|
9
|
+
name (str): The name of the tool.
|
|
10
|
+
args (dict[str, Any]): The arguments of the tool call.
|
|
11
|
+
"""
|
|
12
|
+
id: str
|
|
13
|
+
name: str
|
|
14
|
+
args: dict[str, Any]
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from pydantic import BaseModel
|
|
2
|
+
|
|
3
|
+
class ToolResult(BaseModel):
|
|
4
|
+
"""Defines a tool result to be sent back to the language model.
|
|
5
|
+
|
|
6
|
+
Attributes:
|
|
7
|
+
id (str): The ID of the tool call.
|
|
8
|
+
output (str): The output of the tool call.
|
|
9
|
+
"""
|
|
10
|
+
id: str
|
|
11
|
+
output: str
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from aiohttp import ClientResponse
|
|
2
|
+
from gllm_inference.schema.code_exec_result import Attachment as Attachment
|
|
3
|
+
from gllm_inference.schema.reasoning import Reasoning as Reasoning
|
|
4
|
+
from gllm_inference.schema.tool_call import ToolCall as ToolCall
|
|
5
|
+
from gllm_inference.schema.tool_result import ToolResult as ToolResult
|
|
6
|
+
from httpx import Response as HttpxResponse
|
|
7
|
+
from pydantic import BaseModel
|
|
8
|
+
from requests import Response
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
ErrorResponse = Response | HttpxResponse | ClientResponse | str | dict[str, Any]
|
|
12
|
+
ResponseSchema = dict[str, Any] | type[BaseModel]
|
|
13
|
+
MessageContent = str | Attachment | ToolCall | ToolResult | Reasoning
|
|
14
|
+
EMContent = str | Attachment | tuple[str | Attachment, ...]
|
|
15
|
+
Vector = list[float]
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
from gllm_inference.utils.langchain import load_langchain_model as load_langchain_model, parse_model_data as parse_model_data
|
|
2
|
+
from gllm_inference.utils.validation import validate_string_enum as validate_string_enum
|
|
3
|
+
|
|
4
|
+
__all__ = ['load_langchain_model', 'parse_model_data', 'validate_string_enum']
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from _typeshed import Incomplete
|
|
2
|
+
from langchain_core.embeddings import Embeddings as Embeddings
|
|
3
|
+
from langchain_core.language_models import BaseChatModel as BaseChatModel
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
MODEL_NAME_KEYS: Incomplete
|
|
7
|
+
|
|
8
|
+
def load_langchain_model(model_class_path: str, model_name: str, model_kwargs: dict[str, Any]) -> BaseChatModel | Embeddings:
|
|
9
|
+
'''Loads the LangChain\'s model instance.
|
|
10
|
+
|
|
11
|
+
Args:
|
|
12
|
+
model_class_path (str): The path to the LangChain\'s class, e.g. "langchain_openai.ChatOpenAI".
|
|
13
|
+
model_name (str): The model name.
|
|
14
|
+
model_kwargs (dict[str, Any]): The additional keyword arguments.
|
|
15
|
+
|
|
16
|
+
Returns:
|
|
17
|
+
BaseChatModel | Embeddings: The LangChain\'s model instance.
|
|
18
|
+
'''
|
|
19
|
+
def parse_model_data(model: BaseChatModel | Embeddings) -> dict[str, str]:
|
|
20
|
+
"""Parses the model data from LangChain's BaseChatModel or Embeddings instance.
|
|
21
|
+
|
|
22
|
+
Args:
|
|
23
|
+
model (BaseChatModel | Embeddings): The LangChain's BaseChatModel or Embeddings instance.
|
|
24
|
+
|
|
25
|
+
Returns:
|
|
26
|
+
dict[str, str]: The dictionary containing the model name and path.
|
|
27
|
+
|
|
28
|
+
Raises:
|
|
29
|
+
ValueError: If the model name is not found in the model data.
|
|
30
|
+
"""
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from enum import StrEnum
|
|
2
|
+
|
|
3
|
+
def validate_string_enum(enum_type: type[StrEnum], value: str) -> None:
|
|
4
|
+
"""Validates that the provided value is a valid string enum value.
|
|
5
|
+
|
|
6
|
+
Args:
|
|
7
|
+
enum_type (type[StrEnum]): The type of the string enum.
|
|
8
|
+
value (str): The value to validate.
|
|
9
|
+
|
|
10
|
+
Raises:
|
|
11
|
+
ValueError: If the provided value is not a valid string enum value.
|
|
12
|
+
"""
|