gllm-inference-binary 0.5.51b3__cp313-cp313-macosx_11_0_arm64.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.
- gllm_inference/__init__.pyi +0 -0
- gllm_inference/builder/__init__.pyi +6 -0
- gllm_inference/builder/_build_invoker.pyi +28 -0
- gllm_inference/builder/build_em_invoker.pyi +130 -0
- gllm_inference/builder/build_lm_invoker.pyi +213 -0
- gllm_inference/builder/build_lm_request_processor.pyi +88 -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 +13 -0
- gllm_inference/em_invoker/__init__.pyi +12 -0
- gllm_inference/em_invoker/azure_openai_em_invoker.pyi +88 -0
- gllm_inference/em_invoker/bedrock_em_invoker.pyi +118 -0
- gllm_inference/em_invoker/cohere_em_invoker.pyi +128 -0
- gllm_inference/em_invoker/em_invoker.pyi +90 -0
- gllm_inference/em_invoker/google_em_invoker.pyi +129 -0
- gllm_inference/em_invoker/jina_em_invoker.pyi +103 -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 +41 -0
- gllm_inference/em_invoker/openai_em_invoker.pyi +118 -0
- gllm_inference/em_invoker/schema/__init__.pyi +0 -0
- gllm_inference/em_invoker/schema/bedrock.pyi +29 -0
- gllm_inference/em_invoker/schema/cohere.pyi +20 -0
- gllm_inference/em_invoker/schema/google.pyi +9 -0
- gllm_inference/em_invoker/schema/jina.pyi +29 -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 +24 -0
- gllm_inference/lm_invoker/__init__.pyi +14 -0
- gllm_inference/lm_invoker/anthropic_lm_invoker.pyi +334 -0
- gllm_inference/lm_invoker/azure_openai_lm_invoker.pyi +254 -0
- gllm_inference/lm_invoker/batch/__init__.pyi +3 -0
- gllm_inference/lm_invoker/batch/batch_operations.pyi +127 -0
- gllm_inference/lm_invoker/bedrock_lm_invoker.pyi +226 -0
- gllm_inference/lm_invoker/datasaur_lm_invoker.pyi +157 -0
- gllm_inference/lm_invoker/google_lm_invoker.pyi +336 -0
- gllm_inference/lm_invoker/langchain_lm_invoker.pyi +251 -0
- gllm_inference/lm_invoker/litellm_lm_invoker.pyi +244 -0
- gllm_inference/lm_invoker/lm_invoker.pyi +164 -0
- gllm_inference/lm_invoker/openai_chat_completions_lm_invoker.pyi +272 -0
- gllm_inference/lm_invoker/openai_compatible_lm_invoker.pyi +52 -0
- gllm_inference/lm_invoker/openai_lm_invoker.pyi +433 -0
- gllm_inference/lm_invoker/portkey_lm_invoker.pyi +260 -0
- gllm_inference/lm_invoker/schema/__init__.pyi +0 -0
- gllm_inference/lm_invoker/schema/anthropic.pyi +56 -0
- gllm_inference/lm_invoker/schema/bedrock.pyi +53 -0
- gllm_inference/lm_invoker/schema/datasaur.pyi +14 -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 +106 -0
- gllm_inference/lm_invoker/schema/openai_chat_completions.pyi +62 -0
- gllm_inference/lm_invoker/schema/portkey.pyi +31 -0
- gllm_inference/lm_invoker/schema/xai.pyi +31 -0
- gllm_inference/lm_invoker/xai_lm_invoker.pyi +289 -0
- gllm_inference/model/__init__.pyi +12 -0
- gllm_inference/model/em/__init__.pyi +0 -0
- gllm_inference/model/em/cohere_em.pyi +17 -0
- gllm_inference/model/em/google_em.pyi +16 -0
- gllm_inference/model/em/jina_em.pyi +22 -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 +22 -0
- gllm_inference/model/lm/google_lm.pyi +18 -0
- gllm_inference/model/lm/openai_lm.pyi +27 -0
- gllm_inference/model/lm/xai_lm.pyi +19 -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/format_strategy/__init__.pyi +4 -0
- gllm_inference/prompt_builder/format_strategy/format_strategy.pyi +55 -0
- gllm_inference/prompt_builder/format_strategy/jinja_format_strategy.pyi +45 -0
- gllm_inference/prompt_builder/format_strategy/string_format_strategy.pyi +20 -0
- gllm_inference/prompt_builder/prompt_builder.pyi +69 -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/realtime_chat/__init__.pyi +3 -0
- gllm_inference/realtime_chat/google_realtime_chat.pyi +205 -0
- gllm_inference/realtime_chat/input_streamer/__init__.pyi +4 -0
- gllm_inference/realtime_chat/input_streamer/input_streamer.pyi +36 -0
- gllm_inference/realtime_chat/input_streamer/keyboard_input_streamer.pyi +27 -0
- gllm_inference/realtime_chat/input_streamer/linux_mic_input_streamer.pyi +36 -0
- gllm_inference/realtime_chat/output_streamer/__init__.pyi +4 -0
- gllm_inference/realtime_chat/output_streamer/console_output_streamer.pyi +21 -0
- gllm_inference/realtime_chat/output_streamer/linux_speaker_output_streamer.pyi +42 -0
- gllm_inference/realtime_chat/output_streamer/output_streamer.pyi +33 -0
- gllm_inference/realtime_chat/realtime_chat.pyi +28 -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 +18 -0
- gllm_inference/schema/activity.pyi +64 -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 +69 -0
- gllm_inference/schema/events.pyi +105 -0
- gllm_inference/schema/lm_input.pyi +4 -0
- gllm_inference/schema/lm_output.pyi +44 -0
- gllm_inference/schema/mcp.pyi +31 -0
- gllm_inference/schema/message.pyi +52 -0
- gllm_inference/schema/model_id.pyi +176 -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-313-darwin.so +0 -0
- gllm_inference.pyi +157 -0
- gllm_inference_binary-0.5.51b3.dist-info/METADATA +138 -0
- gllm_inference_binary-0.5.51b3.dist-info/RECORD +137 -0
- gllm_inference_binary-0.5.51b3.dist-info/WHEEL +5 -0
- gllm_inference_binary-0.5.51b3.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
from gllm_inference.exceptions import InvokerRuntimeError as InvokerRuntimeError
|
|
2
|
+
from gllm_inference.schema import BatchStatus as BatchStatus, LMInput as LMInput, LMOutput as LMOutput
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
DEFAULT_STATUS_CHECK_INTERVAL: float
|
|
6
|
+
|
|
7
|
+
class BatchOperations:
|
|
8
|
+
"""Handles batch operations for an LM invoker.
|
|
9
|
+
|
|
10
|
+
This class provides a wrapper around the batch operations of an LM invoker.
|
|
11
|
+
It provides a simple interface to perform batch invocation:
|
|
12
|
+
```python
|
|
13
|
+
results = await lm_invoker.batch.invoke(...)
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Additionally, it also supports the following standalone batch operations:
|
|
17
|
+
|
|
18
|
+
1. Create a batch job:
|
|
19
|
+
```python
|
|
20
|
+
batch_id = await lm_invoker.batch.create(...)
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
2. Get the status of a batch job:
|
|
24
|
+
```python
|
|
25
|
+
status = await lm_invoker.batch.status(batch_id)
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
3. Retrieve the results of a batch job:
|
|
29
|
+
```python
|
|
30
|
+
results = await lm_invoker.batch.retrieve(batch_id)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
4. List the batch jobs:
|
|
34
|
+
```python
|
|
35
|
+
batch_jobs = await lm_invoker.batch.list()
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
5. Cancel a batch job:
|
|
39
|
+
```python
|
|
40
|
+
await lm_invoker.batch.cancel(batch_id)
|
|
41
|
+
```
|
|
42
|
+
"""
|
|
43
|
+
def __init__(self, invoker: BaseLMInvoker) -> None:
|
|
44
|
+
"""Initializes the batch operations.
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
invoker (BaseLMInvoker): The LM invoker to use for the batch operations.
|
|
48
|
+
"""
|
|
49
|
+
async def invoke(self, requests: dict[str, LMInput], hyperparameters: dict[str, Any] | None = None, status_check_interval: float = ..., max_iterations: int | None = None) -> dict[str, LMOutput]:
|
|
50
|
+
"""Invokes the language model in batch mode.
|
|
51
|
+
|
|
52
|
+
This method orchestrates the entire batch invocation process, including;
|
|
53
|
+
1. Creating a batch job.
|
|
54
|
+
2. Iteratively checking the status of the batch job until it is finished.
|
|
55
|
+
3. Retrieving the results of the batch job.
|
|
56
|
+
The method includes retry logic with exponential backoff for transient failures.
|
|
57
|
+
|
|
58
|
+
Args:
|
|
59
|
+
requests (dict[str, LMInput]): The dictionary of requests that maps request ID to the request.
|
|
60
|
+
Each request must be a valid input for the language model.
|
|
61
|
+
1. If the request is a list of Message objects, it is used as is.
|
|
62
|
+
2. If the request is a list of MessageContent or a string, it is converted into a user message.
|
|
63
|
+
hyperparameters (dict[str, Any] | None, optional): A dictionary of hyperparameters for the language model.
|
|
64
|
+
Defaults to None, in which case the default hyperparameters are used.
|
|
65
|
+
status_check_interval (float, optional): The interval in seconds to check the status of the batch job.
|
|
66
|
+
Defaults to DEFAULT_STATUS_CHECK_INTERVAL.
|
|
67
|
+
max_iterations (int | None, optional): The maximum number of iterations to check the status of the batch
|
|
68
|
+
job. Defaults to None, in which case the number of iterations is infinite.
|
|
69
|
+
|
|
70
|
+
Returns:
|
|
71
|
+
dict[str, LMOutput]: The results of the batch job.
|
|
72
|
+
|
|
73
|
+
Raises:
|
|
74
|
+
CancelledError: If the invocation is cancelled.
|
|
75
|
+
ModelNotFoundError: If the model is not found.
|
|
76
|
+
ProviderAuthError: If the model authentication fails.
|
|
77
|
+
ProviderInternalError: If the model internal error occurs.
|
|
78
|
+
ProviderInvalidArgsError: If the model parameters are invalid.
|
|
79
|
+
ProviderOverloadedError: If the model is overloaded.
|
|
80
|
+
ProviderRateLimitError: If the model rate limit is exceeded.
|
|
81
|
+
TimeoutError: If the invocation times out.
|
|
82
|
+
ValueError: If the messages are not in the correct format.
|
|
83
|
+
"""
|
|
84
|
+
async def create(self, requests: dict[str, LMInput], hyperparameters: dict[str, Any] | None = None) -> str:
|
|
85
|
+
"""Creates a new batch job.
|
|
86
|
+
|
|
87
|
+
Args:
|
|
88
|
+
requests (dict[str, LMInput]): The dictionary of requests that maps request ID to the request.
|
|
89
|
+
Each request must be a valid input for the language model.
|
|
90
|
+
1. If the request is a list of Message objects, it is used as is.
|
|
91
|
+
2. If the request is a list of MessageContent or a string, it is converted into a user message.
|
|
92
|
+
hyperparameters (dict[str, Any] | None, optional): A dictionary of hyperparameters for the language model.
|
|
93
|
+
Defaults to None, in which case the default hyperparameters are used.
|
|
94
|
+
|
|
95
|
+
Returns:
|
|
96
|
+
str: The ID of the batch job.
|
|
97
|
+
"""
|
|
98
|
+
async def status(self, batch_id: str) -> BatchStatus:
|
|
99
|
+
"""Gets the status of a batch job.
|
|
100
|
+
|
|
101
|
+
Args:
|
|
102
|
+
batch_id (str): The ID of the batch job to get the status of.
|
|
103
|
+
|
|
104
|
+
Returns:
|
|
105
|
+
BatchStatus: The status of the batch job.
|
|
106
|
+
"""
|
|
107
|
+
async def retrieve(self, batch_id: str) -> dict[str, LMOutput]:
|
|
108
|
+
"""Retrieves the results of a batch job.
|
|
109
|
+
|
|
110
|
+
Args:
|
|
111
|
+
batch_id (str): The ID of the batch job to get the results of.
|
|
112
|
+
|
|
113
|
+
Returns:
|
|
114
|
+
dict[str, LMOutput]: The results of the batch job.
|
|
115
|
+
"""
|
|
116
|
+
async def list(self) -> list[dict[str, Any]]:
|
|
117
|
+
"""Lists the batch jobs.
|
|
118
|
+
|
|
119
|
+
Returns:
|
|
120
|
+
list[dict[str, Any]]: The list of batch jobs.
|
|
121
|
+
"""
|
|
122
|
+
async def cancel(self, batch_id: str) -> None:
|
|
123
|
+
"""Cancels a batch job.
|
|
124
|
+
|
|
125
|
+
Args:
|
|
126
|
+
batch_id (str): The ID of the batch job to cancel.
|
|
127
|
+
"""
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
from _typeshed import Incomplete
|
|
2
|
+
from gllm_core.event import EventEmitter as EventEmitter
|
|
3
|
+
from gllm_core.schema.tool import Tool as Tool
|
|
4
|
+
from gllm_core.utils.retry import RetryConfig as RetryConfig
|
|
5
|
+
from gllm_inference.exceptions import BaseInvokerError as BaseInvokerError, convert_http_status_to_base_invoker_error as convert_http_status_to_base_invoker_error
|
|
6
|
+
from gllm_inference.exceptions.provider_error_map import BEDROCK_ERROR_MAPPING as BEDROCK_ERROR_MAPPING
|
|
7
|
+
from gllm_inference.lm_invoker.lm_invoker import BaseLMInvoker as BaseLMInvoker
|
|
8
|
+
from gllm_inference.lm_invoker.schema.bedrock import InputType as InputType, Key as Key, OutputType as OutputType
|
|
9
|
+
from gllm_inference.schema import Attachment as Attachment, AttachmentType as AttachmentType, LMOutput as LMOutput, Message as Message, ModelId as ModelId, ModelProvider as ModelProvider, ResponseSchema as ResponseSchema, TokenUsage as TokenUsage, ToolCall as ToolCall, ToolResult as ToolResult
|
|
10
|
+
from langchain_core.tools import Tool as LangChainTool
|
|
11
|
+
from typing import Any
|
|
12
|
+
|
|
13
|
+
FILENAME_SANITIZATION_REGEX: Incomplete
|
|
14
|
+
SUPPORTED_ATTACHMENTS: Incomplete
|
|
15
|
+
|
|
16
|
+
class BedrockLMInvoker(BaseLMInvoker):
|
|
17
|
+
'''A language model invoker to interact with AWS Bedrock language models.
|
|
18
|
+
|
|
19
|
+
Attributes:
|
|
20
|
+
model_id (str): The model ID of the language model.
|
|
21
|
+
model_provider (str): The provider of the language model.
|
|
22
|
+
model_name (str): The name of the language model.
|
|
23
|
+
session (Session): The Bedrock client session.
|
|
24
|
+
client_kwargs (dict[str, Any]): The Bedrock client kwargs.
|
|
25
|
+
default_hyperparameters (dict[str, Any]): Default hyperparameters for invoking the model.
|
|
26
|
+
tools (list[Tool]): Tools provided to the model to enable tool calling.
|
|
27
|
+
response_schema (ResponseSchema | None): The schema of the response. If provided, the model will output a
|
|
28
|
+
structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema dictionary.
|
|
29
|
+
output_analytics (bool): Whether to output the invocation analytics.
|
|
30
|
+
retry_config (RetryConfig): The retry configuration for the language model.
|
|
31
|
+
|
|
32
|
+
Basic usage:
|
|
33
|
+
The `BedrockLMInvoker` can be used as follows:
|
|
34
|
+
```python
|
|
35
|
+
lm_invoker = BedrockLMInvoker(
|
|
36
|
+
model_name="us.anthropic.claude-sonnet-4-20250514-v1:0",
|
|
37
|
+
aws_access_key_id="<your-aws-access-key-id>",
|
|
38
|
+
aws_secret_access_key="<your-aws-secret-access-key>",
|
|
39
|
+
)
|
|
40
|
+
result = await lm_invoker.invoke("Hi there!")
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Input types:
|
|
44
|
+
The `BedrockLMInvoker` supports the following input types: text, document, image, and video.
|
|
45
|
+
Non-text inputs can be passed as an `Attachment` object with the `user` role.
|
|
46
|
+
|
|
47
|
+
Usage example:
|
|
48
|
+
```python
|
|
49
|
+
text = "What animal is in this image?"
|
|
50
|
+
image = Attachment.from_path("path/to/local/image.png")
|
|
51
|
+
result = await lm_invoker.invoke([text, image])
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Tool calling:
|
|
55
|
+
Tool calling is a feature that allows the language model to call tools to perform tasks.
|
|
56
|
+
Tools can be passed to the via the `tools` parameter as a list of `Tool` objects.
|
|
57
|
+
When tools are provided and the model decides to call a tool, the tool calls are stored in the
|
|
58
|
+
`tool_calls` attribute in the output.
|
|
59
|
+
|
|
60
|
+
Usage example:
|
|
61
|
+
```python
|
|
62
|
+
lm_invoker = BedrockLMInvoker(..., tools=[tool_1, tool_2])
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Output example:
|
|
66
|
+
```python
|
|
67
|
+
LMOutput(
|
|
68
|
+
response="Let me call the tools...",
|
|
69
|
+
tool_calls=[
|
|
70
|
+
ToolCall(id="123", name="tool_1", args={"key": "value"}),
|
|
71
|
+
ToolCall(id="456", name="tool_2", args={"key": "value"}),
|
|
72
|
+
]
|
|
73
|
+
)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Structured output:
|
|
77
|
+
Structured output is a feature that allows the language model to output a structured response.
|
|
78
|
+
This feature can be enabled by providing a schema to the `response_schema` parameter.
|
|
79
|
+
|
|
80
|
+
The schema must be either a JSON schema dictionary or a Pydantic BaseModel class.
|
|
81
|
+
If JSON schema is used, it must be compatible with Pydantic\'s JSON schema, especially for complex schemas.
|
|
82
|
+
For this reason, it is recommended to create the JSON schema using Pydantic\'s `model_json_schema` method.
|
|
83
|
+
|
|
84
|
+
Structured output is achieved by providing the schema name in the `tool_choice` parameter. This forces
|
|
85
|
+
the model to call the provided schema as a tool. Thus, structured output is not compatible with tool calling,
|
|
86
|
+
since the tool calling is reserved to force the model to call the provided schema as a tool.
|
|
87
|
+
The language model also doesn\'t need to stream anything when structured output is enabled. Thus, standard
|
|
88
|
+
invocation will be performed regardless of whether the `event_emitter` parameter is provided or not.
|
|
89
|
+
|
|
90
|
+
When enabled, the structured output is stored in the `structured_output` attribute in the output.
|
|
91
|
+
1. If the schema is a JSON schema dictionary, the structured output is a dictionary.
|
|
92
|
+
2. If the schema is a Pydantic BaseModel class, the structured output is a Pydantic model.
|
|
93
|
+
|
|
94
|
+
# Example 1: Using a JSON schema dictionary
|
|
95
|
+
Usage example:
|
|
96
|
+
```python
|
|
97
|
+
schema = {
|
|
98
|
+
"title": "Animal",
|
|
99
|
+
"description": "A description of an animal.",
|
|
100
|
+
"properties": {
|
|
101
|
+
"color": {"title": "Color", "type": "string"},
|
|
102
|
+
"name": {"title": "Name", "type": "string"},
|
|
103
|
+
},
|
|
104
|
+
"required": ["name", "color"],
|
|
105
|
+
"type": "object",
|
|
106
|
+
}
|
|
107
|
+
lm_invoker = BedrockLMInvoker(..., response_schema=schema)
|
|
108
|
+
```
|
|
109
|
+
Output example:
|
|
110
|
+
```python
|
|
111
|
+
LMOutput(structured_output={"name": "Golden retriever", "color": "Golden"})
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
# Example 2: Using a Pydantic BaseModel class
|
|
115
|
+
Usage example:
|
|
116
|
+
```python
|
|
117
|
+
class Animal(BaseModel):
|
|
118
|
+
name: str
|
|
119
|
+
color: str
|
|
120
|
+
|
|
121
|
+
lm_invoker = BedrockLMInvoker(..., response_schema=Animal)
|
|
122
|
+
```
|
|
123
|
+
Output example:
|
|
124
|
+
```python
|
|
125
|
+
LMOutput(structured_output=Animal(name="Golden retriever", color="Golden"))
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Analytics tracking:
|
|
129
|
+
Analytics tracking is a feature that allows the module to output additional information about the invocation.
|
|
130
|
+
This feature can be enabled by setting the `output_analytics` parameter to `True`.
|
|
131
|
+
When enabled, the following attributes will be stored in the output:
|
|
132
|
+
1. `token_usage`: The token usage.
|
|
133
|
+
2. `duration`: The duration in seconds.
|
|
134
|
+
3. `finish_details`: The details about how the generation finished.
|
|
135
|
+
|
|
136
|
+
Output example:
|
|
137
|
+
```python
|
|
138
|
+
LMOutput(
|
|
139
|
+
response="Golden retriever is a good dog breed.",
|
|
140
|
+
token_usage=TokenUsage(input_tokens=100, output_tokens=50),
|
|
141
|
+
duration=0.729,
|
|
142
|
+
finish_details={"stop_reason": "end_turn"},
|
|
143
|
+
)
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Retry and timeout:
|
|
147
|
+
The `BedrockLMInvoker` supports retry and timeout configuration.
|
|
148
|
+
By default, the max retries is set to 0 and the timeout is set to 30.0 seconds.
|
|
149
|
+
They can be customized by providing a custom `RetryConfig` object to the `retry_config` parameter.
|
|
150
|
+
|
|
151
|
+
Retry config examples:
|
|
152
|
+
```python
|
|
153
|
+
retry_config = RetryConfig(max_retries=0, timeout=None) # No retry, no timeout
|
|
154
|
+
retry_config = RetryConfig(max_retries=0, timeout=10.0) # No retry, 10.0 seconds timeout
|
|
155
|
+
retry_config = RetryConfig(max_retries=5, timeout=None) # 5 max retries, no timeout
|
|
156
|
+
retry_config = RetryConfig(max_retries=5, timeout=10.0) # 5 max retries, 10.0 seconds timeout
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Usage example:
|
|
160
|
+
```python
|
|
161
|
+
lm_invoker = BedrockLMInvoker(..., retry_config=retry_config)
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Output types:
|
|
165
|
+
The output of the `BedrockLMInvoker` can either be:
|
|
166
|
+
1. `str`: A text response.
|
|
167
|
+
2. `LMOutput`: A Pydantic model that may contain the following attributes:
|
|
168
|
+
2.1. response (str)
|
|
169
|
+
2.2. tool_calls (list[ToolCall])
|
|
170
|
+
2.3. structured_output (dict[str, Any] | BaseModel | None)
|
|
171
|
+
2.4. token_usage (TokenUsage | None)
|
|
172
|
+
2.5. duration (float | None)
|
|
173
|
+
2.6. finish_details (dict[str, Any] | None)
|
|
174
|
+
'''
|
|
175
|
+
session: Incomplete
|
|
176
|
+
client_kwargs: Incomplete
|
|
177
|
+
def __init__(self, model_name: str, access_key_id: str | None = None, secret_access_key: str | None = None, region_name: str = 'us-east-1', model_kwargs: dict[str, Any] | None = None, default_hyperparameters: dict[str, Any] | None = None, tools: list[Tool | LangChainTool] | None = None, response_schema: ResponseSchema | None = None, output_analytics: bool = False, retry_config: RetryConfig | None = None) -> None:
|
|
178
|
+
'''Initializes the BedrockLMInvoker instance.
|
|
179
|
+
|
|
180
|
+
Args:
|
|
181
|
+
model_name (str): The name of the Bedrock language model.
|
|
182
|
+
access_key_id (str | None, optional): The AWS access key ID. Defaults to None, in which case
|
|
183
|
+
the `AWS_ACCESS_KEY_ID` environment variable will be used.
|
|
184
|
+
secret_access_key (str | None, optional): The AWS secret access key. Defaults to None, in which case
|
|
185
|
+
the `AWS_SECRET_ACCESS_KEY` environment variable will be used.
|
|
186
|
+
region_name (str, optional): The AWS region name. Defaults to "us-east-1".
|
|
187
|
+
model_kwargs (dict[str, Any] | None, optional): Additional keyword arguments for the Bedrock client.
|
|
188
|
+
default_hyperparameters (dict[str, Any] | None, optional): Default hyperparameters for invoking the model.
|
|
189
|
+
Defaults to None.
|
|
190
|
+
tools (list[Tool | LangChainTool] | None, optional): Tools provided to the model to enable tool calling.
|
|
191
|
+
Defaults to None.
|
|
192
|
+
response_schema (ResponseSchema | None, optional): The schema of the response. If provided, the model will
|
|
193
|
+
output a structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema
|
|
194
|
+
dictionary. Defaults to None.
|
|
195
|
+
output_analytics (bool, optional): Whether to output the invocation analytics. Defaults to False.
|
|
196
|
+
retry_config (RetryConfig | None, optional): The retry configuration for the language model.
|
|
197
|
+
Defaults to None, in which case a default config with no retry and 30.0 seconds timeout will be used.
|
|
198
|
+
|
|
199
|
+
Raises:
|
|
200
|
+
ValueError: If `response_schema` is provided, but `tools` are also provided.
|
|
201
|
+
ValueError: If `access_key_id` or `secret_access_key` is neither provided nor set in the
|
|
202
|
+
`AWS_ACCESS_KEY_ID` or `AWS_SECRET_ACCESS_KEY` environment variables, respectively.
|
|
203
|
+
'''
|
|
204
|
+
def set_tools(self, tools: list[Tool | LangChainTool]) -> None:
|
|
205
|
+
"""Sets the tools for the Bedrock language model.
|
|
206
|
+
|
|
207
|
+
This method sets the tools for the Bedrock language model. Any existing tools will be replaced.
|
|
208
|
+
|
|
209
|
+
Args:
|
|
210
|
+
tools (list[Tool | LangChainTool]): The list of tools to be used.
|
|
211
|
+
|
|
212
|
+
Raises:
|
|
213
|
+
ValueError: If `response_schema` exists.
|
|
214
|
+
"""
|
|
215
|
+
def set_response_schema(self, response_schema: ResponseSchema | None) -> None:
|
|
216
|
+
"""Sets the response schema for the Bedrock language model.
|
|
217
|
+
|
|
218
|
+
This method sets the response schema for the Bedrock language model. Any existing response schema will be
|
|
219
|
+
replaced.
|
|
220
|
+
|
|
221
|
+
Args:
|
|
222
|
+
response_schema (ResponseSchema | None): The response schema to be used.
|
|
223
|
+
|
|
224
|
+
Raises:
|
|
225
|
+
ValueError: If `tools` exists.
|
|
226
|
+
"""
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
from _typeshed import Incomplete
|
|
2
|
+
from gllm_core.event import EventEmitter as EventEmitter
|
|
3
|
+
from gllm_core.schema.tool import Tool as Tool
|
|
4
|
+
from gllm_core.utils.retry import RetryConfig as RetryConfig
|
|
5
|
+
from gllm_inference.constants import DOCUMENT_MIME_TYPES as DOCUMENT_MIME_TYPES, INVOKER_PROPAGATED_MAX_RETRIES as INVOKER_PROPAGATED_MAX_RETRIES
|
|
6
|
+
from gllm_inference.lm_invoker.openai_chat_completions_lm_invoker import OpenAIChatCompletionsLMInvoker as OpenAIChatCompletionsLMInvoker
|
|
7
|
+
from gllm_inference.lm_invoker.schema.datasaur import InputType as InputType, Key as Key
|
|
8
|
+
from gllm_inference.schema import Attachment as Attachment, AttachmentType as AttachmentType, LMOutput as LMOutput, Message as Message, ModelId as ModelId, ModelProvider as ModelProvider, ResponseSchema as ResponseSchema, ToolCall as ToolCall, ToolResult as ToolResult
|
|
9
|
+
from langchain_core.tools import Tool as LangChainTool
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
SUPPORTED_ATTACHMENTS: Incomplete
|
|
13
|
+
|
|
14
|
+
class DatasaurLMInvoker(OpenAIChatCompletionsLMInvoker):
|
|
15
|
+
'''A language model invoker to interact with Datasaur LLM Projects Deployment API.
|
|
16
|
+
|
|
17
|
+
Attributes:
|
|
18
|
+
model_id (str): The model ID of the language model.
|
|
19
|
+
model_provider (str): The provider of the language model.
|
|
20
|
+
model_name (str): The name of the language model.
|
|
21
|
+
client_kwargs (dict[str, Any]): The keyword arguments for the OpenAI client.
|
|
22
|
+
default_hyperparameters (dict[str, Any]): Default hyperparameters for invoking the model.
|
|
23
|
+
tools (list[Any]): The list of tools provided to the model to enable tool calling. Currently not supported.
|
|
24
|
+
response_schema (ResponseSchema | None): The schema of the response. Currently not supported.
|
|
25
|
+
output_analytics (bool): Whether to output the invocation analytics.
|
|
26
|
+
retry_config (RetryConfig | None): The retry configuration for the language model.
|
|
27
|
+
citations (bool): Whether to output the citations.
|
|
28
|
+
|
|
29
|
+
Basic usage:
|
|
30
|
+
The `DatasaurLMInvoker` can be used as follows:
|
|
31
|
+
```python
|
|
32
|
+
lm_invoker = DatasaurLMInvoker(base_url="https://deployment.datasaur.ai/api/deployment/teamId/deploymentId/")
|
|
33
|
+
result = await lm_invoker.invoke("Hi there!")
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Input types:
|
|
37
|
+
The `DatasaurLMInvoker` supports the following input types: text, audio, image, and document.
|
|
38
|
+
Non-text inputs can be passed as an `Attachment` object with the `user` role.
|
|
39
|
+
|
|
40
|
+
Usage example:
|
|
41
|
+
```python
|
|
42
|
+
text = "What animal is in this image?"
|
|
43
|
+
image = Attachment.from_path("path/to/local/image.png")
|
|
44
|
+
result = await lm_invoker.invoke([text, image])
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Analytics tracking:
|
|
48
|
+
Analytics tracking is a feature that allows the module to output additional information about the invocation.
|
|
49
|
+
This feature can be enabled by setting the `output_analytics` parameter to `True`.
|
|
50
|
+
When enabled, the following attributes will be stored in the output:
|
|
51
|
+
1. `token_usage`: The token usage.
|
|
52
|
+
2. `duration`: The duration in seconds.
|
|
53
|
+
3. `finish_details`: The details about how the generation finished.
|
|
54
|
+
|
|
55
|
+
Output example:
|
|
56
|
+
```python
|
|
57
|
+
LMOutput(
|
|
58
|
+
response="Golden retriever is a good dog breed.",
|
|
59
|
+
token_usage=TokenUsage(input_tokens=100, output_tokens=50),
|
|
60
|
+
duration=0.729,
|
|
61
|
+
finish_details={"finish_reason": "stop"},
|
|
62
|
+
)
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
When streaming is enabled, token usage is not supported. Therefore, the `token_usage` attribute will be `None`
|
|
66
|
+
regardless of the value of the `output_analytics` parameter.
|
|
67
|
+
|
|
68
|
+
Retry and timeout:
|
|
69
|
+
The `DatasaurLMInvoker` supports retry and timeout configuration.
|
|
70
|
+
By default, the max retries is set to 0 and the timeout is set to 30.0 seconds.
|
|
71
|
+
They can be customized by providing a custom `RetryConfig` object to the `retry_config` parameter.
|
|
72
|
+
|
|
73
|
+
Retry config examples:
|
|
74
|
+
```python
|
|
75
|
+
retry_config = RetryConfig(max_retries=0, timeout=None) # No retry, no timeout
|
|
76
|
+
retry_config = RetryConfig(max_retries=0, timeout=10.0) # No retry, 10.0 seconds timeout
|
|
77
|
+
retry_config = RetryConfig(max_retries=5, timeout=None) # 5 max retries, no timeout
|
|
78
|
+
retry_config = RetryConfig(max_retries=5, timeout=10.0) # 5 max retries, 10.0 seconds timeout
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Usage example:
|
|
82
|
+
```python
|
|
83
|
+
lm_invoker = DatasaurLMInvoker(..., retry_config=retry_config)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Citations:
|
|
87
|
+
The `DatasaurLMInvoker` can be configured to output the citations used to generate the response.
|
|
88
|
+
They can be enabled by setting the `citations` parameter to `True`.
|
|
89
|
+
When enabled, the citations will be stored as `Chunk` objects in the `citations` attribute in the output.
|
|
90
|
+
|
|
91
|
+
Usage example:
|
|
92
|
+
```python
|
|
93
|
+
lm_invoker = DatasaurLMInvoker(..., citations=True)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Output example:
|
|
97
|
+
```python
|
|
98
|
+
LMOutput(
|
|
99
|
+
response="The winner of the match is team A ([Example title](https://www.example.com)).",
|
|
100
|
+
citations=[Chunk(id="123", content="...", metadata={...}, score=0.95)],
|
|
101
|
+
)
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Output types:
|
|
105
|
+
The output of the `DatasaurLMInvoker` can either be:
|
|
106
|
+
1. `str`: A text response.
|
|
107
|
+
2. `LMOutput`: A Pydantic model that may contain the following attributes:
|
|
108
|
+
2.1. response (str)
|
|
109
|
+
2.2. token_usage (TokenUsage | None)
|
|
110
|
+
2.3. duration (float | None)
|
|
111
|
+
2.4. finish_details (dict[str, Any] | None)
|
|
112
|
+
2.5. citations (list[Chunk])
|
|
113
|
+
'''
|
|
114
|
+
client_kwargs: Incomplete
|
|
115
|
+
citations: Incomplete
|
|
116
|
+
def __init__(self, base_url: str, api_key: str | None = None, model_kwargs: dict[str, Any] | None = None, default_hyperparameters: dict[str, Any] | None = None, output_analytics: bool = False, retry_config: RetryConfig | None = None, citations: bool = False) -> None:
|
|
117
|
+
"""Initializes a new instance of the DatasaurLMInvoker class.
|
|
118
|
+
|
|
119
|
+
Args:
|
|
120
|
+
base_url (str): The base URL of the Datasaur LLM Projects Deployment API.
|
|
121
|
+
api_key (str | None, optional): The API key for authenticating with Datasaur LLM Projects Deployment API.
|
|
122
|
+
Defaults to None, in which case the `DATASAUR_API_KEY` environment variable will be used.
|
|
123
|
+
model_kwargs (dict[str, Any] | None, optional): Additional model parameters. Defaults to None.
|
|
124
|
+
default_hyperparameters (dict[str, Any] | None, optional): Default hyperparameters for invoking the model.
|
|
125
|
+
Defaults to None.
|
|
126
|
+
output_analytics (bool, optional): Whether to output the invocation analytics. Defaults to False.
|
|
127
|
+
retry_config (RetryConfig | None, optional): The retry configuration for the language model.
|
|
128
|
+
Defaults to None, in which case a default config with no retry and 30.0 seconds timeout will be used.
|
|
129
|
+
citations (bool, optional): Whether to output the citations. Defaults to False.
|
|
130
|
+
|
|
131
|
+
Raises:
|
|
132
|
+
ValueError: If the `api_key` is not provided and the `DATASAUR_API_KEY` environment variable is not set.
|
|
133
|
+
"""
|
|
134
|
+
def set_tools(self, tools: list[Tool | LangChainTool]) -> None:
|
|
135
|
+
"""Sets the tools for the Datasaur LLM Projects Deployment API.
|
|
136
|
+
|
|
137
|
+
This method is raises a `NotImplementedError` because the Datasaur LLM Projects Deployment API does not
|
|
138
|
+
support tools.
|
|
139
|
+
|
|
140
|
+
Args:
|
|
141
|
+
tools (list[Tool | LangChainTool]): The list of tools to be used.
|
|
142
|
+
|
|
143
|
+
Raises:
|
|
144
|
+
NotImplementedError: This method is not supported for the Datasaur LLM Projects Deployment API.
|
|
145
|
+
"""
|
|
146
|
+
def set_response_schema(self, response_schema: ResponseSchema | None) -> None:
|
|
147
|
+
"""Sets the response schema for the Datasaur LLM Projects Deployment API.
|
|
148
|
+
|
|
149
|
+
This method is raises a `NotImplementedError` because the Datasaur LLM Projects Deployment API does not
|
|
150
|
+
support response schema.
|
|
151
|
+
|
|
152
|
+
Args:
|
|
153
|
+
response_schema (ResponseSchema | None): The response schema to be used.
|
|
154
|
+
|
|
155
|
+
Raises:
|
|
156
|
+
NotImplementedError: This method is not supported for the Datasaur LLM Projects Deployment API.
|
|
157
|
+
"""
|