gllm-inference-binary 0.5.55__cp311-cp311-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.

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 +12 -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 +318 -0
  43. gllm_inference/lm_invoker/azure_openai_lm_invoker.pyi +237 -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 +212 -0
  47. gllm_inference/lm_invoker/datasaur_lm_invoker.pyi +157 -0
  48. gllm_inference/lm_invoker/google_lm_invoker.pyi +327 -0
  49. gllm_inference/lm_invoker/langchain_lm_invoker.pyi +239 -0
  50. gllm_inference/lm_invoker/litellm_lm_invoker.pyi +224 -0
  51. gllm_inference/lm_invoker/lm_invoker.pyi +165 -0
  52. gllm_inference/lm_invoker/openai_chat_completions_lm_invoker.pyi +253 -0
  53. gllm_inference/lm_invoker/openai_compatible_lm_invoker.pyi +52 -0
  54. gllm_inference/lm_invoker/openai_lm_invoker.pyi +404 -0
  55. gllm_inference/lm_invoker/portkey_lm_invoker.pyi +296 -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 +253 -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 +80 -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 +188 -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-311-darwin.so +0 -0
  133. gllm_inference.pyi +153 -0
  134. gllm_inference_binary-0.5.55.dist-info/METADATA +138 -0
  135. gllm_inference_binary-0.5.55.dist-info/RECORD +137 -0
  136. gllm_inference_binary-0.5.55.dist-info/WHEEL +5 -0
  137. gllm_inference_binary-0.5.55.dist-info/top_level.txt +1 -0
@@ -0,0 +1,327 @@
1
+ from _typeshed import Incomplete
2
+ from gllm_core.event import EventEmitter as EventEmitter
3
+ from gllm_core.schema.tool import Tool
4
+ from gllm_core.utils.retry import RetryConfig as RetryConfig
5
+ from gllm_inference.constants import GOOGLE_SCOPES as GOOGLE_SCOPES, SECONDS_TO_MILLISECONDS as SECONDS_TO_MILLISECONDS
6
+ from gllm_inference.exceptions import BaseInvokerError as BaseInvokerError, convert_http_status_to_base_invoker_error as convert_http_status_to_base_invoker_error
7
+ from gllm_inference.exceptions.provider_error_map import GOOGLE_ERROR_MAPPING as GOOGLE_ERROR_MAPPING
8
+ from gllm_inference.lm_invoker.lm_invoker import BaseLMInvoker as BaseLMInvoker
9
+ from gllm_inference.lm_invoker.schema.google import InputType as InputType, Key as Key
10
+ from gllm_inference.schema import Attachment as Attachment, AttachmentType as AttachmentType, LMOutput as LMOutput, Message as Message, MessageRole as MessageRole, ModelId as ModelId, ModelProvider as ModelProvider, Reasoning as Reasoning, ResponseSchema as ResponseSchema, ThinkingEvent as ThinkingEvent, TokenUsage as TokenUsage, ToolCall as ToolCall, ToolResult as ToolResult
11
+ from langchain_core.tools import Tool as LangChainTool
12
+ from typing import Any
13
+
14
+ SUPPORTED_ATTACHMENTS: Incomplete
15
+ DEFAULT_THINKING_BUDGET: int
16
+ REQUIRE_THINKING_MODEL_PREFIX: Incomplete
17
+ IMAGE_GENERATION_MODELS: Incomplete
18
+ YOUTUBE_URL_PATTERN: Incomplete
19
+
20
+ class GoogleLMInvoker(BaseLMInvoker):
21
+ '''A language model invoker to interact with Google language models.
22
+
23
+ Attributes:
24
+ model_id (str): The model ID of the language model.
25
+ model_provider (str): The provider of the language model.
26
+ model_name (str): The name of the language model.
27
+ client_params (dict[str, Any]): The Google client instance init parameters.
28
+ default_hyperparameters (dict[str, Any]): Default hyperparameters for invoking the model.
29
+ tools (list[Any]): The list of tools provided to the model to enable tool calling.
30
+ response_schema (ResponseSchema | None): The schema of the response. If provided, the model will output a
31
+ structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema dictionary.
32
+ output_analytics (bool): Whether to output the invocation analytics.
33
+ retry_config (RetryConfig | None): The retry configuration for the language model.
34
+ generate_image (bool): Whether to generate image. Only allowed for image generation models.
35
+ thinking (bool): Whether to enable thinking. Only allowed for thinking models.
36
+ thinking_budget (int): The tokens allowed for thinking process. Only allowed for thinking models.
37
+ If set to -1, the model will control the budget automatically.
38
+
39
+ Basic usage:
40
+ The `GoogleLMInvoker` can be used as follows:
41
+ ```python
42
+ lm_invoker = GoogleLMInvoker(model_name="gemini-2.5-flash")
43
+ result = await lm_invoker.invoke("Hi there!")
44
+ ```
45
+
46
+ Authentication:
47
+ The `GoogleLMInvoker` can use either Google Gen AI or Google Vertex AI.
48
+
49
+ Google Gen AI is recommended for quick prototyping and development.
50
+ It requires a Gemini API key for authentication.
51
+
52
+ Usage example:
53
+ ```python
54
+ lm_invoker = GoogleLMInvoker(
55
+ model_name="gemini-2.5-flash",
56
+ api_key="your_api_key"
57
+ )
58
+ ```
59
+
60
+ Google Vertex AI is recommended to build production-ready applications.
61
+ It requires a service account JSON file for authentication.
62
+
63
+ Usage example:
64
+ ```python
65
+ lm_invoker = GoogleLMInvoker(
66
+ model_name="gemini-2.5-flash",
67
+ credentials_path="path/to/service_account.json"
68
+ )
69
+ ```
70
+
71
+ If neither `api_key` nor `credentials_path` is provided, Google Gen AI will be used by default.
72
+ The `GOOGLE_API_KEY` environment variable will be used for authentication.
73
+
74
+ Input types:
75
+ The `GoogleLMInvoker` supports the following input types: text, audio, document, image, and video.
76
+ Non-text inputs can be passed as an `Attachment` object with either the `user` or `assistant` role.
77
+
78
+ Usage example:
79
+ ```python
80
+ text = "What animal is in this image?"
81
+ image = Attachment.from_path("path/to/local/image.png")
82
+ result = await lm_invoker.invoke([text, image])
83
+ ```
84
+
85
+ Text output:
86
+ The `GoogleLMInvoker` generates text outputs by default.
87
+ Text outputs are stored in the `outputs` attribute of the `LMOutput` object and can be accessed
88
+ via the `texts` (all text outputs) or `text` (first text output) properties.
89
+
90
+ Output example:
91
+ ```python
92
+ LMOutput(outputs=[LMOutputItem(type="text", output="Hello, there!")])
93
+ ```
94
+
95
+ Structured output:
96
+ The `GoogleLMInvoker` can be configured to generate structured outputs.
97
+ This feature can be enabled by providing a schema to the `response_schema` parameter.
98
+
99
+ Structured outputs are stored in the `outputs` attribute of the `LMOutput` object and can be accessed
100
+ via the `structureds` (all structured outputs) or `structured` (first structured output) properties.
101
+
102
+ The schema must either be one of the following:
103
+ 1. A Pydantic BaseModel class
104
+ The structured output will be a Pydantic model.
105
+ 2. A JSON schema dictionary
106
+ JSON dictionary schema must be compatible with Pydantic\'s JSON schema, especially for complex schemas.
107
+ Thus, it is recommended to create the JSON schema using Pydantic\'s `model_json_schema` method.
108
+ The structured output will be a dictionary.
109
+
110
+ Usage example:
111
+ ```python
112
+ class Animal(BaseModel):
113
+ name: str
114
+ color: str
115
+
116
+ json_schema = Animal.model_json_schema()
117
+
118
+ lm_invoker = GoogleLMInvoker(..., response_schema=Animal) # Using Pydantic BaseModel class
119
+ lm_invoker = GoogleLMInvoker(..., response_schema=json_schema) # Using JSON schema dictionary
120
+ ```
121
+
122
+ Output example:
123
+ ```python
124
+ # Using Pydantic BaseModel class outputs a Pydantic model
125
+ LMOutput(outputs=[LMOutputItem(type="structured", output=Animal(name="dog", color="white"))])
126
+
127
+ # Using JSON schema dictionary outputs a dictionary
128
+ LMOutput(outputs=[LMOutputItem(type="structured", output={"name": "dog", "color": "white"})])
129
+ ```
130
+
131
+ Structured output is not compatible with tool calling.
132
+ When structured output is enabled, streaming is disabled.
133
+
134
+ Image generation:
135
+ The `GoogleLMInvoker` can be configured to generate images.
136
+ This feature can be enabled by using an image generation model, such as `gemini-2.5-flash-image`.
137
+
138
+ Image outputs are stored in the `outputs` attribute of the `LMOutput` object and can be accessed
139
+ via the `attachments` property.
140
+
141
+ Usage example:
142
+ ```python
143
+ lm_invoker = GoogleLMInvoker("gemini-2.5-flash-image")
144
+ result = await lm_invoker.invoke("Create a picture...")
145
+ result.attachments[0].write_to_file("path/to/local/image.png")
146
+ ```
147
+
148
+ Output example:
149
+ ```python
150
+ LMOutput(
151
+ outputs=[
152
+ LMOutputItem(type="text", output="Creating a picture..."),
153
+ LMOutputItem(
154
+ type="attachment",
155
+ output=Attachment(filename="image.png", mime_type="image/png", data=b"..."),
156
+ ),
157
+ ],
158
+ )
159
+ ```
160
+
161
+ Image generation is not compatible with tool calling and thinking.
162
+ When image generation is enabled, streaming is disabled.
163
+
164
+ Tool calling:
165
+ The `GoogleLMInvoker` can be configured to call tools to perform certain tasks.
166
+ This feature can be enabled by providing a list of `Tool` objects to the `tools` parameter.
167
+
168
+ Tool calls outputs are stored in the `outputs` attribute of the `LMOutput` object and
169
+ can be accessed via the `tool_calls` property.
170
+
171
+ Usage example:
172
+ ```python
173
+ lm_invoker = GoogleLMInvoker(..., tools=[tool_1, tool_2])
174
+ ```
175
+
176
+ Output example:
177
+ ```python
178
+ LMOutput(
179
+ outputs=[
180
+ LMOutputItem(type="text", output="I\'m using tools..."),
181
+ LMOutputItem(type="tool_call", output=ToolCall(id="123", name="tool_1", args={"key": "value"})),
182
+ LMOutputItem(type="tool_call", output=ToolCall(id="456", name="tool_2", args={"key": "value"})),
183
+ ]
184
+ )
185
+ ```
186
+
187
+ Thinking:
188
+ The `GoogleLMInvoker` can be configured to perform step-by-step thinking process before answering.
189
+ This feature can be enabled by setting the `thinking` parameter to `True`.
190
+
191
+ Thinking outputs are stored in the `outputs` attribute of the `LMOutput` object
192
+ and can be accessed via the `thinkings` property.
193
+
194
+ Usage example:
195
+ ```python
196
+ lm_invoker = GoogleLMInvoker(..., thinking=True, thinking_budget=1024)
197
+ ```
198
+
199
+ Output example:
200
+ ```python
201
+ LMOutput(
202
+ outputs=[
203
+ LMOutputItem(type="thinking", output=Reasoning(type="thinking", reasoning="I\'m thinking...", ...)),
204
+ LMOutputItem(type="text", output="Golden retriever is a good dog breed."),
205
+ ]
206
+ )
207
+ ```
208
+
209
+ Streaming output example:
210
+ ```python
211
+ {"type": "thinking_start", "value": "", ...}
212
+ {"type": "thinking", "value": "I\'m ", ...}
213
+ {"type": "thinking", "value": "thinking...", ...}
214
+ {"type": "thinking_end", "value": "", ...}
215
+ {"type": "response", "value": "Golden retriever ", ...}
216
+ {"type": "response", "value": "is a good dog breed.", ...}
217
+ ```
218
+ Note: By default, the thinking token will be streamed with the legacy `EventType.DATA` event type.
219
+ To use the new simplified streamed event format, set the `simplify_events` parameter to `True` during
220
+ LM invoker initialization. The legacy event format support will be removed in v0.6.
221
+
222
+ The amount of tokens allocated for the thinking process can be set via the `thinking_budget` parameter.
223
+ For more information, please refer to the following documentation:
224
+ https://ai.google.dev/gemini-api/docs/thinking
225
+
226
+ Thinking is only available for certain models, starting from Gemini 2.5 series.
227
+ Thinking is required for Gemini 2.5 Pro models.
228
+
229
+ Analytics tracking:
230
+ The `GoogleLMInvoker` can be configured to output additional information about the invocation.
231
+ This feature can be enabled by setting the `output_analytics` parameter to `True`.
232
+
233
+ When enabled, the following attributes will be stored in the output:
234
+ 1. `token_usage`: The token usage.
235
+ 2. `duration`: The duration in seconds.
236
+ 3. `finish_details`: The details about how the generation finished.
237
+
238
+ Output example:
239
+ ```python
240
+ LMOutput(
241
+ outputs=[...],
242
+ token_usage=TokenUsage(input_tokens=100, output_tokens=50),
243
+ duration=0.729,
244
+ finish_details={"stop_reason": "end_turn"},
245
+ )
246
+ ```
247
+
248
+ Retry and timeout:
249
+ The `GoogleLMInvoker` supports retry and timeout configuration.
250
+ By default, the max retries is set to 0 and the timeout is set to 30.0 seconds.
251
+ They can be customized by providing a custom `RetryConfig` object to the `retry_config` parameter.
252
+
253
+ Retry config examples:
254
+ ```python
255
+ retry_config = RetryConfig(max_retries=0, timeout=None) # No retry, no timeout
256
+ retry_config = RetryConfig(max_retries=5, timeout=10.0) # 5 max retries, 10.0 seconds timeout
257
+ ```
258
+
259
+ Usage example:
260
+ ```python
261
+ lm_invoker = GoogleLMInvoker(..., retry_config=retry_config)
262
+ ```
263
+ '''
264
+ client_params: Incomplete
265
+ generate_image: Incomplete
266
+ thinking: Incomplete
267
+ thinking_budget: Incomplete
268
+ def __init__(self, model_name: str, api_key: str | None = None, credentials_path: str | None = None, project_id: str | None = None, location: str = 'us-central1', 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, thinking: bool | None = None, thinking_budget: int = ..., simplify_events: bool = False) -> None:
269
+ '''Initializes a new instance of the GoogleLMInvoker class.
270
+
271
+ Args:
272
+ model_name (str): The name of the model to use.
273
+ api_key (str | None, optional): Required for Google Gen AI authentication. Cannot be used together
274
+ with `credentials_path`. Defaults to None.
275
+ credentials_path (str | None, optional): Required for Google Vertex AI authentication. Path to the service
276
+ account credentials JSON file. Cannot be used together with `api_key`. Defaults to None.
277
+ project_id (str | None, optional): The Google Cloud project ID for Vertex AI. Only used when authenticating
278
+ with `credentials_path`. Defaults to None, in which case it will be loaded from the credentials file.
279
+ location (str, optional): The location of the Google Cloud project for Vertex AI. Only used when
280
+ authenticating with `credentials_path`. Defaults to "us-central1".
281
+ model_kwargs (dict[str, Any] | None, optional): Additional keyword arguments for the Google Vertex AI
282
+ client.
283
+ default_hyperparameters (dict[str, Any] | None, optional): Default hyperparameters for invoking the model.
284
+ Defaults to None.
285
+ tools (list[Tool | LangChainTool] | None, optional): Tools provided to the model to enable tool calling.
286
+ Defaults to None.
287
+ response_schema (ResponseSchema | None, optional): The schema of the response. If provided, the model will
288
+ output a structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema
289
+ dictionary. Defaults to None.
290
+ output_analytics (bool, optional): Whether to output the invocation analytics. Defaults to False.
291
+ retry_config (RetryConfig | None, optional): The retry configuration for the language model.
292
+ Defaults to None, in which case a default config with no retry and 30.0 seconds timeout will be used.
293
+ thinking (bool | None, optional): Whether to enable thinking. Only allowed for thinking models.
294
+ Defaults to True for Gemini 2.5 Pro models and False for other models.
295
+ thinking_budget (int, optional): The tokens allowed for thinking process. Only allowed for thinking models.
296
+ Defaults to -1, in which case the model will control the budget automatically.
297
+ simplify_events (bool, optional): Temporary parameter to control the streamed events format.
298
+ When True, uses the simplified events format. When False, uses the legacy events format for
299
+ backward compatibility. Will be removed in v0.6. Defaults to False.
300
+
301
+ Note:
302
+ If neither `api_key` nor `credentials_path` is provided, Google Gen AI will be used by default.
303
+ The `GOOGLE_API_KEY` environment variable will be used for authentication.
304
+ '''
305
+ def set_tools(self, tools: list[Tool | LangChainTool]) -> None:
306
+ """Sets the tools for the Google language model.
307
+
308
+ This method sets the tools for the Google language model. Any existing tools will be replaced.
309
+
310
+ Args:
311
+ tools (list[Tool | LangChainTool]): The list of tools to be used.
312
+
313
+ Raises:
314
+ ValueError: If `response_schema` exists.
315
+ """
316
+ def set_response_schema(self, response_schema: ResponseSchema | None) -> None:
317
+ """Sets the response schema for the Google language model.
318
+
319
+ This method sets the response schema for the Google language model. Any existing response schema will be
320
+ replaced.
321
+
322
+ Args:
323
+ response_schema (ResponseSchema | None): The response schema to be used.
324
+
325
+ Raises:
326
+ ValueError: If `tools` exists.
327
+ """
@@ -0,0 +1,239 @@
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
5
+ from gllm_inference.constants import INVOKER_DEFAULT_TIMEOUT as INVOKER_DEFAULT_TIMEOUT, INVOKER_PROPAGATED_MAX_RETRIES as INVOKER_PROPAGATED_MAX_RETRIES
6
+ from gllm_inference.exceptions import BaseInvokerError as BaseInvokerError, InvokerRuntimeError as InvokerRuntimeError, build_debug_info as build_debug_info
7
+ from gllm_inference.exceptions.provider_error_map import ALL_PROVIDER_ERROR_MAPPINGS as ALL_PROVIDER_ERROR_MAPPINGS, LANGCHAIN_ERROR_CODE_MAPPING as LANGCHAIN_ERROR_CODE_MAPPING
8
+ from gllm_inference.lm_invoker.lm_invoker import BaseLMInvoker as BaseLMInvoker
9
+ from gllm_inference.lm_invoker.schema.langchain import InputType as InputType, Key as Key
10
+ from gllm_inference.schema import Attachment as Attachment, AttachmentType as AttachmentType, LMOutput as LMOutput, Message as Message, MessageRole as MessageRole, ModelId as ModelId, ModelProvider as ModelProvider, ResponseSchema as ResponseSchema, TokenUsage as TokenUsage, ToolCall as ToolCall, ToolResult as ToolResult
11
+ from gllm_inference.utils import load_langchain_model as load_langchain_model, parse_model_data as parse_model_data
12
+ from langchain_core.language_models import BaseChatModel as BaseChatModel
13
+ from langchain_core.messages import BaseMessage as BaseMessage
14
+ from langchain_core.tools import Tool as LangChainTool
15
+ from typing import Any
16
+
17
+ SUPPORTED_ATTACHMENTS: Incomplete
18
+ MESSAGE_CLASS_MAP: Incomplete
19
+
20
+ class LangChainLMInvoker(BaseLMInvoker):
21
+ '''A language model invoker to interact with LangChain\'s BaseChatModel.
22
+
23
+ Attributes:
24
+ model_id (str): The model ID of the language model.
25
+ model_provider (str): The provider of the language model.
26
+ model_name (str): The name of the language model.
27
+ model (BaseChatModel): The LangChain\'s BaseChatModel instance.
28
+ default_hyperparameters (dict[str, Any]): Default hyperparameters for invoking the model.
29
+ tools (list[Any]): The list of tools provided to the model to enable tool calling.
30
+ response_schema (ResponseSchema | None): The schema of the response. If provided, the model will output a
31
+ structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema dictionary.
32
+ output_analytics (bool): Whether to output the invocation analytics.
33
+ retry_config (RetryConfig | None): The retry configuration for the language model.
34
+
35
+ Basic usage:
36
+ The `LangChainLMInvoker` can be used as follows:
37
+ ```python
38
+ lm_invoker = LangChainLMInvoker(
39
+ model_class_path="langchain_openai.ChatOpenAI",
40
+ model_name="gpt-5-nano",
41
+ )
42
+ result = await lm_invoker.invoke("Hi there!")
43
+ ```
44
+
45
+ Initialization:
46
+ The `LangChainLMInvoker` can be initialized by either passing:
47
+
48
+ 1. A LangChain\'s BaseChatModel instance:
49
+ Usage example:
50
+ ```python
51
+ from langchain_openai import ChatOpenAI
52
+
53
+ model = ChatOpenAI(model="gpt-5-nano", api_key="your_api_key")
54
+ lm_invoker = LangChainLMInvoker(model=model)
55
+ ```
56
+
57
+ 2. A model path in the format of "<package>.<class>":
58
+ Usage example:
59
+ ```python
60
+ lm_invoker = LangChainLMInvoker(
61
+ model_class_path="langchain_openai.ChatOpenAI",
62
+ model_name="gpt-5-nano",
63
+ model_kwargs={"api_key": "your_api_key"}
64
+ )
65
+ ```
66
+ For the list of supported providers, please refer to the following table:
67
+ https://python.langchain.com/docs/integrations/chat/#featured-providers
68
+
69
+ Input types:
70
+ The `LangChainLMInvoker` supports the following input types: text and image.
71
+ Non-text inputs can be passed as an `Attachment` object and with specific roles,
72
+ depending on the language model\'s capabilities.
73
+
74
+ Usage example:
75
+ ```python
76
+ text = "What animal is in this image?"
77
+ image = Attachment.from_path("path/to/local/image.png")
78
+ result = await lm_invoker.invoke([text, image])
79
+ ```
80
+
81
+ Text output:
82
+ The `LangChainLMInvoker` generates text outputs by default.
83
+ Text outputs are stored in the `outputs` attribute of the `LMOutput` object and can be accessed
84
+ via the `texts` (all text outputs) or `text` (first text output) properties.
85
+
86
+ Output example:
87
+ ```python
88
+ LMOutput(outputs=[LMOutputItem(type="text", output="Hello, there!")])
89
+ ```
90
+
91
+ Structured output:
92
+ The `LangChainLMInvoker` can be configured to generate structured outputs.
93
+ This feature can be enabled by providing a schema to the `response_schema` parameter.
94
+
95
+ Structured outputs are stored in the `outputs` attribute of the `LMOutput` object and can be accessed
96
+ via the `structureds` (all structured outputs) or `structured` (first structured output) properties.
97
+
98
+ The schema must either be one of the following:
99
+ 1. A Pydantic BaseModel class
100
+ The structured output will be a Pydantic model.
101
+ 2. A JSON schema dictionary
102
+ JSON dictionary schema must be compatible with Pydantic\'s JSON schema, especially for complex schemas.
103
+ Thus, it is recommended to create the JSON schema using Pydantic\'s `model_json_schema` method.
104
+ The structured output will be a dictionary.
105
+
106
+ Usage example:
107
+ ```python
108
+ class Animal(BaseModel):
109
+ name: str
110
+ color: str
111
+
112
+ json_schema = Animal.model_json_schema()
113
+
114
+ lm_invoker = LangChainLMInvoker(..., response_schema=Animal) # Using Pydantic BaseModel class
115
+ lm_invoker = LangChainLMInvoker(..., response_schema=json_schema) # Using JSON schema dictionary
116
+ ```
117
+
118
+ Output example:
119
+ ```python
120
+ # Using Pydantic BaseModel class outputs a Pydantic model
121
+ LMOutput(outputs=[LMOutputItem(type="structured", output=Animal(name="dog", color="white"))])
122
+
123
+ # Using JSON schema dictionary outputs a dictionary
124
+ LMOutput(outputs=[LMOutputItem(type="structured", output={"name": "dog", "color": "white"})])
125
+ ```
126
+
127
+ Structured output is not compatible with tool calling.
128
+ When structured output is enabled, streaming is disabled.
129
+
130
+ Tool calling:
131
+ The `LangChainLMInvoker` can be configured to call tools to perform certain tasks.
132
+ This feature can be enabled by providing a list of `Tool` objects to the `tools` parameter.
133
+
134
+ Tool calls outputs are stored in the `outputs` attribute of the `LMOutput` object and
135
+ can be accessed via the `tool_calls` property.
136
+
137
+ Usage example:
138
+ ```python
139
+ lm_invoker = LangChainLMInvoker(..., tools=[tool_1, tool_2])
140
+ ```
141
+
142
+ Output example:
143
+ ```python
144
+ LMOutput(
145
+ outputs=[
146
+ LMOutputItem(type="text", output="I\'m using tools..."),
147
+ LMOutputItem(type="tool_call", output=ToolCall(id="123", name="tool_1", args={"key": "value"})),
148
+ LMOutputItem(type="tool_call", output=ToolCall(id="456", name="tool_2", args={"key": "value"})),
149
+ ]
150
+ )
151
+ ```
152
+
153
+ Analytics tracking:
154
+ The `LangChainLMInvoker` can be configured to output additional information about the invocation.
155
+ This feature can be enabled by setting the `output_analytics` parameter to `True`.
156
+
157
+ When enabled, the following attributes will be stored in the output:
158
+ 1. `token_usage`: The token usage.
159
+ 2. `duration`: The duration in seconds.
160
+ 3. `finish_details`: The details about how the generation finished.
161
+
162
+ Output example:
163
+ ```python
164
+ LMOutput(
165
+ outputs=[...],
166
+ token_usage=TokenUsage(input_tokens=100, output_tokens=50),
167
+ duration=0.729,
168
+ finish_details={"stop_reason": "end_turn"},
169
+ )
170
+ ```
171
+
172
+ Retry and timeout:
173
+ The `LangChainLMInvoker` supports retry and timeout configuration.
174
+ By default, the max retries is set to 0 and the timeout is set to 30.0 seconds.
175
+ They can be customized by providing a custom `RetryConfig` object to the `retry_config` parameter.
176
+
177
+ Retry config examples:
178
+ ```python
179
+ retry_config = RetryConfig(max_retries=0, timeout=None) # No retry, no timeout
180
+ retry_config = RetryConfig(max_retries=5, timeout=10.0) # 5 max retries, 10.0 seconds timeout
181
+ ```
182
+
183
+ Usage example:
184
+ ```python
185
+ lm_invoker = LangChainLMInvoker(..., retry_config=retry_config)
186
+ ```
187
+ '''
188
+ model: Incomplete
189
+ def __init__(self, model: BaseChatModel | None = None, model_class_path: str | None = None, model_name: 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) -> None:
190
+ '''Initializes a new instance of the LangChainLMInvoker class.
191
+
192
+ Args:
193
+ model (BaseChatModel | None, optional): The LangChain\'s BaseChatModel instance. If provided, will take
194
+ precedence over the `model_class_path` parameter. Defaults to None.
195
+ model_class_path (str | None, optional): The LangChain\'s BaseChatModel class path. Must be formatted as
196
+ "<package>.<class>" (e.g. "langchain_openai.ChatOpenAI"). Ignored if `model` is provided.
197
+ Defaults to None.
198
+ model_name (str | None, optional): The model name. Only used if `model_class_path` is provided.
199
+ Defaults to None.
200
+ model_kwargs (dict[str, Any] | None, optional): The additional keyword arguments. Only used if
201
+ `model_class_path` is provided. Defaults to None.
202
+ default_hyperparameters (dict[str, Any] | None, optional): Default hyperparameters for invoking the model.
203
+ Defaults to None.
204
+ tools (list[Tool | LangChainTool] | None, optional): Tools provided to the model to enable tool calling.
205
+ Defaults to None.
206
+ response_schema (ResponseSchema | None, optional): The schema of the response. If provided, the model will
207
+ output a structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema
208
+ dictionary. Defaults to None.
209
+ output_analytics (bool, optional): Whether to output the invocation analytics. Defaults to False.
210
+ retry_config (RetryConfig | None, optional): The retry configuration for the language model.
211
+ Defaults to None, in which case a default config with no retry and 30.0 seconds timeout will be used.
212
+
213
+ Raises:
214
+ ValueError: If `response_schema` is provided, but `tools` are also provided.
215
+ '''
216
+ tools: Incomplete
217
+ def set_tools(self, tools: list[Tool | LangChainTool]) -> None:
218
+ """Sets the tools for LangChain's BaseChatModel.
219
+
220
+ This method sets the tools for LangChain's BaseChatModel. Any existing tools will be replaced.
221
+
222
+ Args:
223
+ tools (list[Tool]): The list of tools to be used.
224
+
225
+ Raises:
226
+ ValueError: If `response_schema` exists.
227
+ """
228
+ def set_response_schema(self, response_schema: ResponseSchema | None) -> None:
229
+ """Sets the response schema for the LangChain's BaseChatModel.
230
+
231
+ This method sets the response schema for the LangChain's BaseChatModel. Any existing response schema will be
232
+ replaced.
233
+
234
+ Args:
235
+ response_schema (ResponseSchema | None): The response schema to be used.
236
+
237
+ Raises:
238
+ ValueError: If `tools` exists.
239
+ """