gllm-inference-binary 0.5.33__cp313-cp313-win_amd64.whl → 0.5.35__cp313-cp313-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.
Potentially problematic release.
This version of gllm-inference-binary might be problematic. Click here for more details.
- gllm_inference/builder/build_em_invoker.pyi +10 -13
- gllm_inference/builder/build_lm_invoker.pyi +30 -17
- gllm_inference/builder/build_lm_request_processor.pyi +2 -7
- gllm_inference/catalog/lm_request_processor_catalog.pyi +2 -2
- gllm_inference/constants.pyi +1 -0
- gllm_inference/em_invoker/openai_compatible_em_invoker.pyi +5 -60
- gllm_inference/em_invoker/openai_em_invoker.pyi +34 -6
- gllm_inference/lm_invoker/__init__.pyi +2 -1
- gllm_inference/lm_invoker/langchain_lm_invoker.pyi +3 -3
- gllm_inference/lm_invoker/litellm_lm_invoker.pyi +1 -1
- gllm_inference/lm_invoker/openai_chat_completions_lm_invoker.pyi +278 -0
- gllm_inference/lm_invoker/openai_compatible_lm_invoker.pyi +9 -226
- gllm_inference/lm_invoker/openai_lm_invoker.pyi +30 -6
- gllm_inference/lm_invoker/schema/{openai_compatible.pyi → openai_chat_completions.pyi} +2 -2
- gllm_inference/schema/__init__.pyi +3 -1
- gllm_inference/schema/activity.pyi +9 -0
- gllm_inference/schema/events.pyi +40 -0
- gllm_inference/schema/model_id.pyi +30 -25
- gllm_inference.cp313-win_amd64.pyd +0 -0
- gllm_inference.pyi +1 -0
- {gllm_inference_binary-0.5.33.dist-info → gllm_inference_binary-0.5.35.dist-info}/METADATA +35 -15
- {gllm_inference_binary-0.5.33.dist-info → gllm_inference_binary-0.5.35.dist-info}/RECORD +24 -21
- {gllm_inference_binary-0.5.33.dist-info → gllm_inference_binary-0.5.35.dist-info}/WHEEL +0 -0
- {gllm_inference_binary-0.5.33.dist-info → gllm_inference_binary-0.5.35.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,278 @@
|
|
|
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_chat_completions import InputType as InputType, Key as Key, ReasoningEffort as ReasoningEffort
|
|
8
|
+
from gllm_inference.schema import Attachment as Attachment, AttachmentType as AttachmentType, EmitDataType as EmitDataType, LMOutput as LMOutput, Message as Message, MessageRole as MessageRole, ModelId as ModelId, ModelProvider as ModelProvider, Reasoning as Reasoning, ResponseSchema as ResponseSchema, TokenUsage as TokenUsage, ToolCall as ToolCall, ToolResult as ToolResult
|
|
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
|
+
|
|
15
|
+
class OpenAIChatCompletionsLMInvoker(BaseLMInvoker):
|
|
16
|
+
'''A language model invoker to interact with OpenAI language models using the Chat Completions API.
|
|
17
|
+
|
|
18
|
+
This class provides support for OpenAI\'s Chat Completions API schema. Use this class only when you have
|
|
19
|
+
a specific reason to use the Chat Completions API over the Responses API, as OpenAI recommends using
|
|
20
|
+
the Responses API whenever possible. The Responses API schema is supported through the `OpenAILMInvoker` class.
|
|
21
|
+
|
|
22
|
+
Attributes:
|
|
23
|
+
model_id (str): The model ID of the language model.
|
|
24
|
+
model_provider (str): The provider of the language model.
|
|
25
|
+
model_name (str): The name of the language model.
|
|
26
|
+
client (AsyncOpenAI): The OpenAI client instance.
|
|
27
|
+
default_hyperparameters (dict[str, Any]): Default hyperparameters for invoking the model.
|
|
28
|
+
tools (list[Tool]): The list of tools provided to the model to enable tool calling.
|
|
29
|
+
response_schema (ResponseSchema | None): The schema of the response. If provided, the model will output a
|
|
30
|
+
structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema dictionary.
|
|
31
|
+
output_analytics (bool): Whether to output the invocation analytics.
|
|
32
|
+
retry_config (RetryConfig | None): The retry configuration for the language model.
|
|
33
|
+
|
|
34
|
+
Basic usage:
|
|
35
|
+
The `OpenAIChatCompletionsLMInvoker` can be used as follows:
|
|
36
|
+
```python
|
|
37
|
+
lm_invoker = OpenAIChatCompletionsLMInvoker(model_name="gpt-5-nano")
|
|
38
|
+
result = await lm_invoker.invoke("Hi there!")
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
OpenAI compatible endpoints:
|
|
42
|
+
The `OpenAIChatCompletionsLMInvoker` can also be used to interact with endpoints that are compatible with
|
|
43
|
+
OpenAI\'s Chat Completions API schema. This includes but are not limited to:
|
|
44
|
+
1. DeepInfra (https://deepinfra.com/)
|
|
45
|
+
2. DeepSeek (https://deepseek.com/)
|
|
46
|
+
3. Groq (https://groq.com/)
|
|
47
|
+
4. OpenRouter (https://openrouter.ai/)
|
|
48
|
+
5. Text Generation Inference (https://github.com/huggingface/text-generation-inference)
|
|
49
|
+
6. Together.ai (https://together.ai/)
|
|
50
|
+
7. vLLM (https://vllm.ai/)
|
|
51
|
+
Please note that the supported features and capabilities may vary between different endpoints and
|
|
52
|
+
language models. Using features that are not supported by the endpoint will result in an error.
|
|
53
|
+
|
|
54
|
+
This customization can be done by setting the `base_url` parameter to the base URL of the endpoint:
|
|
55
|
+
```python
|
|
56
|
+
lm_invoker = OpenAIChatCompletionsLMInvoker(
|
|
57
|
+
model_name="llama3-8b-8192",
|
|
58
|
+
api_key="<your-api-key>",
|
|
59
|
+
base_url="https://api.groq.com/openai/v1",
|
|
60
|
+
)
|
|
61
|
+
result = await lm_invoker.invoke("Hi there!")
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Input types:
|
|
65
|
+
The `OpenAIChatCompletionsLMInvoker` supports the following input types: text, audio, document, and image.
|
|
66
|
+
Non-text inputs can be passed as an `Attachment` object with the `user` role.
|
|
67
|
+
|
|
68
|
+
Usage example:
|
|
69
|
+
```python
|
|
70
|
+
text = "What animal is in this image?"
|
|
71
|
+
image = Attachment.from_path("path/to/local/image.png")
|
|
72
|
+
result = await lm_invoker.invoke([text, image])
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Tool calling:
|
|
76
|
+
Tool calling is a feature that allows the language model to call tools to perform tasks.
|
|
77
|
+
Tools can be passed to the via the `tools` parameter as a list of `Tool` objects.
|
|
78
|
+
When tools are provided and the model decides to call a tool, the tool calls are stored in the
|
|
79
|
+
`tool_calls` attribute in the output.
|
|
80
|
+
|
|
81
|
+
Usage example:
|
|
82
|
+
```python
|
|
83
|
+
lm_invoker = OpenAIChatCompletionsLMInvoker(..., tools=[tool_1, tool_2])
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Output example:
|
|
87
|
+
```python
|
|
88
|
+
LMOutput(
|
|
89
|
+
response="Let me call the tools...",
|
|
90
|
+
tool_calls=[
|
|
91
|
+
ToolCall(id="123", name="tool_1", args={"key": "value"}),
|
|
92
|
+
ToolCall(id="456", name="tool_2", args={"key": "value"}),
|
|
93
|
+
]
|
|
94
|
+
)
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Structured output:
|
|
98
|
+
Structured output is a feature that allows the language model to output a structured response.
|
|
99
|
+
This feature can be enabled by providing a schema to the `response_schema` parameter.
|
|
100
|
+
|
|
101
|
+
The schema must be either a JSON schema dictionary or a Pydantic BaseModel class.
|
|
102
|
+
If JSON schema is used, it must be compatible with Pydantic\'s JSON schema, especially for complex schemas.
|
|
103
|
+
For this reason, it is recommended to create the JSON schema using Pydantic\'s `model_json_schema` method.
|
|
104
|
+
|
|
105
|
+
The language model also doesn\'t need to stream anything when structured output is enabled. Thus, standard
|
|
106
|
+
invocation will be performed regardless of whether the `event_emitter` parameter is provided or not.
|
|
107
|
+
|
|
108
|
+
When enabled, the structured output is stored in the `structured_output` attribute in the output.
|
|
109
|
+
1. If the schema is a JSON schema dictionary, the structured output is a dictionary.
|
|
110
|
+
2. If the schema is a Pydantic BaseModel class, the structured output is a Pydantic model.
|
|
111
|
+
|
|
112
|
+
# Example 1: Using a JSON schema dictionary
|
|
113
|
+
Usage example:
|
|
114
|
+
```python
|
|
115
|
+
schema = {
|
|
116
|
+
"title": "Animal",
|
|
117
|
+
"description": "A description of an animal.",
|
|
118
|
+
"properties": {
|
|
119
|
+
"color": {"title": "Color", "type": "string"},
|
|
120
|
+
"name": {"title": "Name", "type": "string"},
|
|
121
|
+
},
|
|
122
|
+
"required": ["name", "color"],
|
|
123
|
+
"type": "object",
|
|
124
|
+
}
|
|
125
|
+
lm_invoker = OpenAIChatCompletionsLMInvoker(..., response_schema=schema)
|
|
126
|
+
```
|
|
127
|
+
Output example:
|
|
128
|
+
```python
|
|
129
|
+
LMOutput(structured_output={"name": "Golden retriever", "color": "Golden"})
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
# Example 2: Using a Pydantic BaseModel class
|
|
133
|
+
Usage example:
|
|
134
|
+
```python
|
|
135
|
+
class Animal(BaseModel):
|
|
136
|
+
name: str
|
|
137
|
+
color: str
|
|
138
|
+
|
|
139
|
+
lm_invoker = OpenAIChatCompletionsLMInvoker(..., response_schema=Animal)
|
|
140
|
+
```
|
|
141
|
+
Output example:
|
|
142
|
+
```python
|
|
143
|
+
LMOutput(structured_output=Animal(name="Golden retriever", color="Golden"))
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Analytics tracking:
|
|
147
|
+
Analytics tracking is a feature that allows the module to output additional information about the invocation.
|
|
148
|
+
This feature can be enabled by setting the `output_analytics` parameter to `True`.
|
|
149
|
+
When enabled, the following attributes will be stored in the output:
|
|
150
|
+
1. `token_usage`: The token usage.
|
|
151
|
+
2. `duration`: The duration in seconds.
|
|
152
|
+
3. `finish_details`: The details about how the generation finished.
|
|
153
|
+
|
|
154
|
+
Output example:
|
|
155
|
+
```python
|
|
156
|
+
LMOutput(
|
|
157
|
+
response="Golden retriever is a good dog breed.",
|
|
158
|
+
token_usage=TokenUsage(input_tokens=100, output_tokens=50),
|
|
159
|
+
duration=0.729,
|
|
160
|
+
finish_details={"finish_reason": "stop"},
|
|
161
|
+
)
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
When streaming is enabled, token usage is not supported. Therefore, the `token_usage` attribute will be `None`
|
|
165
|
+
regardless of the value of the `output_analytics` parameter.
|
|
166
|
+
|
|
167
|
+
Retry and timeout:
|
|
168
|
+
The `OpenAIChatCompletionsLMInvoker` supports retry and timeout configuration.
|
|
169
|
+
By default, the max retries is set to 0 and the timeout is set to 30.0 seconds.
|
|
170
|
+
They can be customized by providing a custom `RetryConfig` object to the `retry_config` parameter.
|
|
171
|
+
|
|
172
|
+
Retry config examples:
|
|
173
|
+
```python
|
|
174
|
+
retry_config = RetryConfig(max_retries=0, timeout=0.0) # No retry, no timeout
|
|
175
|
+
retry_config = RetryConfig(max_retries=0, timeout=10.0) # No retry, 10.0 seconds timeout
|
|
176
|
+
retry_config = RetryConfig(max_retries=5, timeout=0.0) # 5 max retries, no timeout
|
|
177
|
+
retry_config = RetryConfig(max_retries=5, timeout=10.0) # 5 max retries, 10.0 seconds timeout
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Usage example:
|
|
181
|
+
```python
|
|
182
|
+
lm_invoker = OpenAIChatCompletionsLMInvoker(..., retry_config=retry_config)
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
Reasoning:
|
|
186
|
+
Some language models support advanced reasoning capabilities. When using such reasoning-capable models,
|
|
187
|
+
you can configure how much reasoning the model should perform before generating a final response by setting
|
|
188
|
+
reasoning-related parameters.
|
|
189
|
+
|
|
190
|
+
The reasoning effort of reasoning models can be set via the `reasoning_effort` parameter. This parameter
|
|
191
|
+
will guide the models on how many reasoning tokens it should generate before creating a response to the prompt.
|
|
192
|
+
The reasoning effort is only supported by some language models.
|
|
193
|
+
Available options include:
|
|
194
|
+
1. "low": Favors speed and economical token usage.
|
|
195
|
+
2. "medium": Favors a balance between speed and reasoning accuracy.
|
|
196
|
+
3. "high": Favors more complete reasoning at the cost of more tokens generated and slower responses.
|
|
197
|
+
This may differ between models. When not set, the reasoning effort will be equivalent to None by default.
|
|
198
|
+
|
|
199
|
+
When using reasoning models, some providers might output the reasoning summary. These will be stored in the
|
|
200
|
+
`reasoning` attribute in the output.
|
|
201
|
+
|
|
202
|
+
Output example:
|
|
203
|
+
```python
|
|
204
|
+
LMOutput(
|
|
205
|
+
response="Golden retriever is a good dog breed.",
|
|
206
|
+
reasoning=[Reasoning(id="", reasoning="Let me think about it...")],
|
|
207
|
+
)
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
When streaming is enabled along with reasoning and the provider supports reasoning output, the reasoning token
|
|
211
|
+
will be streamed with the `EventType.DATA` event type.
|
|
212
|
+
|
|
213
|
+
Streaming output example:
|
|
214
|
+
```python
|
|
215
|
+
{"type": "data", "value": \'{"data_type": "thinking_start", "data_value": ""}\', ...}
|
|
216
|
+
{"type": "data", "value": \'{"data_type": "thinking", "data_value": "Let me think "}\', ...}
|
|
217
|
+
{"type": "data", "value": \'{"data_type": "thinking", "data_value": "about it..."}\', ...}
|
|
218
|
+
{"type": "data", "value": \'{"data_type": "thinking_end", "data_value": ""}\', ...}
|
|
219
|
+
{"type": "response", "value": "Golden retriever ", ...}
|
|
220
|
+
{"type": "response", "value": "is a good dog breed.", ...}
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
Setting reasoning-related parameters for non-reasoning models will raise an error.
|
|
224
|
+
|
|
225
|
+
Output types:
|
|
226
|
+
The output of the `OpenAIChatCompletionsLMInvoker` can either be:
|
|
227
|
+
1. `str`: The text response if no additional output is needed.
|
|
228
|
+
2. `LMOutput`: A Pydantic model with the following attributes if any additional output is needed:
|
|
229
|
+
2.1. response (str): The text response.
|
|
230
|
+
2.2. tool_calls (list[ToolCall]): The tool calls, if the `tools` parameter is defined and the language
|
|
231
|
+
model decides to invoke tools. Defaults to an empty list.
|
|
232
|
+
2.3. structured_output (dict[str, Any] | BaseModel | None): The structured output, if the `response_schema`
|
|
233
|
+
parameter is defined. Defaults to None.
|
|
234
|
+
2.4. token_usage (TokenUsage | None): The token usage analytics, if the `output_analytics` parameter is
|
|
235
|
+
set to `True`. Defaults to None.
|
|
236
|
+
2.5. duration (float | None): The duration of the invocation in seconds, if the `output_analytics`
|
|
237
|
+
parameter is set to `True`. Defaults to None.
|
|
238
|
+
2.6. finish_details (dict[str, Any] | None): The details about how the generation finished, if the
|
|
239
|
+
`output_analytics` parameter is set to `True`. Defaults to None.
|
|
240
|
+
2.7. reasoning (list[Reasoning]): The reasoning objects. Currently not supported. Defaults to an empty list.
|
|
241
|
+
2.8. citations (list[Chunk]): The citations. Currently not supported. Defaults to an empty list.
|
|
242
|
+
2.9. code_exec_results (list[CodeExecResult]): The code execution results. Currently not supported.
|
|
243
|
+
Defaults to an empty list.
|
|
244
|
+
2.10. mcp_calls (list[MCPCall]): The MCP calls. Currently not supported. Defaults to an empty list.
|
|
245
|
+
'''
|
|
246
|
+
client: Incomplete
|
|
247
|
+
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) -> None:
|
|
248
|
+
'''Initializes a new instance of the OpenAIChatCompletionsLMInvoker class.
|
|
249
|
+
|
|
250
|
+
Args:
|
|
251
|
+
model_name (str): The name of the OpenAI model.
|
|
252
|
+
api_key (str | None, optional): The API key for authenticating with OpenAI. Defaults to None, in which
|
|
253
|
+
case the `OPENAI_API_KEY` environment variable will be used. If the endpoint does not require an
|
|
254
|
+
API key, a dummy value can be passed (e.g. "<empty>").
|
|
255
|
+
base_url (str, optional): The base URL of a custom endpoint that is compatible with OpenAI\'s
|
|
256
|
+
Chat Completions API schema. Defaults to OpenAI\'s default URL.
|
|
257
|
+
model_kwargs (dict[str, Any] | None, optional): Additional model parameters. Defaults to None.
|
|
258
|
+
default_hyperparameters (dict[str, Any] | None, optional): Default hyperparameters for invoking the model.
|
|
259
|
+
Defaults to None.
|
|
260
|
+
tools (list[Tool | LangChainTool] | None, optional): Tools provided to the model to enable tool calling.
|
|
261
|
+
Defaults to None.
|
|
262
|
+
response_schema (ResponseSchema | None, optional): The schema of the response. If provided, the model will
|
|
263
|
+
output a structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema
|
|
264
|
+
dictionary. Defaults to None.
|
|
265
|
+
output_analytics (bool, optional): Whether to output the invocation analytics. Defaults to False.
|
|
266
|
+
retry_config (RetryConfig | None, optional): The retry configuration for the language model.
|
|
267
|
+
Defaults to None, in which case a default config with no retry and 30.0 seconds timeout will be used.
|
|
268
|
+
reasoning_effort (str | None, optional): The reasoning effort for the language model. Defaults to None.
|
|
269
|
+
'''
|
|
270
|
+
def set_response_schema(self, response_schema: ResponseSchema | None) -> None:
|
|
271
|
+
"""Sets the response schema for the OpenAI language model.
|
|
272
|
+
|
|
273
|
+
This method sets the response schema for the OpenAI language model.
|
|
274
|
+
Any existing response schema will be replaced.
|
|
275
|
+
|
|
276
|
+
Args:
|
|
277
|
+
response_schema (ResponseSchema | None): The response schema to be used.
|
|
278
|
+
"""
|
|
@@ -1,19 +1,15 @@
|
|
|
1
|
-
from _typeshed import Incomplete
|
|
2
|
-
from gllm_core.event import EventEmitter as EventEmitter
|
|
3
1
|
from gllm_core.schema.tool import Tool as Tool
|
|
4
|
-
from gllm_core.utils
|
|
5
|
-
from gllm_inference.
|
|
6
|
-
from gllm_inference.lm_invoker.
|
|
7
|
-
from gllm_inference.
|
|
8
|
-
from gllm_inference.schema import Attachment as Attachment, AttachmentType as AttachmentType, EmitDataType as EmitDataType, LMOutput as LMOutput, Message as Message, MessageRole as MessageRole, ModelId as ModelId, ModelProvider as ModelProvider, Reasoning as Reasoning, ResponseSchema as ResponseSchema, TokenUsage as TokenUsage, ToolCall as ToolCall, ToolResult as ToolResult
|
|
9
|
-
from gllm_inference.utils import validate_string_enum as validate_string_enum
|
|
2
|
+
from gllm_core.utils import RetryConfig as RetryConfig
|
|
3
|
+
from gllm_inference.lm_invoker.openai_chat_completions_lm_invoker import OpenAIChatCompletionsLMInvoker as OpenAIChatCompletionsLMInvoker
|
|
4
|
+
from gllm_inference.lm_invoker.schema.openai_chat_completions import ReasoningEffort as ReasoningEffort
|
|
5
|
+
from gllm_inference.schema import ResponseSchema as ResponseSchema
|
|
10
6
|
from langchain_core.tools import Tool as LangChainTool
|
|
11
7
|
from typing import Any
|
|
12
8
|
|
|
13
|
-
|
|
9
|
+
DEPRECATION_MESSAGE: str
|
|
14
10
|
|
|
15
|
-
class OpenAICompatibleLMInvoker(
|
|
16
|
-
|
|
11
|
+
class OpenAICompatibleLMInvoker(OpenAIChatCompletionsLMInvoker):
|
|
12
|
+
"""A language model invoker to interact with endpoints compatible with OpenAI's chat completion API contract.
|
|
17
13
|
|
|
18
14
|
Attributes:
|
|
19
15
|
model_id (str): The model ID of the language model.
|
|
@@ -27,212 +23,8 @@ class OpenAICompatibleLMInvoker(BaseLMInvoker):
|
|
|
27
23
|
output_analytics (bool): Whether to output the invocation analytics.
|
|
28
24
|
retry_config (RetryConfig | None): The retry configuration for the language model.
|
|
29
25
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
completion API contract. This includes but are not limited to:
|
|
33
|
-
1. DeepInfra (https://deepinfra.com/)
|
|
34
|
-
2. DeepSeek (https://deepseek.com/)
|
|
35
|
-
3. Groq (https://groq.com/)
|
|
36
|
-
4. OpenRouter (https://openrouter.ai/)
|
|
37
|
-
5. Text Generation Inference (https://github.com/huggingface/text-generation-inference)
|
|
38
|
-
6. Together.ai (https://together.ai/)
|
|
39
|
-
7. vLLM (https://vllm.ai/)
|
|
40
|
-
When using this invoker, please note that the supported features and capabilities may vary between different
|
|
41
|
-
endpoints and language models. Using features that are not supported by the endpoint will result in an error.
|
|
42
|
-
|
|
43
|
-
Basic usage:
|
|
44
|
-
The `OpenAICompatibleLMInvoker` can be used as follows:
|
|
45
|
-
```python
|
|
46
|
-
lm_invoker = OpenAICompatibleLMInvoker(
|
|
47
|
-
model_name="llama3-8b-8192",
|
|
48
|
-
base_url="https://api.groq.com/openai/v1",
|
|
49
|
-
api_key="<your-api-key>"
|
|
50
|
-
)
|
|
51
|
-
result = await lm_invoker.invoke("Hi there!")
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
Input types:
|
|
55
|
-
The `OpenAICompatibleLMInvoker` supports the following input types: text, audio, document, and image.
|
|
56
|
-
Non-text inputs can be passed as an `Attachment` object with the `user` role.
|
|
57
|
-
|
|
58
|
-
Usage example:
|
|
59
|
-
```python
|
|
60
|
-
text = "What animal is in this image?"
|
|
61
|
-
image = Attachment.from_path("path/to/local/image.png")
|
|
62
|
-
result = await lm_invoker.invoke([text, image])
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
Tool calling:
|
|
66
|
-
Tool calling is a feature that allows the language model to call tools to perform tasks.
|
|
67
|
-
Tools can be passed to the via the `tools` parameter as a list of `Tool` objects.
|
|
68
|
-
When tools are provided and the model decides to call a tool, the tool calls are stored in the
|
|
69
|
-
`tool_calls` attribute in the output.
|
|
70
|
-
|
|
71
|
-
Usage example:
|
|
72
|
-
```python
|
|
73
|
-
lm_invoker = OpenAICompatibleLMInvoker(..., tools=[tool_1, tool_2])
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
Output example:
|
|
77
|
-
```python
|
|
78
|
-
LMOutput(
|
|
79
|
-
response="Let me call the tools...",
|
|
80
|
-
tool_calls=[
|
|
81
|
-
ToolCall(id="123", name="tool_1", args={"key": "value"}),
|
|
82
|
-
ToolCall(id="456", name="tool_2", args={"key": "value"}),
|
|
83
|
-
]
|
|
84
|
-
)
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
Structured output:
|
|
88
|
-
Structured output is a feature that allows the language model to output a structured response.
|
|
89
|
-
This feature can be enabled by providing a schema to the `response_schema` parameter.
|
|
90
|
-
|
|
91
|
-
The schema must be either a JSON schema dictionary or a Pydantic BaseModel class.
|
|
92
|
-
If JSON schema is used, it must be compatible with Pydantic\'s JSON schema, especially for complex schemas.
|
|
93
|
-
For this reason, it is recommended to create the JSON schema using Pydantic\'s `model_json_schema` method.
|
|
94
|
-
|
|
95
|
-
The language model also doesn\'t need to stream anything when structured output is enabled. Thus, standard
|
|
96
|
-
invocation will be performed regardless of whether the `event_emitter` parameter is provided or not.
|
|
97
|
-
|
|
98
|
-
When enabled, the structured output is stored in the `structured_output` attribute in the output.
|
|
99
|
-
1. If the schema is a JSON schema dictionary, the structured output is a dictionary.
|
|
100
|
-
2. If the schema is a Pydantic BaseModel class, the structured output is a Pydantic model.
|
|
101
|
-
|
|
102
|
-
# Example 1: Using a JSON schema dictionary
|
|
103
|
-
Usage example:
|
|
104
|
-
```python
|
|
105
|
-
schema = {
|
|
106
|
-
"title": "Animal",
|
|
107
|
-
"description": "A description of an animal.",
|
|
108
|
-
"properties": {
|
|
109
|
-
"color": {"title": "Color", "type": "string"},
|
|
110
|
-
"name": {"title": "Name", "type": "string"},
|
|
111
|
-
},
|
|
112
|
-
"required": ["name", "color"],
|
|
113
|
-
"type": "object",
|
|
114
|
-
}
|
|
115
|
-
lm_invoker = OpenAICompatibleLMInvoker(..., response_schema=schema)
|
|
116
|
-
```
|
|
117
|
-
Output example:
|
|
118
|
-
```python
|
|
119
|
-
LMOutput(structured_output={"name": "Golden retriever", "color": "Golden"})
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
# Example 2: Using a Pydantic BaseModel class
|
|
123
|
-
Usage example:
|
|
124
|
-
```python
|
|
125
|
-
class Animal(BaseModel):
|
|
126
|
-
name: str
|
|
127
|
-
color: str
|
|
128
|
-
|
|
129
|
-
lm_invoker = OpenAICompatibleLMInvoker(..., response_schema=Animal)
|
|
130
|
-
```
|
|
131
|
-
Output example:
|
|
132
|
-
```python
|
|
133
|
-
LMOutput(structured_output=Animal(name="Golden retriever", color="Golden"))
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
Analytics tracking:
|
|
137
|
-
Analytics tracking is a feature that allows the module to output additional information about the invocation.
|
|
138
|
-
This feature can be enabled by setting the `output_analytics` parameter to `True`.
|
|
139
|
-
When enabled, the following attributes will be stored in the output:
|
|
140
|
-
1. `token_usage`: The token usage.
|
|
141
|
-
2. `duration`: The duration in seconds.
|
|
142
|
-
3. `finish_details`: The details about how the generation finished.
|
|
143
|
-
|
|
144
|
-
Output example:
|
|
145
|
-
```python
|
|
146
|
-
LMOutput(
|
|
147
|
-
response="Golden retriever is a good dog breed.",
|
|
148
|
-
token_usage=TokenUsage(input_tokens=100, output_tokens=50),
|
|
149
|
-
duration=0.729,
|
|
150
|
-
finish_details={"finish_reason": "stop"},
|
|
151
|
-
)
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
When streaming is enabled, token usage is not supported. Therefore, the `token_usage` attribute will be `None`
|
|
155
|
-
regardless of the value of the `output_analytics` parameter.
|
|
156
|
-
|
|
157
|
-
Retry and timeout:
|
|
158
|
-
The `OpenAICompatibleLMInvoker` supports retry and timeout configuration.
|
|
159
|
-
By default, the max retries is set to 0 and the timeout is set to 30.0 seconds.
|
|
160
|
-
They can be customized by providing a custom `RetryConfig` object to the `retry_config` parameter.
|
|
161
|
-
|
|
162
|
-
Retry config examples:
|
|
163
|
-
```python
|
|
164
|
-
retry_config = RetryConfig(max_retries=0, timeout=0.0) # No retry, no timeout
|
|
165
|
-
retry_config = RetryConfig(max_retries=0, timeout=10.0) # No retry, 10.0 seconds timeout
|
|
166
|
-
retry_config = RetryConfig(max_retries=5, timeout=0.0) # 5 max retries, no timeout
|
|
167
|
-
retry_config = RetryConfig(max_retries=5, timeout=10.0) # 5 max retries, 10.0 seconds timeout
|
|
168
|
-
```
|
|
169
|
-
|
|
170
|
-
Usage example:
|
|
171
|
-
```python
|
|
172
|
-
lm_invoker = OpenAICompatibleLMInvoker(..., retry_config=retry_config)
|
|
173
|
-
```
|
|
174
|
-
|
|
175
|
-
Reasoning:
|
|
176
|
-
Some language models support advanced reasoning capabilities. When using such reasoning-capable models,
|
|
177
|
-
you can configure how much reasoning the model should perform before generating a final response by setting
|
|
178
|
-
reasoning-related parameters.
|
|
179
|
-
|
|
180
|
-
The reasoning effort of reasoning models can be set via the `reasoning_effort` parameter. This parameter
|
|
181
|
-
will guide the models on how many reasoning tokens it should generate before creating a response to the prompt.
|
|
182
|
-
The reasoning effort is only supported by some language models.
|
|
183
|
-
Available options include:
|
|
184
|
-
1. "low": Favors speed and economical token usage.
|
|
185
|
-
2. "medium": Favors a balance between speed and reasoning accuracy.
|
|
186
|
-
3. "high": Favors more complete reasoning at the cost of more tokens generated and slower responses.
|
|
187
|
-
This may differ between models. When not set, the reasoning effort will be equivalent to None by default.
|
|
188
|
-
|
|
189
|
-
When using reasoning models, some providers might output the reasoning summary. These will be stored in the
|
|
190
|
-
`reasoning` attribute in the output.
|
|
191
|
-
|
|
192
|
-
Output example:
|
|
193
|
-
```python
|
|
194
|
-
LMOutput(
|
|
195
|
-
response="Golden retriever is a good dog breed.",
|
|
196
|
-
reasoning=[Reasoning(id="", reasoning="Let me think about it...")],
|
|
197
|
-
)
|
|
198
|
-
```
|
|
199
|
-
|
|
200
|
-
When streaming is enabled along with reasoning and the provider supports reasoning output, the reasoning token
|
|
201
|
-
will be streamed with the `EventType.DATA` event type.
|
|
202
|
-
|
|
203
|
-
Streaming output example:
|
|
204
|
-
```python
|
|
205
|
-
{"type": "data", "value": \'{"data_type": "thinking_start", "data_value": ""}\', ...}
|
|
206
|
-
{"type": "data", "value": \'{"data_type": "thinking", "data_value": "Let me think "}\', ...}
|
|
207
|
-
{"type": "data", "value": \'{"data_type": "thinking", "data_value": "about it..."}\', ...}
|
|
208
|
-
{"type": "data", "value": \'{"data_type": "thinking_end", "data_value": ""}\', ...}
|
|
209
|
-
{"type": "response", "value": "Golden retriever ", ...}
|
|
210
|
-
{"type": "response", "value": "is a good dog breed.", ...}
|
|
211
|
-
|
|
212
|
-
Setting reasoning-related parameters for non-reasoning models will raise an error.
|
|
213
|
-
|
|
214
|
-
Output types:
|
|
215
|
-
The output of the `OpenAICompatibleLMInvoker` can either be:
|
|
216
|
-
1. `str`: The text response if no additional output is needed.
|
|
217
|
-
2. `LMOutput`: A Pydantic model with the following attributes if any additional output is needed:
|
|
218
|
-
2.1. response (str): The text response.
|
|
219
|
-
2.2. tool_calls (list[ToolCall]): The tool calls, if the `tools` parameter is defined and the language
|
|
220
|
-
model decides to invoke tools. Defaults to an empty list.
|
|
221
|
-
2.3. structured_output (dict[str, Any] | BaseModel | None): The structured output, if the `response_schema`
|
|
222
|
-
parameter is defined. Defaults to None.
|
|
223
|
-
2.4. token_usage (TokenUsage | None): The token usage analytics, if the `output_analytics` parameter is
|
|
224
|
-
set to `True`. Defaults to None.
|
|
225
|
-
2.5. duration (float | None): The duration of the invocation in seconds, if the `output_analytics`
|
|
226
|
-
parameter is set to `True`. Defaults to None.
|
|
227
|
-
2.6. finish_details (dict[str, Any] | None): The details about how the generation finished, if the
|
|
228
|
-
`output_analytics` parameter is set to `True`. Defaults to None.
|
|
229
|
-
2.7. reasoning (list[Reasoning]): The reasoning objects. Currently not supported. Defaults to an empty list.
|
|
230
|
-
2.8. citations (list[Chunk]): The citations. Currently not supported. Defaults to an empty list.
|
|
231
|
-
2.9. code_exec_results (list[CodeExecResult]): The code execution results. Currently not supported.
|
|
232
|
-
Defaults to an empty list.
|
|
233
|
-
2.10. mcp_calls (list[MCPCall]): The MCP calls. Currently not supported. Defaults to an empty list.
|
|
234
|
-
'''
|
|
235
|
-
client: Incomplete
|
|
26
|
+
This class is deprecated and will be removed in v0.6. Please use the `OpenAIChatCompletionsLMInvoker` class instead.
|
|
27
|
+
"""
|
|
236
28
|
def __init__(self, model_name: str, base_url: str, api_key: str | None = None, model_kwargs: dict[str, Any] | None = None, default_hyperparameters: dict[str, Any] | None = None, tools: list[Tool | LangChainTool] | None = None, response_schema: ResponseSchema | None = None, output_analytics: bool = False, retry_config: RetryConfig | None = None, reasoning_effort: ReasoningEffort | None = None) -> None:
|
|
237
29
|
'''Initializes a new instance of the OpenAICompatibleLMInvoker class.
|
|
238
30
|
|
|
@@ -255,12 +47,3 @@ class OpenAICompatibleLMInvoker(BaseLMInvoker):
|
|
|
255
47
|
Defaults to None, in which case a default config with no retry and 30.0 seconds timeout will be used.
|
|
256
48
|
reasoning_effort (str | None, optional): The reasoning effort for the language model. Defaults to None.
|
|
257
49
|
'''
|
|
258
|
-
def set_response_schema(self, response_schema: ResponseSchema | None) -> None:
|
|
259
|
-
"""Sets the response schema for the language model hosted on the OpenAI compatible endpoint.
|
|
260
|
-
|
|
261
|
-
This method sets the response schema for the language model hosted on the OpenAI compatible endpoint. Any
|
|
262
|
-
existing response schema will be replaced.
|
|
263
|
-
|
|
264
|
-
Args:
|
|
265
|
-
response_schema (ResponseSchema | None): The response schema to be used.
|
|
266
|
-
"""
|
|
@@ -2,7 +2,7 @@ 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
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
|
|
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
8
|
from gllm_inference.schema import Attachment as Attachment, AttachmentType as AttachmentType, CodeExecResult as CodeExecResult, EmitDataType as EmitDataType, LMOutput as LMOutput, MCPCall as MCPCall, MCPServer as MCPServer, Message as Message, MessageRole as MessageRole, ModelId as ModelId, ModelProvider as ModelProvider, Reasoning as Reasoning, ResponseSchema as ResponseSchema, TokenUsage as TokenUsage, ToolCall as ToolCall, ToolResult as ToolResult
|
|
@@ -17,6 +17,10 @@ STREAM_DATA_CONTENT_TYPE_MAP: Incomplete
|
|
|
17
17
|
class OpenAILMInvoker(BaseLMInvoker):
|
|
18
18
|
'''A language model invoker to interact with OpenAI language models.
|
|
19
19
|
|
|
20
|
+
This class provides support for OpenAI\'s Responses API schema, which is recommended by OpenAI as the preferred API
|
|
21
|
+
to use whenever possible. Use this class unless you have a specific reason to use the Chat Completions API instead.
|
|
22
|
+
The Chat Completions API schema is supported through the `OpenAIChatCompletionsLMInvoker` class.
|
|
23
|
+
|
|
20
24
|
Attributes:
|
|
21
25
|
model_id (str): The model ID of the language model.
|
|
22
26
|
model_provider (str): The provider of the language model.
|
|
@@ -39,7 +43,24 @@ class OpenAILMInvoker(BaseLMInvoker):
|
|
|
39
43
|
Basic usage:
|
|
40
44
|
The `OpenAILMInvoker` can be used as follows:
|
|
41
45
|
```python
|
|
42
|
-
lm_invoker = OpenAILMInvoker(model_name="gpt-
|
|
46
|
+
lm_invoker = OpenAILMInvoker(model_name="gpt-5-nano")
|
|
47
|
+
result = await lm_invoker.invoke("Hi there!")
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
OpenAI compatible endpoints:
|
|
51
|
+
The `OpenAILMInvoker` can also be used to interact with endpoints that are compatible with
|
|
52
|
+
OpenAI\'s Responses API schema. This includes but are not limited to:
|
|
53
|
+
1. SGLang (https://github.com/sgl-project/sglang)
|
|
54
|
+
Please note that the supported features and capabilities may vary between different endpoints and
|
|
55
|
+
language models. Using features that are not supported by the endpoint will result in an error.
|
|
56
|
+
|
|
57
|
+
This customization can be done by setting the `base_url` parameter to the base URL of the endpoint:
|
|
58
|
+
```python
|
|
59
|
+
lm_invoker = OpenAILMInvoker(
|
|
60
|
+
model_name="<model-name>",
|
|
61
|
+
api_key="<your-api-key>",
|
|
62
|
+
base_url="<https://base-url>",
|
|
63
|
+
)
|
|
43
64
|
result = await lm_invoker.invoke("Hi there!")
|
|
44
65
|
```
|
|
45
66
|
|
|
@@ -371,13 +392,16 @@ class OpenAILMInvoker(BaseLMInvoker):
|
|
|
371
392
|
decides to invoke MCP tools. Defaults to an empty list.
|
|
372
393
|
'''
|
|
373
394
|
client: Incomplete
|
|
374
|
-
def __init__(self, model_name: str, api_key: str | None = None, model_kwargs: dict[str, Any] | None = None, default_hyperparameters: dict[str, Any] | None = None, tools: list[Tool | LangChainTool] | None = None, response_schema: ResponseSchema | None = None, output_analytics: bool = False, retry_config: RetryConfig | None = None, reasoning_effort: ReasoningEffort | None = None, reasoning_summary: ReasoningSummary | None = None, mcp_servers: list[MCPServer] | None = None, code_interpreter: bool = False, web_search: bool = False) -> None:
|
|
375
|
-
|
|
395
|
+
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) -> None:
|
|
396
|
+
'''Initializes a new instance of the OpenAILMInvoker class.
|
|
376
397
|
|
|
377
398
|
Args:
|
|
378
399
|
model_name (str): The name of the OpenAI model.
|
|
379
400
|
api_key (str | None, optional): The API key for authenticating with OpenAI. Defaults to None, in which
|
|
380
|
-
case the `OPENAI_API_KEY` environment variable will be used.
|
|
401
|
+
case the `OPENAI_API_KEY` environment variable will be used. If the endpoint does not require an
|
|
402
|
+
API key, a dummy value can be passed (e.g. "<empty>").
|
|
403
|
+
base_url (str, optional): The base URL of a custom endpoint that is compatible with OpenAI\'s
|
|
404
|
+
Responses API schema. Defaults to OpenAI\'s default URL.
|
|
381
405
|
model_kwargs (dict[str, Any] | None, optional): Additional model parameters. Defaults to None.
|
|
382
406
|
default_hyperparameters (dict[str, Any] | None, optional): Default hyperparameters for invoking the model.
|
|
383
407
|
Defaults to None.
|
|
@@ -402,7 +426,7 @@ class OpenAILMInvoker(BaseLMInvoker):
|
|
|
402
426
|
ValueError:
|
|
403
427
|
1. `reasoning_effort` is provided, but is not a valid ReasoningEffort.
|
|
404
428
|
2. `reasoning_summary` is provided, but is not a valid ReasoningSummary.
|
|
405
|
-
|
|
429
|
+
'''
|
|
406
430
|
def set_response_schema(self, response_schema: ResponseSchema | None) -> None:
|
|
407
431
|
"""Sets the response schema for the OpenAI language model.
|
|
408
432
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from enum import StrEnum
|
|
2
2
|
|
|
3
3
|
class Key:
|
|
4
|
-
"""Defines valid keys in OpenAI
|
|
4
|
+
"""Defines valid keys in OpenAI Chat Completions."""
|
|
5
5
|
ARGUMENTS: str
|
|
6
6
|
CONTENT: str
|
|
7
7
|
CHOICES: str
|
|
@@ -42,7 +42,7 @@ class Key:
|
|
|
42
42
|
SUMMARY: str
|
|
43
43
|
|
|
44
44
|
class InputType:
|
|
45
|
-
"""Defines valid input types in OpenAI
|
|
45
|
+
"""Defines valid input types in OpenAI Chat Completions."""
|
|
46
46
|
FILE: str
|
|
47
47
|
FUNCTION: str
|
|
48
48
|
IMAGE_URL: str
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
from gllm_inference.schema.activity import Activity as Activity
|
|
1
2
|
from gllm_inference.schema.attachment import Attachment as Attachment
|
|
2
3
|
from gllm_inference.schema.code_exec_result import CodeExecResult as CodeExecResult
|
|
3
4
|
from gllm_inference.schema.config import TruncationConfig as TruncationConfig
|
|
4
5
|
from gllm_inference.schema.enums import AttachmentType as AttachmentType, BatchStatus as BatchStatus, EmitDataType as EmitDataType, MessageRole as MessageRole, TruncateSide as TruncateSide
|
|
6
|
+
from gllm_inference.schema.events import ActivityEvent as ActivityEvent, CodeEvent as CodeEvent, ReasoningEvent as ReasoningEvent
|
|
5
7
|
from gllm_inference.schema.lm_input import LMInput as LMInput
|
|
6
8
|
from gllm_inference.schema.lm_output import LMOutput as LMOutput
|
|
7
9
|
from gllm_inference.schema.mcp import MCPCall as MCPCall, MCPServer as MCPServer
|
|
@@ -13,4 +15,4 @@ from gllm_inference.schema.tool_call import ToolCall as ToolCall
|
|
|
13
15
|
from gllm_inference.schema.tool_result import ToolResult as ToolResult
|
|
14
16
|
from gllm_inference.schema.type_alias import EMContent as EMContent, MessageContent as MessageContent, ResponseSchema as ResponseSchema, Vector as Vector
|
|
15
17
|
|
|
16
|
-
__all__ = ['Attachment', 'AttachmentType', 'BatchStatus', 'CodeExecResult', 'EMContent', 'EmitDataType', 'MCPCall', 'MCPServer', 'InputTokenDetails', 'MessageContent', 'LMInput', 'LMOutput', 'ModelId', 'ModelProvider', 'Message', 'MessageRole', 'OutputTokenDetails', 'Reasoning', 'ResponseSchema', 'TokenUsage', 'ToolCall', 'ToolResult', 'TruncateSide', 'TruncationConfig', 'Vector']
|
|
18
|
+
__all__ = ['Activity', 'ActivityEvent', 'Attachment', 'AttachmentType', 'BatchStatus', 'CodeEvent', 'CodeExecResult', 'EMContent', 'EmitDataType', 'MCPCall', 'MCPServer', 'InputTokenDetails', 'MessageContent', 'LMInput', 'LMOutput', 'ModelId', 'ModelProvider', 'Message', 'MessageRole', 'OutputTokenDetails', 'Reasoning', 'ReasoningEvent', 'ResponseSchema', 'TokenUsage', 'ToolCall', 'ToolResult', 'TruncateSide', 'TruncationConfig', 'Vector']
|