gllm-inference-binary 0.5.55__cp313-cp313-macosx_13_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.
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_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 +12 -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 +318 -0
- gllm_inference/lm_invoker/azure_openai_lm_invoker.pyi +237 -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 +212 -0
- gllm_inference/lm_invoker/datasaur_lm_invoker.pyi +157 -0
- gllm_inference/lm_invoker/google_lm_invoker.pyi +327 -0
- gllm_inference/lm_invoker/langchain_lm_invoker.pyi +239 -0
- gllm_inference/lm_invoker/litellm_lm_invoker.pyi +224 -0
- gllm_inference/lm_invoker/lm_invoker.pyi +165 -0
- gllm_inference/lm_invoker/openai_chat_completions_lm_invoker.pyi +253 -0
- gllm_inference/lm_invoker/openai_compatible_lm_invoker.pyi +52 -0
- gllm_inference/lm_invoker/openai_lm_invoker.pyi +404 -0
- gllm_inference/lm_invoker/portkey_lm_invoker.pyi +296 -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 +253 -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 +80 -0
- gllm_inference/schema/events.pyi +105 -0
- gllm_inference/schema/lm_input.pyi +4 -0
- gllm_inference/schema/lm_output.pyi +188 -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 +154 -0
- gllm_inference_binary-0.5.55.dist-info/METADATA +138 -0
- gllm_inference_binary-0.5.55.dist-info/RECORD +137 -0
- gllm_inference_binary-0.5.55.dist-info/WHEEL +5 -0
- gllm_inference_binary-0.5.55.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,212 @@
|
|
|
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
|
+
Text output:
|
|
55
|
+
The `BedrockLMInvoker` generates text outputs by default.
|
|
56
|
+
Text outputs are stored in the `outputs` attribute of the `LMOutput` object and can be accessed
|
|
57
|
+
via the `texts` (all text outputs) or `text` (first text output) properties.
|
|
58
|
+
|
|
59
|
+
Output example:
|
|
60
|
+
```python
|
|
61
|
+
LMOutput(outputs=[LMOutputItem(type="text", output="Hello, there!")])
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Structured output:
|
|
65
|
+
The `BedrockLMInvoker` can be configured to generate structured outputs.
|
|
66
|
+
This feature can be enabled by providing a schema to the `response_schema` parameter.
|
|
67
|
+
|
|
68
|
+
Structured outputs are stored in the `outputs` attribute of the `LMOutput` object and can be accessed
|
|
69
|
+
via the `structureds` (all structured outputs) or `structured` (first structured output) properties.
|
|
70
|
+
|
|
71
|
+
The schema must either be one of the following:
|
|
72
|
+
1. A Pydantic BaseModel class
|
|
73
|
+
The structured output will be a Pydantic model.
|
|
74
|
+
2. A JSON schema dictionary
|
|
75
|
+
JSON dictionary schema must be compatible with Pydantic\'s JSON schema, especially for complex schemas.
|
|
76
|
+
Thus, it is recommended to create the JSON schema using Pydantic\'s `model_json_schema` method.
|
|
77
|
+
The structured output will be a dictionary.
|
|
78
|
+
|
|
79
|
+
Usage example:
|
|
80
|
+
```python
|
|
81
|
+
class Animal(BaseModel):
|
|
82
|
+
name: str
|
|
83
|
+
color: str
|
|
84
|
+
|
|
85
|
+
json_schema = Animal.model_json_schema()
|
|
86
|
+
|
|
87
|
+
lm_invoker = BedrockLMInvoker(..., response_schema=Animal) # Using Pydantic BaseModel class
|
|
88
|
+
lm_invoker = BedrockLMInvoker(..., response_schema=json_schema) # Using JSON schema dictionary
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Output example:
|
|
92
|
+
```python
|
|
93
|
+
# Using Pydantic BaseModel class outputs a Pydantic model
|
|
94
|
+
LMOutput(outputs=[LMOutputItem(type="structured", output=Animal(name="dog", color="white"))])
|
|
95
|
+
|
|
96
|
+
# Using JSON schema dictionary outputs a dictionary
|
|
97
|
+
LMOutput(outputs=[LMOutputItem(type="structured", output={"name": "dog", "color": "white"})])
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Structured output is not compatible with tool calling.
|
|
101
|
+
When structured output is enabled, streaming is disabled.
|
|
102
|
+
|
|
103
|
+
Tool calling:
|
|
104
|
+
The `BedrockLMInvoker` can be configured to call tools to perform certain tasks.
|
|
105
|
+
This feature can be enabled by providing a list of `Tool` objects to the `tools` parameter.
|
|
106
|
+
|
|
107
|
+
Tool calls outputs are stored in the `outputs` attribute of the `LMOutput` object and
|
|
108
|
+
can be accessed via the `tool_calls` property.
|
|
109
|
+
|
|
110
|
+
Usage example:
|
|
111
|
+
```python
|
|
112
|
+
lm_invoker = BedrockLMInvoker(..., tools=[tool_1, tool_2])
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Output example:
|
|
116
|
+
```python
|
|
117
|
+
LMOutput(
|
|
118
|
+
outputs=[
|
|
119
|
+
LMOutputItem(type="text", output="I\'m using tools..."),
|
|
120
|
+
LMOutputItem(type="tool_call", output=ToolCall(id="123", name="tool_1", args={"key": "value"})),
|
|
121
|
+
LMOutputItem(type="tool_call", output=ToolCall(id="456", name="tool_2", args={"key": "value"})),
|
|
122
|
+
]
|
|
123
|
+
)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Analytics tracking:
|
|
127
|
+
The `BedrockLMInvoker` can be configured to output additional information about the invocation.
|
|
128
|
+
This feature can be enabled by setting the `output_analytics` parameter to `True`.
|
|
129
|
+
|
|
130
|
+
When enabled, the following attributes will be stored in the output:
|
|
131
|
+
1. `token_usage`: The token usage.
|
|
132
|
+
2. `duration`: The duration in seconds.
|
|
133
|
+
3. `finish_details`: The details about how the generation finished.
|
|
134
|
+
|
|
135
|
+
Output example:
|
|
136
|
+
```python
|
|
137
|
+
LMOutput(
|
|
138
|
+
outputs=[...],
|
|
139
|
+
token_usage=TokenUsage(input_tokens=100, output_tokens=50),
|
|
140
|
+
duration=0.729,
|
|
141
|
+
finish_details={"stop_reason": "end_turn"},
|
|
142
|
+
)
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Retry and timeout:
|
|
146
|
+
The `BedrockLMInvoker` supports retry and timeout configuration.
|
|
147
|
+
By default, the max retries is set to 0 and the timeout is set to 30.0 seconds.
|
|
148
|
+
They can be customized by providing a custom `RetryConfig` object to the `retry_config` parameter.
|
|
149
|
+
|
|
150
|
+
Retry config examples:
|
|
151
|
+
```python
|
|
152
|
+
retry_config = RetryConfig(max_retries=0, timeout=None) # No retry, no timeout
|
|
153
|
+
retry_config = RetryConfig(max_retries=5, timeout=10.0) # 5 max retries, 10.0 seconds timeout
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Usage example:
|
|
157
|
+
```python
|
|
158
|
+
lm_invoker = BedrockLMInvoker(..., retry_config=retry_config)
|
|
159
|
+
```
|
|
160
|
+
'''
|
|
161
|
+
session: Incomplete
|
|
162
|
+
client_kwargs: Incomplete
|
|
163
|
+
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:
|
|
164
|
+
'''Initializes the BedrockLMInvoker instance.
|
|
165
|
+
|
|
166
|
+
Args:
|
|
167
|
+
model_name (str): The name of the Bedrock language model.
|
|
168
|
+
access_key_id (str | None, optional): The AWS access key ID. Defaults to None, in which case
|
|
169
|
+
the `AWS_ACCESS_KEY_ID` environment variable will be used.
|
|
170
|
+
secret_access_key (str | None, optional): The AWS secret access key. Defaults to None, in which case
|
|
171
|
+
the `AWS_SECRET_ACCESS_KEY` environment variable will be used.
|
|
172
|
+
region_name (str, optional): The AWS region name. Defaults to "us-east-1".
|
|
173
|
+
model_kwargs (dict[str, Any] | None, optional): Additional keyword arguments for the Bedrock client.
|
|
174
|
+
default_hyperparameters (dict[str, Any] | None, optional): Default hyperparameters for invoking the model.
|
|
175
|
+
Defaults to None.
|
|
176
|
+
tools (list[Tool | LangChainTool] | None, optional): Tools provided to the model to enable tool calling.
|
|
177
|
+
Defaults to None.
|
|
178
|
+
response_schema (ResponseSchema | None, optional): The schema of the response. If provided, the model will
|
|
179
|
+
output a structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema
|
|
180
|
+
dictionary. Defaults to None.
|
|
181
|
+
output_analytics (bool, optional): Whether to output the invocation analytics. Defaults to False.
|
|
182
|
+
retry_config (RetryConfig | None, optional): The retry configuration for the language model.
|
|
183
|
+
Defaults to None, in which case a default config with no retry and 30.0 seconds timeout will be used.
|
|
184
|
+
|
|
185
|
+
Raises:
|
|
186
|
+
ValueError: If `response_schema` is provided, but `tools` are also provided.
|
|
187
|
+
ValueError: If `access_key_id` or `secret_access_key` is neither provided nor set in the
|
|
188
|
+
`AWS_ACCESS_KEY_ID` or `AWS_SECRET_ACCESS_KEY` environment variables, respectively.
|
|
189
|
+
'''
|
|
190
|
+
def set_tools(self, tools: list[Tool | LangChainTool]) -> None:
|
|
191
|
+
"""Sets the tools for the Bedrock language model.
|
|
192
|
+
|
|
193
|
+
This method sets the tools for the Bedrock language model. Any existing tools will be replaced.
|
|
194
|
+
|
|
195
|
+
Args:
|
|
196
|
+
tools (list[Tool | LangChainTool]): The list of tools to be used.
|
|
197
|
+
|
|
198
|
+
Raises:
|
|
199
|
+
ValueError: If `response_schema` exists.
|
|
200
|
+
"""
|
|
201
|
+
def set_response_schema(self, response_schema: ResponseSchema | None) -> None:
|
|
202
|
+
"""Sets the response schema for the Bedrock language model.
|
|
203
|
+
|
|
204
|
+
This method sets the response schema for the Bedrock language model. Any existing response schema will be
|
|
205
|
+
replaced.
|
|
206
|
+
|
|
207
|
+
Args:
|
|
208
|
+
response_schema (ResponseSchema | None): The response schema to be used.
|
|
209
|
+
|
|
210
|
+
Raises:
|
|
211
|
+
ValueError: If `tools` exists.
|
|
212
|
+
"""
|
|
@@ -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
|
+
Text output:
|
|
48
|
+
The `DatasaurLMInvoker` generates text outputs by default.
|
|
49
|
+
Text outputs are stored in the `outputs` attribute of the `LMOutput` object and can be accessed
|
|
50
|
+
via the `texts` (all text outputs) or `text` (first text output) properties.
|
|
51
|
+
|
|
52
|
+
Output example:
|
|
53
|
+
```python
|
|
54
|
+
LMOutput(outputs=[LMOutputItem(type="text", output="Hello, there!")])
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Citations:
|
|
58
|
+
The `DatasaurLMInvoker` can be configured to output the citations used to generate the response.
|
|
59
|
+
This feature can be enabled by setting the `citations` parameter to `True`.
|
|
60
|
+
|
|
61
|
+
Citations outputs are stored in the `outputs` attribute of the `LMOutput` object and
|
|
62
|
+
can be accessed via the `citations` property.
|
|
63
|
+
|
|
64
|
+
Usage example:
|
|
65
|
+
```python
|
|
66
|
+
lm_invoker = DatasaurLMInvoker(..., citations=True)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Output example:
|
|
70
|
+
```python
|
|
71
|
+
LMOutput(
|
|
72
|
+
outputs=[
|
|
73
|
+
LMOutputItem(type="citation", output=Chunk(id="123", content="...", metadata={...}, score=0.95)),
|
|
74
|
+
LMOutputItem(type="text", output="According to recent reports... ([Source](https://www.example.com))."),
|
|
75
|
+
],
|
|
76
|
+
)
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Analytics tracking:
|
|
80
|
+
The `DatasaurLMInvoker` can be configured to output additional information about the invocation.
|
|
81
|
+
This feature can be enabled by setting the `output_analytics` parameter to `True`.
|
|
82
|
+
|
|
83
|
+
When enabled, the following attributes will be stored in the output:
|
|
84
|
+
1. `token_usage`: The token usage.
|
|
85
|
+
2. `duration`: The duration in seconds.
|
|
86
|
+
3. `finish_details`: The details about how the generation finished.
|
|
87
|
+
|
|
88
|
+
Output example:
|
|
89
|
+
```python
|
|
90
|
+
LMOutput(
|
|
91
|
+
outputs=[...],
|
|
92
|
+
token_usage=TokenUsage(input_tokens=100, output_tokens=50),
|
|
93
|
+
duration=0.729,
|
|
94
|
+
finish_details={"stop_reason": "end_turn"},
|
|
95
|
+
)
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Retry and timeout:
|
|
99
|
+
The `DatasaurLMInvoker` supports retry and timeout configuration.
|
|
100
|
+
By default, the max retries is set to 0 and the timeout is set to 30.0 seconds.
|
|
101
|
+
They can be customized by providing a custom `RetryConfig` object to the `retry_config` parameter.
|
|
102
|
+
|
|
103
|
+
Retry config examples:
|
|
104
|
+
```python
|
|
105
|
+
retry_config = RetryConfig(max_retries=0, timeout=None) # No retry, no timeout
|
|
106
|
+
retry_config = RetryConfig(max_retries=5, timeout=10.0) # 5 max retries, 10.0 seconds timeout
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Usage example:
|
|
110
|
+
```python
|
|
111
|
+
lm_invoker = DatasaurLMInvoker(..., retry_config=retry_config)
|
|
112
|
+
```
|
|
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
|
+
"""
|