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.
Files changed (137) hide show
  1. gllm_inference/__init__.pyi +0 -0
  2. gllm_inference/builder/__init__.pyi +6 -0
  3. gllm_inference/builder/_build_invoker.pyi +28 -0
  4. gllm_inference/builder/build_em_invoker.pyi +130 -0
  5. gllm_inference/builder/build_lm_invoker.pyi +213 -0
  6. gllm_inference/builder/build_lm_request_processor.pyi +88 -0
  7. gllm_inference/builder/build_output_parser.pyi +29 -0
  8. gllm_inference/catalog/__init__.pyi +4 -0
  9. gllm_inference/catalog/catalog.pyi +121 -0
  10. gllm_inference/catalog/lm_request_processor_catalog.pyi +112 -0
  11. gllm_inference/catalog/prompt_builder_catalog.pyi +82 -0
  12. gllm_inference/constants.pyi +13 -0
  13. gllm_inference/em_invoker/__init__.pyi +12 -0
  14. gllm_inference/em_invoker/azure_openai_em_invoker.pyi +88 -0
  15. gllm_inference/em_invoker/bedrock_em_invoker.pyi +118 -0
  16. gllm_inference/em_invoker/cohere_em_invoker.pyi +128 -0
  17. gllm_inference/em_invoker/em_invoker.pyi +90 -0
  18. gllm_inference/em_invoker/google_em_invoker.pyi +129 -0
  19. gllm_inference/em_invoker/jina_em_invoker.pyi +103 -0
  20. gllm_inference/em_invoker/langchain/__init__.pyi +3 -0
  21. gllm_inference/em_invoker/langchain/em_invoker_embeddings.pyi +84 -0
  22. gllm_inference/em_invoker/langchain_em_invoker.pyi +46 -0
  23. gllm_inference/em_invoker/openai_compatible_em_invoker.pyi +41 -0
  24. gllm_inference/em_invoker/openai_em_invoker.pyi +118 -0
  25. gllm_inference/em_invoker/schema/__init__.pyi +0 -0
  26. gllm_inference/em_invoker/schema/bedrock.pyi +29 -0
  27. gllm_inference/em_invoker/schema/cohere.pyi +20 -0
  28. gllm_inference/em_invoker/schema/google.pyi +9 -0
  29. gllm_inference/em_invoker/schema/jina.pyi +29 -0
  30. gllm_inference/em_invoker/schema/langchain.pyi +5 -0
  31. gllm_inference/em_invoker/schema/openai.pyi +7 -0
  32. gllm_inference/em_invoker/schema/openai_compatible.pyi +7 -0
  33. gllm_inference/em_invoker/schema/twelvelabs.pyi +17 -0
  34. gllm_inference/em_invoker/schema/voyage.pyi +15 -0
  35. gllm_inference/em_invoker/twelevelabs_em_invoker.pyi +101 -0
  36. gllm_inference/em_invoker/voyage_em_invoker.pyi +104 -0
  37. gllm_inference/exceptions/__init__.pyi +4 -0
  38. gllm_inference/exceptions/error_parser.pyi +41 -0
  39. gllm_inference/exceptions/exceptions.pyi +132 -0
  40. gllm_inference/exceptions/provider_error_map.pyi +24 -0
  41. gllm_inference/lm_invoker/__init__.pyi +14 -0
  42. gllm_inference/lm_invoker/anthropic_lm_invoker.pyi +334 -0
  43. gllm_inference/lm_invoker/azure_openai_lm_invoker.pyi +254 -0
  44. gllm_inference/lm_invoker/batch/__init__.pyi +3 -0
  45. gllm_inference/lm_invoker/batch/batch_operations.pyi +127 -0
  46. gllm_inference/lm_invoker/bedrock_lm_invoker.pyi +226 -0
  47. gllm_inference/lm_invoker/datasaur_lm_invoker.pyi +157 -0
  48. gllm_inference/lm_invoker/google_lm_invoker.pyi +336 -0
  49. gllm_inference/lm_invoker/langchain_lm_invoker.pyi +251 -0
  50. gllm_inference/lm_invoker/litellm_lm_invoker.pyi +244 -0
  51. gllm_inference/lm_invoker/lm_invoker.pyi +164 -0
  52. gllm_inference/lm_invoker/openai_chat_completions_lm_invoker.pyi +272 -0
  53. gllm_inference/lm_invoker/openai_compatible_lm_invoker.pyi +52 -0
  54. gllm_inference/lm_invoker/openai_lm_invoker.pyi +433 -0
  55. gllm_inference/lm_invoker/portkey_lm_invoker.pyi +260 -0
  56. gllm_inference/lm_invoker/schema/__init__.pyi +0 -0
  57. gllm_inference/lm_invoker/schema/anthropic.pyi +56 -0
  58. gllm_inference/lm_invoker/schema/bedrock.pyi +53 -0
  59. gllm_inference/lm_invoker/schema/datasaur.pyi +14 -0
  60. gllm_inference/lm_invoker/schema/google.pyi +24 -0
  61. gllm_inference/lm_invoker/schema/langchain.pyi +23 -0
  62. gllm_inference/lm_invoker/schema/openai.pyi +106 -0
  63. gllm_inference/lm_invoker/schema/openai_chat_completions.pyi +62 -0
  64. gllm_inference/lm_invoker/schema/portkey.pyi +31 -0
  65. gllm_inference/lm_invoker/schema/xai.pyi +31 -0
  66. gllm_inference/lm_invoker/xai_lm_invoker.pyi +289 -0
  67. gllm_inference/model/__init__.pyi +12 -0
  68. gllm_inference/model/em/__init__.pyi +0 -0
  69. gllm_inference/model/em/cohere_em.pyi +17 -0
  70. gllm_inference/model/em/google_em.pyi +16 -0
  71. gllm_inference/model/em/jina_em.pyi +22 -0
  72. gllm_inference/model/em/openai_em.pyi +15 -0
  73. gllm_inference/model/em/twelvelabs_em.pyi +13 -0
  74. gllm_inference/model/em/voyage_em.pyi +20 -0
  75. gllm_inference/model/lm/__init__.pyi +0 -0
  76. gllm_inference/model/lm/anthropic_lm.pyi +22 -0
  77. gllm_inference/model/lm/google_lm.pyi +18 -0
  78. gllm_inference/model/lm/openai_lm.pyi +27 -0
  79. gllm_inference/model/lm/xai_lm.pyi +19 -0
  80. gllm_inference/output_parser/__init__.pyi +3 -0
  81. gllm_inference/output_parser/json_output_parser.pyi +60 -0
  82. gllm_inference/output_parser/output_parser.pyi +27 -0
  83. gllm_inference/prompt_builder/__init__.pyi +3 -0
  84. gllm_inference/prompt_builder/format_strategy/__init__.pyi +4 -0
  85. gllm_inference/prompt_builder/format_strategy/format_strategy.pyi +55 -0
  86. gllm_inference/prompt_builder/format_strategy/jinja_format_strategy.pyi +45 -0
  87. gllm_inference/prompt_builder/format_strategy/string_format_strategy.pyi +20 -0
  88. gllm_inference/prompt_builder/prompt_builder.pyi +69 -0
  89. gllm_inference/prompt_formatter/__init__.pyi +7 -0
  90. gllm_inference/prompt_formatter/agnostic_prompt_formatter.pyi +49 -0
  91. gllm_inference/prompt_formatter/huggingface_prompt_formatter.pyi +55 -0
  92. gllm_inference/prompt_formatter/llama_prompt_formatter.pyi +59 -0
  93. gllm_inference/prompt_formatter/mistral_prompt_formatter.pyi +53 -0
  94. gllm_inference/prompt_formatter/openai_prompt_formatter.pyi +35 -0
  95. gllm_inference/prompt_formatter/prompt_formatter.pyi +30 -0
  96. gllm_inference/realtime_chat/__init__.pyi +3 -0
  97. gllm_inference/realtime_chat/google_realtime_chat.pyi +205 -0
  98. gllm_inference/realtime_chat/input_streamer/__init__.pyi +4 -0
  99. gllm_inference/realtime_chat/input_streamer/input_streamer.pyi +36 -0
  100. gllm_inference/realtime_chat/input_streamer/keyboard_input_streamer.pyi +27 -0
  101. gllm_inference/realtime_chat/input_streamer/linux_mic_input_streamer.pyi +36 -0
  102. gllm_inference/realtime_chat/output_streamer/__init__.pyi +4 -0
  103. gllm_inference/realtime_chat/output_streamer/console_output_streamer.pyi +21 -0
  104. gllm_inference/realtime_chat/output_streamer/linux_speaker_output_streamer.pyi +42 -0
  105. gllm_inference/realtime_chat/output_streamer/output_streamer.pyi +33 -0
  106. gllm_inference/realtime_chat/realtime_chat.pyi +28 -0
  107. gllm_inference/request_processor/__init__.pyi +4 -0
  108. gllm_inference/request_processor/lm_request_processor.pyi +101 -0
  109. gllm_inference/request_processor/uses_lm_mixin.pyi +130 -0
  110. gllm_inference/schema/__init__.pyi +18 -0
  111. gllm_inference/schema/activity.pyi +64 -0
  112. gllm_inference/schema/attachment.pyi +88 -0
  113. gllm_inference/schema/code_exec_result.pyi +14 -0
  114. gllm_inference/schema/config.pyi +15 -0
  115. gllm_inference/schema/enums.pyi +69 -0
  116. gllm_inference/schema/events.pyi +105 -0
  117. gllm_inference/schema/lm_input.pyi +4 -0
  118. gllm_inference/schema/lm_output.pyi +44 -0
  119. gllm_inference/schema/mcp.pyi +31 -0
  120. gllm_inference/schema/message.pyi +52 -0
  121. gllm_inference/schema/model_id.pyi +176 -0
  122. gllm_inference/schema/reasoning.pyi +15 -0
  123. gllm_inference/schema/token_usage.pyi +75 -0
  124. gllm_inference/schema/tool_call.pyi +14 -0
  125. gllm_inference/schema/tool_result.pyi +11 -0
  126. gllm_inference/schema/type_alias.pyi +11 -0
  127. gllm_inference/utils/__init__.pyi +5 -0
  128. gllm_inference/utils/io_utils.pyi +26 -0
  129. gllm_inference/utils/langchain.pyi +30 -0
  130. gllm_inference/utils/validation.pyi +12 -0
  131. gllm_inference.build/.gitignore +1 -0
  132. gllm_inference.cpython-313-darwin.so +0 -0
  133. gllm_inference.pyi +157 -0
  134. gllm_inference_binary-0.5.51b3.dist-info/METADATA +138 -0
  135. gllm_inference_binary-0.5.51b3.dist-info/RECORD +137 -0
  136. gllm_inference_binary-0.5.51b3.dist-info/WHEEL +5 -0
  137. gllm_inference_binary-0.5.51b3.dist-info/top_level.txt +1 -0
@@ -0,0 +1,244 @@
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.lm_invoker.openai_chat_completions_lm_invoker import OpenAIChatCompletionsLMInvoker as OpenAIChatCompletionsLMInvoker
6
+ from gllm_inference.lm_invoker.openai_lm_invoker import ReasoningEffort as ReasoningEffort
7
+ from gllm_inference.schema import AttachmentType as AttachmentType, LMOutput as LMOutput, 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
+ SUPPORTED_ATTACHMENTS: Incomplete
12
+
13
+ class LiteLLMLMInvoker(OpenAIChatCompletionsLMInvoker):
14
+ '''A language model invoker to interact with language models using LiteLLM.
15
+
16
+ Attributes:
17
+ model_id (str): The model ID of the language model.
18
+ model_provider (str): The provider of the language model.
19
+ model_name (str): The name of the language model.
20
+ completion (function): The LiteLLM\'s completion function.
21
+ default_hyperparameters (dict[str, Any]): Default hyperparameters for invoking the model.
22
+ tools (list[Tool]): The list of tools provided to the model to enable tool calling.
23
+ response_schema (ResponseSchema | None): The schema of the response. If provided, the model will output a
24
+ structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema dictionary.
25
+ output_analytics (bool): Whether to output the invocation analytics.
26
+
27
+ Basic usage:
28
+ The `LiteLLMLMInvoker` can be used as follows:
29
+ ```python
30
+ lm_invoker = LiteLLMLMInvoker(model_id="openai/gpt-5-nano")
31
+ result = await lm_invoker.invoke("Hi there!")
32
+ ```
33
+
34
+ Initialization:
35
+ The `LiteLLMLMInvoker` provides an interface to interact with multiple language model providers.
36
+ In order to use this class:
37
+ 1. The `model_id` parameter must be in the format of `provider/model_name`. e.g. `openai/gpt-4o-mini`.
38
+ 2. The required credentials must be provided via the environment variables.
39
+
40
+ Usage example:
41
+ ```python
42
+ os.environ["OPENAI_API_KEY"] = "your_openai_api_key"
43
+ lm_invoker = LiteLLMLMInvoker(model_id="openai/gpt-4o-mini")
44
+ ```
45
+
46
+ For the complete list of supported providers and their required credentials, please refer to the
47
+ LiteLLM documentation: https://docs.litellm.ai/docs/providers/
48
+
49
+ Input types:
50
+ The `LiteLLMLMInvoker` supports the following input types: text, audio, and image.
51
+ Non-text inputs can be passed as a `Attachment` object with the `user` role.
52
+
53
+ Usage example:
54
+ ```python
55
+ text = "What animal is in this image?"
56
+ image = Attachment.from_path("path/to/local/image.png")
57
+ result = await lm_invoker.invoke([text, image])
58
+ ```
59
+
60
+ Tool calling:
61
+ Tool calling is a feature that allows the language model to call tools to perform tasks.
62
+ Tools can be passed to the via the `tools` parameter as a list of `Tool` objects.
63
+ When tools are provided and the model decides to call a tool, the tool calls are stored in the
64
+ `tool_calls` attribute in the output.
65
+
66
+ Usage example:
67
+ ```python
68
+ lm_invoker = LiteLLMLMInvoker(..., tools=[tool_1, tool_2])
69
+ ```
70
+
71
+ Output example:
72
+ ```python
73
+ LMOutput(
74
+ response="Let me call the tools...",
75
+ tool_calls=[
76
+ ToolCall(id="123", name="tool_1", args={"key": "value"}),
77
+ ToolCall(id="456", name="tool_2", args={"key": "value"}),
78
+ ]
79
+ )
80
+ ```
81
+
82
+ Structured output:
83
+ Structured output is a feature that allows the language model to output a structured response.
84
+ This feature can be enabled by providing a schema to the `response_schema` parameter.
85
+
86
+ The schema must be either a JSON schema dictionary or a Pydantic BaseModel class.
87
+ If JSON schema is used, it must be compatible with Pydantic\'s JSON schema, especially for complex schemas.
88
+ For this reason, it is recommended to create the JSON schema using Pydantic\'s `model_json_schema` method.
89
+
90
+ The language model also doesn\'t need to stream anything when structured output is enabled. Thus, standard
91
+ invocation will be performed regardless of whether the `event_emitter` parameter is provided or not.
92
+
93
+ When enabled, the structured output is stored in the `structured_output` attribute in the output.
94
+ 1. If the schema is a JSON schema dictionary, the structured output is a dictionary.
95
+ 2. If the schema is a Pydantic BaseModel class, the structured output is a Pydantic model.
96
+
97
+ # Example 1: Using a JSON schema dictionary
98
+ Usage example:
99
+ ```python
100
+ schema = {
101
+ "title": "Animal",
102
+ "description": "A description of an animal.",
103
+ "properties": {
104
+ "color": {"title": "Color", "type": "string"},
105
+ "name": {"title": "Name", "type": "string"},
106
+ },
107
+ "required": ["name", "color"],
108
+ "type": "object",
109
+ }
110
+ lm_invoker = LiteLLMLMInvoker(..., response_schema=schema)
111
+ ```
112
+ Output example:
113
+ ```python
114
+ LMOutput(structured_output={"name": "Golden retriever", "color": "Golden"})
115
+ ```
116
+
117
+ # Example 2: Using a Pydantic BaseModel class
118
+ Usage example:
119
+ ```python
120
+ class Animal(BaseModel):
121
+ name: str
122
+ color: str
123
+
124
+ lm_invoker = LiteLLMLMInvoker(..., response_schema=Animal)
125
+ ```
126
+ Output example:
127
+ ```python
128
+ LMOutput(structured_output=Animal(name="Golden retriever", color="Golden"))
129
+ ```
130
+
131
+ Analytics tracking:
132
+ Analytics tracking is a feature that allows the module to output additional information about the invocation.
133
+ This feature can be enabled by setting the `output_analytics` parameter to `True`.
134
+ When enabled, the following attributes will be stored in the output:
135
+ 1. `token_usage`: The token usage.
136
+ 2. `duration`: The duration in seconds.
137
+ 3. `finish_details`: The details about how the generation finished.
138
+
139
+ Output example:
140
+ ```python
141
+ LMOutput(
142
+ response="Golden retriever is a good dog breed.",
143
+ token_usage=TokenUsage(input_tokens=100, output_tokens=50),
144
+ duration=0.729,
145
+ finish_details={"finish_reason": "stop"},
146
+ )
147
+ ```
148
+
149
+ When streaming is enabled, token usage is not supported. Therefore, the `token_usage` attribute will be `None`
150
+ regardless of the value of the `output_analytics` parameter.
151
+
152
+ Retry and timeout:
153
+ The `LiteLLMLMInvoker` supports retry and timeout configuration.
154
+ By default, the max retries is set to 0 and the timeout is set to 30.0 seconds.
155
+ They can be customized by providing a custom `RetryConfig` object to the `retry_config` parameter.
156
+
157
+ Retry config examples:
158
+ ```python
159
+ retry_config = RetryConfig(max_retries=0, timeout=None) # No retry, no timeout
160
+ retry_config = RetryConfig(max_retries=0, timeout=10.0) # No retry, 10.0 seconds timeout
161
+ retry_config = RetryConfig(max_retries=5, timeout=None) # 5 max retries, no timeout
162
+ retry_config = RetryConfig(max_retries=5, timeout=10.0) # 5 max retries, 10.0 seconds timeout
163
+ ```
164
+
165
+ Usage example:
166
+ ```python
167
+ lm_invoker = LiteLLMLMInvoker(..., retry_config=retry_config)
168
+ ```
169
+
170
+ Reasoning:
171
+ Some language models support advanced reasoning capabilities. When using such reasoning-capable models,
172
+ you can configure how much reasoning the model should perform before generating a final response by setting
173
+ reasoning-related parameters.
174
+
175
+ The reasoning effort of reasoning models can be set via the `reasoning_effort` parameter. This parameter
176
+ will guide the models on how many reasoning tokens it should generate before creating a response to the prompt.
177
+ The reasoning effort is only supported by some language models.
178
+ Available options include:
179
+ 1. "low": Favors speed and economical token usage.
180
+ 2. "medium": Favors a balance between speed and reasoning accuracy.
181
+ 3. "high": Favors more complete reasoning at the cost of more tokens generated and slower responses.
182
+ This may differ between models. When not set, the reasoning effort will be equivalent to None by default.
183
+
184
+ When using reasoning models, some providers might output the reasoning summary. These will be stored in the
185
+ `reasoning` attribute in the output.
186
+
187
+ Output example:
188
+ ```python
189
+ LMOutput(
190
+ response="Golden retriever is a good dog breed.",
191
+ reasoning=[Reasoning(id="", reasoning="Let me think about it...")],
192
+ )
193
+ ```
194
+
195
+ Streaming output example:
196
+ ```python
197
+ {"type": "thinking_start", "value": ""}\', ...}
198
+ {"type": "thinking", "value": "Let me think "}\', ...}
199
+ {"type": "thinking", "value": "about it..."}\', ...}
200
+ {"type": "thinking_end", "value": ""}\', ...}
201
+ {"type": "response", "value": "Golden retriever ", ...}
202
+ {"type": "response", "value": "is a good dog breed.", ...}
203
+ ```
204
+ Note: By default, the thinking token will be streamed with the legacy `EventType.DATA` event type.
205
+ To use the new simplified streamed event format, set the `simplify_events` parameter to `True` during
206
+ LM invoker initialization. The legacy event format support will be removed in v0.6.
207
+
208
+ Setting reasoning-related parameters for non-reasoning models will raise an error.
209
+
210
+
211
+ Output types:
212
+ The output of the `LiteLLMLMInvoker` can either be:
213
+ 1. `str`: A text response.
214
+ 2. `LMOutput`: A Pydantic model that may contain the following attributes:
215
+ 2.1. response (str)
216
+ 2.2. tool_calls (list[ToolCall])
217
+ 2.3. structured_output (dict[str, Any] | BaseModel | None)
218
+ 2.4. token_usage (TokenUsage | None)
219
+ 2.5. duration (float | None)
220
+ 2.6. finish_details (dict[str, Any])
221
+ 2.7. reasoning (list[Reasoning])
222
+ '''
223
+ completion: Incomplete
224
+ def __init__(self, model_id: str, 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, simplify_events: bool = False) -> None:
225
+ """Initializes a new instance of the LiteLLMLMInvoker class.
226
+
227
+ Args:
228
+ model_id (str): The ID of the model to use. Must be in the format of `provider/model_name`.
229
+ default_hyperparameters (dict[str, Any] | None, optional): Default hyperparameters for invoking the model.
230
+ Defaults to None.
231
+ tools (list[Tool | LangChainTool] | None, optional): Tools provided to the model to enable tool calling.
232
+ Defaults to None.
233
+ response_schema (ResponseSchema | None, optional): The schema of the response. If provided, the model will
234
+ output a structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema
235
+ dictionary. Defaults to None.
236
+ output_analytics (bool, optional): Whether to output the invocation analytics. Defaults to False.
237
+ retry_config (RetryConfig | None, optional): The retry configuration for the language model.
238
+ Defaults to None, in which case a default config with no retry and 30.0 seconds timeout will be used.
239
+ reasoning_effort (ReasoningEffort | None, optional): The reasoning effort for reasoning models.
240
+ Defaults to None.
241
+ simplify_events (bool, optional): Temporary parameter to control the streamed events format.
242
+ When True, uses the simplified events format. When False, uses the legacy events format for
243
+ backward compatibility. Will be removed in v0.6. Defaults to False.
244
+ """
@@ -0,0 +1,164 @@
1
+ import abc
2
+ from _typeshed import Incomplete
3
+ from abc import ABC
4
+ from gllm_core.event import EventEmitter as EventEmitter
5
+ from gllm_core.schema import Event as Event, Tool
6
+ from gllm_core.utils import RetryConfig
7
+ from gllm_inference.constants import DOCUMENT_MIME_TYPES as DOCUMENT_MIME_TYPES, INVOKER_DEFAULT_TIMEOUT as INVOKER_DEFAULT_TIMEOUT
8
+ from gllm_inference.exceptions import BaseInvokerError as BaseInvokerError, convert_to_base_invoker_error as convert_to_base_invoker_error
9
+ from gllm_inference.lm_invoker.batch import BatchOperations as BatchOperations
10
+ from gllm_inference.schema import Attachment as Attachment, AttachmentType as AttachmentType, BatchStatus as BatchStatus, LMEventType as LMEventType, LMInput as LMInput, LMOutput as LMOutput, Message as Message, MessageContent as MessageContent, MessageRole as MessageRole, ModelId as ModelId, Reasoning as Reasoning, ResponseSchema as ResponseSchema, ToolCall as ToolCall, ToolResult as ToolResult
11
+ from langchain_core.tools import Tool as LangChainTool
12
+ from typing import Any
13
+
14
+ class Key:
15
+ """Defines valid keys in LM invokers JSON schema."""
16
+ ADDITIONAL_PROPERTIES: str
17
+ ANY_OF: str
18
+ ARGS_SCHEMA: str
19
+ ARUN: str
20
+ COROUTINE: str
21
+ DATA_TYPE: str
22
+ DATA_VALUE: str
23
+ DEFAULT: str
24
+ DESCRIPTION: str
25
+ FUNC: str
26
+ ID: str
27
+ NAME: str
28
+ PROPERTIES: str
29
+ REQUIRED: str
30
+ TITLE: str
31
+ TYPE: str
32
+
33
+ class InputType:
34
+ """Defines valid input types in LM invokers JSON schema."""
35
+ NULL: str
36
+
37
+ class BaseLMInvoker(ABC, metaclass=abc.ABCMeta):
38
+ """A base class for language model invokers used in Gen AI applications.
39
+
40
+ The `BaseLMInvoker` class provides a framework for invoking language models.
41
+ It handles both standard and streaming invocation.
42
+
43
+ Attributes:
44
+ model_id (str): The model ID of the language model.
45
+ model_provider (str): The provider of the language model.
46
+ model_name (str): The name of the language model.
47
+ default_hyperparameters (dict[str, Any]): Default hyperparameters for invoking the language model.
48
+ tools (list[Tool]): Tools provided to the language model to enable tool calling.
49
+ response_schema (ResponseSchema | None): The schema of the response. If provided, the model will output a
50
+ structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema dictionary.
51
+ output_analytics (bool): Whether to output the invocation analytics.
52
+ retry_config (RetryConfig): The retry configuration for the language model.
53
+ """
54
+ default_hyperparameters: Incomplete
55
+ tools: Incomplete
56
+ response_schema: Incomplete
57
+ output_analytics: Incomplete
58
+ retry_config: Incomplete
59
+ def __init__(self, model_id: ModelId, default_hyperparameters: dict[str, Any] | None = None, supported_attachments: set[str] | None = None, tools: list[Tool | LangChainTool] | None = None, response_schema: ResponseSchema | None = None, output_analytics: bool = False, retry_config: RetryConfig | None = None, simplify_events: bool = False) -> None:
60
+ """Initializes a new instance of the BaseLMInvoker class.
61
+
62
+ Args:
63
+ model_id (ModelId): The model ID of the language model.
64
+ default_hyperparameters (dict[str, Any] | None, optional): Default hyperparameters for invoking the
65
+ language model. Defaults to None, in which case an empty dictionary is used.
66
+ supported_attachments (set[str] | None, optional): A set of supported attachment types. Defaults to None,
67
+ in which case an empty set is used (indicating that no attachments are supported).
68
+ tools (list[Tool | LangChainTool] | None, optional): Tools provided to the model to enable tool calling.
69
+ Defaults to None, in which case an empty list is used.
70
+ response_schema (ResponseSchema | None, optional): The schema of the response. If provided, the model will
71
+ output a structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema
72
+ dictionary. Defaults to None.
73
+ output_analytics (bool, optional): Whether to output the invocation analytics. Defaults to False.
74
+ retry_config (RetryConfig | None, optional): The retry configuration for the language model.
75
+ Defaults to None, in which case a default config with no retry and 30.0 seconds timeout will be used.
76
+ simplify_events (bool, optional): Temporary parameter to control the streamed events format.
77
+ When True, uses the simplified events format. When False, uses the legacy events format for
78
+ backward compatibility. Will be removed in v0.6. Defaults to False.
79
+ """
80
+ @property
81
+ def model_id(self) -> str:
82
+ """The model ID of the language model.
83
+
84
+ Returns:
85
+ str: The model ID of the language model.
86
+ """
87
+ @property
88
+ def model_provider(self) -> str:
89
+ """The provider of the language model.
90
+
91
+ Returns:
92
+ str: The provider of the language model.
93
+ """
94
+ @property
95
+ def model_name(self) -> str:
96
+ """The name of the language model.
97
+
98
+ Returns:
99
+ str: The name of the language model.
100
+ """
101
+ @property
102
+ def batch(self) -> BatchOperations:
103
+ """The batch operations for the language model.
104
+
105
+ Returns:
106
+ BatchOperations: The batch operations for the language model.
107
+ """
108
+ def set_tools(self, tools: list[Tool | LangChainTool]) -> None:
109
+ """Sets the tools for the language model.
110
+
111
+ This method sets the tools for the language model. Any existing tools will be replaced.
112
+
113
+ Args:
114
+ tools (list[Tool | LangChainTool]): The list of tools to be used.
115
+ """
116
+ def clear_tools(self) -> None:
117
+ """Clears the tools for the language model.
118
+
119
+ This method clears the tools for the language model by calling the `set_tools` method with an empty list.
120
+ """
121
+ def set_response_schema(self, response_schema: ResponseSchema | None) -> None:
122
+ """Sets the response schema for the language model.
123
+
124
+ This method sets the response schema for the language model. Any existing response schema will be replaced.
125
+
126
+ Args:
127
+ response_schema (ResponseSchema | None): The response schema to be used.
128
+ """
129
+ def clear_response_schema(self) -> None:
130
+ """Clears the response schema for the language model.
131
+
132
+ This method clears the response schema for the language model by calling the `set_response_schema` method with
133
+ None.
134
+ """
135
+ async def invoke(self, messages: LMInput, hyperparameters: dict[str, Any] | None = None, event_emitter: EventEmitter | None = None) -> str | LMOutput:
136
+ """Invokes the language model.
137
+
138
+ This method validates the messages and invokes the language model. It handles both standard
139
+ and streaming invocation. Streaming mode is enabled if an event emitter is provided.
140
+ The method includes retry logic with exponential backoff for transient failures.
141
+
142
+ Args:
143
+ messages (LMInput): The input messages for the language model.
144
+ 1. If a list of Message objects is provided, it is used as is.
145
+ 2. If a list of MessageContent or a string is provided, it is converted into a user message.
146
+ hyperparameters (dict[str, Any] | None, optional): A dictionary of hyperparameters for the language model.
147
+ Defaults to None, in which case the default hyperparameters are used.
148
+ event_emitter (EventEmitter | None, optional): The event emitter for streaming tokens. If provided,
149
+ streaming invocation is enabled. Defaults to None.
150
+
151
+ Returns:
152
+ str | LMOutput: The generated response from the language model.
153
+
154
+ Raises:
155
+ CancelledError: If the invocation is cancelled.
156
+ ModelNotFoundError: If the model is not found.
157
+ ProviderAuthError: If the model authentication fails.
158
+ ProviderInternalError: If the model internal error occurs.
159
+ ProviderInvalidArgsError: If the model parameters are invalid.
160
+ ProviderOverloadedError: If the model is overloaded.
161
+ ProviderRateLimitError: If the model rate limit is exceeded.
162
+ TimeoutError: If the invocation times out.
163
+ ValueError: If the messages are not in the correct format.
164
+ """