gllm-inference-binary 0.5.9b1__cp313-cp313-macosx_10_13_universal2.macosx_13_0_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of gllm-inference-binary might be problematic. Click here for more details.

Files changed (105) hide show
  1. gllm_inference/__init__.pyi +0 -0
  2. gllm_inference/builder/__init__.pyi +6 -0
  3. gllm_inference/builder/build_em_invoker.pyi +137 -0
  4. gllm_inference/builder/build_lm_invoker.pyi +161 -0
  5. gllm_inference/builder/build_lm_request_processor.pyi +93 -0
  6. gllm_inference/builder/build_output_parser.pyi +29 -0
  7. gllm_inference/catalog/__init__.pyi +4 -0
  8. gllm_inference/catalog/catalog.pyi +121 -0
  9. gllm_inference/catalog/lm_request_processor_catalog.pyi +112 -0
  10. gllm_inference/catalog/prompt_builder_catalog.pyi +82 -0
  11. gllm_inference/constants.pyi +10 -0
  12. gllm_inference/em_invoker/__init__.pyi +10 -0
  13. gllm_inference/em_invoker/azure_openai_em_invoker.pyi +88 -0
  14. gllm_inference/em_invoker/bedrock_em_invoker.pyi +106 -0
  15. gllm_inference/em_invoker/em_invoker.pyi +90 -0
  16. gllm_inference/em_invoker/google_em_invoker.pyi +129 -0
  17. gllm_inference/em_invoker/langchain/__init__.pyi +3 -0
  18. gllm_inference/em_invoker/langchain/em_invoker_embeddings.pyi +84 -0
  19. gllm_inference/em_invoker/langchain_em_invoker.pyi +46 -0
  20. gllm_inference/em_invoker/openai_compatible_em_invoker.pyi +96 -0
  21. gllm_inference/em_invoker/openai_em_invoker.pyi +90 -0
  22. gllm_inference/em_invoker/schema/__init__.pyi +0 -0
  23. gllm_inference/em_invoker/schema/bedrock.pyi +22 -0
  24. gllm_inference/em_invoker/schema/google.pyi +9 -0
  25. gllm_inference/em_invoker/schema/langchain.pyi +5 -0
  26. gllm_inference/em_invoker/schema/openai.pyi +7 -0
  27. gllm_inference/em_invoker/schema/openai_compatible.pyi +7 -0
  28. gllm_inference/em_invoker/schema/twelvelabs.pyi +17 -0
  29. gllm_inference/em_invoker/schema/voyage.pyi +15 -0
  30. gllm_inference/em_invoker/twelevelabs_em_invoker.pyi +101 -0
  31. gllm_inference/em_invoker/voyage_em_invoker.pyi +104 -0
  32. gllm_inference/exceptions/__init__.pyi +4 -0
  33. gllm_inference/exceptions/error_parser.pyi +41 -0
  34. gllm_inference/exceptions/exceptions.pyi +132 -0
  35. gllm_inference/exceptions/provider_error_map.pyi +23 -0
  36. gllm_inference/lm_invoker/__init__.pyi +12 -0
  37. gllm_inference/lm_invoker/anthropic_lm_invoker.pyi +275 -0
  38. gllm_inference/lm_invoker/azure_openai_lm_invoker.pyi +252 -0
  39. gllm_inference/lm_invoker/bedrock_lm_invoker.pyi +234 -0
  40. gllm_inference/lm_invoker/datasaur_lm_invoker.pyi +166 -0
  41. gllm_inference/lm_invoker/google_lm_invoker.pyi +317 -0
  42. gllm_inference/lm_invoker/langchain_lm_invoker.pyi +260 -0
  43. gllm_inference/lm_invoker/litellm_lm_invoker.pyi +248 -0
  44. gllm_inference/lm_invoker/lm_invoker.pyi +152 -0
  45. gllm_inference/lm_invoker/openai_compatible_lm_invoker.pyi +265 -0
  46. gllm_inference/lm_invoker/openai_lm_invoker.pyi +362 -0
  47. gllm_inference/lm_invoker/schema/__init__.pyi +0 -0
  48. gllm_inference/lm_invoker/schema/anthropic.pyi +50 -0
  49. gllm_inference/lm_invoker/schema/bedrock.pyi +53 -0
  50. gllm_inference/lm_invoker/schema/datasaur.pyi +12 -0
  51. gllm_inference/lm_invoker/schema/google.pyi +24 -0
  52. gllm_inference/lm_invoker/schema/langchain.pyi +23 -0
  53. gllm_inference/lm_invoker/schema/openai.pyi +91 -0
  54. gllm_inference/lm_invoker/schema/openai_compatible.pyi +60 -0
  55. gllm_inference/lm_invoker/schema/xai.pyi +31 -0
  56. gllm_inference/lm_invoker/xai_lm_invoker.pyi +305 -0
  57. gllm_inference/model/__init__.pyi +9 -0
  58. gllm_inference/model/em/__init__.pyi +0 -0
  59. gllm_inference/model/em/google_em.pyi +16 -0
  60. gllm_inference/model/em/openai_em.pyi +15 -0
  61. gllm_inference/model/em/twelvelabs_em.pyi +13 -0
  62. gllm_inference/model/em/voyage_em.pyi +20 -0
  63. gllm_inference/model/lm/__init__.pyi +0 -0
  64. gllm_inference/model/lm/anthropic_lm.pyi +20 -0
  65. gllm_inference/model/lm/google_lm.pyi +17 -0
  66. gllm_inference/model/lm/openai_lm.pyi +27 -0
  67. gllm_inference/output_parser/__init__.pyi +3 -0
  68. gllm_inference/output_parser/json_output_parser.pyi +60 -0
  69. gllm_inference/output_parser/output_parser.pyi +27 -0
  70. gllm_inference/prompt_builder/__init__.pyi +3 -0
  71. gllm_inference/prompt_builder/prompt_builder.pyi +56 -0
  72. gllm_inference/prompt_formatter/__init__.pyi +7 -0
  73. gllm_inference/prompt_formatter/agnostic_prompt_formatter.pyi +49 -0
  74. gllm_inference/prompt_formatter/huggingface_prompt_formatter.pyi +55 -0
  75. gllm_inference/prompt_formatter/llama_prompt_formatter.pyi +59 -0
  76. gllm_inference/prompt_formatter/mistral_prompt_formatter.pyi +53 -0
  77. gllm_inference/prompt_formatter/openai_prompt_formatter.pyi +35 -0
  78. gllm_inference/prompt_formatter/prompt_formatter.pyi +30 -0
  79. gllm_inference/request_processor/__init__.pyi +4 -0
  80. gllm_inference/request_processor/lm_request_processor.pyi +101 -0
  81. gllm_inference/request_processor/uses_lm_mixin.pyi +130 -0
  82. gllm_inference/schema/__init__.pyi +14 -0
  83. gllm_inference/schema/attachment.pyi +88 -0
  84. gllm_inference/schema/code_exec_result.pyi +14 -0
  85. gllm_inference/schema/config.pyi +15 -0
  86. gllm_inference/schema/enums.pyi +29 -0
  87. gllm_inference/schema/lm_output.pyi +36 -0
  88. gllm_inference/schema/message.pyi +52 -0
  89. gllm_inference/schema/model_id.pyi +147 -0
  90. gllm_inference/schema/reasoning.pyi +15 -0
  91. gllm_inference/schema/token_usage.pyi +75 -0
  92. gllm_inference/schema/tool_call.pyi +14 -0
  93. gllm_inference/schema/tool_result.pyi +11 -0
  94. gllm_inference/schema/type_alias.pyi +11 -0
  95. gllm_inference/utils/__init__.pyi +5 -0
  96. gllm_inference/utils/io_utils.pyi +26 -0
  97. gllm_inference/utils/langchain.pyi +30 -0
  98. gllm_inference/utils/validation.pyi +12 -0
  99. gllm_inference.build/.gitignore +1 -0
  100. gllm_inference.cpython-313-darwin.so +0 -0
  101. gllm_inference.pyi +124 -0
  102. gllm_inference_binary-0.5.9b1.dist-info/METADATA +71 -0
  103. gllm_inference_binary-0.5.9b1.dist-info/RECORD +105 -0
  104. gllm_inference_binary-0.5.9b1.dist-info/WHEEL +6 -0
  105. gllm_inference_binary-0.5.9b1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,252 @@
1
+ from _typeshed import Incomplete
2
+ from gllm_core.schema.tool import Tool as Tool
3
+ from gllm_core.utils.retry import RetryConfig as RetryConfig
4
+ from gllm_inference.constants import AZURE_OPENAI_URL_SUFFIX as AZURE_OPENAI_URL_SUFFIX, INVOKER_PROPAGATED_MAX_RETRIES as INVOKER_PROPAGATED_MAX_RETRIES
5
+ from gllm_inference.lm_invoker.openai_lm_invoker import OpenAILMInvoker as OpenAILMInvoker, ReasoningEffort as ReasoningEffort, ReasoningSummary as ReasoningSummary
6
+ from gllm_inference.lm_invoker.schema.openai import Key as Key
7
+ from gllm_inference.schema import ModelId as ModelId, ModelProvider as ModelProvider, ResponseSchema as ResponseSchema
8
+ from langchain_core.tools import Tool as LangChainTool
9
+ from typing import Any
10
+
11
+ class AzureOpenAILMInvoker(OpenAILMInvoker):
12
+ '''A language model invoker to interact with Azure OpenAI language models.
13
+
14
+ Attributes:
15
+ model_id (str): The model ID of the language model.
16
+ model_provider (str): The provider of the language model.
17
+ model_name (str): The name of the Azure OpenAI language model deployment.
18
+ client (AsyncAzureOpenAI): The Azure OpenAI client instance.
19
+ default_hyperparameters (dict[str, Any]): Default hyperparameters for invoking the model.
20
+ tools (list[Tool]): The list of tools provided to the model to enable tool calling.
21
+ response_schema (ResponseSchema | None): The schema of the response. If provided, the model will output a
22
+ structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema dictionary.
23
+ output_analytics (bool): Whether to output the invocation analytics.
24
+ retry_config (RetryConfig): The retry configuration for the language model.
25
+ reasoning_effort (ReasoningEffort | None): The reasoning effort for reasoning models. Not allowed
26
+ for non-reasoning models. If None, the model will perform medium reasoning effort.
27
+ reasoning_summary (ReasoningSummary | None): The reasoning summary level for reasoning models. Not allowed
28
+ for non-reasoning models. If None, no summary will be generated.
29
+ code_interpreter (bool): Whether to enable the code interpreter. Currently not supported.
30
+ web_search (bool): Whether to enable the web search. Currently not supported.
31
+
32
+ Basic usage:
33
+ The `AzureOpenAILMInvoker` can be used as follows:
34
+ ```python
35
+ lm_invoker = AzureOpenAILMInvoker(
36
+ azure_endpoint="https://<your-azure-openai-endpoint>.openai.azure.com/openai/v1",
37
+ azure_deployment="<your-azure-openai-deployment>",
38
+ )
39
+ result = await lm_invoker.invoke("Hi there!")
40
+ ```
41
+
42
+ Input types:
43
+ The `AzureOpenAILMInvoker` supports the following input types: text, document, and image.
44
+ Non-text inputs can be passed as an `Attachment` object with the `user` role.
45
+
46
+ Usage example:
47
+ ```python
48
+ text = "What animal is in this image?"
49
+ image = Attachment.from_path("path/to/local/image.png")
50
+ result = await lm_invoker.invoke([text, image])
51
+ ```
52
+
53
+ Tool calling:
54
+ Tool calling is a feature that allows the language model to call tools to perform tasks.
55
+ Tools can be passed to the via the `tools` parameter as a list of `Tool` objects.
56
+ When tools are provided and the model decides to call a tool, the tool calls are stored in the
57
+ `tool_calls` attribute in the output.
58
+
59
+ Usage example:
60
+ ```python
61
+ lm_invoker = AzureOpenAILMInvoker(..., tools=[tool_1, tool_2])
62
+ ```
63
+
64
+ Output example:
65
+ ```python
66
+ LMOutput(
67
+ response="Let me call the tools...",
68
+ tool_calls=[
69
+ ToolCall(id="123", name="tool_1", args={"key": "value"}),
70
+ ToolCall(id="456", name="tool_2", args={"key": "value"}),
71
+ ]
72
+ )
73
+ ```
74
+
75
+ Structured output:
76
+ Structured output is a feature that allows the language model to output a structured response.
77
+ This feature can be enabled by providing a schema to the `response_schema` parameter.
78
+
79
+ The schema must be either a JSON schema dictionary or a Pydantic BaseModel class.
80
+ If JSON schema is used, it must be compatible with Pydantic\'s JSON schema, especially for complex schemas.
81
+ For this reason, it is recommended to create the JSON schema using Pydantic\'s `model_json_schema` method.
82
+
83
+ The language model also doesn\'t need to stream anything when structured output is enabled. Thus, standard
84
+ invocation will be performed regardless of whether the `event_emitter` parameter is provided or not.
85
+
86
+ When enabled, the structured output is stored in the `structured_output` attribute in the output.
87
+ 1. If the schema is a JSON schema dictionary, the structured output is a dictionary.
88
+ 2. If the schema is a Pydantic BaseModel class, the structured output is a Pydantic model.
89
+
90
+ # Example 1: Using a JSON schema dictionary
91
+ Usage example:
92
+ ```python
93
+ schema = {
94
+ "title": "Animal",
95
+ "description": "A description of an animal.",
96
+ "properties": {
97
+ "color": {"title": "Color", "type": "string"},
98
+ "name": {"title": "Name", "type": "string"},
99
+ },
100
+ "required": ["name", "color"],
101
+ "type": "object",
102
+ }
103
+ lm_invoker = AzureOpenAILMInvoker(..., response_schema=schema)
104
+ ```
105
+ Output example:
106
+ ```python
107
+ LMOutput(structured_output={"name": "Golden retriever", "color": "Golden"})
108
+ ```
109
+
110
+ # Example 2: Using a Pydantic BaseModel class
111
+ Usage example:
112
+ ```python
113
+ class Animal(BaseModel):
114
+ name: str
115
+ color: str
116
+
117
+ lm_invoker = AzureOpenAILMInvoker(..., response_schema=Animal)
118
+ ```
119
+ Output example:
120
+ ```python
121
+ LMOutput(structured_output=Animal(name="Golden retriever", color="Golden"))
122
+ ```
123
+
124
+ Analytics tracking:
125
+ Analytics tracking is a feature that allows the module to output additional information about the invocation.
126
+ This feature can be enabled by setting the `output_analytics` parameter to `True`.
127
+ When enabled, the following attributes will be stored in the output:
128
+ 1. `token_usage`: The token usage.
129
+ 2. `duration`: The duration in seconds.
130
+ 3. `finish_details`: The details about how the generation finished.
131
+
132
+ Output example:
133
+ ```python
134
+ LMOutput(
135
+ response="Golden retriever is a good dog breed.",
136
+ token_usage=TokenUsage(input_tokens=100, output_tokens=50),
137
+ duration=0.729,
138
+ finish_details={"status": "completed", "incomplete_details": {"reason": None}},
139
+ )
140
+ ```
141
+
142
+ Retry and timeout:
143
+ The `AzureOpenAILMInvoker` supports retry and timeout configuration.
144
+ By default, the max retries is set to 0 and the timeout is set to 30.0 seconds.
145
+ They can be customized by providing a custom `RetryConfig` object to the `retry_config` parameter.
146
+
147
+ Retry config examples:
148
+ ```python
149
+ retry_config = RetryConfig(max_retries=0, timeout=0.0) # No retry, no timeout
150
+ retry_config = RetryConfig(max_retries=0, timeout=10.0) # No retry, 10.0 seconds timeout
151
+ retry_config = RetryConfig(max_retries=5, timeout=0.0) # 5 max retries, no timeout
152
+ retry_config = RetryConfig(max_retries=5, timeout=10.0) # 5 max retries, 10.0 seconds timeout
153
+ ```
154
+
155
+ Usage example:
156
+ ```python
157
+ lm_invoker = AzureOpenAILMInvoker(..., retry_config=retry_config)
158
+ ```
159
+
160
+ Reasoning:
161
+ Azure OpenAI\'s GPT-5 models and o-series models are classified as reasoning models. Reasoning models think
162
+ before they answer, producing a long internal chain of thought before responding to the user. Reasoning models
163
+ excel in complex problem solving, coding, scientific reasoning, and multi-step planning for agentic workflows.
164
+
165
+ The reasoning effort of reasoning models can be set via the `reasoning_effort` parameter. This parameter
166
+ will guide the models on how many reasoning tokens it should generate before creating a response.
167
+ Available options include:
168
+ 1. "minimal": Favors the least amount of reasoning, only supported for GPT-5 models onwards.
169
+ 2. "low": Favors speed and economical token usage.
170
+ 3. "medium": Favors a balance between speed and reasoning accuracy.
171
+ 4. "high": Favors more complete reasoning at the cost of more tokens generated and slower responses.
172
+
173
+ Azure OpenAI doesn\'t expose the raw reasoning tokens. However, the summary of the reasoning tokens can still be
174
+ generated. The summary level can be set via the `reasoning_summary` parameter. Available options include:
175
+ 1. "auto": The model decides the summary level automatically.
176
+ 2. "detailed": The model will generate a detailed summary of the reasoning tokens.
177
+ Reasoning summary is not compatible with tool calling.
178
+ When enabled, the reasoning summary will be stored in the `reasoning` attribute in the output.
179
+
180
+ Output example:
181
+ ```python
182
+ LMOutput(
183
+ response="Golden retriever is a good dog breed.",
184
+ reasoning=[Reasoning(id="x", reasoning="Let me think about it...")],
185
+ )
186
+ ```
187
+
188
+ When streaming is enabled along with reasoning summary, the reasoning summary token will be streamed with the
189
+ `EventType.DATA` event type.
190
+
191
+ Streaming output example:
192
+ ```python
193
+ {"type": "data", "value": "Let me think ", ...} # Reasoning summary token
194
+ {"type": "data", "value": "about it...", ...} # Reasoning summary token
195
+ {"type": "response", "value": "Golden retriever ", ...} # Response token
196
+ {"type": "response", "value": "is a good dog breed.", ...} # Response token
197
+ ```
198
+
199
+ Setting reasoning-related parameters for non-reasoning models will raise an error.
200
+
201
+ Output types:
202
+ The output of the `AzureOpenAILMInvoker` can either be:
203
+ 1. `str`: The text response if no additional output is needed.
204
+ 2. `LMOutput`: A Pydantic model with the following attributes if any additional output is needed:
205
+ 2.1. response (str): The text response.
206
+ 2.2. tool_calls (list[ToolCall]): The tool calls, if the `tools` parameter is defined and the language
207
+ model decides to invoke tools. Defaults to an empty list.
208
+ 2.3. structured_output (dict[str, Any] | BaseModel | None): The structured output, if the `response_schema`
209
+ parameter is defined. Defaults to None.
210
+ 2.4. token_usage (TokenUsage | None): The token usage analytics, if the `output_analytics` parameter is
211
+ set to `True`. Defaults to None.
212
+ 2.5. duration (float | None): The duration of the invocation in seconds, if the `output_analytics`
213
+ parameter is set to `True`. Defaults to None.
214
+ 2.6. finish_details (dict[str, Any] | None): The details about how the generation finished, if the
215
+ `output_analytics` parameter is set to `True`. Defaults to None.
216
+ 2.7. reasoning (list[Reasoning]): The reasoning objects, if the `reasoning_summary` parameter is provided
217
+ for reasoning models. Defaults to an empty list.
218
+ 2.8. citations (list[Chunk]): The citations. Currently not supported. Defaults to an empty list.
219
+ 2.9. code_exec_results (list[CodeExecResult]): The code execution results. Currently not supported.
220
+ Defaults to an empty list.
221
+ '''
222
+ client: Incomplete
223
+ def __init__(self, azure_endpoint: str, azure_deployment: str, api_key: str | None = None, api_version: str | None = None, 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, reasoning_effort: ReasoningEffort | None = None, reasoning_summary: ReasoningSummary | None = None) -> None:
224
+ """Initializes a new instance of the AzureOpenAILMInvoker class.
225
+
226
+ Args:
227
+ azure_endpoint (str): The endpoint of the Azure OpenAI service.
228
+ azure_deployment (str): The deployment name of the Azure OpenAI service.
229
+ api_key (str | None, optional): The API key for authenticating with Azure OpenAI. Defaults to None, in
230
+ which case the `AZURE_OPENAI_API_KEY` environment variable will be used.
231
+ api_version (str | None, optional): Deprecated parameter to be removed in v0.6. Defaults to None.
232
+ model_kwargs (dict[str, Any] | None, optional): Additional model parameters. Defaults to None.
233
+ default_hyperparameters (dict[str, Any] | None, optional): Default hyperparameters for invoking the model.
234
+ Defaults to None.
235
+ tools (list[Tool | LangChainTool] | None, optional): Tools provided to the model to enable tool calling.
236
+ Defaults to None.
237
+ response_schema (ResponseSchema | None, optional): The schema of the response. If provided, the model will
238
+ output a structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema
239
+ dictionary. Defaults to None.
240
+ output_analytics (bool, optional): Whether to output the invocation analytics. Defaults to False.
241
+ retry_config (RetryConfig | None, optional): The retry configuration for the language model.
242
+ Defaults to None, in which case a default config with no retry and 30.0 seconds timeout will be used.
243
+ reasoning_effort (ReasoningEffort | None, optional): The reasoning effort for reasoning models. Not allowed
244
+ for non-reasoning models. If None, the model will perform medium reasoning effort. Defaults to None.
245
+ reasoning_summary (ReasoningSummary | None, optional): The reasoning summary level for reasoning models.
246
+ Not allowed for non-reasoning models. If None, no summary will be generated. Defaults to None.
247
+
248
+ Raises:
249
+ ValueError:
250
+ 1. `reasoning_effort` is provided, but is not a valid ReasoningEffort.
251
+ 2. `reasoning_summary` is provided, but is not a valid ReasoningSummary.
252
+ """
@@ -0,0 +1,234 @@
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
+ SUPPORTED_ATTACHMENTS: Incomplete
14
+
15
+ class BedrockLMInvoker(BaseLMInvoker):
16
+ '''A language model invoker to interact with AWS Bedrock language models.
17
+
18
+ Attributes:
19
+ model_id (str): The model ID of the language model.
20
+ model_provider (str): The provider of the language model.
21
+ model_name (str): The name of the language model.
22
+ session (Session): The Bedrock client session.
23
+ client_kwargs (dict[str, Any]): The Bedrock client kwargs.
24
+ default_hyperparameters (dict[str, Any]): Default hyperparameters for invoking the model.
25
+ tools (list[Tool]): Tools provided to the model to enable tool calling.
26
+ response_schema (ResponseSchema | None): The schema of the response. If provided, the model will output a
27
+ structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema dictionary.
28
+ output_analytics (bool): Whether to output the invocation analytics.
29
+ retry_config (RetryConfig): The retry configuration for the language model.
30
+
31
+ Basic usage:
32
+ The `BedrockLMInvoker` can be used as follows:
33
+ ```python
34
+ lm_invoker = BedrockLMInvoker(
35
+ model_name="us.anthropic.claude-sonnet-4-20250514-v1:0",
36
+ aws_access_key_id="<your-aws-access-key-id>",
37
+ aws_secret_access_key="<your-aws-secret-access-key>",
38
+ )
39
+ result = await lm_invoker.invoke("Hi there!")
40
+ ```
41
+
42
+ Input types:
43
+ The `BedrockLMInvoker` supports the following input types: text, document, image, and video.
44
+ Non-text inputs can be passed as an `Attachment` object with the `user` role.
45
+
46
+ Usage example:
47
+ ```python
48
+ text = "What animal is in this image?"
49
+ image = Attachment.from_path("path/to/local/image.png")
50
+ result = await lm_invoker.invoke([text, image])
51
+ ```
52
+
53
+ Tool calling:
54
+ Tool calling is a feature that allows the language model to call tools to perform tasks.
55
+ Tools can be passed to the via the `tools` parameter as a list of `Tool` objects.
56
+ When tools are provided and the model decides to call a tool, the tool calls are stored in the
57
+ `tool_calls` attribute in the output.
58
+
59
+ Usage example:
60
+ ```python
61
+ lm_invoker = BedrockLMInvoker(..., tools=[tool_1, tool_2])
62
+ ```
63
+
64
+ Output example:
65
+ ```python
66
+ LMOutput(
67
+ response="Let me call the tools...",
68
+ tool_calls=[
69
+ ToolCall(id="123", name="tool_1", args={"key": "value"}),
70
+ ToolCall(id="456", name="tool_2", args={"key": "value"}),
71
+ ]
72
+ )
73
+ ```
74
+
75
+ Structured output:
76
+ Structured output is a feature that allows the language model to output a structured response.
77
+ This feature can be enabled by providing a schema to the `response_schema` parameter.
78
+
79
+ The schema must be either a JSON schema dictionary or a Pydantic BaseModel class.
80
+ If JSON schema is used, it must be compatible with Pydantic\'s JSON schema, especially for complex schemas.
81
+ For this reason, it is recommended to create the JSON schema using Pydantic\'s `model_json_schema` method.
82
+
83
+ Structured output is achieved by providing the schema name in the `tool_choice` parameter. This forces
84
+ the model to call the provided schema as a tool. Thus, structured output is not compatible with tool calling,
85
+ since the tool calling is reserved to force the model to call the provided schema as a tool.
86
+ The language model also doesn\'t need to stream anything when structured output is enabled. Thus, standard
87
+ invocation will be performed regardless of whether the `event_emitter` parameter is provided or not.
88
+
89
+ When enabled, the structured output is stored in the `structured_output` attribute in the output.
90
+ 1. If the schema is a JSON schema dictionary, the structured output is a dictionary.
91
+ 2. If the schema is a Pydantic BaseModel class, the structured output is a Pydantic model.
92
+
93
+ # Example 1: Using a JSON schema dictionary
94
+ Usage example:
95
+ ```python
96
+ schema = {
97
+ "title": "Animal",
98
+ "description": "A description of an animal.",
99
+ "properties": {
100
+ "color": {"title": "Color", "type": "string"},
101
+ "name": {"title": "Name", "type": "string"},
102
+ },
103
+ "required": ["name", "color"],
104
+ "type": "object",
105
+ }
106
+ lm_invoker = BedrockLMInvoker(..., response_schema=schema)
107
+ ```
108
+ Output example:
109
+ ```python
110
+ LMOutput(structured_output={"name": "Golden retriever", "color": "Golden"})
111
+ ```
112
+
113
+ # Example 2: Using a Pydantic BaseModel class
114
+ Usage example:
115
+ ```python
116
+ class Animal(BaseModel):
117
+ name: str
118
+ color: str
119
+
120
+ lm_invoker = BedrockLMInvoker(..., response_schema=Animal)
121
+ ```
122
+ Output example:
123
+ ```python
124
+ LMOutput(structured_output=Animal(name="Golden retriever", color="Golden"))
125
+ ```
126
+
127
+ Analytics tracking:
128
+ Analytics tracking is a feature that allows the module to output additional information about the invocation.
129
+ This feature can be enabled by setting the `output_analytics` parameter to `True`.
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
+ response="Golden retriever is a good dog breed.",
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=0.0) # No retry, no timeout
153
+ retry_config = RetryConfig(max_retries=0, timeout=10.0) # No retry, 10.0 seconds timeout
154
+ retry_config = RetryConfig(max_retries=5, timeout=0.0) # 5 max retries, no timeout
155
+ retry_config = RetryConfig(max_retries=5, timeout=10.0) # 5 max retries, 10.0 seconds timeout
156
+ ```
157
+
158
+ Usage example:
159
+ ```python
160
+ lm_invoker = BedrockLMInvoker(..., retry_config=retry_config)
161
+ ```
162
+
163
+ Output types:
164
+ The output of the `BedrockLMInvoker` can either be:
165
+ 1. `str`: The text response if no additional output is needed.
166
+ 2. `LMOutput`: A Pydantic model with the following attributes if any additional output is needed:
167
+ 2.1. response (str): The text response.
168
+ 2.2. tool_calls (list[ToolCall]): The tool calls, if the `tools` parameter is defined and the language
169
+ model decides to invoke tools. Defaults to an empty list.
170
+ 2.3. structured_output (dict[str, Any] | BaseModel | None): The structured output, if the `response_schema`
171
+ parameter is defined. Defaults to None.
172
+ 2.4. token_usage (TokenUsage | None): The token usage information, if the `output_analytics` parameter is
173
+ set to `True`. Defaults to None.
174
+ 2.5. duration (float | None): The duration of the invocation in seconds, if the `output_analytics`
175
+ parameter is set to `True`. Defaults to None.
176
+ 2.6. finish_details (dict[str, Any]): The details about how the generation finished, if the
177
+ `output_analytics` parameter is set to `True`. Defaults to an empty dictionary.
178
+ 2.7. reasoning (list[Reasoning]): The reasoning objects. Currently not supported. Defaults to an empty list.
179
+ 2.8. citations (list[Chunk]): The citations. Currently not supported. Defaults to an empty list.
180
+ 2.9. code_exec_results (list[CodeExecResult]): The code execution results. Currently not supported.
181
+ Defaults to an empty list.
182
+ '''
183
+ session: Incomplete
184
+ client_kwargs: Incomplete
185
+ 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:
186
+ '''Initializes the BedrockLMInvoker instance.
187
+
188
+ Args:
189
+ model_name (str): The name of the Bedrock language model.
190
+ access_key_id (str | None, optional): The AWS access key ID. Defaults to None, in which case
191
+ the `AWS_ACCESS_KEY_ID` environment variable will be used.
192
+ secret_access_key (str | None, optional): The AWS secret access key. Defaults to None, in which case
193
+ the `AWS_SECRET_ACCESS_KEY` environment variable will be used.
194
+ region_name (str, optional): The AWS region name. Defaults to "us-east-1".
195
+ model_kwargs (dict[str, Any] | None, optional): Additional keyword arguments for the Bedrock client.
196
+ default_hyperparameters (dict[str, Any] | None, optional): Default hyperparameters for invoking the model.
197
+ Defaults to None.
198
+ tools (list[Tool | LangChainTool] | None, optional): Tools provided to the model to enable tool calling.
199
+ Defaults to None.
200
+ response_schema (ResponseSchema | None, optional): The schema of the response. If provided, the model will
201
+ output a structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema
202
+ dictionary. Defaults to None.
203
+ output_analytics (bool, optional): Whether to output the invocation analytics. Defaults to False.
204
+ retry_config (RetryConfig | None, optional): The retry configuration for the language model.
205
+ Defaults to None, in which case a default config with no retry and 30.0 seconds timeout will be used.
206
+
207
+ Raises:
208
+ ValueError: If `response_schema` is provided, but `tools` are also provided.
209
+ ValueError: If `access_key_id` or `secret_access_key` is neither provided nor set in the
210
+ `AWS_ACCESS_KEY_ID` or `AWS_SECRET_ACCESS_KEY` environment variables, respectively.
211
+ '''
212
+ def set_tools(self, tools: list[Tool | LangChainTool]) -> None:
213
+ """Sets the tools for the Bedrock language model.
214
+
215
+ This method sets the tools for the Bedrock language model. Any existing tools will be replaced.
216
+
217
+ Args:
218
+ tools (list[Tool | LangChainTool]): The list of tools to be used.
219
+
220
+ Raises:
221
+ ValueError: If `response_schema` exists.
222
+ """
223
+ def set_response_schema(self, response_schema: ResponseSchema | None) -> None:
224
+ """Sets the response schema for the Bedrock language model.
225
+
226
+ This method sets the response schema for the Bedrock language model. Any existing response schema will be
227
+ replaced.
228
+
229
+ Args:
230
+ response_schema (ResponseSchema | None): The response schema to be used.
231
+
232
+ Raises:
233
+ ValueError: If `tools` exists.
234
+ """
@@ -0,0 +1,166 @@
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_compatible_lm_invoker import OpenAICompatibleLMInvoker as OpenAICompatibleLMInvoker
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(OpenAICompatibleLMInvoker):
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 (AsyncOpenAI): The OpenAI client instance.
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=0.0) # 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=0.0) # 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`: The text response if no additional output is needed.
107
+ 2. `LMOutput`: A Pydantic model with the following attributes if any additional output is needed:
108
+ 2.1. response (str): The text response.
109
+ 2.2. tool_calls (list[ToolCall]): The tool calls. Currently not supported. Defaults to an empty list.
110
+ 2.3. structured_output (dict[str, Any] | BaseModel | None): The structured output. Currently not supported.
111
+ Defaults to None.
112
+ 2.4. token_usage (TokenUsage | None): The token usage analytics, if the `output_analytics` parameter is
113
+ set to `True`. Defaults to None.
114
+ 2.5. duration (float | None): The duration of the invocation in seconds, if the `output_analytics`
115
+ parameter is set to `True`. Defaults to None.
116
+ 2.6. finish_details (dict[str, Any] | None): The details about how the generation finished, if the
117
+ `output_analytics` parameter is set to `True`. Defaults to None.
118
+ 2.7. reasoning (list[Reasoning]): The reasoning objects. Currently not supported. Defaults to an empty list.
119
+ 2.8. citations (list[Chunk]): The citations. Currently not supported. Defaults to an empty list.
120
+ 2.9. code_exec_results (list[CodeExecResult]): The code execution results. Currently not supported.
121
+ Defaults to an empty list.
122
+ '''
123
+ client: Incomplete
124
+ citations: Incomplete
125
+ 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:
126
+ """Initializes a new instance of the DatasaurLMInvoker class.
127
+
128
+ Args:
129
+ base_url (str): The base URL of the Datasaur LLM Projects Deployment API.
130
+ api_key (str | None, optional): The API key for authenticating with Datasaur LLM Projects Deployment API.
131
+ Defaults to None, in which case the `DATASAUR_API_KEY` environment variable will be used.
132
+ model_kwargs (dict[str, Any] | None, optional): Additional model parameters. Defaults to None.
133
+ default_hyperparameters (dict[str, Any] | None, optional): Default hyperparameters for invoking the model.
134
+ Defaults to None.
135
+ output_analytics (bool, optional): Whether to output the invocation analytics. Defaults to False.
136
+ retry_config (RetryConfig | None, optional): The retry configuration for the language model.
137
+ Defaults to None, in which case a default config with no retry and 30.0 seconds timeout will be used.
138
+ citations (bool, optional): Whether to output the citations. Defaults to False.
139
+
140
+ Raises:
141
+ ValueError: If the `api_key` is not provided and the `DATASAUR_API_KEY` environment variable is not set.
142
+ """
143
+ def set_tools(self, tools: list[Tool | LangChainTool]) -> None:
144
+ """Sets the tools for the Datasaur LLM Projects Deployment API.
145
+
146
+ This method is raises a `NotImplementedError` because the Datasaur LLM Projects Deployment API does not
147
+ support tools.
148
+
149
+ Args:
150
+ tools (list[Tool | LangChainTool]): The list of tools to be used.
151
+
152
+ Raises:
153
+ NotImplementedError: This method is not supported for the Datasaur LLM Projects Deployment API.
154
+ """
155
+ def set_response_schema(self, response_schema: ResponseSchema | None) -> None:
156
+ """Sets the response schema for the Datasaur LLM Projects Deployment API.
157
+
158
+ This method is raises a `NotImplementedError` because the Datasaur LLM Projects Deployment API does not
159
+ support response schema.
160
+
161
+ Args:
162
+ response_schema (ResponseSchema | None): The response schema to be used.
163
+
164
+ Raises:
165
+ NotImplementedError: This method is not supported for the Datasaur LLM Projects Deployment API.
166
+ """