gllm-inference-binary 0.5.51b3__cp313-cp313-macosx_11_0_arm64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gllm_inference/__init__.pyi +0 -0
- gllm_inference/builder/__init__.pyi +6 -0
- gllm_inference/builder/_build_invoker.pyi +28 -0
- gllm_inference/builder/build_em_invoker.pyi +130 -0
- gllm_inference/builder/build_lm_invoker.pyi +213 -0
- gllm_inference/builder/build_lm_request_processor.pyi +88 -0
- gllm_inference/builder/build_output_parser.pyi +29 -0
- gllm_inference/catalog/__init__.pyi +4 -0
- gllm_inference/catalog/catalog.pyi +121 -0
- gllm_inference/catalog/lm_request_processor_catalog.pyi +112 -0
- gllm_inference/catalog/prompt_builder_catalog.pyi +82 -0
- gllm_inference/constants.pyi +13 -0
- gllm_inference/em_invoker/__init__.pyi +12 -0
- gllm_inference/em_invoker/azure_openai_em_invoker.pyi +88 -0
- gllm_inference/em_invoker/bedrock_em_invoker.pyi +118 -0
- gllm_inference/em_invoker/cohere_em_invoker.pyi +128 -0
- gllm_inference/em_invoker/em_invoker.pyi +90 -0
- gllm_inference/em_invoker/google_em_invoker.pyi +129 -0
- gllm_inference/em_invoker/jina_em_invoker.pyi +103 -0
- gllm_inference/em_invoker/langchain/__init__.pyi +3 -0
- gllm_inference/em_invoker/langchain/em_invoker_embeddings.pyi +84 -0
- gllm_inference/em_invoker/langchain_em_invoker.pyi +46 -0
- gllm_inference/em_invoker/openai_compatible_em_invoker.pyi +41 -0
- gllm_inference/em_invoker/openai_em_invoker.pyi +118 -0
- gllm_inference/em_invoker/schema/__init__.pyi +0 -0
- gllm_inference/em_invoker/schema/bedrock.pyi +29 -0
- gllm_inference/em_invoker/schema/cohere.pyi +20 -0
- gllm_inference/em_invoker/schema/google.pyi +9 -0
- gllm_inference/em_invoker/schema/jina.pyi +29 -0
- gllm_inference/em_invoker/schema/langchain.pyi +5 -0
- gllm_inference/em_invoker/schema/openai.pyi +7 -0
- gllm_inference/em_invoker/schema/openai_compatible.pyi +7 -0
- gllm_inference/em_invoker/schema/twelvelabs.pyi +17 -0
- gllm_inference/em_invoker/schema/voyage.pyi +15 -0
- gllm_inference/em_invoker/twelevelabs_em_invoker.pyi +101 -0
- gllm_inference/em_invoker/voyage_em_invoker.pyi +104 -0
- gllm_inference/exceptions/__init__.pyi +4 -0
- gllm_inference/exceptions/error_parser.pyi +41 -0
- gllm_inference/exceptions/exceptions.pyi +132 -0
- gllm_inference/exceptions/provider_error_map.pyi +24 -0
- gllm_inference/lm_invoker/__init__.pyi +14 -0
- gllm_inference/lm_invoker/anthropic_lm_invoker.pyi +334 -0
- gllm_inference/lm_invoker/azure_openai_lm_invoker.pyi +254 -0
- gllm_inference/lm_invoker/batch/__init__.pyi +3 -0
- gllm_inference/lm_invoker/batch/batch_operations.pyi +127 -0
- gllm_inference/lm_invoker/bedrock_lm_invoker.pyi +226 -0
- gllm_inference/lm_invoker/datasaur_lm_invoker.pyi +157 -0
- gllm_inference/lm_invoker/google_lm_invoker.pyi +336 -0
- gllm_inference/lm_invoker/langchain_lm_invoker.pyi +251 -0
- gllm_inference/lm_invoker/litellm_lm_invoker.pyi +244 -0
- gllm_inference/lm_invoker/lm_invoker.pyi +164 -0
- gllm_inference/lm_invoker/openai_chat_completions_lm_invoker.pyi +272 -0
- gllm_inference/lm_invoker/openai_compatible_lm_invoker.pyi +52 -0
- gllm_inference/lm_invoker/openai_lm_invoker.pyi +433 -0
- gllm_inference/lm_invoker/portkey_lm_invoker.pyi +260 -0
- gllm_inference/lm_invoker/schema/__init__.pyi +0 -0
- gllm_inference/lm_invoker/schema/anthropic.pyi +56 -0
- gllm_inference/lm_invoker/schema/bedrock.pyi +53 -0
- gllm_inference/lm_invoker/schema/datasaur.pyi +14 -0
- gllm_inference/lm_invoker/schema/google.pyi +24 -0
- gllm_inference/lm_invoker/schema/langchain.pyi +23 -0
- gllm_inference/lm_invoker/schema/openai.pyi +106 -0
- gllm_inference/lm_invoker/schema/openai_chat_completions.pyi +62 -0
- gllm_inference/lm_invoker/schema/portkey.pyi +31 -0
- gllm_inference/lm_invoker/schema/xai.pyi +31 -0
- gllm_inference/lm_invoker/xai_lm_invoker.pyi +289 -0
- gllm_inference/model/__init__.pyi +12 -0
- gllm_inference/model/em/__init__.pyi +0 -0
- gllm_inference/model/em/cohere_em.pyi +17 -0
- gllm_inference/model/em/google_em.pyi +16 -0
- gllm_inference/model/em/jina_em.pyi +22 -0
- gllm_inference/model/em/openai_em.pyi +15 -0
- gllm_inference/model/em/twelvelabs_em.pyi +13 -0
- gllm_inference/model/em/voyage_em.pyi +20 -0
- gllm_inference/model/lm/__init__.pyi +0 -0
- gllm_inference/model/lm/anthropic_lm.pyi +22 -0
- gllm_inference/model/lm/google_lm.pyi +18 -0
- gllm_inference/model/lm/openai_lm.pyi +27 -0
- gllm_inference/model/lm/xai_lm.pyi +19 -0
- gllm_inference/output_parser/__init__.pyi +3 -0
- gllm_inference/output_parser/json_output_parser.pyi +60 -0
- gllm_inference/output_parser/output_parser.pyi +27 -0
- gllm_inference/prompt_builder/__init__.pyi +3 -0
- gllm_inference/prompt_builder/format_strategy/__init__.pyi +4 -0
- gllm_inference/prompt_builder/format_strategy/format_strategy.pyi +55 -0
- gllm_inference/prompt_builder/format_strategy/jinja_format_strategy.pyi +45 -0
- gllm_inference/prompt_builder/format_strategy/string_format_strategy.pyi +20 -0
- gllm_inference/prompt_builder/prompt_builder.pyi +69 -0
- gllm_inference/prompt_formatter/__init__.pyi +7 -0
- gllm_inference/prompt_formatter/agnostic_prompt_formatter.pyi +49 -0
- gllm_inference/prompt_formatter/huggingface_prompt_formatter.pyi +55 -0
- gllm_inference/prompt_formatter/llama_prompt_formatter.pyi +59 -0
- gllm_inference/prompt_formatter/mistral_prompt_formatter.pyi +53 -0
- gllm_inference/prompt_formatter/openai_prompt_formatter.pyi +35 -0
- gllm_inference/prompt_formatter/prompt_formatter.pyi +30 -0
- gllm_inference/realtime_chat/__init__.pyi +3 -0
- gllm_inference/realtime_chat/google_realtime_chat.pyi +205 -0
- gllm_inference/realtime_chat/input_streamer/__init__.pyi +4 -0
- gllm_inference/realtime_chat/input_streamer/input_streamer.pyi +36 -0
- gllm_inference/realtime_chat/input_streamer/keyboard_input_streamer.pyi +27 -0
- gllm_inference/realtime_chat/input_streamer/linux_mic_input_streamer.pyi +36 -0
- gllm_inference/realtime_chat/output_streamer/__init__.pyi +4 -0
- gllm_inference/realtime_chat/output_streamer/console_output_streamer.pyi +21 -0
- gllm_inference/realtime_chat/output_streamer/linux_speaker_output_streamer.pyi +42 -0
- gllm_inference/realtime_chat/output_streamer/output_streamer.pyi +33 -0
- gllm_inference/realtime_chat/realtime_chat.pyi +28 -0
- gllm_inference/request_processor/__init__.pyi +4 -0
- gllm_inference/request_processor/lm_request_processor.pyi +101 -0
- gllm_inference/request_processor/uses_lm_mixin.pyi +130 -0
- gllm_inference/schema/__init__.pyi +18 -0
- gllm_inference/schema/activity.pyi +64 -0
- gllm_inference/schema/attachment.pyi +88 -0
- gllm_inference/schema/code_exec_result.pyi +14 -0
- gllm_inference/schema/config.pyi +15 -0
- gllm_inference/schema/enums.pyi +69 -0
- gllm_inference/schema/events.pyi +105 -0
- gllm_inference/schema/lm_input.pyi +4 -0
- gllm_inference/schema/lm_output.pyi +44 -0
- gllm_inference/schema/mcp.pyi +31 -0
- gllm_inference/schema/message.pyi +52 -0
- gllm_inference/schema/model_id.pyi +176 -0
- gllm_inference/schema/reasoning.pyi +15 -0
- gllm_inference/schema/token_usage.pyi +75 -0
- gllm_inference/schema/tool_call.pyi +14 -0
- gllm_inference/schema/tool_result.pyi +11 -0
- gllm_inference/schema/type_alias.pyi +11 -0
- gllm_inference/utils/__init__.pyi +5 -0
- gllm_inference/utils/io_utils.pyi +26 -0
- gllm_inference/utils/langchain.pyi +30 -0
- gllm_inference/utils/validation.pyi +12 -0
- gllm_inference.build/.gitignore +1 -0
- gllm_inference.cpython-313-darwin.so +0 -0
- gllm_inference.pyi +157 -0
- gllm_inference_binary-0.5.51b3.dist-info/METADATA +138 -0
- gllm_inference_binary-0.5.51b3.dist-info/RECORD +137 -0
- gllm_inference_binary-0.5.51b3.dist-info/WHEEL +5 -0
- gllm_inference_binary-0.5.51b3.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,334 @@
|
|
|
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 INVOKER_PROPAGATED_MAX_RETRIES as INVOKER_PROPAGATED_MAX_RETRIES
|
|
6
|
+
from gllm_inference.lm_invoker.lm_invoker import BaseLMInvoker as BaseLMInvoker
|
|
7
|
+
from gllm_inference.lm_invoker.schema.anthropic import InputType as InputType, Key as Key, OutputType as OutputType
|
|
8
|
+
from gllm_inference.schema import Attachment as Attachment, AttachmentType as AttachmentType, BatchStatus as BatchStatus, LMInput as LMInput, LMOutput as LMOutput, Message as Message, ModelId as ModelId, ModelProvider as ModelProvider, Reasoning as Reasoning, ResponseSchema as ResponseSchema, ThinkingEvent as ThinkingEvent, TokenUsage as TokenUsage, 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
|
+
DEFAULT_MAX_TOKENS: int
|
|
14
|
+
DEFAULT_THINKING_BUDGET: int
|
|
15
|
+
BATCH_STATUS_MAP: Incomplete
|
|
16
|
+
|
|
17
|
+
class AnthropicLMInvoker(BaseLMInvoker):
|
|
18
|
+
'''A language model invoker to interact with Anthropic language models.
|
|
19
|
+
|
|
20
|
+
Attributes:
|
|
21
|
+
model_id (str): The model ID of the language model.
|
|
22
|
+
model_provider (str): The provider of the language model.
|
|
23
|
+
model_name (str): The name of the language model.
|
|
24
|
+
client (AsyncAnthropic): The Anthropic client instance.
|
|
25
|
+
default_hyperparameters (dict[str, Any]): Default hyperparameters for invoking the model.
|
|
26
|
+
tools (list[Tool]): Tools provided to the model to enable tool calling.
|
|
27
|
+
response_schema (ResponseSchema | None): The schema of the response. If provided, the model will output a
|
|
28
|
+
structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema dictionary.
|
|
29
|
+
output_analytics (bool): Whether to output the invocation analytics.
|
|
30
|
+
retry_config (RetryConfig): The retry configuration for the language model.
|
|
31
|
+
thinking (bool): Whether to enable thinking. Only allowed for thinking models.
|
|
32
|
+
thinking_budget (int): The tokens allocated for the thinking process. Only allowed for thinking models.
|
|
33
|
+
|
|
34
|
+
Basic usage:
|
|
35
|
+
The `AnthropicLMInvoker` can be used as follows:
|
|
36
|
+
```python
|
|
37
|
+
lm_invoker = AnthropicLMInvoker(model_name="claude-sonnet-4-20250514")
|
|
38
|
+
result = await lm_invoker.invoke("Hi there!")
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Input types:
|
|
42
|
+
The `AnthropicLMInvoker` supports the following input types: text, image, and document.
|
|
43
|
+
Non-text inputs can be passed as an `Attachment` object with the `user` role.
|
|
44
|
+
|
|
45
|
+
Usage example:
|
|
46
|
+
```python
|
|
47
|
+
text = "What animal is in this image?"
|
|
48
|
+
image = Attachment.from_path("path/to/local/image.png")
|
|
49
|
+
result = await lm_invoker.invoke([text, image])
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Tool calling:
|
|
53
|
+
Tool calling is a feature that allows the language model to call tools to perform tasks.
|
|
54
|
+
Tools can be passed to the via the `tools` parameter as a list of `Tool` objects.
|
|
55
|
+
When tools are provided and the model decides to call a tool, the tool calls are stored in the
|
|
56
|
+
`tool_calls` attribute in the output.
|
|
57
|
+
|
|
58
|
+
Usage example:
|
|
59
|
+
```python
|
|
60
|
+
lm_invoker = AnthropicLMInvoker(..., tools=[tool_1, tool_2])
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Output example:
|
|
64
|
+
```python
|
|
65
|
+
LMOutput(
|
|
66
|
+
response="Let me call the tools...",
|
|
67
|
+
tool_calls=[
|
|
68
|
+
ToolCall(id="123", name="tool_1", args={"key": "value"}),
|
|
69
|
+
ToolCall(id="456", name="tool_2", args={"key": "value"}),
|
|
70
|
+
]
|
|
71
|
+
)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Structured output:
|
|
75
|
+
Structured output is a feature that allows the language model to output a structured response.
|
|
76
|
+
This feature can be enabled by providing a schema to the `response_schema` parameter.
|
|
77
|
+
|
|
78
|
+
The schema must be either a JSON schema dictionary or a Pydantic BaseModel class.
|
|
79
|
+
If JSON schema is used, it must be compatible with Pydantic\'s JSON schema, especially for complex schemas.
|
|
80
|
+
For this reason, it is recommended to create the JSON schema using Pydantic\'s `model_json_schema` method.
|
|
81
|
+
|
|
82
|
+
Structured output is achieved by providing the schema name in the `tool_choice` parameter. This forces
|
|
83
|
+
the model to call the provided schema as a tool. Thus, structured output is not compatible with:
|
|
84
|
+
1. Tool calling, since the tool calling is reserved to force the model to call the provided schema as a tool.
|
|
85
|
+
2. Thinking, since thinking is not allowed when a tool use is forced through the `tool_choice` parameter.
|
|
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 = AnthropicLMInvoker(..., 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 = AnthropicLMInvoker(..., 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 `AnthropicLMInvoker` supports retry and timeout configuration.
|
|
147
|
+
By default, the max retries is set to 0 and the timeout is set to 30.0 seconds.
|
|
148
|
+
They can be customized by providing a custom `RetryConfig` object to the `retry_config` parameter.
|
|
149
|
+
|
|
150
|
+
Retry config examples:
|
|
151
|
+
```python
|
|
152
|
+
retry_config = RetryConfig(max_retries=0, timeout=None) # No retry, no timeout
|
|
153
|
+
retry_config = RetryConfig(max_retries=0, timeout=10.0) # No retry, 10.0 seconds timeout
|
|
154
|
+
retry_config = RetryConfig(max_retries=5, timeout=None) # 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 = AnthropicLMInvoker(..., retry_config=retry_config)
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Thinking:
|
|
164
|
+
Thinking is a feature that allows the language model to have enhanced reasoning capabilities for complex tasks,
|
|
165
|
+
while also providing transparency into its step-by-step thought process before it delivers its final answer.
|
|
166
|
+
This feature is only available for certain models, starting from Claude 3.7 Sonnet.
|
|
167
|
+
It can be enabled by setting the `thinking` parameter to `True`.
|
|
168
|
+
|
|
169
|
+
When thinking is enabled, the amount of tokens allocated for the thinking process can be set via the
|
|
170
|
+
`thinking_budget` parameter. The `thinking_budget`:
|
|
171
|
+
1. Must be greater than or equal to 1024.
|
|
172
|
+
2. Must be less than the `max_tokens` hyperparameter, as the `thinking_budget` is allocated from the
|
|
173
|
+
`max_tokens`. For example, if `max_tokens=2048` and `thinking_budget=1024`, the language model will
|
|
174
|
+
allocate at most 1024 tokens for thinking and the remaining 1024 tokens for generating the response.
|
|
175
|
+
|
|
176
|
+
When enabled, the reasoning is stored in the `reasoning` attribute in the output.
|
|
177
|
+
|
|
178
|
+
Usage example:
|
|
179
|
+
```python
|
|
180
|
+
lm_invoker = AnthropicLMInvoker(..., thinking=True, thinking_budget=1024)
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
Output example:
|
|
184
|
+
```python
|
|
185
|
+
LMOutput(
|
|
186
|
+
response="Golden retriever is a good dog breed.",
|
|
187
|
+
reasoning=[Reasoning(type="thinking", reasoning="Let me think about it...", signature="x")],
|
|
188
|
+
)
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
Streaming output example:
|
|
192
|
+
```python
|
|
193
|
+
{"type": "thinking_start", "value": "", ...}
|
|
194
|
+
{"type": "thinking", "value": "Let me think "\', ...}
|
|
195
|
+
{"type": "thinking", "value": "about it..."}\', ...}
|
|
196
|
+
{"type": "thinking_end", "value": ""}\', ...}
|
|
197
|
+
{"type": "response", "value": "Golden retriever ", ...}
|
|
198
|
+
{"type": "response", "value": "is a good dog breed.", ...}
|
|
199
|
+
```
|
|
200
|
+
Note: By default, the thinking token will be streamed with the legacy `EventType.DATA` event type.
|
|
201
|
+
To use the new simplified streamed event format, set the `simplify_events` parameter to `True` during
|
|
202
|
+
LM invoker initialization. The legacy event format support will be removed in v0.6.
|
|
203
|
+
|
|
204
|
+
Batch processing:
|
|
205
|
+
The `AnthropicLMInvoker` supports batch processing, which allows the language model to process multiple
|
|
206
|
+
requests in a single call. Batch processing is supported through the `batch` attribute.
|
|
207
|
+
|
|
208
|
+
Usage example:
|
|
209
|
+
```python
|
|
210
|
+
requests = {"request_1": "What color is the sky?", "request_2": "What color is the grass?"}
|
|
211
|
+
results = await lm_invoker.batch.invoke(requests)
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
Output example:
|
|
215
|
+
```python
|
|
216
|
+
{
|
|
217
|
+
"request_1": LMOutput(response="The sky is blue."),
|
|
218
|
+
"request_2": LMOutput(finish_details={"type": "error", "error": {"message": "...", ...}, ...}),
|
|
219
|
+
}
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
The `AnthropicLMInvoker` also supports the following standalone batch processing operations:
|
|
223
|
+
|
|
224
|
+
1. Create a batch job:
|
|
225
|
+
```python
|
|
226
|
+
requests = {"request_1": "What color is the sky?", "request_2": "What color is the grass?"}
|
|
227
|
+
batch_id = await lm_invoker.batch.create(requests)
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
2. Get the status of a batch job:
|
|
231
|
+
```python
|
|
232
|
+
status = await lm_invoker.batch.status(batch_id)
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
3. Retrieve the results of a batch job:
|
|
236
|
+
```python
|
|
237
|
+
results = await lm_invoker.batch.retrieve(batch_id)
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
Output example:
|
|
241
|
+
```python
|
|
242
|
+
{
|
|
243
|
+
"request_1": LMOutput(response="The sky is blue."),
|
|
244
|
+
"request_2": LMOutput(finish_details={"type": "error", "error": {"message": "...", ...}, ...}),
|
|
245
|
+
}
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
4. List the batch jobs:
|
|
249
|
+
```python
|
|
250
|
+
batch_jobs = await lm_invoker.batch.list()
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
Output example:
|
|
254
|
+
```python
|
|
255
|
+
[
|
|
256
|
+
{"id": "batch_123", "status": "finished"},
|
|
257
|
+
{"id": "batch_456", "status": "in_progress"},
|
|
258
|
+
{"id": "batch_789", "status": "canceling"},
|
|
259
|
+
]
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
5. Cancel a batch job:
|
|
263
|
+
```python
|
|
264
|
+
await lm_invoker.batch.cancel(batch_id)
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
Output types:
|
|
268
|
+
The output of the `AnthropicLMInvoker` can either be:
|
|
269
|
+
1. `str`: A text response.
|
|
270
|
+
2. `LMOutput`: A Pydantic model that may contain the following attributes:
|
|
271
|
+
2.1. response (str)
|
|
272
|
+
2.2. tool_calls (list[ToolCall])
|
|
273
|
+
2.3. structured_output (dict[str, Any] | BaseModel | None)
|
|
274
|
+
2.4. token_usage (TokenUsage | None)
|
|
275
|
+
2.5. duration (float | None)
|
|
276
|
+
2.6. finish_details (dict[str, Any])
|
|
277
|
+
2.7. reasoning (list[Reasoning])
|
|
278
|
+
'''
|
|
279
|
+
client: Incomplete
|
|
280
|
+
thinking: Incomplete
|
|
281
|
+
thinking_budget: Incomplete
|
|
282
|
+
def __init__(self, model_name: str, api_key: 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, thinking: bool = False, thinking_budget: int = ..., simplify_events: bool = False) -> None:
|
|
283
|
+
"""Initializes the AnthropicLmInvoker instance.
|
|
284
|
+
|
|
285
|
+
Args:
|
|
286
|
+
model_name (str): The name of the Anthropic language model.
|
|
287
|
+
api_key (str | None, optional): The Anthropic API key. Defaults to None, in which case the
|
|
288
|
+
`ANTHROPIC_API_KEY` environment variable will be used.
|
|
289
|
+
model_kwargs (dict[str, Any] | None, optional): Additional keyword arguments for the Anthropic client.
|
|
290
|
+
default_hyperparameters (dict[str, Any] | None, optional): Default hyperparameters for invoking the model.
|
|
291
|
+
Defaults to None.
|
|
292
|
+
tools (list[Tool | LangChainTool] | None, optional): Tools provided to the model to enable tool calling.
|
|
293
|
+
Defaults to None, in which case an empty list is used.
|
|
294
|
+
response_schema (ResponseSchema | None, optional): The schema of the response. If provided, the model will
|
|
295
|
+
output a structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema
|
|
296
|
+
dictionary. Defaults to None.
|
|
297
|
+
output_analytics (bool, optional): Whether to output the invocation analytics. Defaults to False.
|
|
298
|
+
retry_config (RetryConfig | None, optional): The retry configuration for the language model.
|
|
299
|
+
Defaults to None, in which case a default config with no retry and 30.0 seconds timeout will be used.
|
|
300
|
+
thinking (bool, optional): Whether to enable thinking. Only allowed for thinking models. Defaults to False.
|
|
301
|
+
thinking_budget (int, optional): The tokens allocated for the thinking process. Must be greater than or
|
|
302
|
+
equal to 1024. Only allowed for thinking models. Defaults to DEFAULT_THINKING_BUDGET.
|
|
303
|
+
simplify_events (bool, optional): Temporary parameter to control the streamed events format.
|
|
304
|
+
When True, uses the simplified events format. When False, uses the legacy events format for
|
|
305
|
+
backward compatibility. Will be removed in v0.6. Defaults to False.
|
|
306
|
+
|
|
307
|
+
Raises:
|
|
308
|
+
ValueError:
|
|
309
|
+
1. `thinking` is True, but the `thinking_budget` is less than 1024.
|
|
310
|
+
3. `response_schema` is provided, but `tools` or `thinking` are also provided.
|
|
311
|
+
"""
|
|
312
|
+
def set_tools(self, tools: list[Tool | LangChainTool]) -> None:
|
|
313
|
+
"""Sets the tools for the Anthropic language model.
|
|
314
|
+
|
|
315
|
+
This method sets the tools for the Anthropic language model. Any existing tools will be replaced.
|
|
316
|
+
|
|
317
|
+
Args:
|
|
318
|
+
tools (list[Tool | LangChainTool]): The list of tools to be used.
|
|
319
|
+
|
|
320
|
+
Raises:
|
|
321
|
+
ValueError: If `response_schema` exists.
|
|
322
|
+
"""
|
|
323
|
+
def set_response_schema(self, response_schema: ResponseSchema | None) -> None:
|
|
324
|
+
"""Sets the response schema for the Anthropic language model.
|
|
325
|
+
|
|
326
|
+
This method sets the response schema for the Anthropic language model. Any existing response schema will be
|
|
327
|
+
replaced.
|
|
328
|
+
|
|
329
|
+
Args:
|
|
330
|
+
response_schema (ResponseSchema | None): The response schema to be used.
|
|
331
|
+
|
|
332
|
+
Raises:
|
|
333
|
+
ValueError: If `tools` exists.
|
|
334
|
+
"""
|
|
@@ -0,0 +1,254 @@
|
|
|
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_kwargs (dict[str, Any]): The keyword arguments for the Azure OpenAI client.
|
|
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
|
+
mcp_servers (list[MCPServer]): The list of MCP servers to enable MCP tool calling.
|
|
30
|
+
code_interpreter (bool): Whether to enable the code interpreter. Currently not supported.
|
|
31
|
+
web_search (bool): Whether to enable the web search. Currently not supported.
|
|
32
|
+
|
|
33
|
+
Basic usage:
|
|
34
|
+
The `AzureOpenAILMInvoker` can be used as follows:
|
|
35
|
+
```python
|
|
36
|
+
lm_invoker = AzureOpenAILMInvoker(
|
|
37
|
+
azure_endpoint="https://<your-azure-openai-endpoint>.openai.azure.com/openai/v1",
|
|
38
|
+
azure_deployment="<your-azure-openai-deployment>",
|
|
39
|
+
)
|
|
40
|
+
result = await lm_invoker.invoke("Hi there!")
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Input types:
|
|
44
|
+
The `AzureOpenAILMInvoker` supports the following input types: text, document, and image.
|
|
45
|
+
Non-text inputs can be passed as an `Attachment` object with the `user` role.
|
|
46
|
+
|
|
47
|
+
Usage example:
|
|
48
|
+
```python
|
|
49
|
+
text = "What animal is in this image?"
|
|
50
|
+
image = Attachment.from_path("path/to/local/image.png")
|
|
51
|
+
result = await lm_invoker.invoke([text, image])
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Tool calling:
|
|
55
|
+
Tool calling is a feature that allows the language model to call tools to perform tasks.
|
|
56
|
+
Tools can be passed to the via the `tools` parameter as a list of `Tool` objects.
|
|
57
|
+
When tools are provided and the model decides to call a tool, the tool calls are stored in the
|
|
58
|
+
`tool_calls` attribute in the output.
|
|
59
|
+
|
|
60
|
+
Usage example:
|
|
61
|
+
```python
|
|
62
|
+
lm_invoker = AzureOpenAILMInvoker(..., tools=[tool_1, tool_2])
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Output example:
|
|
66
|
+
```python
|
|
67
|
+
LMOutput(
|
|
68
|
+
response="Let me call the tools...",
|
|
69
|
+
tool_calls=[
|
|
70
|
+
ToolCall(id="123", name="tool_1", args={"key": "value"}),
|
|
71
|
+
ToolCall(id="456", name="tool_2", args={"key": "value"}),
|
|
72
|
+
]
|
|
73
|
+
)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Structured output:
|
|
77
|
+
Structured output is a feature that allows the language model to output a structured response.
|
|
78
|
+
This feature can be enabled by providing a schema to the `response_schema` parameter.
|
|
79
|
+
|
|
80
|
+
The schema must be either a JSON schema dictionary or a Pydantic BaseModel class.
|
|
81
|
+
If JSON schema is used, it must be compatible with Pydantic\'s JSON schema, especially for complex schemas.
|
|
82
|
+
For this reason, it is recommended to create the JSON schema using Pydantic\'s `model_json_schema` method.
|
|
83
|
+
|
|
84
|
+
The language model also doesn\'t need to stream anything when structured output is enabled. Thus, standard
|
|
85
|
+
invocation will be performed regardless of whether the `event_emitter` parameter is provided or not.
|
|
86
|
+
|
|
87
|
+
When enabled, the structured output is stored in the `structured_output` attribute in the output.
|
|
88
|
+
1. If the schema is a JSON schema dictionary, the structured output is a dictionary.
|
|
89
|
+
2. If the schema is a Pydantic BaseModel class, the structured output is a Pydantic model.
|
|
90
|
+
|
|
91
|
+
# Example 1: Using a JSON schema dictionary
|
|
92
|
+
Usage example:
|
|
93
|
+
```python
|
|
94
|
+
schema = {
|
|
95
|
+
"title": "Animal",
|
|
96
|
+
"description": "A description of an animal.",
|
|
97
|
+
"properties": {
|
|
98
|
+
"color": {"title": "Color", "type": "string"},
|
|
99
|
+
"name": {"title": "Name", "type": "string"},
|
|
100
|
+
},
|
|
101
|
+
"required": ["name", "color"],
|
|
102
|
+
"type": "object",
|
|
103
|
+
}
|
|
104
|
+
lm_invoker = AzureOpenAILMInvoker(..., response_schema=schema)
|
|
105
|
+
```
|
|
106
|
+
Output example:
|
|
107
|
+
```python
|
|
108
|
+
LMOutput(structured_output={"name": "Golden retriever", "color": "Golden"})
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
# Example 2: Using a Pydantic BaseModel class
|
|
112
|
+
Usage example:
|
|
113
|
+
```python
|
|
114
|
+
class Animal(BaseModel):
|
|
115
|
+
name: str
|
|
116
|
+
color: str
|
|
117
|
+
|
|
118
|
+
lm_invoker = AzureOpenAILMInvoker(..., response_schema=Animal)
|
|
119
|
+
```
|
|
120
|
+
Output example:
|
|
121
|
+
```python
|
|
122
|
+
LMOutput(structured_output=Animal(name="Golden retriever", color="Golden"))
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Analytics tracking:
|
|
126
|
+
Analytics tracking is a feature that allows the module to output additional information about the invocation.
|
|
127
|
+
This feature can be enabled by setting the `output_analytics` parameter to `True`.
|
|
128
|
+
When enabled, the following attributes will be stored in the output:
|
|
129
|
+
1. `token_usage`: The token usage.
|
|
130
|
+
2. `duration`: The duration in seconds.
|
|
131
|
+
3. `finish_details`: The details about how the generation finished.
|
|
132
|
+
|
|
133
|
+
Output example:
|
|
134
|
+
```python
|
|
135
|
+
LMOutput(
|
|
136
|
+
response="Golden retriever is a good dog breed.",
|
|
137
|
+
token_usage=TokenUsage(
|
|
138
|
+
input_tokens=1500,
|
|
139
|
+
output_tokens=200,
|
|
140
|
+
input_token_details=InputTokenDetails(cached_tokens=1200, uncached_tokens=300),
|
|
141
|
+
output_token_details=OutputTokenDetails(reasoning_tokens=180, response_tokens=20),
|
|
142
|
+
),
|
|
143
|
+
duration=0.729,
|
|
144
|
+
finish_details={"status": "completed", "incomplete_details": {"reason": None}},
|
|
145
|
+
)
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Retry and timeout:
|
|
149
|
+
The `AzureOpenAILMInvoker` supports retry and timeout configuration.
|
|
150
|
+
By default, the max retries is set to 0 and the timeout is set to 30.0 seconds.
|
|
151
|
+
They can be customized by providing a custom `RetryConfig` object to the `retry_config` parameter.
|
|
152
|
+
|
|
153
|
+
Retry config examples:
|
|
154
|
+
```python
|
|
155
|
+
retry_config = RetryConfig(max_retries=0, timeout=None) # No retry, no timeout
|
|
156
|
+
retry_config = RetryConfig(max_retries=0, timeout=10.0) # No retry, 10.0 seconds timeout
|
|
157
|
+
retry_config = RetryConfig(max_retries=5, timeout=None) # 5 max retries, no timeout
|
|
158
|
+
retry_config = RetryConfig(max_retries=5, timeout=10.0) # 5 max retries, 10.0 seconds timeout
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Usage example:
|
|
162
|
+
```python
|
|
163
|
+
lm_invoker = AzureOpenAILMInvoker(..., retry_config=retry_config)
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
Reasoning:
|
|
167
|
+
Azure OpenAI\'s GPT-5 models and o-series models are classified as reasoning models. Reasoning models think
|
|
168
|
+
before they answer, producing a long internal chain of thought before responding to the user. Reasoning models
|
|
169
|
+
excel in complex problem solving, coding, scientific reasoning, and multi-step planning for agentic workflows.
|
|
170
|
+
|
|
171
|
+
The reasoning effort of reasoning models can be set via the `reasoning_effort` parameter. This parameter
|
|
172
|
+
will guide the models on how many reasoning tokens it should generate before creating a response.
|
|
173
|
+
Available options include:
|
|
174
|
+
1. "minimal": Favors the least amount of reasoning, only supported for GPT-5 models onwards.
|
|
175
|
+
2. "low": Favors speed and economical token usage.
|
|
176
|
+
3. "medium": Favors a balance between speed and reasoning accuracy.
|
|
177
|
+
4. "high": Favors more complete reasoning at the cost of more tokens generated and slower responses.
|
|
178
|
+
|
|
179
|
+
Azure OpenAI doesn\'t expose the raw reasoning tokens. However, the summary of the reasoning tokens can still be
|
|
180
|
+
generated. The summary level can be set via the `reasoning_summary` parameter. Available options include:
|
|
181
|
+
1. "auto": The model decides the summary level automatically.
|
|
182
|
+
2. "detailed": The model will generate a detailed summary of the reasoning tokens.
|
|
183
|
+
Reasoning summary is not compatible with tool calling.
|
|
184
|
+
When enabled, the reasoning summary will be stored in the `reasoning` attribute in the output.
|
|
185
|
+
|
|
186
|
+
Output example:
|
|
187
|
+
```python
|
|
188
|
+
LMOutput(
|
|
189
|
+
response="Golden retriever is a good dog breed.",
|
|
190
|
+
reasoning=[Reasoning(id="x", reasoning="Let me think about it...")],
|
|
191
|
+
)
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
Streaming output example:
|
|
195
|
+
```python
|
|
196
|
+
{"type": "thinking_start", "value": ""}\', ...}
|
|
197
|
+
{"type": "thinking", "value": "Let me think "}\', ...}
|
|
198
|
+
{"type": "thinking", "value": "about it..."}\', ...}
|
|
199
|
+
{"type": "thinking_end", "value": ""}\', ...}
|
|
200
|
+
{"type": "response", "value": "Golden retriever ", ...}
|
|
201
|
+
{"type": "response", "value": "is a good dog breed.", ...}
|
|
202
|
+
```
|
|
203
|
+
Note: By default, the thinking token will be streamed with the legacy `EventType.DATA` event type.
|
|
204
|
+
To use the new simplified streamed event format, set the `simplify_events` parameter to `True` during
|
|
205
|
+
LM invoker initialization. The legacy event format support will be removed in v0.6.
|
|
206
|
+
|
|
207
|
+
Setting reasoning-related parameters for non-reasoning models will raise an error.
|
|
208
|
+
|
|
209
|
+
Output types:
|
|
210
|
+
The output of the `AzureOpenAILMInvoker` can either be:
|
|
211
|
+
1. `str`: A text response.
|
|
212
|
+
2. `LMOutput`: A Pydantic model that may contain the following attributes:
|
|
213
|
+
2.1. response (str)
|
|
214
|
+
2.2. tool_calls (list[ToolCall])
|
|
215
|
+
2.3. structured_output (dict[str, Any] | BaseModel | None)
|
|
216
|
+
2.4. token_usage (TokenUsage | None)
|
|
217
|
+
2.5. duration (float | None)
|
|
218
|
+
2.6. finish_details (dict[str, Any] | None)
|
|
219
|
+
2.7. reasoning (list[Reasoning])
|
|
220
|
+
'''
|
|
221
|
+
client_kwargs: Incomplete
|
|
222
|
+
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, simplify_events: bool = False) -> None:
|
|
223
|
+
"""Initializes a new instance of the AzureOpenAILMInvoker class.
|
|
224
|
+
|
|
225
|
+
Args:
|
|
226
|
+
azure_endpoint (str): The endpoint of the Azure OpenAI service.
|
|
227
|
+
azure_deployment (str): The deployment name of the Azure OpenAI service.
|
|
228
|
+
api_key (str | None, optional): The API key for authenticating with Azure OpenAI. Defaults to None, in
|
|
229
|
+
which case the `AZURE_OPENAI_API_KEY` environment variable will be used.
|
|
230
|
+
api_version (str | None, optional): Deprecated parameter to be removed in v0.6. Defaults to None.
|
|
231
|
+
model_kwargs (dict[str, Any] | None, optional): Additional model parameters. Defaults to None.
|
|
232
|
+
default_hyperparameters (dict[str, Any] | None, optional): Default hyperparameters for invoking the model.
|
|
233
|
+
Defaults to None.
|
|
234
|
+
tools (list[Tool | LangChainTool] | None, optional): Tools provided to the model to enable tool calling.
|
|
235
|
+
Defaults to None.
|
|
236
|
+
response_schema (ResponseSchema | None, optional): The schema of the response. If provided, the model will
|
|
237
|
+
output a structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema
|
|
238
|
+
dictionary. Defaults to None.
|
|
239
|
+
output_analytics (bool, optional): Whether to output the invocation analytics. Defaults to False.
|
|
240
|
+
retry_config (RetryConfig | None, optional): The retry configuration for the language model.
|
|
241
|
+
Defaults to None, in which case a default config with no retry and 30.0 seconds timeout will be used.
|
|
242
|
+
reasoning_effort (ReasoningEffort | None, optional): The reasoning effort for reasoning models. Not allowed
|
|
243
|
+
for non-reasoning models. If None, the model will perform medium reasoning effort. Defaults to None.
|
|
244
|
+
reasoning_summary (ReasoningSummary | None, optional): The reasoning summary level for reasoning models.
|
|
245
|
+
Not allowed for non-reasoning models. If None, no summary will be generated. Defaults to None.
|
|
246
|
+
simplify_events (bool, optional): Temporary parameter to control the streamed events format.
|
|
247
|
+
When True, uses the simplified events format. When False, uses the legacy events format for
|
|
248
|
+
backward compatibility. Will be removed in v0.6. Defaults to False.
|
|
249
|
+
|
|
250
|
+
Raises:
|
|
251
|
+
ValueError:
|
|
252
|
+
1. `reasoning_effort` is provided, but is not a valid ReasoningEffort.
|
|
253
|
+
2. `reasoning_summary` is provided, but is not a valid ReasoningSummary.
|
|
254
|
+
"""
|