gllm-inference-binary 0.5.51__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_lm_invoker.pyi +10 -1
- gllm_inference/constants.pyi +0 -1
- gllm_inference/em_invoker/cohere_em_invoker.pyi +1 -2
- gllm_inference/lm_invoker/__init__.pyi +2 -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 +51 -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 +235 -185
- gllm_inference/lm_invoker/portkey_lm_invoker.pyi +104 -68
- gllm_inference/lm_invoker/schema/google.pyi +12 -0
- gllm_inference/lm_invoker/schema/openai.pyi +22 -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 +2 -1
- gllm_inference/model/lm/sea_lion_lm.pyi +16 -0
- gllm_inference/prompt_builder/prompt_builder.pyi +6 -2
- gllm_inference/schema/__init__.pyi +4 -3
- gllm_inference/schema/attachment.pyi +20 -6
- gllm_inference/schema/enums.pyi +14 -1
- gllm_inference/schema/events.pyi +2 -2
- gllm_inference/schema/formatter.pyi +31 -0
- gllm_inference/schema/lm_output.pyi +245 -23
- gllm_inference/schema/model_id.pyi +1 -1
- gllm_inference/utils/validation.pyi +3 -0
- gllm_inference.cp311-win_amd64.pyd +0 -0
- gllm_inference.pyi +5 -6
- {gllm_inference_binary-0.5.51.dist-info → gllm_inference_binary-0.5.66.dist-info}/METADATA +5 -5
- {gllm_inference_binary-0.5.51.dist-info → gllm_inference_binary-0.5.66.dist-info}/RECORD +37 -34
- {gllm_inference_binary-0.5.51.dist-info → gllm_inference_binary-0.5.66.dist-info}/WHEEL +0 -0
- {gllm_inference_binary-0.5.51.dist-info → gllm_inference_binary-0.5.66.dist-info}/top_level.txt +0 -0
|
@@ -1,15 +1,20 @@
|
|
|
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
|
|
15
20
|
STREAM_DATA_END_TYPE_MAP: Incomplete
|
|
@@ -37,6 +42,7 @@ class OpenAILMInvoker(BaseLMInvoker):
|
|
|
37
42
|
for non-reasoning models. If None, the model will perform medium reasoning effort.
|
|
38
43
|
reasoning_summary (ReasoningSummary | None): The reasoning summary level for reasoning models. Not allowed
|
|
39
44
|
for non-reasoning models. If None, no summary will be generated.
|
|
45
|
+
image_generation (bool): Whether to enable image generation.
|
|
40
46
|
mcp_servers (list[MCPServer]): The list of MCP servers to enable MCP tool calling.
|
|
41
47
|
code_interpreter (bool): Whether to enable the code interpreter.
|
|
42
48
|
web_search (bool): Whether to enable the web search.
|
|
@@ -76,188 +82,135 @@ class OpenAILMInvoker(BaseLMInvoker):
|
|
|
76
82
|
result = await lm_invoker.invoke([text, image])
|
|
77
83
|
```
|
|
78
84
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
`tool_calls` attribute in the output.
|
|
84
|
-
|
|
85
|
-
Usage example:
|
|
86
|
-
```python
|
|
87
|
-
lm_invoker = OpenAILMInvoker(..., tools=[tool_1, tool_2])
|
|
88
|
-
```
|
|
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.
|
|
89
89
|
|
|
90
90
|
Output example:
|
|
91
91
|
```python
|
|
92
|
-
LMOutput(
|
|
93
|
-
response="Let me call the tools...",
|
|
94
|
-
tool_calls=[
|
|
95
|
-
ToolCall(id="123", name="tool_1", args={"key": "value"}),
|
|
96
|
-
ToolCall(id="456", name="tool_2", args={"key": "value"}),
|
|
97
|
-
]
|
|
98
|
-
)
|
|
92
|
+
LMOutput(outputs=[LMOutputItem(type="text", output="Hello, there!")])
|
|
99
93
|
```
|
|
100
94
|
|
|
101
95
|
Structured output:
|
|
102
|
-
|
|
96
|
+
The `OpenAILMInvoker` can be configured to generate structured outputs.
|
|
103
97
|
This feature can be enabled by providing a schema to the `response_schema` parameter.
|
|
104
98
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
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.
|
|
108
101
|
|
|
109
|
-
The
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
# Example 1: Using a JSON schema dictionary
|
|
117
|
-
Usage example:
|
|
118
|
-
```python
|
|
119
|
-
schema = {
|
|
120
|
-
"title": "Animal",
|
|
121
|
-
"description": "A description of an animal.",
|
|
122
|
-
"properties": {
|
|
123
|
-
"color": {"title": "Color", "type": "string"},
|
|
124
|
-
"name": {"title": "Name", "type": "string"},
|
|
125
|
-
},
|
|
126
|
-
"required": ["name", "color"],
|
|
127
|
-
"type": "object",
|
|
128
|
-
}
|
|
129
|
-
lm_invoker = OpenAILMInvoker(..., response_schema=schema)
|
|
130
|
-
```
|
|
131
|
-
Output example:
|
|
132
|
-
```python
|
|
133
|
-
LMOutput(structured_output={"name": "Golden retriever", "color": "Golden"})
|
|
134
|
-
```
|
|
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.
|
|
135
109
|
|
|
136
|
-
# Example 2: Using a Pydantic BaseModel class
|
|
137
110
|
Usage example:
|
|
138
111
|
```python
|
|
139
112
|
class Animal(BaseModel):
|
|
140
113
|
name: str
|
|
141
114
|
color: str
|
|
142
115
|
|
|
143
|
-
|
|
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
|
|
144
120
|
```
|
|
121
|
+
|
|
145
122
|
Output example:
|
|
146
123
|
```python
|
|
147
|
-
|
|
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"})])
|
|
148
129
|
```
|
|
149
130
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
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
|
+
```
|
|
157
146
|
|
|
158
147
|
Output example:
|
|
159
148
|
```python
|
|
160
149
|
LMOutput(
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
),
|
|
168
|
-
duration=0.729,
|
|
169
|
-
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
|
+
],
|
|
170
156
|
)
|
|
171
157
|
```
|
|
172
158
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
By default, the max retries is set to 0 and the timeout is set to 30.0 seconds.
|
|
176
|
-
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.
|
|
177
161
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
```
|
|
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.
|
|
185
168
|
|
|
186
169
|
Usage example:
|
|
187
170
|
```python
|
|
188
|
-
lm_invoker = OpenAILMInvoker(...,
|
|
171
|
+
lm_invoker = OpenAILMInvoker(..., tools=[tool_1, tool_2])
|
|
189
172
|
```
|
|
190
173
|
|
|
191
|
-
Reasoning:
|
|
192
|
-
OpenAI\'s GPT-5 models and o-series models are classified as reasoning models. Reasoning models think before
|
|
193
|
-
they answer, producing a long internal chain of thought before responding to the user. Reasoning models
|
|
194
|
-
excel in complex problem solving, coding, scientific reasoning, and multi-step planning for agentic workflows.
|
|
195
|
-
|
|
196
|
-
The reasoning effort of reasoning models can be set via the `reasoning_effort` parameter. This parameter
|
|
197
|
-
will guide the models on how many reasoning tokens it should generate before creating a response.
|
|
198
|
-
Available options include:
|
|
199
|
-
1. "minimal": Favors the least amount of reasoning, only supported for GPT-5 models onwards.
|
|
200
|
-
2. "low": Favors speed and economical token usage.
|
|
201
|
-
3. "medium": Favors a balance between speed and reasoning accuracy.
|
|
202
|
-
4. "high": Favors more complete reasoning at the cost of more tokens generated and slower responses.
|
|
203
|
-
When not set, the reasoning effort will be equivalent to `medium` by default.
|
|
204
|
-
|
|
205
|
-
OpenAI doesn\'t expose the raw reasoning tokens. However, the summary of the reasoning tokens can still be
|
|
206
|
-
generated. The summary level can be set via the `reasoning_summary` parameter. Available options include:
|
|
207
|
-
1. "auto": The model decides the summary level automatically.
|
|
208
|
-
2. "detailed": The model will generate a detailed summary of the reasoning tokens.
|
|
209
|
-
Reasoning summary is not compatible with tool calling.
|
|
210
|
-
When enabled, the reasoning summary will be stored in the `reasoning` attribute in the output.
|
|
211
|
-
|
|
212
174
|
Output example:
|
|
213
175
|
```python
|
|
214
176
|
LMOutput(
|
|
215
|
-
|
|
216
|
-
|
|
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
|
+
]
|
|
217
182
|
)
|
|
218
183
|
```
|
|
219
184
|
|
|
220
|
-
Streaming output example:
|
|
221
|
-
```python
|
|
222
|
-
{"type": "thinking_start", "value": ""}\', ...}
|
|
223
|
-
{"type": "thinking", "value": "Let me think "}\', ...}
|
|
224
|
-
{"type": "thinking", "value": "about it..."}\', ...}
|
|
225
|
-
{"type": "thinking_end", "value": ""}\', ...}
|
|
226
|
-
{"type": "response", "value": "Golden retriever ", ...}
|
|
227
|
-
{"type": "response", "value": "is a good dog breed.", ...}
|
|
228
|
-
```
|
|
229
|
-
Note: By default, the thinking token will be streamed with the legacy `EventType.DATA` event type.
|
|
230
|
-
To use the new simplified streamed event format, set the `simplify_events` parameter to `True` during
|
|
231
|
-
LM invoker initialization. The legacy event format support will be removed in v0.6.
|
|
232
|
-
|
|
233
|
-
Setting reasoning-related parameters for non-reasoning models will raise an error.
|
|
234
|
-
|
|
235
185
|
MCP tool calling:
|
|
236
|
-
The `OpenAILMInvoker`
|
|
237
|
-
|
|
238
|
-
|
|
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.
|
|
239
191
|
|
|
240
192
|
Usage example:
|
|
241
193
|
```python
|
|
242
194
|
from gllm_inference.schema import MCPServer
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
name="mcp_server_1",
|
|
246
|
-
)
|
|
195
|
+
|
|
196
|
+
mcp_server_1 = MCPServer(url="https://mcp_server_1.com", name="mcp_server_1")
|
|
247
197
|
lm_invoker = OpenAILMInvoker(..., mcp_servers=[mcp_server_1])
|
|
248
198
|
```
|
|
249
199
|
|
|
250
200
|
Output example:
|
|
251
201
|
```python
|
|
252
202
|
LMOutput(
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
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
|
+
),
|
|
261
214
|
),
|
|
262
215
|
],
|
|
263
216
|
)
|
|
@@ -274,39 +227,83 @@ class OpenAILMInvoker(BaseLMInvoker):
|
|
|
274
227
|
To use the new simplified streamed event format, set the `simplify_events` parameter to `True` during
|
|
275
228
|
LM invoker initialization. The legacy event format support will be removed in v0.6.
|
|
276
229
|
|
|
277
|
-
|
|
278
|
-
The
|
|
279
|
-
|
|
280
|
-
|
|
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.
|
|
281
243
|
|
|
282
244
|
Usage example:
|
|
283
245
|
```python
|
|
284
|
-
lm_invoker = OpenAILMInvoker(...,
|
|
246
|
+
lm_invoker = OpenAILMInvoker(..., reasoning_effort="high", reasoning_summary="detailed")
|
|
285
247
|
```
|
|
286
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
|
+
|
|
287
279
|
When code interpreter is enabled, it is highly recommended to instruct the model to use the "python tool"
|
|
288
280
|
in the system message, as "python tool" is the term recognized by the model to refer to the code interpreter.
|
|
289
281
|
|
|
290
|
-
|
|
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:
|
|
291
286
|
```python
|
|
287
|
+
lm_invoker = OpenAILMInvoker(..., code_interpreter=True)
|
|
292
288
|
messages = [
|
|
293
289
|
Message.system("You are a data analyst. Use the python tool to generate a file."]),
|
|
294
290
|
Message.user("Show an histogram of the following data: [1, 2, 1, 4, 1, 2, 4, 2, 3, 1]"),
|
|
295
291
|
]
|
|
292
|
+
result = await lm_invoker.invoke(messages)
|
|
296
293
|
```
|
|
297
294
|
|
|
298
|
-
When code interpreter is enabled, the code execution results are stored in the `code_exec_results`
|
|
299
|
-
attribute in the output.
|
|
300
|
-
|
|
301
295
|
Output example:
|
|
302
296
|
```python
|
|
303
297
|
LMOutput(
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
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
|
+
),
|
|
310
307
|
),
|
|
311
308
|
],
|
|
312
309
|
)
|
|
@@ -325,35 +322,24 @@ class OpenAILMInvoker(BaseLMInvoker):
|
|
|
325
322
|
To use the new simplified streamed event format, set the `simplify_events` parameter to `True` during
|
|
326
323
|
LM invoker initialization. The legacy event format support will be removed in v0.6.
|
|
327
324
|
|
|
328
|
-
Web
|
|
329
|
-
The
|
|
325
|
+
Web Search:
|
|
326
|
+
The `OpenAILMInvoker` can be configured to search the web for relevant information.
|
|
330
327
|
This feature can be enabled by setting the `web_search` parameter to `True`.
|
|
331
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
|
+
|
|
332
332
|
Usage example:
|
|
333
333
|
```python
|
|
334
334
|
lm_invoker = OpenAILMInvoker(..., web_search=True)
|
|
335
335
|
```
|
|
336
336
|
|
|
337
|
-
When web search is enabled, the language model will search the web for relevant information and may cite the
|
|
338
|
-
relevant sources. The citations will be stored as `Chunk` objects in the `citations` attribute in the output.
|
|
339
|
-
The content of the `Chunk` object is the type of the citation, e.g. "url_citation".
|
|
340
|
-
|
|
341
337
|
Output example:
|
|
342
338
|
```python
|
|
343
339
|
LMOutput(
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
id="123",
|
|
348
|
-
content="url_citation",
|
|
349
|
-
metadata={
|
|
350
|
-
"start_index": 164,
|
|
351
|
-
"end_index": 275,
|
|
352
|
-
"title": "Example title",
|
|
353
|
-
"url": "https://www.example.com",
|
|
354
|
-
"type": "url_citation",
|
|
355
|
-
},
|
|
356
|
-
),
|
|
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))."),
|
|
357
343
|
],
|
|
358
344
|
)
|
|
359
345
|
```
|
|
@@ -361,30 +347,93 @@ class OpenAILMInvoker(BaseLMInvoker):
|
|
|
361
347
|
Streaming output example:
|
|
362
348
|
```python
|
|
363
349
|
{"type": "activity", "value": {"query": "search query"}, ...}
|
|
364
|
-
{"type": "response", "value": "
|
|
365
|
-
{"type": "response", "value": "
|
|
350
|
+
{"type": "response", "value": "According to recent ", ...}
|
|
351
|
+
{"type": "response", "value": "reports... ([Source](https://example.com)).", ...}
|
|
366
352
|
```
|
|
367
353
|
Note: By default, the activity token will be streamed with the legacy `EventType.DATA` event type.
|
|
368
354
|
To use the new simplified streamed event format, set the `simplify_events` parameter to `True` during
|
|
369
355
|
LM invoker initialization. The legacy event format support will be removed in v0.6.
|
|
370
356
|
|
|
371
|
-
|
|
372
|
-
The
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
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
|
+
```
|
|
385
434
|
'''
|
|
386
435
|
client_kwargs: Incomplete
|
|
387
|
-
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:
|
|
388
437
|
'''Initializes a new instance of the OpenAILMInvoker class.
|
|
389
438
|
|
|
390
439
|
Args:
|
|
@@ -409,6 +458,7 @@ class OpenAILMInvoker(BaseLMInvoker):
|
|
|
409
458
|
for non-reasoning models. If None, the model will perform medium reasoning effort. Defaults to None.
|
|
410
459
|
reasoning_summary (ReasoningSummary | None, optional): The reasoning summary level for reasoning models.
|
|
411
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.
|
|
412
462
|
mcp_servers (list[MCPServer] | None, optional): The MCP servers containing tools to be accessed by the
|
|
413
463
|
language model. Defaults to None.
|
|
414
464
|
code_interpreter (bool, optional): Whether to enable the code interpreter. Defaults to False.
|