gllm-inference-binary 0.5.51b3__cp311-cp311-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.
Potentially problematic release.
This version of gllm-inference-binary might be problematic. Click here for more details.
- 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-311-darwin.so +0 -0
- gllm_inference.pyi +156 -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,433 @@
|
|
|
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, OPENAI_DEFAULT_URL as OPENAI_DEFAULT_URL
|
|
6
|
+
from gllm_inference.lm_invoker.lm_invoker import BaseLMInvoker as BaseLMInvoker
|
|
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
|
|
10
|
+
from langchain_core.tools import Tool as LangChainTool
|
|
11
|
+
from typing import Any
|
|
12
|
+
|
|
13
|
+
SUPPORTED_ATTACHMENTS: Incomplete
|
|
14
|
+
STREAM_DATA_START_TYPE_MAP: Incomplete
|
|
15
|
+
STREAM_DATA_END_TYPE_MAP: Incomplete
|
|
16
|
+
STREAM_DATA_CONTENT_TYPE_MAP: Incomplete
|
|
17
|
+
|
|
18
|
+
class OpenAILMInvoker(BaseLMInvoker):
|
|
19
|
+
'''A language model invoker to interact with OpenAI language models.
|
|
20
|
+
|
|
21
|
+
This class provides support for OpenAI\'s Responses API schema, which is recommended by OpenAI as the preferred API
|
|
22
|
+
to use whenever possible. Use this class unless you have a specific reason to use the Chat Completions API instead.
|
|
23
|
+
The Chat Completions API schema is supported through the `OpenAIChatCompletionsLMInvoker` class.
|
|
24
|
+
|
|
25
|
+
Attributes:
|
|
26
|
+
model_id (str): The model ID of the language model.
|
|
27
|
+
model_provider (str): The provider of the language model.
|
|
28
|
+
model_name (str): The name of the language model.
|
|
29
|
+
client_kwargs (dict[str, Any]): The keyword arguments for the OpenAI client.
|
|
30
|
+
default_hyperparameters (dict[str, Any]): Default hyperparameters for invoking the model.
|
|
31
|
+
tools (list[Tool]): The list of tools provided to the model to enable tool calling.
|
|
32
|
+
response_schema (ResponseSchema | None): The schema of the response. If provided, the model will output a
|
|
33
|
+
structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema dictionary.
|
|
34
|
+
output_analytics (bool): Whether to output the invocation analytics.
|
|
35
|
+
retry_config (RetryConfig): The retry configuration for the language model.
|
|
36
|
+
reasoning_effort (ReasoningEffort | None): The reasoning effort for reasoning models. Not allowed
|
|
37
|
+
for non-reasoning models. If None, the model will perform medium reasoning effort.
|
|
38
|
+
reasoning_summary (ReasoningSummary | None): The reasoning summary level for reasoning models. Not allowed
|
|
39
|
+
for non-reasoning models. If None, no summary will be generated.
|
|
40
|
+
mcp_servers (list[MCPServer]): The list of MCP servers to enable MCP tool calling.
|
|
41
|
+
code_interpreter (bool): Whether to enable the code interpreter.
|
|
42
|
+
web_search (bool): Whether to enable the web search.
|
|
43
|
+
|
|
44
|
+
Basic usage:
|
|
45
|
+
The `OpenAILMInvoker` can be used as follows:
|
|
46
|
+
```python
|
|
47
|
+
lm_invoker = OpenAILMInvoker(model_name="gpt-5-nano")
|
|
48
|
+
result = await lm_invoker.invoke("Hi there!")
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
OpenAI compatible endpoints:
|
|
52
|
+
The `OpenAILMInvoker` can also be used to interact with endpoints that are compatible with
|
|
53
|
+
OpenAI\'s Responses API schema. This includes but are not limited to:
|
|
54
|
+
1. SGLang (https://github.com/sgl-project/sglang)
|
|
55
|
+
Please note that the supported features and capabilities may vary between different endpoints and
|
|
56
|
+
language models. Using features that are not supported by the endpoint will result in an error.
|
|
57
|
+
|
|
58
|
+
This customization can be done by setting the `base_url` parameter to the base URL of the endpoint:
|
|
59
|
+
```python
|
|
60
|
+
lm_invoker = OpenAILMInvoker(
|
|
61
|
+
model_name="<model-name>",
|
|
62
|
+
api_key="<your-api-key>",
|
|
63
|
+
base_url="<https://base-url>",
|
|
64
|
+
)
|
|
65
|
+
result = await lm_invoker.invoke("Hi there!")
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Input types:
|
|
69
|
+
The `OpenAILMInvoker` supports the following input types: text, document, and image.
|
|
70
|
+
Non-text inputs can be passed as an `Attachment` object with the `user` role.
|
|
71
|
+
|
|
72
|
+
Usage example:
|
|
73
|
+
```python
|
|
74
|
+
text = "What animal is in this image?"
|
|
75
|
+
image = Attachment.from_path("path/to/local/image.png")
|
|
76
|
+
result = await lm_invoker.invoke([text, image])
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Tool calling:
|
|
80
|
+
Tool calling is a feature that allows the language model to call tools to perform tasks.
|
|
81
|
+
Tools can be passed to the via the `tools` parameter as a list of `Tool` objects.
|
|
82
|
+
When tools are provided and the model decides to call a tool, the tool calls are stored in the
|
|
83
|
+
`tool_calls` attribute in the output.
|
|
84
|
+
|
|
85
|
+
Usage example:
|
|
86
|
+
```python
|
|
87
|
+
lm_invoker = OpenAILMInvoker(..., tools=[tool_1, tool_2])
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Output example:
|
|
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
|
+
)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Structured output:
|
|
102
|
+
Structured output is a feature that allows the language model to output a structured response.
|
|
103
|
+
This feature can be enabled by providing a schema to the `response_schema` parameter.
|
|
104
|
+
|
|
105
|
+
The schema must be either a JSON schema dictionary or a Pydantic BaseModel class.
|
|
106
|
+
If JSON schema is used, it must be compatible with Pydantic\'s JSON schema, especially for complex schemas.
|
|
107
|
+
For this reason, it is recommended to create the JSON schema using Pydantic\'s `model_json_schema` method.
|
|
108
|
+
|
|
109
|
+
The language model also doesn\'t need to stream anything when structured output is enabled. Thus, standard
|
|
110
|
+
invocation will be performed regardless of whether the `event_emitter` parameter is provided or not.
|
|
111
|
+
|
|
112
|
+
When enabled, the structured output is stored in the `structured_output` attribute in the output.
|
|
113
|
+
1. If the schema is a JSON schema dictionary, the structured output is a dictionary.
|
|
114
|
+
2. If the schema is a Pydantic BaseModel class, the structured output is a Pydantic model.
|
|
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
|
+
```
|
|
135
|
+
|
|
136
|
+
# Example 2: Using a Pydantic BaseModel class
|
|
137
|
+
Usage example:
|
|
138
|
+
```python
|
|
139
|
+
class Animal(BaseModel):
|
|
140
|
+
name: str
|
|
141
|
+
color: str
|
|
142
|
+
|
|
143
|
+
lm_invoker = OpenAILMInvoker(..., response_schema=Animal)
|
|
144
|
+
```
|
|
145
|
+
Output example:
|
|
146
|
+
```python
|
|
147
|
+
LMOutput(structured_output=Animal(name="Golden retriever", color="Golden"))
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Analytics tracking:
|
|
151
|
+
Analytics tracking is a feature that allows the module to output additional information about the invocation.
|
|
152
|
+
This feature can be enabled by setting the `output_analytics` parameter to `True`.
|
|
153
|
+
When enabled, the following attributes will be stored in the output:
|
|
154
|
+
1. `token_usage`: The token usage.
|
|
155
|
+
2. `duration`: The duration in seconds.
|
|
156
|
+
3. `finish_details`: The details about how the generation finished.
|
|
157
|
+
|
|
158
|
+
Output example:
|
|
159
|
+
```python
|
|
160
|
+
LMOutput(
|
|
161
|
+
response="Golden retriever is a good dog breed.",
|
|
162
|
+
token_usage=TokenUsage(
|
|
163
|
+
input_tokens=1500,
|
|
164
|
+
output_tokens=200,
|
|
165
|
+
input_token_details=InputTokenDetails(cached_tokens=1200, uncached_tokens=300),
|
|
166
|
+
output_token_details=OutputTokenDetails(reasoning_tokens=180, response_tokens=20),
|
|
167
|
+
),
|
|
168
|
+
duration=0.729,
|
|
169
|
+
finish_details={"status": "completed", "incomplete_details": {"reason": None}},
|
|
170
|
+
)
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Retry and timeout:
|
|
174
|
+
The `OpenAILMInvoker` supports retry and timeout configuration.
|
|
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.
|
|
177
|
+
|
|
178
|
+
Retry config examples:
|
|
179
|
+
```python
|
|
180
|
+
retry_config = RetryConfig(max_retries=0, timeout=None) # No retry, no timeout
|
|
181
|
+
retry_config = RetryConfig(max_retries=0, timeout=10.0) # No retry, 10.0 seconds timeout
|
|
182
|
+
retry_config = RetryConfig(max_retries=5, timeout=None) # 5 max retries, no timeout
|
|
183
|
+
retry_config = RetryConfig(max_retries=5, timeout=10.0) # 5 max retries, 10.0 seconds timeout
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
Usage example:
|
|
187
|
+
```python
|
|
188
|
+
lm_invoker = OpenAILMInvoker(..., retry_config=retry_config)
|
|
189
|
+
```
|
|
190
|
+
|
|
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
|
+
Output example:
|
|
213
|
+
```python
|
|
214
|
+
LMOutput(
|
|
215
|
+
response="Golden retriever is a good dog breed.",
|
|
216
|
+
reasoning=[Reasoning(id="x", reasoning="Let me think about it...")],
|
|
217
|
+
)
|
|
218
|
+
```
|
|
219
|
+
|
|
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
|
+
MCP tool calling:
|
|
236
|
+
The `OpenAILMInvoker` supports MCP tool calling. This feature can be enabled by providing a list of
|
|
237
|
+
MCP servers to the `mcp_servers` parameter. When MCP servers are provided and the model decides to call
|
|
238
|
+
an MCP tool, the MCP calls are stored in the `mcp_calls` attribute in the output.
|
|
239
|
+
|
|
240
|
+
Usage example:
|
|
241
|
+
```python
|
|
242
|
+
from gllm_inference.schema import MCPServer
|
|
243
|
+
mcp_server_1 = MCPServer(
|
|
244
|
+
url="https://mcp_server_1.com",
|
|
245
|
+
name="mcp_server_1",
|
|
246
|
+
)
|
|
247
|
+
lm_invoker = OpenAILMInvoker(..., mcp_servers=[mcp_server_1])
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
Output example:
|
|
251
|
+
```python
|
|
252
|
+
LMOutput(
|
|
253
|
+
response="The result is 10.",
|
|
254
|
+
mcp_calls=[
|
|
255
|
+
MCPCall(
|
|
256
|
+
id="123",
|
|
257
|
+
server_name="mcp_server_1",
|
|
258
|
+
tool_name="mcp_tool_1",
|
|
259
|
+
args={"key": "value"},
|
|
260
|
+
output="The result is 10.",
|
|
261
|
+
),
|
|
262
|
+
],
|
|
263
|
+
)
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
Streaming output example:
|
|
267
|
+
```python
|
|
268
|
+
{"type": "activity", "value": {"type": "mcp_list_tools", ...}, ...}
|
|
269
|
+
{"type": "activity", "value": {"type": "mcp_call", ...}, ...}
|
|
270
|
+
{"type": "response", "value": "The result ", ...}
|
|
271
|
+
{"type": "response", "value": "is 10.", ...}
|
|
272
|
+
```
|
|
273
|
+
Note: By default, the activity token will be streamed with the legacy `EventType.DATA` event type.
|
|
274
|
+
To use the new simplified streamed event format, set the `simplify_events` parameter to `True` during
|
|
275
|
+
LM invoker initialization. The legacy event format support will be removed in v0.6.
|
|
276
|
+
|
|
277
|
+
Code interpreter:
|
|
278
|
+
The code interpreter is a feature that allows the language model to write and run Python code in a
|
|
279
|
+
sandboxed environment to solve complex problems in domains like data analysis, coding, and math.
|
|
280
|
+
This feature can be enabled by setting the `code_interpreter` parameter to `True`.
|
|
281
|
+
|
|
282
|
+
Usage example:
|
|
283
|
+
```python
|
|
284
|
+
lm_invoker = OpenAILMInvoker(..., code_interpreter=True)
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
When code interpreter is enabled, it is highly recommended to instruct the model to use the "python tool"
|
|
288
|
+
in the system message, as "python tool" is the term recognized by the model to refer to the code interpreter.
|
|
289
|
+
|
|
290
|
+
Messages example:
|
|
291
|
+
```python
|
|
292
|
+
messages = [
|
|
293
|
+
Message.system("You are a data analyst. Use the python tool to generate a file."]),
|
|
294
|
+
Message.user("Show an histogram of the following data: [1, 2, 1, 4, 1, 2, 4, 2, 3, 1]"),
|
|
295
|
+
]
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
When code interpreter is enabled, the code execution results are stored in the `code_exec_results`
|
|
299
|
+
attribute in the output.
|
|
300
|
+
|
|
301
|
+
Output example:
|
|
302
|
+
```python
|
|
303
|
+
LMOutput(
|
|
304
|
+
response="The histogram is attached.",
|
|
305
|
+
code_exec_results=[
|
|
306
|
+
CodeExecResult(
|
|
307
|
+
id="123",
|
|
308
|
+
code="import matplotlib.pyplot as plt...",
|
|
309
|
+
output=[Attachment(data=b"...", mime_type="image/png")],
|
|
310
|
+
),
|
|
311
|
+
],
|
|
312
|
+
)
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
Streaming output example:
|
|
316
|
+
```python
|
|
317
|
+
{"type": "code_start", "value": ""}\', ...}
|
|
318
|
+
{"type": "code", "value": "import matplotlib"}\', ...}
|
|
319
|
+
{"type": "code", "value": ".pyplot as plt..."}\', ...}
|
|
320
|
+
{"type": "code_end", "value": ""}\', ...}
|
|
321
|
+
{"type": "response", "value": "The histogram ", ...}
|
|
322
|
+
{"type": "response", "value": "is attached.", ...}
|
|
323
|
+
```
|
|
324
|
+
Note: By default, the code token will be streamed with the legacy `EventType.DATA` event type.
|
|
325
|
+
To use the new simplified streamed event format, set the `simplify_events` parameter to `True` during
|
|
326
|
+
LM invoker initialization. The legacy event format support will be removed in v0.6.
|
|
327
|
+
|
|
328
|
+
Web search:
|
|
329
|
+
The web search is a feature that allows the language model to search the web for relevant information.
|
|
330
|
+
This feature can be enabled by setting the `web_search` parameter to `True`.
|
|
331
|
+
|
|
332
|
+
Usage example:
|
|
333
|
+
```python
|
|
334
|
+
lm_invoker = OpenAILMInvoker(..., web_search=True)
|
|
335
|
+
```
|
|
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
|
+
Output example:
|
|
342
|
+
```python
|
|
343
|
+
LMOutput(
|
|
344
|
+
response="The winner of the match is team A ([Example title](https://www.example.com)).",
|
|
345
|
+
citations=[
|
|
346
|
+
Chunk(
|
|
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
|
+
),
|
|
357
|
+
],
|
|
358
|
+
)
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
Streaming output example:
|
|
362
|
+
```python
|
|
363
|
+
{"type": "activity", "value": {"query": "search query"}, ...}
|
|
364
|
+
{"type": "response", "value": "The winner of the match ", ...}
|
|
365
|
+
{"type": "response", "value": "is team A ([Example title](https://www.example.com)).", ...}
|
|
366
|
+
```
|
|
367
|
+
Note: By default, the activity token will be streamed with the legacy `EventType.DATA` event type.
|
|
368
|
+
To use the new simplified streamed event format, set the `simplify_events` parameter to `True` during
|
|
369
|
+
LM invoker initialization. The legacy event format support will be removed in v0.6.
|
|
370
|
+
|
|
371
|
+
Output types:
|
|
372
|
+
The output of the `OpenAILMInvoker` can either be:
|
|
373
|
+
1. `str`: A text response.
|
|
374
|
+
2. `LMOutput`: A Pydantic model that may contain the following attributes:
|
|
375
|
+
2.1. response (str)
|
|
376
|
+
2.2. tool_calls (list[ToolCall])
|
|
377
|
+
2.3. structured_output (dict[str, Any] | BaseModel | None)
|
|
378
|
+
2.4. token_usage (TokenUsage | None)
|
|
379
|
+
2.5. duration (float | None)
|
|
380
|
+
2.6. finish_details (dict[str, Any])
|
|
381
|
+
2.7. reasoning (list[Reasoning])
|
|
382
|
+
2.8. citations (list[Chunk])
|
|
383
|
+
2.9. code_exec_results (list[CodeExecResult])
|
|
384
|
+
2.10. mcp_calls (list[MCPCall])
|
|
385
|
+
'''
|
|
386
|
+
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:
|
|
388
|
+
'''Initializes a new instance of the OpenAILMInvoker class.
|
|
389
|
+
|
|
390
|
+
Args:
|
|
391
|
+
model_name (str): The name of the OpenAI model.
|
|
392
|
+
api_key (str | None, optional): The API key for authenticating with OpenAI. Defaults to None, in which
|
|
393
|
+
case the `OPENAI_API_KEY` environment variable will be used. If the endpoint does not require an
|
|
394
|
+
API key, a dummy value can be passed (e.g. "<empty>").
|
|
395
|
+
base_url (str, optional): The base URL of a custom endpoint that is compatible with OpenAI\'s
|
|
396
|
+
Responses API schema. Defaults to OpenAI\'s default URL.
|
|
397
|
+
model_kwargs (dict[str, Any] | None, optional): Additional model parameters. Defaults to None.
|
|
398
|
+
default_hyperparameters (dict[str, Any] | None, optional): Default hyperparameters for invoking the model.
|
|
399
|
+
Defaults to None.
|
|
400
|
+
tools (list[Tool | LangChainTool] | None, optional): Tools provided to the model to enable tool calling.
|
|
401
|
+
Defaults to None, in which case an empty list is used.
|
|
402
|
+
response_schema (ResponseSchema | None, optional): The schema of the response. If provided, the model will
|
|
403
|
+
output a structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema
|
|
404
|
+
dictionary. Defaults to None.
|
|
405
|
+
output_analytics (bool, optional): Whether to output the invocation analytics. Defaults to False.
|
|
406
|
+
retry_config (RetryConfig | None, optional): The retry configuration for the language model.
|
|
407
|
+
Defaults to None, in which case a default config with no retry and 30.0 seconds timeout will be used.
|
|
408
|
+
reasoning_effort (ReasoningEffort | None, optional): The reasoning effort for reasoning models. Not allowed
|
|
409
|
+
for non-reasoning models. If None, the model will perform medium reasoning effort. Defaults to None.
|
|
410
|
+
reasoning_summary (ReasoningSummary | None, optional): The reasoning summary level for reasoning models.
|
|
411
|
+
Not allowed for non-reasoning models. If None, no summary will be generated. Defaults to None.
|
|
412
|
+
mcp_servers (list[MCPServer] | None, optional): The MCP servers containing tools to be accessed by the
|
|
413
|
+
language model. Defaults to None.
|
|
414
|
+
code_interpreter (bool, optional): Whether to enable the code interpreter. Defaults to False.
|
|
415
|
+
web_search (bool, optional): Whether to enable the web search. Defaults to False.
|
|
416
|
+
simplify_events (bool, optional): Temporary parameter to control the streamed events format.
|
|
417
|
+
When True, uses the simplified events format. When False, uses the legacy events format for
|
|
418
|
+
backward compatibility. Will be removed in v0.6. Defaults to False.
|
|
419
|
+
|
|
420
|
+
Raises:
|
|
421
|
+
ValueError:
|
|
422
|
+
1. `reasoning_effort` is provided, but is not a valid ReasoningEffort.
|
|
423
|
+
2. `reasoning_summary` is provided, but is not a valid ReasoningSummary.
|
|
424
|
+
'''
|
|
425
|
+
def set_response_schema(self, response_schema: ResponseSchema | None) -> None:
|
|
426
|
+
"""Sets the response schema for the OpenAI language model.
|
|
427
|
+
|
|
428
|
+
This method sets the response schema for the OpenAI language model. Any existing response schema will be
|
|
429
|
+
replaced.
|
|
430
|
+
|
|
431
|
+
Args:
|
|
432
|
+
response_schema (ResponseSchema | None): The response schema to be used.
|
|
433
|
+
"""
|