gllm-inference-binary 0.5.40__cp311-cp311-win_amd64.whl → 0.5.66__cp311-cp311-win_amd64.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/builder/_build_invoker.pyi +28 -0
- gllm_inference/builder/build_em_invoker.pyi +12 -16
- gllm_inference/builder/build_lm_invoker.pyi +65 -17
- gllm_inference/constants.pyi +3 -2
- gllm_inference/em_invoker/__init__.pyi +3 -1
- gllm_inference/em_invoker/bedrock_em_invoker.pyi +16 -4
- gllm_inference/em_invoker/cohere_em_invoker.pyi +127 -0
- gllm_inference/em_invoker/jina_em_invoker.pyi +103 -0
- gllm_inference/em_invoker/schema/bedrock.pyi +7 -0
- gllm_inference/em_invoker/schema/cohere.pyi +20 -0
- gllm_inference/em_invoker/schema/jina.pyi +29 -0
- gllm_inference/exceptions/provider_error_map.pyi +1 -0
- gllm_inference/lm_invoker/__init__.pyi +3 -1
- gllm_inference/lm_invoker/anthropic_lm_invoker.pyi +95 -109
- gllm_inference/lm_invoker/azure_openai_lm_invoker.pyi +92 -109
- gllm_inference/lm_invoker/batch/batch_operations.pyi +2 -1
- gllm_inference/lm_invoker/bedrock_lm_invoker.pyi +52 -65
- gllm_inference/lm_invoker/datasaur_lm_invoker.pyi +36 -36
- gllm_inference/lm_invoker/google_lm_invoker.pyi +195 -110
- gllm_inference/lm_invoker/langchain_lm_invoker.pyi +52 -64
- gllm_inference/lm_invoker/litellm_lm_invoker.pyi +86 -106
- gllm_inference/lm_invoker/lm_invoker.pyi +20 -1
- gllm_inference/lm_invoker/openai_chat_completions_lm_invoker.pyi +87 -107
- gllm_inference/lm_invoker/openai_lm_invoker.pyi +237 -186
- gllm_inference/lm_invoker/portkey_lm_invoker.pyi +296 -0
- gllm_inference/lm_invoker/schema/google.pyi +12 -0
- gllm_inference/lm_invoker/schema/openai.pyi +22 -0
- gllm_inference/lm_invoker/schema/portkey.pyi +31 -0
- gllm_inference/lm_invoker/sea_lion_lm_invoker.pyi +48 -0
- gllm_inference/lm_invoker/xai_lm_invoker.pyi +94 -131
- gllm_inference/model/__init__.pyi +5 -1
- gllm_inference/model/em/cohere_em.pyi +17 -0
- gllm_inference/model/em/jina_em.pyi +22 -0
- gllm_inference/model/lm/anthropic_lm.pyi +2 -0
- gllm_inference/model/lm/google_lm.pyi +1 -0
- gllm_inference/model/lm/sea_lion_lm.pyi +16 -0
- gllm_inference/model/lm/xai_lm.pyi +19 -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 +23 -6
- gllm_inference/schema/__init__.pyi +4 -3
- gllm_inference/schema/activity.pyi +13 -11
- gllm_inference/schema/attachment.pyi +20 -6
- gllm_inference/schema/enums.pyi +30 -1
- gllm_inference/schema/events.pyi +69 -73
- gllm_inference/schema/formatter.pyi +31 -0
- gllm_inference/schema/lm_output.pyi +245 -23
- gllm_inference/schema/model_id.pyi +27 -3
- gllm_inference/utils/validation.pyi +3 -0
- gllm_inference.cp311-win_amd64.pyd +0 -0
- gllm_inference.pyi +23 -13
- {gllm_inference_binary-0.5.40.dist-info → gllm_inference_binary-0.5.66.dist-info}/METADATA +10 -6
- {gllm_inference_binary-0.5.40.dist-info → gllm_inference_binary-0.5.66.dist-info}/RECORD +57 -40
- {gllm_inference_binary-0.5.40.dist-info → gllm_inference_binary-0.5.66.dist-info}/WHEEL +0 -0
- {gllm_inference_binary-0.5.40.dist-info → gllm_inference_binary-0.5.66.dist-info}/top_level.txt +0 -0
|
@@ -1,17 +1,23 @@
|
|
|
1
1
|
from _typeshed import Incomplete
|
|
2
2
|
from gllm_core.event import EventEmitter as EventEmitter
|
|
3
3
|
from gllm_core.schema.tool import Tool as Tool
|
|
4
|
-
from gllm_core.utils
|
|
4
|
+
from gllm_core.utils import RetryConfig as RetryConfig
|
|
5
5
|
from gllm_inference.constants import INVOKER_PROPAGATED_MAX_RETRIES as INVOKER_PROPAGATED_MAX_RETRIES, OPENAI_DEFAULT_URL as OPENAI_DEFAULT_URL
|
|
6
6
|
from gllm_inference.lm_invoker.lm_invoker import BaseLMInvoker as BaseLMInvoker
|
|
7
7
|
from gllm_inference.lm_invoker.schema.openai import InputType as InputType, Key as Key, OutputType as OutputType, ReasoningEffort as ReasoningEffort, ReasoningSummary as ReasoningSummary
|
|
8
|
-
from gllm_inference.schema import ActivityEvent as ActivityEvent, Attachment as Attachment, AttachmentType as AttachmentType, CodeEvent as CodeEvent, CodeExecResult as CodeExecResult, LMOutput as LMOutput, MCPCall as MCPCall, MCPCallActivity as MCPCallActivity, MCPListToolsActivity as MCPListToolsActivity, MCPServer as MCPServer, 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, WebSearchActivity as WebSearchActivity
|
|
9
|
-
from gllm_inference.utils import validate_string_enum as validate_string_enum
|
|
8
|
+
from gllm_inference.schema import ActivityEvent as ActivityEvent, Attachment as Attachment, AttachmentType as AttachmentType, BatchStatus as BatchStatus, CodeEvent as CodeEvent, CodeExecResult as CodeExecResult, LMOutput as LMOutput, MCPCall as MCPCall, MCPCallActivity as MCPCallActivity, MCPListToolsActivity as MCPListToolsActivity, MCPServer as MCPServer, Message as Message, MessageContent as MessageContent, 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, WebSearchActivity as WebSearchActivity
|
|
10
9
|
from langchain_core.tools import Tool as LangChainTool
|
|
10
|
+
from openai import AsyncStream as AsyncStream
|
|
11
|
+
from openai.types.responses import Response as Response, ResponseFunctionWebSearch as ResponseFunctionWebSearch, ResponseOutputItem as ResponseOutputItem
|
|
12
|
+
from openai.types.responses.response_output_item import McpCall as McpCall, McpListTools as McpListTools
|
|
11
13
|
from typing import Any
|
|
12
14
|
|
|
15
|
+
BATCH_STATUS_MAP: Incomplete
|
|
16
|
+
OPENAI_RESPONSES_API_ENDPOINT: str
|
|
17
|
+
DEFAULT_BATCH_COMPLETION_WINDOW: str
|
|
13
18
|
SUPPORTED_ATTACHMENTS: Incomplete
|
|
14
|
-
|
|
19
|
+
STREAM_DATA_START_TYPE_MAP: Incomplete
|
|
20
|
+
STREAM_DATA_END_TYPE_MAP: Incomplete
|
|
15
21
|
STREAM_DATA_CONTENT_TYPE_MAP: Incomplete
|
|
16
22
|
|
|
17
23
|
class OpenAILMInvoker(BaseLMInvoker):
|
|
@@ -36,6 +42,7 @@ class OpenAILMInvoker(BaseLMInvoker):
|
|
|
36
42
|
for non-reasoning models. If None, the model will perform medium reasoning effort.
|
|
37
43
|
reasoning_summary (ReasoningSummary | None): The reasoning summary level for reasoning models. Not allowed
|
|
38
44
|
for non-reasoning models. If None, no summary will be generated.
|
|
45
|
+
image_generation (bool): Whether to enable image generation.
|
|
39
46
|
mcp_servers (list[MCPServer]): The list of MCP servers to enable MCP tool calling.
|
|
40
47
|
code_interpreter (bool): Whether to enable the code interpreter.
|
|
41
48
|
web_search (bool): Whether to enable the web search.
|
|
@@ -75,188 +82,135 @@ class OpenAILMInvoker(BaseLMInvoker):
|
|
|
75
82
|
result = await lm_invoker.invoke([text, image])
|
|
76
83
|
```
|
|
77
84
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
`tool_calls` attribute in the output.
|
|
83
|
-
|
|
84
|
-
Usage example:
|
|
85
|
-
```python
|
|
86
|
-
lm_invoker = OpenAILMInvoker(..., tools=[tool_1, tool_2])
|
|
87
|
-
```
|
|
85
|
+
Text output:
|
|
86
|
+
The `OpenAILMInvoker` 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.
|
|
88
89
|
|
|
89
90
|
Output example:
|
|
90
91
|
```python
|
|
91
|
-
LMOutput(
|
|
92
|
-
response="Let me call the tools...",
|
|
93
|
-
tool_calls=[
|
|
94
|
-
ToolCall(id="123", name="tool_1", args={"key": "value"}),
|
|
95
|
-
ToolCall(id="456", name="tool_2", args={"key": "value"}),
|
|
96
|
-
]
|
|
97
|
-
)
|
|
92
|
+
LMOutput(outputs=[LMOutputItem(type="text", output="Hello, there!")])
|
|
98
93
|
```
|
|
99
94
|
|
|
100
95
|
Structured output:
|
|
101
|
-
|
|
96
|
+
The `OpenAILMInvoker` can be configured to generate structured outputs.
|
|
102
97
|
This feature can be enabled by providing a schema to the `response_schema` parameter.
|
|
103
98
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
For this reason, it is recommended to create the JSON schema using Pydantic\'s `model_json_schema` method.
|
|
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.
|
|
107
101
|
|
|
108
|
-
The
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
# Example 1: Using a JSON schema dictionary
|
|
116
|
-
Usage example:
|
|
117
|
-
```python
|
|
118
|
-
schema = {
|
|
119
|
-
"title": "Animal",
|
|
120
|
-
"description": "A description of an animal.",
|
|
121
|
-
"properties": {
|
|
122
|
-
"color": {"title": "Color", "type": "string"},
|
|
123
|
-
"name": {"title": "Name", "type": "string"},
|
|
124
|
-
},
|
|
125
|
-
"required": ["name", "color"],
|
|
126
|
-
"type": "object",
|
|
127
|
-
}
|
|
128
|
-
lm_invoker = OpenAILMInvoker(..., response_schema=schema)
|
|
129
|
-
```
|
|
130
|
-
Output example:
|
|
131
|
-
```python
|
|
132
|
-
LMOutput(structured_output={"name": "Golden retriever", "color": "Golden"})
|
|
133
|
-
```
|
|
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.
|
|
134
109
|
|
|
135
|
-
# Example 2: Using a Pydantic BaseModel class
|
|
136
110
|
Usage example:
|
|
137
111
|
```python
|
|
138
112
|
class Animal(BaseModel):
|
|
139
113
|
name: str
|
|
140
114
|
color: str
|
|
141
115
|
|
|
142
|
-
|
|
116
|
+
json_schema = Animal.model_json_schema()
|
|
117
|
+
|
|
118
|
+
lm_invoker = OpenAILMInvoker(..., response_schema=Animal) # Using Pydantic BaseModel class
|
|
119
|
+
lm_invoker = OpenAILMInvoker(..., response_schema=json_schema) # Using JSON schema dictionary
|
|
143
120
|
```
|
|
121
|
+
|
|
144
122
|
Output example:
|
|
145
123
|
```python
|
|
146
|
-
|
|
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"})])
|
|
147
129
|
```
|
|
148
130
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
131
|
+
When structured output is enabled, streaming is disabled.
|
|
132
|
+
|
|
133
|
+
Image generation:
|
|
134
|
+
The `OpenAILMInvoker` can be configured to generate images.
|
|
135
|
+
This feature can be enabled by setting the `image_generation` parameter to `True`.
|
|
136
|
+
|
|
137
|
+
Image outputs are stored in the `outputs` attribute of the `LMOutput` object and can be accessed
|
|
138
|
+
via the `attachments` property.
|
|
139
|
+
|
|
140
|
+
Usage example:
|
|
141
|
+
```python
|
|
142
|
+
lm_invoker = OpenAILMInvoker(..., image_generation=True)
|
|
143
|
+
result = await lm_invoker.invoke("Create a picture...")
|
|
144
|
+
result.attachments[0].write_to_file("path/to/local/image.png")
|
|
145
|
+
```
|
|
156
146
|
|
|
157
147
|
Output example:
|
|
158
148
|
```python
|
|
159
149
|
LMOutput(
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
),
|
|
167
|
-
duration=0.729,
|
|
168
|
-
finish_details={"status": "completed", "incomplete_details": {"reason": None}},
|
|
150
|
+
outputs=[
|
|
151
|
+
LMOutputItem(
|
|
152
|
+
type="attachment",
|
|
153
|
+
output=Attachment(filename="image.png", mime_type="image/png", data=b"..."),
|
|
154
|
+
),
|
|
155
|
+
],
|
|
169
156
|
)
|
|
170
157
|
```
|
|
171
158
|
|
|
172
|
-
|
|
173
|
-
|
|
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.
|
|
159
|
+
When image generation is enabled, streaming is disabled.
|
|
160
|
+
Image generation is only available for certain models.
|
|
176
161
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
```
|
|
162
|
+
Tool calling:
|
|
163
|
+
The `OpenAILMInvoker` can be configured to call tools to perform certain tasks.
|
|
164
|
+
This feature can be enabled by providing a list of `Tool` objects to the `tools` parameter.
|
|
165
|
+
|
|
166
|
+
Tool calls outputs are stored in the `outputs` attribute of the `LMOutput` object and
|
|
167
|
+
can be accessed via the `tool_calls` property.
|
|
184
168
|
|
|
185
169
|
Usage example:
|
|
186
170
|
```python
|
|
187
|
-
lm_invoker = OpenAILMInvoker(...,
|
|
171
|
+
lm_invoker = OpenAILMInvoker(..., tools=[tool_1, tool_2])
|
|
188
172
|
```
|
|
189
173
|
|
|
190
|
-
Reasoning:
|
|
191
|
-
OpenAI\'s GPT-5 models and o-series models are classified as reasoning models. Reasoning models think before
|
|
192
|
-
they answer, producing a long internal chain of thought before responding to the user. Reasoning models
|
|
193
|
-
excel in complex problem solving, coding, scientific reasoning, and multi-step planning for agentic workflows.
|
|
194
|
-
|
|
195
|
-
The reasoning effort of reasoning models can be set via the `reasoning_effort` parameter. This parameter
|
|
196
|
-
will guide the models on how many reasoning tokens it should generate before creating a response.
|
|
197
|
-
Available options include:
|
|
198
|
-
1. "minimal": Favors the least amount of reasoning, only supported for GPT-5 models onwards.
|
|
199
|
-
2. "low": Favors speed and economical token usage.
|
|
200
|
-
3. "medium": Favors a balance between speed and reasoning accuracy.
|
|
201
|
-
4. "high": Favors more complete reasoning at the cost of more tokens generated and slower responses.
|
|
202
|
-
When not set, the reasoning effort will be equivalent to `medium` by default.
|
|
203
|
-
|
|
204
|
-
OpenAI doesn\'t expose the raw reasoning tokens. However, the summary of the reasoning tokens can still be
|
|
205
|
-
generated. The summary level can be set via the `reasoning_summary` parameter. Available options include:
|
|
206
|
-
1. "auto": The model decides the summary level automatically.
|
|
207
|
-
2. "detailed": The model will generate a detailed summary of the reasoning tokens.
|
|
208
|
-
Reasoning summary is not compatible with tool calling.
|
|
209
|
-
When enabled, the reasoning summary will be stored in the `reasoning` attribute in the output.
|
|
210
|
-
|
|
211
174
|
Output example:
|
|
212
175
|
```python
|
|
213
176
|
LMOutput(
|
|
214
|
-
|
|
215
|
-
|
|
177
|
+
outputs=[
|
|
178
|
+
LMOutputItem(type="text", output="I\'m using tools..."),
|
|
179
|
+
LMOutputItem(type="tool_call", output=ToolCall(id="123", name="tool_1", args={"key": "value"})),
|
|
180
|
+
LMOutputItem(type="tool_call", output=ToolCall(id="456", name="tool_2", args={"key": "value"})),
|
|
181
|
+
]
|
|
216
182
|
)
|
|
217
183
|
```
|
|
218
184
|
|
|
219
|
-
Streaming output example:
|
|
220
|
-
```python
|
|
221
|
-
{"type": "thinking_start", "value": ""}\', ...}
|
|
222
|
-
{"type": "thinking", "value": "Let me think "}\', ...}
|
|
223
|
-
{"type": "thinking", "value": "about it..."}\', ...}
|
|
224
|
-
{"type": "thinking_end", "value": ""}\', ...}
|
|
225
|
-
{"type": "response", "value": "Golden retriever ", ...}
|
|
226
|
-
{"type": "response", "value": "is a good dog breed.", ...}
|
|
227
|
-
```
|
|
228
|
-
Note: By default, the thinking token will be streamed with the legacy `EventType.DATA` event type.
|
|
229
|
-
To use the new simplified streamed event format, set the `simplify_events` parameter to `True` during
|
|
230
|
-
LM invoker initialization. The legacy event format support will be removed in v0.6.
|
|
231
|
-
|
|
232
|
-
Setting reasoning-related parameters for non-reasoning models will raise an error.
|
|
233
|
-
|
|
234
185
|
MCP tool calling:
|
|
235
|
-
The `OpenAILMInvoker`
|
|
236
|
-
|
|
237
|
-
|
|
186
|
+
The `OpenAILMInvoker` can be configured to call MCP tools to perform certain tasks.
|
|
187
|
+
This feature can be enabled by providing a list of MCP servers to the `mcp_servers` parameter.
|
|
188
|
+
|
|
189
|
+
MCP calls outputs are stored in the `outputs` attribute of the `LMOutput` object and
|
|
190
|
+
can be accessed via the `mcp_calls` property.
|
|
238
191
|
|
|
239
192
|
Usage example:
|
|
240
193
|
```python
|
|
241
194
|
from gllm_inference.schema import MCPServer
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
name="mcp_server_1",
|
|
245
|
-
)
|
|
195
|
+
|
|
196
|
+
mcp_server_1 = MCPServer(url="https://mcp_server_1.com", name="mcp_server_1")
|
|
246
197
|
lm_invoker = OpenAILMInvoker(..., mcp_servers=[mcp_server_1])
|
|
247
198
|
```
|
|
248
199
|
|
|
249
200
|
Output example:
|
|
250
201
|
```python
|
|
251
202
|
LMOutput(
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
203
|
+
outputs=[
|
|
204
|
+
LMOutputItem(type="text", output="I\'m using MCP tools..."),
|
|
205
|
+
LMOutputItem(
|
|
206
|
+
type="mcp_call",
|
|
207
|
+
output=MCPCall(
|
|
208
|
+
id="123",
|
|
209
|
+
server_name="mcp_server_1",
|
|
210
|
+
tool_name="mcp_tool_1",
|
|
211
|
+
args={"key": "value"},
|
|
212
|
+
output="The result is 10."
|
|
213
|
+
),
|
|
260
214
|
),
|
|
261
215
|
],
|
|
262
216
|
)
|
|
@@ -273,39 +227,83 @@ class OpenAILMInvoker(BaseLMInvoker):
|
|
|
273
227
|
To use the new simplified streamed event format, set the `simplify_events` parameter to `True` during
|
|
274
228
|
LM invoker initialization. The legacy event format support will be removed in v0.6.
|
|
275
229
|
|
|
276
|
-
|
|
277
|
-
The
|
|
278
|
-
|
|
279
|
-
|
|
230
|
+
Reasoning:
|
|
231
|
+
The `OpenAILMInvoker` performs step-by-step reasoning before generating a response when reasoning
|
|
232
|
+
models are used, such as GPT-5 models and o-series models.
|
|
233
|
+
|
|
234
|
+
The reasoning effort can be set via the `reasoning_effort` parameter, which guides the models on the amount
|
|
235
|
+
of reasoning tokens to generate. Available options include `minimal`, `low`, `medium`, and `high`.
|
|
236
|
+
|
|
237
|
+
While the raw reasoning tokens are not available, the summary of the reasoning tokens can still be generated.
|
|
238
|
+
This can be done by passing the desired summary level via the `reasoning_summary` parameter.
|
|
239
|
+
Available options include `auto` and `detailed`.
|
|
240
|
+
|
|
241
|
+
Reasoning summaries are stored in the `outputs` attribute of the `LMOutput` object
|
|
242
|
+
and can be accessed via the `thinkings` property.
|
|
280
243
|
|
|
281
244
|
Usage example:
|
|
282
245
|
```python
|
|
283
|
-
lm_invoker = OpenAILMInvoker(...,
|
|
246
|
+
lm_invoker = OpenAILMInvoker(..., reasoning_effort="high", reasoning_summary="detailed")
|
|
284
247
|
```
|
|
285
248
|
|
|
249
|
+
Output example:
|
|
250
|
+
```python
|
|
251
|
+
LMOutput(
|
|
252
|
+
outputs=[
|
|
253
|
+
LMOutputItem(type="thinking", output=Reasoning(type="thinking", reasoning="I\'m thinking...", ...)),
|
|
254
|
+
LMOutputItem(type="text", output="Golden retriever is a good dog breed."),
|
|
255
|
+
]
|
|
256
|
+
)
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
Streaming output example:
|
|
260
|
+
```python
|
|
261
|
+
{"type": "thinking_start", "value": "", ...}
|
|
262
|
+
{"type": "thinking", "value": "I\'m ", ...}
|
|
263
|
+
{"type": "thinking", "value": "thinking...", ...}
|
|
264
|
+
{"type": "thinking_end", "value": "", ...}
|
|
265
|
+
{"type": "response", "value": "Golden retriever ", ...}
|
|
266
|
+
{"type": "response", "value": "is a good dog breed.", ...}
|
|
267
|
+
```
|
|
268
|
+
Note: By default, the thinking token will be streamed with the legacy `EventType.DATA` event type.
|
|
269
|
+
To use the new simplified streamed event format, set the `simplify_events` parameter to `True` during
|
|
270
|
+
LM invoker initialization. The legacy event format support will be removed in v0.6.
|
|
271
|
+
|
|
272
|
+
Reasoning summary is not compatible with tool calling.
|
|
273
|
+
|
|
274
|
+
Code interpreter:
|
|
275
|
+
The `OpenAILMInvoker` can be configured to write and run Python code in a sandboxed environment.
|
|
276
|
+
This is useful for solving complex problems in domains like data analysis, coding, and math.
|
|
277
|
+
This feature can be enabled by setting the `code_interpreter` parameter to `True`.
|
|
278
|
+
|
|
286
279
|
When code interpreter is enabled, it is highly recommended to instruct the model to use the "python tool"
|
|
287
280
|
in the system message, as "python tool" is the term recognized by the model to refer to the code interpreter.
|
|
288
281
|
|
|
289
|
-
|
|
282
|
+
Code execution results are stored in the `outputs` attribute of the `LMOutput` object and
|
|
283
|
+
can be accessed via the `code_exec_results` property.
|
|
284
|
+
|
|
285
|
+
Usage example:
|
|
290
286
|
```python
|
|
287
|
+
lm_invoker = OpenAILMInvoker(..., code_interpreter=True)
|
|
291
288
|
messages = [
|
|
292
289
|
Message.system("You are a data analyst. Use the python tool to generate a file."]),
|
|
293
290
|
Message.user("Show an histogram of the following data: [1, 2, 1, 4, 1, 2, 4, 2, 3, 1]"),
|
|
294
291
|
]
|
|
292
|
+
result = await lm_invoker.invoke(messages)
|
|
295
293
|
```
|
|
296
294
|
|
|
297
|
-
When code interpreter is enabled, the code execution results are stored in the `code_exec_results`
|
|
298
|
-
attribute in the output.
|
|
299
|
-
|
|
300
295
|
Output example:
|
|
301
296
|
```python
|
|
302
297
|
LMOutput(
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
298
|
+
outputs=[
|
|
299
|
+
LMOutputItem(type="text", output="The histogram is attached."),
|
|
300
|
+
LMOutputItem(
|
|
301
|
+
type="code_exec_result",
|
|
302
|
+
output=CodeExecResult(
|
|
303
|
+
id="123",
|
|
304
|
+
code="import matplotlib.pyplot as plt...",
|
|
305
|
+
output=[Attachment(data=b"...", mime_type="image/png")],
|
|
306
|
+
),
|
|
309
307
|
),
|
|
310
308
|
],
|
|
311
309
|
)
|
|
@@ -324,35 +322,24 @@ class OpenAILMInvoker(BaseLMInvoker):
|
|
|
324
322
|
To use the new simplified streamed event format, set the `simplify_events` parameter to `True` during
|
|
325
323
|
LM invoker initialization. The legacy event format support will be removed in v0.6.
|
|
326
324
|
|
|
327
|
-
Web
|
|
328
|
-
The
|
|
325
|
+
Web Search:
|
|
326
|
+
The `OpenAILMInvoker` can be configured to search the web for relevant information.
|
|
329
327
|
This feature can be enabled by setting the `web_search` parameter to `True`.
|
|
330
328
|
|
|
329
|
+
Web search citations are stored in the `outputs` attribute of the `LMOutput` object and
|
|
330
|
+
can be accessed via the `citations` property.
|
|
331
|
+
|
|
331
332
|
Usage example:
|
|
332
333
|
```python
|
|
333
334
|
lm_invoker = OpenAILMInvoker(..., web_search=True)
|
|
334
335
|
```
|
|
335
336
|
|
|
336
|
-
When web search is enabled, the language model will search the web for relevant information and may cite the
|
|
337
|
-
relevant sources. The citations will be stored as `Chunk` objects in the `citations` attribute in the output.
|
|
338
|
-
The content of the `Chunk` object is the type of the citation, e.g. "url_citation".
|
|
339
|
-
|
|
340
337
|
Output example:
|
|
341
338
|
```python
|
|
342
339
|
LMOutput(
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
id="123",
|
|
347
|
-
content="url_citation",
|
|
348
|
-
metadata={
|
|
349
|
-
"start_index": 164,
|
|
350
|
-
"end_index": 275,
|
|
351
|
-
"title": "Example title",
|
|
352
|
-
"url": "https://www.example.com",
|
|
353
|
-
"type": "url_citation",
|
|
354
|
-
},
|
|
355
|
-
),
|
|
340
|
+
outputs=[
|
|
341
|
+
LMOutputItem(type="citation", output=Chunk(id="123", content="...", metadata={...}, score=None)),
|
|
342
|
+
LMOutputItem(type="text", output="According to recent reports... ([Source](https://example.com))."),
|
|
356
343
|
],
|
|
357
344
|
)
|
|
358
345
|
```
|
|
@@ -360,30 +347,93 @@ class OpenAILMInvoker(BaseLMInvoker):
|
|
|
360
347
|
Streaming output example:
|
|
361
348
|
```python
|
|
362
349
|
{"type": "activity", "value": {"query": "search query"}, ...}
|
|
363
|
-
{"type": "response", "value": "
|
|
364
|
-
{"type": "response", "value": "
|
|
350
|
+
{"type": "response", "value": "According to recent ", ...}
|
|
351
|
+
{"type": "response", "value": "reports... ([Source](https://example.com)).", ...}
|
|
365
352
|
```
|
|
366
353
|
Note: By default, the activity token will be streamed with the legacy `EventType.DATA` event type.
|
|
367
354
|
To use the new simplified streamed event format, set the `simplify_events` parameter to `True` during
|
|
368
355
|
LM invoker initialization. The legacy event format support will be removed in v0.6.
|
|
369
356
|
|
|
370
|
-
|
|
371
|
-
The
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
357
|
+
Analytics tracking:
|
|
358
|
+
The `OpenAILMInvoker` can be configured to output additional information about the invocation.
|
|
359
|
+
This feature can be enabled by setting the `output_analytics` parameter to `True`.
|
|
360
|
+
|
|
361
|
+
When enabled, the following attributes will be stored in the output:
|
|
362
|
+
1. `token_usage`: The token usage.
|
|
363
|
+
2. `duration`: The duration in seconds.
|
|
364
|
+
3. `finish_details`: The details about how the generation finished.
|
|
365
|
+
|
|
366
|
+
Output example:
|
|
367
|
+
```python
|
|
368
|
+
LMOutput(
|
|
369
|
+
outputs=[...],
|
|
370
|
+
token_usage=TokenUsage(input_tokens=100, output_tokens=50),
|
|
371
|
+
duration=0.729,
|
|
372
|
+
finish_details={"stop_reason": "end_turn"},
|
|
373
|
+
)
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
Retry and timeout:
|
|
377
|
+
The `OpenAILMInvoker` supports retry and timeout configuration.
|
|
378
|
+
By default, the max retries is set to 0 and the timeout is set to 30.0 seconds.
|
|
379
|
+
They can be customized by providing a custom `RetryConfig` object to the `retry_config` parameter.
|
|
380
|
+
|
|
381
|
+
Retry config examples:
|
|
382
|
+
```python
|
|
383
|
+
retry_config = RetryConfig(max_retries=0, timeout=None) # No retry, no timeout
|
|
384
|
+
retry_config = RetryConfig(max_retries=5, timeout=10.0) # 5 max retries, 10.0 seconds timeout
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
Usage example:
|
|
388
|
+
```python
|
|
389
|
+
lm_invoker = OpenAILMInvoker(..., retry_config=retry_config)
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
Batch processing:
|
|
393
|
+
The `OpenAILMInvoker` supports batch processing, which allows the language model to process multiple
|
|
394
|
+
requests in a single call. The batch processing operations include:
|
|
395
|
+
|
|
396
|
+
1. Create a batch job:
|
|
397
|
+
```python
|
|
398
|
+
requests = {"request_1": "What color is the sky?", "request_2": "What color is the grass?"}
|
|
399
|
+
batch_id = await lm_invoker.batch.create(requests)
|
|
400
|
+
```
|
|
401
|
+
2. Get the status of a batch job:
|
|
402
|
+
```python
|
|
403
|
+
status = await lm_invoker.batch.status(batch_id)
|
|
404
|
+
```
|
|
405
|
+
3. Retrieve the results of a batch job:
|
|
406
|
+
Results are keyed by the request indices provided during batch creation.
|
|
407
|
+
|
|
408
|
+
```python
|
|
409
|
+
results = await lm_invoker.batch.retrieve(batch_id)
|
|
410
|
+
```
|
|
411
|
+
Output example:
|
|
412
|
+
```python
|
|
413
|
+
{
|
|
414
|
+
"request_1": LMOutput(outputs=[LMOutputItem(type="text", output="The sky is blue.")]),
|
|
415
|
+
"request_2": LMOutput(finish_details={"type": "error", "error": {"message": "...", ...}, ...}),
|
|
416
|
+
}
|
|
417
|
+
```
|
|
418
|
+
4. List the batch jobs:
|
|
419
|
+
```python
|
|
420
|
+
batch_jobs = await lm_invoker.batch.list()
|
|
421
|
+
```
|
|
422
|
+
Output example:
|
|
423
|
+
```python
|
|
424
|
+
[
|
|
425
|
+
{"id": "batch_123", "status": "finished"},
|
|
426
|
+
{"id": "batch_456", "status": "in_progress"},
|
|
427
|
+
{"id": "batch_789", "status": "canceling"},
|
|
428
|
+
]
|
|
429
|
+
```
|
|
430
|
+
5. Cancel a batch job:
|
|
431
|
+
```python
|
|
432
|
+
await lm_invoker.batch.cancel(batch_id)
|
|
433
|
+
```
|
|
384
434
|
'''
|
|
385
435
|
client_kwargs: Incomplete
|
|
386
|
-
def __init__(self, model_name: str, api_key: str | None = None, base_url: str = ..., 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, mcp_servers: list[MCPServer] | None = None, code_interpreter: bool = False, web_search: bool = False, simplify_events: bool = False) -> None:
|
|
436
|
+
def __init__(self, model_name: str, api_key: str | None = None, base_url: str = ..., 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, image_generation: bool = False, mcp_servers: list[MCPServer] | None = None, code_interpreter: bool = False, web_search: bool = False, simplify_events: bool = False) -> None:
|
|
387
437
|
'''Initializes a new instance of the OpenAILMInvoker class.
|
|
388
438
|
|
|
389
439
|
Args:
|
|
@@ -408,6 +458,7 @@ class OpenAILMInvoker(BaseLMInvoker):
|
|
|
408
458
|
for non-reasoning models. If None, the model will perform medium reasoning effort. Defaults to None.
|
|
409
459
|
reasoning_summary (ReasoningSummary | None, optional): The reasoning summary level for reasoning models.
|
|
410
460
|
Not allowed for non-reasoning models. If None, no summary will be generated. Defaults to None.
|
|
461
|
+
image_generation (bool, optional): Whether to enable image generation. Defaults to False.
|
|
411
462
|
mcp_servers (list[MCPServer] | None, optional): The MCP servers containing tools to be accessed by the
|
|
412
463
|
language model. Defaults to None.
|
|
413
464
|
code_interpreter (bool, optional): Whether to enable the code interpreter. Defaults to False.
|