gllm-inference-binary 0.5.65__cp313-cp313-macosx_13_0_arm64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gllm_inference/__init__.pyi +0 -0
- gllm_inference/builder/__init__.pyi +6 -0
- gllm_inference/builder/_build_invoker.pyi +28 -0
- gllm_inference/builder/build_em_invoker.pyi +130 -0
- gllm_inference/builder/build_lm_invoker.pyi +222 -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 +12 -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 +127 -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 +15 -0
- gllm_inference/lm_invoker/anthropic_lm_invoker.pyi +320 -0
- gllm_inference/lm_invoker/azure_openai_lm_invoker.pyi +237 -0
- gllm_inference/lm_invoker/batch/__init__.pyi +3 -0
- gllm_inference/lm_invoker/batch/batch_operations.pyi +128 -0
- gllm_inference/lm_invoker/bedrock_lm_invoker.pyi +212 -0
- gllm_inference/lm_invoker/datasaur_lm_invoker.pyi +157 -0
- gllm_inference/lm_invoker/google_lm_invoker.pyi +421 -0
- gllm_inference/lm_invoker/langchain_lm_invoker.pyi +239 -0
- gllm_inference/lm_invoker/litellm_lm_invoker.pyi +224 -0
- gllm_inference/lm_invoker/lm_invoker.pyi +183 -0
- gllm_inference/lm_invoker/openai_chat_completions_lm_invoker.pyi +252 -0
- gllm_inference/lm_invoker/openai_compatible_lm_invoker.pyi +52 -0
- gllm_inference/lm_invoker/openai_lm_invoker.pyi +437 -0
- gllm_inference/lm_invoker/portkey_lm_invoker.pyi +296 -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 +36 -0
- gllm_inference/lm_invoker/schema/langchain.pyi +23 -0
- gllm_inference/lm_invoker/schema/openai.pyi +109 -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/sea_lion_lm_invoker.pyi +48 -0
- gllm_inference/lm_invoker/xai_lm_invoker.pyi +252 -0
- gllm_inference/model/__init__.pyi +13 -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/sea_lion_lm.pyi +16 -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 +73 -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 +19 -0
- gllm_inference/schema/activity.pyi +64 -0
- gllm_inference/schema/attachment.pyi +102 -0
- gllm_inference/schema/code_exec_result.pyi +14 -0
- gllm_inference/schema/config.pyi +15 -0
- gllm_inference/schema/enums.pyi +82 -0
- gllm_inference/schema/events.pyi +105 -0
- gllm_inference/schema/formatter.pyi +31 -0
- gllm_inference/schema/lm_input.pyi +4 -0
- gllm_inference/schema/lm_output.pyi +266 -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 +15 -0
- gllm_inference.build/.gitignore +1 -0
- gllm_inference.cpython-313-darwin.so +0 -0
- gllm_inference.pyi +156 -0
- gllm_inference_binary-0.5.65.dist-info/METADATA +138 -0
- gllm_inference_binary-0.5.65.dist-info/RECORD +140 -0
- gllm_inference_binary-0.5.65.dist-info/WHEEL +5 -0
- gllm_inference_binary-0.5.65.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,224 @@
|
|
|
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.lm_invoker.openai_chat_completions_lm_invoker import OpenAIChatCompletionsLMInvoker as OpenAIChatCompletionsLMInvoker
|
|
6
|
+
from gllm_inference.lm_invoker.openai_lm_invoker import ReasoningEffort as ReasoningEffort
|
|
7
|
+
from gllm_inference.schema import AttachmentType as AttachmentType, LMOutput as LMOutput, ModelId as ModelId, ModelProvider as ModelProvider, ResponseSchema as ResponseSchema
|
|
8
|
+
from langchain_core.tools import Tool as LangChainTool
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
SUPPORTED_ATTACHMENTS: Incomplete
|
|
12
|
+
|
|
13
|
+
class LiteLLMLMInvoker(OpenAIChatCompletionsLMInvoker):
|
|
14
|
+
'''A language model invoker to interact with language models using LiteLLM.
|
|
15
|
+
|
|
16
|
+
Attributes:
|
|
17
|
+
model_id (str): The model ID of the language model.
|
|
18
|
+
model_provider (str): The provider of the language model.
|
|
19
|
+
model_name (str): The name of the language model.
|
|
20
|
+
completion (function): The LiteLLM\'s completion function.
|
|
21
|
+
default_hyperparameters (dict[str, Any]): Default hyperparameters for invoking the model.
|
|
22
|
+
tools (list[Tool]): The list of tools provided to the model to enable tool calling.
|
|
23
|
+
response_schema (ResponseSchema | None): The schema of the response. If provided, the model will output a
|
|
24
|
+
structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema dictionary.
|
|
25
|
+
output_analytics (bool): Whether to output the invocation analytics.
|
|
26
|
+
|
|
27
|
+
Basic usage:
|
|
28
|
+
The `LiteLLMLMInvoker` can be used as follows:
|
|
29
|
+
```python
|
|
30
|
+
lm_invoker = LiteLLMLMInvoker(model_id="openai/gpt-5-nano")
|
|
31
|
+
result = await lm_invoker.invoke("Hi there!")
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Initialization:
|
|
35
|
+
The `LiteLLMLMInvoker` provides an interface to interact with multiple language model providers.
|
|
36
|
+
In order to use this class:
|
|
37
|
+
1. The `model_id` parameter must be in the format of `provider/model_name`. e.g. `openai/gpt-4o-mini`.
|
|
38
|
+
2. The required credentials must be provided via the environment variables.
|
|
39
|
+
|
|
40
|
+
Usage example:
|
|
41
|
+
```python
|
|
42
|
+
os.environ["OPENAI_API_KEY"] = "your_openai_api_key"
|
|
43
|
+
lm_invoker = LiteLLMLMInvoker(model_id="openai/gpt-4o-mini")
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
For the complete list of supported providers and their required credentials, please refer to the
|
|
47
|
+
LiteLLM documentation: https://docs.litellm.ai/docs/providers/
|
|
48
|
+
|
|
49
|
+
Input types:
|
|
50
|
+
The `LiteLLMLMInvoker` supports the following input types: text, audio, and image.
|
|
51
|
+
Non-text inputs can be passed as a `Attachment` object with the `user` role.
|
|
52
|
+
|
|
53
|
+
Usage example:
|
|
54
|
+
```python
|
|
55
|
+
text = "What animal is in this image?"
|
|
56
|
+
image = Attachment.from_path("path/to/local/image.png")
|
|
57
|
+
result = await lm_invoker.invoke([text, image])
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Text output:
|
|
61
|
+
The `LiteLLMLMInvoker` generates text outputs by default.
|
|
62
|
+
Text outputs are stored in the `outputs` attribute of the `LMOutput` object and can be accessed
|
|
63
|
+
via the `texts` (all text outputs) or `text` (first text output) properties.
|
|
64
|
+
|
|
65
|
+
Output example:
|
|
66
|
+
```python
|
|
67
|
+
LMOutput(outputs=[LMOutputItem(type="text", output="Hello, there!")])
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Structured output:
|
|
71
|
+
The `LiteLLMLMInvoker` can be configured to generate structured outputs.
|
|
72
|
+
This feature can be enabled by providing a schema to the `response_schema` parameter.
|
|
73
|
+
|
|
74
|
+
Structured outputs are stored in the `outputs` attribute of the `LMOutput` object and can be accessed
|
|
75
|
+
via the `structureds` (all structured outputs) or `structured` (first structured output) properties.
|
|
76
|
+
|
|
77
|
+
The schema must either be one of the following:
|
|
78
|
+
1. A Pydantic BaseModel class
|
|
79
|
+
The structured output will be a Pydantic model.
|
|
80
|
+
2. A JSON schema dictionary
|
|
81
|
+
JSON dictionary schema must be compatible with Pydantic\'s JSON schema, especially for complex schemas.
|
|
82
|
+
Thus, it is recommended to create the JSON schema using Pydantic\'s `model_json_schema` method.
|
|
83
|
+
The structured output will be a dictionary.
|
|
84
|
+
|
|
85
|
+
Usage example:
|
|
86
|
+
```python
|
|
87
|
+
class Animal(BaseModel):
|
|
88
|
+
name: str
|
|
89
|
+
color: str
|
|
90
|
+
|
|
91
|
+
json_schema = Animal.model_json_schema()
|
|
92
|
+
|
|
93
|
+
lm_invoker = LiteLLMLMInvoker(..., response_schema=Animal) # Using Pydantic BaseModel class
|
|
94
|
+
lm_invoker = LiteLLMLMInvoker(..., response_schema=json_schema) # Using JSON schema dictionary
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Output example:
|
|
98
|
+
```python
|
|
99
|
+
# Using Pydantic BaseModel class outputs a Pydantic model
|
|
100
|
+
LMOutput(outputs=[LMOutputItem(type="structured", output=Animal(name="dog", color="white"))])
|
|
101
|
+
|
|
102
|
+
# Using JSON schema dictionary outputs a dictionary
|
|
103
|
+
LMOutput(outputs=[LMOutputItem(type="structured", output={"name": "dog", "color": "white"})])
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
When structured output is enabled, streaming is disabled.
|
|
107
|
+
|
|
108
|
+
Tool calling:
|
|
109
|
+
The `LiteLLMLMInvoker` can be configured to call tools to perform certain tasks.
|
|
110
|
+
This feature can be enabled by providing a list of `Tool` objects to the `tools` parameter.
|
|
111
|
+
|
|
112
|
+
Tool calls outputs are stored in the `outputs` attribute of the `LMOutput` object and
|
|
113
|
+
can be accessed via the `tool_calls` property.
|
|
114
|
+
|
|
115
|
+
Usage example:
|
|
116
|
+
```python
|
|
117
|
+
lm_invoker = LiteLLMLMInvoker(..., tools=[tool_1, tool_2])
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Output example:
|
|
121
|
+
```python
|
|
122
|
+
LMOutput(
|
|
123
|
+
outputs=[
|
|
124
|
+
LMOutputItem(type="text", output="I\'m using tools..."),
|
|
125
|
+
LMOutputItem(type="tool_call", output=ToolCall(id="123", name="tool_1", args={"key": "value"})),
|
|
126
|
+
LMOutputItem(type="tool_call", output=ToolCall(id="456", name="tool_2", args={"key": "value"})),
|
|
127
|
+
]
|
|
128
|
+
)
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Reasoning:
|
|
132
|
+
The `LiteLLMLMInvoker` performs step-by-step reasoning before generating a response when reasoning
|
|
133
|
+
models are used, such as GPT-5 models and o-series models.
|
|
134
|
+
|
|
135
|
+
The reasoning effort can be set via the `reasoning_effort` parameter, which guides the models on the amount
|
|
136
|
+
of reasoning tokens to generate. Available options include `minimal`, `low`, `medium`, and `high`.
|
|
137
|
+
|
|
138
|
+
Some models may also output the reasoning tokens. In this case, the reasoning tokens are stored in
|
|
139
|
+
the `outputs` attribute of the `LMOutput` object and can be accessed via the `thinkings` property.
|
|
140
|
+
|
|
141
|
+
Output example:
|
|
142
|
+
```python
|
|
143
|
+
LMOutput(
|
|
144
|
+
outputs=[
|
|
145
|
+
LMOutputItem(type="thinking", output=Reasoning(reasoning="I\'m thinking...", ...)),
|
|
146
|
+
LMOutputItem(type="text", output="Golden retriever is a good dog breed."),
|
|
147
|
+
]
|
|
148
|
+
)
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Streaming output example:
|
|
152
|
+
```python
|
|
153
|
+
{"type": "thinking_start", "value": "", ...}
|
|
154
|
+
{"type": "thinking", "value": "I\'m ", ...}
|
|
155
|
+
{"type": "thinking", "value": "thinking...", ...}
|
|
156
|
+
{"type": "thinking_end", "value": "", ...}
|
|
157
|
+
{"type": "response", "value": "Golden retriever ", ...}
|
|
158
|
+
{"type": "response", "value": "is a good dog breed.", ...}
|
|
159
|
+
```
|
|
160
|
+
Note: By default, the thinking token will be streamed with the legacy `EventType.DATA` event type.
|
|
161
|
+
To use the new simplified streamed event format, set the `simplify_events` parameter to `True` during
|
|
162
|
+
LM invoker initialization. The legacy event format support will be removed in v0.6.
|
|
163
|
+
|
|
164
|
+
Setting reasoning-related parameters for non-reasoning models will raise an error.
|
|
165
|
+
|
|
166
|
+
Analytics tracking:
|
|
167
|
+
The `LiteLLMLMInvoker` can be configured to output additional information about the invocation.
|
|
168
|
+
This feature can be enabled by setting the `output_analytics` parameter to `True`.
|
|
169
|
+
|
|
170
|
+
When enabled, the following attributes will be stored in the output:
|
|
171
|
+
1. `token_usage`: The token usage.
|
|
172
|
+
2. `duration`: The duration in seconds.
|
|
173
|
+
3. `finish_details`: The details about how the generation finished.
|
|
174
|
+
|
|
175
|
+
Output example:
|
|
176
|
+
```python
|
|
177
|
+
LMOutput(
|
|
178
|
+
outputs=[...],
|
|
179
|
+
token_usage=TokenUsage(input_tokens=100, output_tokens=50),
|
|
180
|
+
duration=0.729,
|
|
181
|
+
finish_details={"stop_reason": "end_turn"},
|
|
182
|
+
)
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
When streaming is enabled, token usage is not supported.
|
|
186
|
+
|
|
187
|
+
Retry and timeout:
|
|
188
|
+
The `LiteLLMLMInvoker` supports retry and timeout configuration.
|
|
189
|
+
By default, the max retries is set to 0 and the timeout is set to 30.0 seconds.
|
|
190
|
+
They can be customized by providing a custom `RetryConfig` object to the `retry_config` parameter.
|
|
191
|
+
|
|
192
|
+
Retry config examples:
|
|
193
|
+
```python
|
|
194
|
+
retry_config = RetryConfig(max_retries=0, timeout=None) # No retry, no timeout
|
|
195
|
+
retry_config = RetryConfig(max_retries=5, timeout=10.0) # 5 max retries, 10.0 seconds timeout
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
Usage example:
|
|
199
|
+
```python
|
|
200
|
+
lm_invoker = LiteLLMLMInvoker(..., retry_config=retry_config)
|
|
201
|
+
```
|
|
202
|
+
'''
|
|
203
|
+
completion: Incomplete
|
|
204
|
+
def __init__(self, model_id: str, 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, simplify_events: bool = False) -> None:
|
|
205
|
+
"""Initializes a new instance of the LiteLLMLMInvoker class.
|
|
206
|
+
|
|
207
|
+
Args:
|
|
208
|
+
model_id (str): The ID of the model to use. Must be in the format of `provider/model_name`.
|
|
209
|
+
default_hyperparameters (dict[str, Any] | None, optional): Default hyperparameters for invoking the model.
|
|
210
|
+
Defaults to None.
|
|
211
|
+
tools (list[Tool | LangChainTool] | None, optional): Tools provided to the model to enable tool calling.
|
|
212
|
+
Defaults to None.
|
|
213
|
+
response_schema (ResponseSchema | None, optional): The schema of the response. If provided, the model will
|
|
214
|
+
output a structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema
|
|
215
|
+
dictionary. Defaults to None.
|
|
216
|
+
output_analytics (bool, optional): Whether to output the invocation analytics. Defaults to False.
|
|
217
|
+
retry_config (RetryConfig | None, optional): The retry configuration for the language model.
|
|
218
|
+
Defaults to None, in which case a default config with no retry and 30.0 seconds timeout will be used.
|
|
219
|
+
reasoning_effort (ReasoningEffort | None, optional): The reasoning effort for reasoning models.
|
|
220
|
+
Defaults to None.
|
|
221
|
+
simplify_events (bool, optional): Temporary parameter to control the streamed events format.
|
|
222
|
+
When True, uses the simplified events format. When False, uses the legacy events format for
|
|
223
|
+
backward compatibility. Will be removed in v0.6. Defaults to False.
|
|
224
|
+
"""
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import abc
|
|
2
|
+
from _typeshed import Incomplete
|
|
3
|
+
from abc import ABC
|
|
4
|
+
from gllm_core.event import EventEmitter as EventEmitter
|
|
5
|
+
from gllm_core.schema import Event as Event, Tool
|
|
6
|
+
from gllm_core.utils import RetryConfig
|
|
7
|
+
from gllm_inference.constants import DOCUMENT_MIME_TYPES as DOCUMENT_MIME_TYPES, INVOKER_DEFAULT_TIMEOUT as INVOKER_DEFAULT_TIMEOUT
|
|
8
|
+
from gllm_inference.exceptions import BaseInvokerError as BaseInvokerError, convert_to_base_invoker_error as convert_to_base_invoker_error
|
|
9
|
+
from gllm_inference.lm_invoker.batch import BatchOperations as BatchOperations
|
|
10
|
+
from gllm_inference.schema import Attachment as Attachment, AttachmentType as AttachmentType, BatchStatus as BatchStatus, LMInput as LMInput, LMOutput as LMOutput, Message as Message, MessageContent as MessageContent, MessageRole as MessageRole, ModelId as ModelId, Reasoning as Reasoning, ResponseSchema as ResponseSchema, ToolCall as ToolCall, ToolResult as ToolResult
|
|
11
|
+
from langchain_core.tools import Tool as LangChainTool
|
|
12
|
+
from typing import Any
|
|
13
|
+
|
|
14
|
+
class Key:
|
|
15
|
+
"""Defines valid keys in LM invokers JSON schema."""
|
|
16
|
+
ADDITIONAL_PROPERTIES: str
|
|
17
|
+
ANY_OF: str
|
|
18
|
+
ARGS_SCHEMA: str
|
|
19
|
+
ARUN: str
|
|
20
|
+
COROUTINE: str
|
|
21
|
+
DATA_TYPE: str
|
|
22
|
+
DATA_VALUE: str
|
|
23
|
+
DEFAULT: str
|
|
24
|
+
DEFS: str
|
|
25
|
+
DESCRIPTION: str
|
|
26
|
+
FUNC: str
|
|
27
|
+
ID: str
|
|
28
|
+
NAME: str
|
|
29
|
+
PROPERTIES: str
|
|
30
|
+
REQUIRED: str
|
|
31
|
+
TITLE: str
|
|
32
|
+
TYPE: str
|
|
33
|
+
|
|
34
|
+
class InputType:
|
|
35
|
+
"""Defines valid input types in LM invokers JSON schema."""
|
|
36
|
+
NULL: str
|
|
37
|
+
|
|
38
|
+
class BaseLMInvoker(ABC, metaclass=abc.ABCMeta):
|
|
39
|
+
"""A base class for language model invokers used in Gen AI applications.
|
|
40
|
+
|
|
41
|
+
The `BaseLMInvoker` class provides a framework for invoking language models.
|
|
42
|
+
It handles both standard and streaming invocation.
|
|
43
|
+
|
|
44
|
+
Attributes:
|
|
45
|
+
model_id (str): The model ID of the language model.
|
|
46
|
+
model_provider (str): The provider of the language model.
|
|
47
|
+
model_name (str): The name of the language model.
|
|
48
|
+
default_hyperparameters (dict[str, Any]): Default hyperparameters for invoking the language model.
|
|
49
|
+
tools (list[Tool]): Tools provided to the language model to enable tool calling.
|
|
50
|
+
response_schema (ResponseSchema | None): The schema of the response. If provided, the model will output a
|
|
51
|
+
structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema dictionary.
|
|
52
|
+
output_analytics (bool): Whether to output the invocation analytics.
|
|
53
|
+
retry_config (RetryConfig): The retry configuration for the language model.
|
|
54
|
+
"""
|
|
55
|
+
default_hyperparameters: Incomplete
|
|
56
|
+
tools: Incomplete
|
|
57
|
+
response_schema: Incomplete
|
|
58
|
+
output_analytics: Incomplete
|
|
59
|
+
retry_config: Incomplete
|
|
60
|
+
def __init__(self, model_id: ModelId, default_hyperparameters: dict[str, Any] | None = None, supported_attachments: set[str] | None = None, tools: list[Tool | LangChainTool] | None = None, response_schema: ResponseSchema | None = None, output_analytics: bool = False, retry_config: RetryConfig | None = None, simplify_events: bool = False) -> None:
|
|
61
|
+
"""Initializes a new instance of the BaseLMInvoker class.
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
model_id (ModelId): The model ID of the language model.
|
|
65
|
+
default_hyperparameters (dict[str, Any] | None, optional): Default hyperparameters for invoking the
|
|
66
|
+
language model. Defaults to None, in which case an empty dictionary is used.
|
|
67
|
+
supported_attachments (set[str] | None, optional): A set of supported attachment types. Defaults to None,
|
|
68
|
+
in which case an empty set is used (indicating that no attachments are supported).
|
|
69
|
+
tools (list[Tool | LangChainTool] | None, optional): Tools provided to the model to enable tool calling.
|
|
70
|
+
Defaults to None, in which case an empty list is used.
|
|
71
|
+
response_schema (ResponseSchema | None, optional): The schema of the response. If provided, the model will
|
|
72
|
+
output a structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema
|
|
73
|
+
dictionary. Defaults to None.
|
|
74
|
+
output_analytics (bool, optional): Whether to output the invocation analytics. Defaults to False.
|
|
75
|
+
retry_config (RetryConfig | None, optional): The retry configuration for the language model.
|
|
76
|
+
Defaults to None, in which case a default config with no retry and 30.0 seconds timeout will be used.
|
|
77
|
+
simplify_events (bool, optional): Temporary parameter to control the streamed events format.
|
|
78
|
+
When True, uses the simplified events format. When False, uses the legacy events format for
|
|
79
|
+
backward compatibility. Will be removed in v0.6. Defaults to False.
|
|
80
|
+
"""
|
|
81
|
+
@property
|
|
82
|
+
def model_id(self) -> str:
|
|
83
|
+
"""The model ID of the language model.
|
|
84
|
+
|
|
85
|
+
Returns:
|
|
86
|
+
str: The model ID of the language model.
|
|
87
|
+
"""
|
|
88
|
+
@property
|
|
89
|
+
def model_provider(self) -> str:
|
|
90
|
+
"""The provider of the language model.
|
|
91
|
+
|
|
92
|
+
Returns:
|
|
93
|
+
str: The provider of the language model.
|
|
94
|
+
"""
|
|
95
|
+
@property
|
|
96
|
+
def model_name(self) -> str:
|
|
97
|
+
"""The name of the language model.
|
|
98
|
+
|
|
99
|
+
Returns:
|
|
100
|
+
str: The name of the language model.
|
|
101
|
+
"""
|
|
102
|
+
@property
|
|
103
|
+
def batch(self) -> BatchOperations:
|
|
104
|
+
"""The batch operations for the language model.
|
|
105
|
+
|
|
106
|
+
Returns:
|
|
107
|
+
BatchOperations: The batch operations for the language model.
|
|
108
|
+
"""
|
|
109
|
+
def set_tools(self, tools: list[Tool | LangChainTool]) -> None:
|
|
110
|
+
"""Sets the tools for the language model.
|
|
111
|
+
|
|
112
|
+
This method sets the tools for the language model. Any existing tools will be replaced.
|
|
113
|
+
|
|
114
|
+
Args:
|
|
115
|
+
tools (list[Tool | LangChainTool]): The list of tools to be used.
|
|
116
|
+
"""
|
|
117
|
+
def clear_tools(self) -> None:
|
|
118
|
+
"""Clears the tools for the language model.
|
|
119
|
+
|
|
120
|
+
This method clears the tools for the language model by calling the `set_tools` method with an empty list.
|
|
121
|
+
"""
|
|
122
|
+
def set_response_schema(self, response_schema: ResponseSchema | None) -> None:
|
|
123
|
+
"""Sets the response schema for the language model.
|
|
124
|
+
|
|
125
|
+
This method sets the response schema for the language model. Any existing response schema will be replaced.
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
response_schema (ResponseSchema | None): The response schema to be used.
|
|
129
|
+
"""
|
|
130
|
+
def clear_response_schema(self) -> None:
|
|
131
|
+
"""Clears the response schema for the language model.
|
|
132
|
+
|
|
133
|
+
This method clears the response schema for the language model by calling the `set_response_schema` method with
|
|
134
|
+
None.
|
|
135
|
+
"""
|
|
136
|
+
async def count_input_tokens(self, messages: LMInput) -> int:
|
|
137
|
+
"""Counts the input tokens for an invocation request inputs.
|
|
138
|
+
|
|
139
|
+
This method counts the input tokens for an invocation request inputs. This method is useful for:
|
|
140
|
+
1. Estimating the cost of an invocation request before invoking the language model.
|
|
141
|
+
2. Checking if the invocation request is too large to be processed by the language model.
|
|
142
|
+
|
|
143
|
+
Args:
|
|
144
|
+
messages (LMInput): The input messages for the language model.
|
|
145
|
+
1. If a list of Message objects is provided, it is used as is.
|
|
146
|
+
2. If a list of MessageContent or a string is provided, it is converted into a user message.
|
|
147
|
+
|
|
148
|
+
Returns:
|
|
149
|
+
int: The number of input tokens for the invocation request.
|
|
150
|
+
|
|
151
|
+
Raises:
|
|
152
|
+
TimeoutError: If the invocation times out.
|
|
153
|
+
"""
|
|
154
|
+
async def invoke(self, messages: LMInput, hyperparameters: dict[str, Any] | None = None, event_emitter: EventEmitter | None = None) -> str | LMOutput:
|
|
155
|
+
"""Invokes the language model.
|
|
156
|
+
|
|
157
|
+
This method validates the messages and invokes the language model. It handles both standard
|
|
158
|
+
and streaming invocation. Streaming mode is enabled if an event emitter is provided.
|
|
159
|
+
The method includes retry logic with exponential backoff for transient failures.
|
|
160
|
+
|
|
161
|
+
Args:
|
|
162
|
+
messages (LMInput): The input messages for the language model.
|
|
163
|
+
1. If a list of Message objects is provided, it is used as is.
|
|
164
|
+
2. If a list of MessageContent or a string is provided, it is converted into a user message.
|
|
165
|
+
hyperparameters (dict[str, Any] | None, optional): A dictionary of hyperparameters for the language model.
|
|
166
|
+
Defaults to None, in which case the default hyperparameters are used.
|
|
167
|
+
event_emitter (EventEmitter | None, optional): The event emitter for streaming tokens. If provided,
|
|
168
|
+
streaming invocation is enabled. Defaults to None.
|
|
169
|
+
|
|
170
|
+
Returns:
|
|
171
|
+
str | LMOutput: The generated response from the language model.
|
|
172
|
+
|
|
173
|
+
Raises:
|
|
174
|
+
CancelledError: If the invocation is cancelled.
|
|
175
|
+
ModelNotFoundError: If the model is not found.
|
|
176
|
+
ProviderAuthError: If the model authentication fails.
|
|
177
|
+
ProviderInternalError: If the model internal error occurs.
|
|
178
|
+
ProviderInvalidArgsError: If the model parameters are invalid.
|
|
179
|
+
ProviderOverloadedError: If the model is overloaded.
|
|
180
|
+
ProviderRateLimitError: If the model rate limit is exceeded.
|
|
181
|
+
TimeoutError: If the invocation times out.
|
|
182
|
+
ValueError: If the messages are not in the correct format.
|
|
183
|
+
"""
|
|
@@ -0,0 +1,252 @@
|
|
|
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 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, LMOutput as LMOutput, 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
|
|
9
|
+
from langchain_core.tools import Tool as LangChainTool
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
SUPPORTED_ATTACHMENTS: Incomplete
|
|
13
|
+
|
|
14
|
+
class OpenAIChatCompletionsLMInvoker(BaseLMInvoker):
|
|
15
|
+
'''A language model invoker to interact with OpenAI language models using the Chat Completions API.
|
|
16
|
+
|
|
17
|
+
This class provides support for OpenAI\'s Chat Completions API schema. Use this class only when you have
|
|
18
|
+
a specific reason to use the Chat Completions API over the Responses API, as OpenAI recommends using
|
|
19
|
+
the Responses API whenever possible. The Responses API schema is supported through the `OpenAILMInvoker` class.
|
|
20
|
+
|
|
21
|
+
Attributes:
|
|
22
|
+
model_id (str): The model ID of the language model.
|
|
23
|
+
model_provider (str): The provider of the language model.
|
|
24
|
+
model_name (str): The name of the language model.
|
|
25
|
+
client_kwargs (dict[str, Any]): The keyword arguments for the OpenAI client.
|
|
26
|
+
default_hyperparameters (dict[str, Any]): Default hyperparameters for invoking the model.
|
|
27
|
+
tools (list[Tool]): The list of tools provided to the model to enable tool calling.
|
|
28
|
+
response_schema (ResponseSchema | None): The schema of the response. If provided, the model will output a
|
|
29
|
+
structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema dictionary.
|
|
30
|
+
output_analytics (bool): Whether to output the invocation analytics.
|
|
31
|
+
retry_config (RetryConfig | None): The retry configuration for the language model.
|
|
32
|
+
|
|
33
|
+
Basic usage:
|
|
34
|
+
The `OpenAIChatCompletionsLMInvoker` can be used as follows:
|
|
35
|
+
```python
|
|
36
|
+
lm_invoker = OpenAIChatCompletionsLMInvoker(model_name="gpt-5-nano")
|
|
37
|
+
result = await lm_invoker.invoke("Hi there!")
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
OpenAI compatible endpoints:
|
|
41
|
+
The `OpenAIChatCompletionsLMInvoker` can also be used to interact with endpoints that are compatible with
|
|
42
|
+
OpenAI\'s Chat Completions API schema. This includes but are not limited to:
|
|
43
|
+
1. DeepInfra (https://deepinfra.com/)
|
|
44
|
+
2. DeepSeek (https://deepseek.com/)
|
|
45
|
+
3. Groq (https://groq.com/)
|
|
46
|
+
4. OpenRouter (https://openrouter.ai/)
|
|
47
|
+
5. Text Generation Inference (https://github.com/huggingface/text-generation-inference)
|
|
48
|
+
6. Together.ai (https://together.ai/)
|
|
49
|
+
7. vLLM (https://vllm.ai/)
|
|
50
|
+
Please note that the supported features and capabilities may vary between different endpoints and
|
|
51
|
+
language models. Using features that are not supported by the endpoint will result in an error.
|
|
52
|
+
|
|
53
|
+
This customization can be done by setting the `base_url` parameter to the base URL of the endpoint:
|
|
54
|
+
```python
|
|
55
|
+
lm_invoker = OpenAIChatCompletionsLMInvoker(
|
|
56
|
+
model_name="llama3-8b-8192",
|
|
57
|
+
api_key="<your-api-key>",
|
|
58
|
+
base_url="https://api.groq.com/openai/v1",
|
|
59
|
+
)
|
|
60
|
+
result = await lm_invoker.invoke("Hi there!")
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Input types:
|
|
64
|
+
The `OpenAIChatCompletionsLMInvoker` supports the following input types: text, audio, document, and image.
|
|
65
|
+
Non-text inputs can be passed as an `Attachment` object with the `user` role.
|
|
66
|
+
|
|
67
|
+
Usage example:
|
|
68
|
+
```python
|
|
69
|
+
text = "What animal is in this image?"
|
|
70
|
+
image = Attachment.from_path("path/to/local/image.png")
|
|
71
|
+
result = await lm_invoker.invoke([text, image])
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Text output:
|
|
75
|
+
The `OpenAIChatCompletionsLMInvoker` generates text outputs by default.
|
|
76
|
+
Text outputs are stored in the `outputs` attribute of the `LMOutput` object and can be accessed
|
|
77
|
+
via the `texts` (all text outputs) or `text` (first text output) properties.
|
|
78
|
+
|
|
79
|
+
Output example:
|
|
80
|
+
```python
|
|
81
|
+
LMOutput(outputs=[LMOutputItem(type="text", output="Hello, there!")])
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Structured output:
|
|
85
|
+
The `OpenAIChatCompletionsLMInvoker` can be configured to generate structured outputs.
|
|
86
|
+
This feature can be enabled by providing a schema to the `response_schema` parameter.
|
|
87
|
+
|
|
88
|
+
Structured outputs are stored in the `outputs` attribute of the `LMOutput` object and can be accessed
|
|
89
|
+
via the `structureds` (all structured outputs) or `structured` (first structured output) properties.
|
|
90
|
+
|
|
91
|
+
The schema must either be one of the following:
|
|
92
|
+
1. A Pydantic BaseModel class
|
|
93
|
+
The structured output will be a Pydantic model.
|
|
94
|
+
2. A JSON schema dictionary
|
|
95
|
+
JSON dictionary schema must be compatible with Pydantic\'s JSON schema, especially for complex schemas.
|
|
96
|
+
Thus, it is recommended to create the JSON schema using Pydantic\'s `model_json_schema` method.
|
|
97
|
+
The structured output will be a dictionary.
|
|
98
|
+
|
|
99
|
+
Usage example:
|
|
100
|
+
```python
|
|
101
|
+
class Animal(BaseModel):
|
|
102
|
+
name: str
|
|
103
|
+
color: str
|
|
104
|
+
|
|
105
|
+
json_schema = Animal.model_json_schema()
|
|
106
|
+
|
|
107
|
+
lm_invoker = OpenAIChatCompletionsLMInvoker(..., response_schema=Animal) # Using Pydantic BaseModel class
|
|
108
|
+
lm_invoker = OpenAIChatCompletionsLMInvoker(..., response_schema=json_schema) # Using JSON schema dictionary
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Output example:
|
|
112
|
+
```python
|
|
113
|
+
# Using Pydantic BaseModel class outputs a Pydantic model
|
|
114
|
+
LMOutput(outputs=[LMOutputItem(type="structured", output=Animal(name="dog", color="white"))])
|
|
115
|
+
|
|
116
|
+
# Using JSON schema dictionary outputs a dictionary
|
|
117
|
+
LMOutput(outputs=[LMOutputItem(type="structured", output={"name": "dog", "color": "white"})])
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
When structured output is enabled, streaming is disabled.
|
|
121
|
+
|
|
122
|
+
Tool calling:
|
|
123
|
+
The `OpenAIChatCompletionsLMInvoker` can be configured to call tools to perform certain tasks.
|
|
124
|
+
This feature can be enabled by providing a list of `Tool` objects to the `tools` parameter.
|
|
125
|
+
|
|
126
|
+
Tool calls outputs are stored in the `outputs` attribute of the `LMOutput` object and
|
|
127
|
+
can be accessed via the `tool_calls` property.
|
|
128
|
+
|
|
129
|
+
Usage example:
|
|
130
|
+
```python
|
|
131
|
+
lm_invoker = OpenAIChatCompletionsLMInvoker(..., tools=[tool_1, tool_2])
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Output example:
|
|
135
|
+
```python
|
|
136
|
+
LMOutput(
|
|
137
|
+
outputs=[
|
|
138
|
+
LMOutputItem(type="text", output="I\'m using tools..."),
|
|
139
|
+
LMOutputItem(type="tool_call", output=ToolCall(id="123", name="tool_1", args={"key": "value"})),
|
|
140
|
+
LMOutputItem(type="tool_call", output=ToolCall(id="456", name="tool_2", args={"key": "value"})),
|
|
141
|
+
]
|
|
142
|
+
)
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Reasoning:
|
|
146
|
+
The `OpenAILMInvoker` performs step-by-step reasoning before generating a response when reasoning
|
|
147
|
+
models are used, such as GPT-5 models and o-series models.
|
|
148
|
+
|
|
149
|
+
The reasoning effort can be set via the `reasoning_effort` parameter, which guides the models on the amount
|
|
150
|
+
of reasoning tokens to generate. Available options include `minimal`, `low`, `medium`, and `high`.
|
|
151
|
+
|
|
152
|
+
Some models may also output the reasoning tokens. In this case, the reasoning tokens are stored in
|
|
153
|
+
the `outputs` attribute of the `LMOutput` object and can be accessed via the `thinkings` property.
|
|
154
|
+
|
|
155
|
+
Output example:
|
|
156
|
+
```python
|
|
157
|
+
LMOutput(
|
|
158
|
+
outputs=[
|
|
159
|
+
LMOutputItem(type="thinking", output=Reasoning(reasoning="I\'m thinking...", ...)),
|
|
160
|
+
LMOutputItem(type="text", output="Golden retriever is a good dog breed."),
|
|
161
|
+
]
|
|
162
|
+
)
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Streaming output example:
|
|
166
|
+
```python
|
|
167
|
+
{"type": "thinking_start", "value": "", ...}
|
|
168
|
+
{"type": "thinking", "value": "I\'m ", ...}
|
|
169
|
+
{"type": "thinking", "value": "thinking...", ...}
|
|
170
|
+
{"type": "thinking_end", "value": "", ...}
|
|
171
|
+
{"type": "response", "value": "Golden retriever ", ...}
|
|
172
|
+
{"type": "response", "value": "is a good dog breed.", ...}
|
|
173
|
+
```
|
|
174
|
+
Note: By default, the thinking token will be streamed with the legacy `EventType.DATA` event type.
|
|
175
|
+
To use the new simplified streamed event format, set the `simplify_events` parameter to `True` during
|
|
176
|
+
LM invoker initialization. The legacy event format support will be removed in v0.6.
|
|
177
|
+
|
|
178
|
+
Setting reasoning-related parameters for non-reasoning models will raise an error.
|
|
179
|
+
|
|
180
|
+
Analytics tracking:
|
|
181
|
+
The `OpenAIChatCompletionsLMInvoker` can be configured to output additional information about the invocation.
|
|
182
|
+
This feature can be enabled by setting the `output_analytics` parameter to `True`.
|
|
183
|
+
|
|
184
|
+
When enabled, the following attributes will be stored in the output:
|
|
185
|
+
1. `token_usage`: The token usage.
|
|
186
|
+
2. `duration`: The duration in seconds.
|
|
187
|
+
3. `finish_details`: The details about how the generation finished.
|
|
188
|
+
|
|
189
|
+
Output example:
|
|
190
|
+
```python
|
|
191
|
+
LMOutput(
|
|
192
|
+
outputs=[...],
|
|
193
|
+
token_usage=TokenUsage(input_tokens=100, output_tokens=50),
|
|
194
|
+
duration=0.729,
|
|
195
|
+
finish_details={"stop_reason": "end_turn"},
|
|
196
|
+
)
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
When streaming is enabled, token usage is not supported.
|
|
200
|
+
|
|
201
|
+
Retry and timeout:
|
|
202
|
+
The `OpenAIChatCompletionsLMInvoker` supports retry and timeout configuration.
|
|
203
|
+
By default, the max retries is set to 0 and the timeout is set to 30.0 seconds.
|
|
204
|
+
They can be customized by providing a custom `RetryConfig` object to the `retry_config` parameter.
|
|
205
|
+
|
|
206
|
+
Retry config examples:
|
|
207
|
+
```python
|
|
208
|
+
retry_config = RetryConfig(max_retries=0, timeout=None) # No retry, no timeout
|
|
209
|
+
retry_config = RetryConfig(max_retries=5, timeout=10.0) # 5 max retries, 10.0 seconds timeout
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
Usage example:
|
|
213
|
+
```python
|
|
214
|
+
lm_invoker = OpenAIChatCompletionsLMInvoker(..., retry_config=retry_config)
|
|
215
|
+
```
|
|
216
|
+
'''
|
|
217
|
+
client_kwargs: Incomplete
|
|
218
|
+
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, simplify_events: bool = False) -> None:
|
|
219
|
+
'''Initializes a new instance of the OpenAIChatCompletionsLMInvoker class.
|
|
220
|
+
|
|
221
|
+
Args:
|
|
222
|
+
model_name (str): The name of the OpenAI model.
|
|
223
|
+
api_key (str | None, optional): The API key for authenticating with OpenAI. Defaults to None, in which
|
|
224
|
+
case the `OPENAI_API_KEY` environment variable will be used. If the endpoint does not require an
|
|
225
|
+
API key, a dummy value can be passed (e.g. "<empty>").
|
|
226
|
+
base_url (str, optional): The base URL of a custom endpoint that is compatible with OpenAI\'s
|
|
227
|
+
Chat Completions API schema. Defaults to OpenAI\'s default URL.
|
|
228
|
+
model_kwargs (dict[str, Any] | None, optional): Additional model parameters. Defaults to None.
|
|
229
|
+
default_hyperparameters (dict[str, Any] | None, optional): Default hyperparameters for invoking the model.
|
|
230
|
+
Defaults to None.
|
|
231
|
+
tools (list[Tool | LangChainTool] | None, optional): Tools provided to the model to enable tool calling.
|
|
232
|
+
Defaults to None.
|
|
233
|
+
response_schema (ResponseSchema | None, optional): The schema of the response. If provided, the model will
|
|
234
|
+
output a structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema
|
|
235
|
+
dictionary. Defaults to None.
|
|
236
|
+
output_analytics (bool, optional): Whether to output the invocation analytics. Defaults to False.
|
|
237
|
+
retry_config (RetryConfig | None, optional): The retry configuration for the language model.
|
|
238
|
+
Defaults to None, in which case a default config with no retry and 30.0 seconds timeout will be used.
|
|
239
|
+
reasoning_effort (str | None, optional): The reasoning effort for the language model. Defaults to None.
|
|
240
|
+
simplify_events (bool, optional): Temporary parameter to control the streamed events format.
|
|
241
|
+
When True, uses the simplified events format. When False, uses the legacy events format for
|
|
242
|
+
backward compatibility. Will be removed in v0.6. Defaults to False.
|
|
243
|
+
'''
|
|
244
|
+
def set_response_schema(self, response_schema: ResponseSchema | None) -> None:
|
|
245
|
+
"""Sets the response schema for the OpenAI language model.
|
|
246
|
+
|
|
247
|
+
This method sets the response schema for the OpenAI language model.
|
|
248
|
+
Any existing response schema will be replaced.
|
|
249
|
+
|
|
250
|
+
Args:
|
|
251
|
+
response_schema (ResponseSchema | None): The response schema to be used.
|
|
252
|
+
"""
|