gllm-inference-binary 0.5.55__cp312-cp312-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 +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 +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 +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 +318 -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 +127 -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 +327 -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 +165 -0
- gllm_inference/lm_invoker/openai_chat_completions_lm_invoker.pyi +253 -0
- gllm_inference/lm_invoker/openai_compatible_lm_invoker.pyi +52 -0
- gllm_inference/lm_invoker/openai_lm_invoker.pyi +404 -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 +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 +253 -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 +80 -0
- gllm_inference/schema/events.pyi +105 -0
- gllm_inference/schema/lm_input.pyi +4 -0
- gllm_inference/schema/lm_output.pyi +188 -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-312-darwin.so +0 -0
- gllm_inference.pyi +153 -0
- gllm_inference_binary-0.5.55.dist-info/METADATA +138 -0
- gllm_inference_binary-0.5.55.dist-info/RECORD +137 -0
- gllm_inference_binary-0.5.55.dist-info/WHEEL +5 -0
- gllm_inference_binary-0.5.55.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
from gllm_core.schema.tool import Tool as Tool
|
|
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
|
|
6
|
+
from langchain_core.tools import Tool as LangChainTool
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
DEPRECATION_MESSAGE: str
|
|
10
|
+
|
|
11
|
+
class OpenAICompatibleLMInvoker(OpenAIChatCompletionsLMInvoker):
|
|
12
|
+
"""A language model invoker to interact with endpoints compatible with OpenAI's chat completion API contract.
|
|
13
|
+
|
|
14
|
+
Attributes:
|
|
15
|
+
model_id (str): The model ID of the language model.
|
|
16
|
+
model_provider (str): The provider of the language model.
|
|
17
|
+
model_name (str): The name of the language model.
|
|
18
|
+
client_kwargs (dict[str, Any]): The keyword arguments for the OpenAI client.
|
|
19
|
+
default_hyperparameters (dict[str, Any]): Default hyperparameters for invoking the model.
|
|
20
|
+
tools (list[Tool]): The list of tools provided to the model to enable tool calling.
|
|
21
|
+
response_schema (ResponseSchema | None): The schema of the response. If provided, the model will output a
|
|
22
|
+
structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema dictionary.
|
|
23
|
+
output_analytics (bool): Whether to output the invocation analytics.
|
|
24
|
+
retry_config (RetryConfig | None): The retry configuration for the language model.
|
|
25
|
+
|
|
26
|
+
This class is deprecated and will be removed in v0.6. Please use the `OpenAIChatCompletionsLMInvoker` class instead.
|
|
27
|
+
"""
|
|
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, simplify_events: bool = False) -> None:
|
|
29
|
+
'''Initializes a new instance of the OpenAICompatibleLMInvoker class.
|
|
30
|
+
|
|
31
|
+
Args:
|
|
32
|
+
model_name (str): The name of the language model hosted on the OpenAI compatible endpoint.
|
|
33
|
+
base_url (str): The base URL for the OpenAI compatible endpoint.
|
|
34
|
+
api_key (str | None, optional): The API key for authenticating with the OpenAI compatible endpoint.
|
|
35
|
+
Defaults to None, in which case the `OPENAI_API_KEY` environment variable will be used.
|
|
36
|
+
If the endpoint does not require an API key, a dummy value can be passed (e.g. "<empty>").
|
|
37
|
+
model_kwargs (dict[str, Any] | None, optional): Additional model parameters. Defaults to None.
|
|
38
|
+
default_hyperparameters (dict[str, Any] | None, optional): Default hyperparameters for invoking the model.
|
|
39
|
+
Defaults to None.
|
|
40
|
+
tools (list[Tool | LangChainTool] | None, optional): Tools provided to the model to enable tool calling.
|
|
41
|
+
Defaults to None.
|
|
42
|
+
response_schema (ResponseSchema | None, optional): The schema of the response. If provided, the model will
|
|
43
|
+
output a structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema
|
|
44
|
+
dictionary. Defaults to None.
|
|
45
|
+
output_analytics (bool, optional): Whether to output the invocation analytics. Defaults to False.
|
|
46
|
+
retry_config (RetryConfig | None, optional): The retry configuration for the language model.
|
|
47
|
+
Defaults to None, in which case a default config with no retry and 30.0 seconds timeout will be used.
|
|
48
|
+
reasoning_effort (str | None, optional): The reasoning effort for the language model. Defaults to None.
|
|
49
|
+
simplify_events (bool, optional): Temporary parameter to control the streamed events format.
|
|
50
|
+
When True, uses the simplified events format. When False, uses the legacy events format for
|
|
51
|
+
backward compatibility. Will be removed in v0.6. Defaults to False.
|
|
52
|
+
'''
|
|
@@ -0,0 +1,404 @@
|
|
|
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
|
+
Text output:
|
|
80
|
+
The `OpenAILMInvoker` generates text outputs by default.
|
|
81
|
+
Text outputs are stored in the `outputs` attribute of the `LMOutput` object and can be accessed
|
|
82
|
+
via the `texts` (all text outputs) or `text` (first text output) properties.
|
|
83
|
+
|
|
84
|
+
Output example:
|
|
85
|
+
```python
|
|
86
|
+
LMOutput(outputs=[LMOutputItem(type="text", output="Hello, there!")])
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Structured output:
|
|
90
|
+
The `OpenAILMInvoker` can be configured to generate structured outputs.
|
|
91
|
+
This feature can be enabled by providing a schema to the `response_schema` parameter.
|
|
92
|
+
|
|
93
|
+
Structured outputs are stored in the `outputs` attribute of the `LMOutput` object and can be accessed
|
|
94
|
+
via the `structureds` (all structured outputs) or `structured` (first structured output) properties.
|
|
95
|
+
|
|
96
|
+
The schema must either be one of the following:
|
|
97
|
+
1. A Pydantic BaseModel class
|
|
98
|
+
The structured output will be a Pydantic model.
|
|
99
|
+
2. A JSON schema dictionary
|
|
100
|
+
JSON dictionary schema must be compatible with Pydantic\'s JSON schema, especially for complex schemas.
|
|
101
|
+
Thus, it is recommended to create the JSON schema using Pydantic\'s `model_json_schema` method.
|
|
102
|
+
The structured output will be a dictionary.
|
|
103
|
+
|
|
104
|
+
Usage example:
|
|
105
|
+
```python
|
|
106
|
+
class Animal(BaseModel):
|
|
107
|
+
name: str
|
|
108
|
+
color: str
|
|
109
|
+
|
|
110
|
+
json_schema = Animal.model_json_schema()
|
|
111
|
+
|
|
112
|
+
lm_invoker = OpenAILMInvoker(..., response_schema=Animal) # Using Pydantic BaseModel class
|
|
113
|
+
lm_invoker = OpenAILMInvoker(..., response_schema=json_schema) # Using JSON schema dictionary
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Output example:
|
|
117
|
+
```python
|
|
118
|
+
# Using Pydantic BaseModel class outputs a Pydantic model
|
|
119
|
+
LMOutput(outputs=[LMOutputItem(type="structured", output=Animal(name="dog", color="white"))])
|
|
120
|
+
|
|
121
|
+
# Using JSON schema dictionary outputs a dictionary
|
|
122
|
+
LMOutput(outputs=[LMOutputItem(type="structured", output={"name": "dog", "color": "white"})])
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
When structured output is enabled, streaming is disabled.
|
|
126
|
+
|
|
127
|
+
Tool calling:
|
|
128
|
+
The `OpenAILMInvoker` can be configured to call tools to perform certain tasks.
|
|
129
|
+
This feature can be enabled by providing a list of `Tool` objects to the `tools` parameter.
|
|
130
|
+
|
|
131
|
+
Tool calls outputs are stored in the `outputs` attribute of the `LMOutput` object and
|
|
132
|
+
can be accessed via the `tool_calls` property.
|
|
133
|
+
|
|
134
|
+
Usage example:
|
|
135
|
+
```python
|
|
136
|
+
lm_invoker = OpenAILMInvoker(..., tools=[tool_1, tool_2])
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Output example:
|
|
140
|
+
```python
|
|
141
|
+
LMOutput(
|
|
142
|
+
outputs=[
|
|
143
|
+
LMOutputItem(type="text", output="I\'m using tools..."),
|
|
144
|
+
LMOutputItem(type="tool_call", output=ToolCall(id="123", name="tool_1", args={"key": "value"})),
|
|
145
|
+
LMOutputItem(type="tool_call", output=ToolCall(id="456", name="tool_2", args={"key": "value"})),
|
|
146
|
+
]
|
|
147
|
+
)
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
MCP tool calling:
|
|
151
|
+
The `OpenAILMInvoker` can be configured to call MCP tools to perform certain tasks.
|
|
152
|
+
This feature can be enabled by providing a list of MCP servers to the `mcp_servers` parameter.
|
|
153
|
+
|
|
154
|
+
MCP calls outputs are stored in the `outputs` attribute of the `LMOutput` object and
|
|
155
|
+
can be accessed via the `mcp_calls` property.
|
|
156
|
+
|
|
157
|
+
Usage example:
|
|
158
|
+
```python
|
|
159
|
+
from gllm_inference.schema import MCPServer
|
|
160
|
+
|
|
161
|
+
mcp_server_1 = MCPServer(url="https://mcp_server_1.com", name="mcp_server_1")
|
|
162
|
+
lm_invoker = OpenAILMInvoker(..., mcp_servers=[mcp_server_1])
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Output example:
|
|
166
|
+
```python
|
|
167
|
+
LMOutput(
|
|
168
|
+
outputs=[
|
|
169
|
+
LMOutputItem(type="text", output="I\'m using MCP tools..."),
|
|
170
|
+
LMOutputItem(
|
|
171
|
+
type="mcp_call",
|
|
172
|
+
output=MCPCall(
|
|
173
|
+
id="123",
|
|
174
|
+
server_name="mcp_server_1",
|
|
175
|
+
tool_name="mcp_tool_1",
|
|
176
|
+
args={"key": "value"},
|
|
177
|
+
output="The result is 10."
|
|
178
|
+
),
|
|
179
|
+
),
|
|
180
|
+
],
|
|
181
|
+
)
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
Streaming output example:
|
|
185
|
+
```python
|
|
186
|
+
{"type": "activity", "value": {"type": "mcp_list_tools", ...}, ...}
|
|
187
|
+
{"type": "activity", "value": {"type": "mcp_call", ...}, ...}
|
|
188
|
+
{"type": "response", "value": "The result ", ...}
|
|
189
|
+
{"type": "response", "value": "is 10.", ...}
|
|
190
|
+
```
|
|
191
|
+
Note: By default, the activity token will be streamed with the legacy `EventType.DATA` event type.
|
|
192
|
+
To use the new simplified streamed event format, set the `simplify_events` parameter to `True` during
|
|
193
|
+
LM invoker initialization. The legacy event format support will be removed in v0.6.
|
|
194
|
+
|
|
195
|
+
Reasoning:
|
|
196
|
+
The `OpenAILMInvoker` performs step-by-step reasoning before generating a response when reasoning
|
|
197
|
+
models are used, such as GPT-5 models and o-series models.
|
|
198
|
+
|
|
199
|
+
The reasoning effort can be set via the `reasoning_effort` parameter, which guides the models on the amount
|
|
200
|
+
of reasoning tokens to generate. Available options include `minimal`, `low`, `medium`, and `high`.
|
|
201
|
+
|
|
202
|
+
While the raw reasoning tokens are not available, the summary of the reasoning tokens can still be generated.
|
|
203
|
+
This can be done by passing the desired summary level via the `reasoning_summary` parameter.
|
|
204
|
+
Available options include `auto` and `detailed`.
|
|
205
|
+
|
|
206
|
+
Reasoning summaries are stored in the `outputs` attribute of the `LMOutput` object
|
|
207
|
+
and can be accessed via the `thinkings` property.
|
|
208
|
+
|
|
209
|
+
Usage example:
|
|
210
|
+
```python
|
|
211
|
+
lm_invoker = OpenAILMInvoker(..., reasoning_effort="high", reasoning_summary="detailed")
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
Output example:
|
|
215
|
+
```python
|
|
216
|
+
LMOutput(
|
|
217
|
+
outputs=[
|
|
218
|
+
LMOutputItem(type="thinking", output=Reasoning(type="thinking", reasoning="I\'m thinking...", ...)),
|
|
219
|
+
LMOutputItem(type="text", output="Golden retriever is a good dog breed."),
|
|
220
|
+
]
|
|
221
|
+
)
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Streaming output example:
|
|
225
|
+
```python
|
|
226
|
+
{"type": "thinking_start", "value": "", ...}
|
|
227
|
+
{"type": "thinking", "value": "I\'m ", ...}
|
|
228
|
+
{"type": "thinking", "value": "thinking...", ...}
|
|
229
|
+
{"type": "thinking_end", "value": "", ...}
|
|
230
|
+
{"type": "response", "value": "Golden retriever ", ...}
|
|
231
|
+
{"type": "response", "value": "is a good dog breed.", ...}
|
|
232
|
+
```
|
|
233
|
+
Note: By default, the thinking token will be streamed with the legacy `EventType.DATA` event type.
|
|
234
|
+
To use the new simplified streamed event format, set the `simplify_events` parameter to `True` during
|
|
235
|
+
LM invoker initialization. The legacy event format support will be removed in v0.6.
|
|
236
|
+
|
|
237
|
+
Reasoning summary is not compatible with tool calling.
|
|
238
|
+
|
|
239
|
+
Code interpreter:
|
|
240
|
+
The `OpenAILMInvoker` can be configured to write and run Python code in a sandboxed environment.
|
|
241
|
+
This is useful for solving complex problems in domains like data analysis, coding, and math.
|
|
242
|
+
This feature can be enabled by setting the `code_interpreter` parameter to `True`.
|
|
243
|
+
|
|
244
|
+
When code interpreter is enabled, it is highly recommended to instruct the model to use the "python tool"
|
|
245
|
+
in the system message, as "python tool" is the term recognized by the model to refer to the code interpreter.
|
|
246
|
+
|
|
247
|
+
Code execution results are stored in the `outputs` attribute of the `LMOutput` object and
|
|
248
|
+
can be accessed via the `code_exec_results` property.
|
|
249
|
+
|
|
250
|
+
Usage example:
|
|
251
|
+
```python
|
|
252
|
+
lm_invoker = OpenAILMInvoker(..., code_interpreter=True)
|
|
253
|
+
messages = [
|
|
254
|
+
Message.system("You are a data analyst. Use the python tool to generate a file."]),
|
|
255
|
+
Message.user("Show an histogram of the following data: [1, 2, 1, 4, 1, 2, 4, 2, 3, 1]"),
|
|
256
|
+
]
|
|
257
|
+
result = await lm_invoker.invoke(messages)
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
Output example:
|
|
261
|
+
```python
|
|
262
|
+
LMOutput(
|
|
263
|
+
outputs=[
|
|
264
|
+
LMOutputItem(type="text", output="The histogram is attached."),
|
|
265
|
+
LMOutputItem(
|
|
266
|
+
type="code_exec_result",
|
|
267
|
+
output=CodeExecResult(
|
|
268
|
+
id="123",
|
|
269
|
+
code="import matplotlib.pyplot as plt...",
|
|
270
|
+
output=[Attachment(data=b"...", mime_type="image/png")],
|
|
271
|
+
),
|
|
272
|
+
),
|
|
273
|
+
],
|
|
274
|
+
)
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
Streaming output example:
|
|
278
|
+
```python
|
|
279
|
+
{"type": "code_start", "value": ""}\', ...}
|
|
280
|
+
{"type": "code", "value": "import matplotlib"}\', ...}
|
|
281
|
+
{"type": "code", "value": ".pyplot as plt..."}\', ...}
|
|
282
|
+
{"type": "code_end", "value": ""}\', ...}
|
|
283
|
+
{"type": "response", "value": "The histogram ", ...}
|
|
284
|
+
{"type": "response", "value": "is attached.", ...}
|
|
285
|
+
```
|
|
286
|
+
Note: By default, the code token will be streamed with the legacy `EventType.DATA` event type.
|
|
287
|
+
To use the new simplified streamed event format, set the `simplify_events` parameter to `True` during
|
|
288
|
+
LM invoker initialization. The legacy event format support will be removed in v0.6.
|
|
289
|
+
|
|
290
|
+
Web Search:
|
|
291
|
+
The `OpenAILMInvoker` can be configured to search the web for relevant information.
|
|
292
|
+
This feature can be enabled by setting the `web_search` parameter to `True`.
|
|
293
|
+
|
|
294
|
+
Web search citations are stored in the `outputs` attribute of the `LMOutput` object and
|
|
295
|
+
can be accessed via the `citations` property.
|
|
296
|
+
|
|
297
|
+
Usage example:
|
|
298
|
+
```python
|
|
299
|
+
lm_invoker = OpenAILMInvoker(..., web_search=True)
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
Output example:
|
|
303
|
+
```python
|
|
304
|
+
LMOutput(
|
|
305
|
+
outputs=[
|
|
306
|
+
LMOutputItem(type="citation", output=Chunk(id="123", content="...", metadata={...}, score=None)),
|
|
307
|
+
LMOutputItem(type="text", output="According to recent reports... ([Source](https://example.com))."),
|
|
308
|
+
],
|
|
309
|
+
)
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
Streaming output example:
|
|
313
|
+
```python
|
|
314
|
+
{"type": "activity", "value": {"query": "search query"}, ...}
|
|
315
|
+
{"type": "response", "value": "According to recent ", ...}
|
|
316
|
+
{"type": "response", "value": "reports... ([Source](https://example.com)).", ...}
|
|
317
|
+
```
|
|
318
|
+
Note: By default, the activity token will be streamed with the legacy `EventType.DATA` event type.
|
|
319
|
+
To use the new simplified streamed event format, set the `simplify_events` parameter to `True` during
|
|
320
|
+
LM invoker initialization. The legacy event format support will be removed in v0.6.
|
|
321
|
+
|
|
322
|
+
Analytics tracking:
|
|
323
|
+
The `OpenAILMInvoker` can be configured to output additional information about the invocation.
|
|
324
|
+
This feature can be enabled by setting the `output_analytics` parameter to `True`.
|
|
325
|
+
|
|
326
|
+
When enabled, the following attributes will be stored in the output:
|
|
327
|
+
1. `token_usage`: The token usage.
|
|
328
|
+
2. `duration`: The duration in seconds.
|
|
329
|
+
3. `finish_details`: The details about how the generation finished.
|
|
330
|
+
|
|
331
|
+
Output example:
|
|
332
|
+
```python
|
|
333
|
+
LMOutput(
|
|
334
|
+
outputs=[...],
|
|
335
|
+
token_usage=TokenUsage(input_tokens=100, output_tokens=50),
|
|
336
|
+
duration=0.729,
|
|
337
|
+
finish_details={"stop_reason": "end_turn"},
|
|
338
|
+
)
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
Retry and timeout:
|
|
342
|
+
The `OpenAILMInvoker` supports retry and timeout configuration.
|
|
343
|
+
By default, the max retries is set to 0 and the timeout is set to 30.0 seconds.
|
|
344
|
+
They can be customized by providing a custom `RetryConfig` object to the `retry_config` parameter.
|
|
345
|
+
|
|
346
|
+
Retry config examples:
|
|
347
|
+
```python
|
|
348
|
+
retry_config = RetryConfig(max_retries=0, timeout=None) # No retry, no timeout
|
|
349
|
+
retry_config = RetryConfig(max_retries=5, timeout=10.0) # 5 max retries, 10.0 seconds timeout
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
Usage example:
|
|
353
|
+
```python
|
|
354
|
+
lm_invoker = OpenAILMInvoker(..., retry_config=retry_config)
|
|
355
|
+
```
|
|
356
|
+
'''
|
|
357
|
+
client_kwargs: Incomplete
|
|
358
|
+
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:
|
|
359
|
+
'''Initializes a new instance of the OpenAILMInvoker class.
|
|
360
|
+
|
|
361
|
+
Args:
|
|
362
|
+
model_name (str): The name of the OpenAI model.
|
|
363
|
+
api_key (str | None, optional): The API key for authenticating with OpenAI. Defaults to None, in which
|
|
364
|
+
case the `OPENAI_API_KEY` environment variable will be used. If the endpoint does not require an
|
|
365
|
+
API key, a dummy value can be passed (e.g. "<empty>").
|
|
366
|
+
base_url (str, optional): The base URL of a custom endpoint that is compatible with OpenAI\'s
|
|
367
|
+
Responses API schema. Defaults to OpenAI\'s default URL.
|
|
368
|
+
model_kwargs (dict[str, Any] | None, optional): Additional model parameters. Defaults to None.
|
|
369
|
+
default_hyperparameters (dict[str, Any] | None, optional): Default hyperparameters for invoking the model.
|
|
370
|
+
Defaults to None.
|
|
371
|
+
tools (list[Tool | LangChainTool] | None, optional): Tools provided to the model to enable tool calling.
|
|
372
|
+
Defaults to None, in which case an empty list is used.
|
|
373
|
+
response_schema (ResponseSchema | None, optional): The schema of the response. If provided, the model will
|
|
374
|
+
output a structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema
|
|
375
|
+
dictionary. Defaults to None.
|
|
376
|
+
output_analytics (bool, optional): Whether to output the invocation analytics. Defaults to False.
|
|
377
|
+
retry_config (RetryConfig | None, optional): The retry configuration for the language model.
|
|
378
|
+
Defaults to None, in which case a default config with no retry and 30.0 seconds timeout will be used.
|
|
379
|
+
reasoning_effort (ReasoningEffort | None, optional): The reasoning effort for reasoning models. Not allowed
|
|
380
|
+
for non-reasoning models. If None, the model will perform medium reasoning effort. Defaults to None.
|
|
381
|
+
reasoning_summary (ReasoningSummary | None, optional): The reasoning summary level for reasoning models.
|
|
382
|
+
Not allowed for non-reasoning models. If None, no summary will be generated. Defaults to None.
|
|
383
|
+
mcp_servers (list[MCPServer] | None, optional): The MCP servers containing tools to be accessed by the
|
|
384
|
+
language model. Defaults to None.
|
|
385
|
+
code_interpreter (bool, optional): Whether to enable the code interpreter. Defaults to False.
|
|
386
|
+
web_search (bool, optional): Whether to enable the web search. Defaults to False.
|
|
387
|
+
simplify_events (bool, optional): Temporary parameter to control the streamed events format.
|
|
388
|
+
When True, uses the simplified events format. When False, uses the legacy events format for
|
|
389
|
+
backward compatibility. Will be removed in v0.6. Defaults to False.
|
|
390
|
+
|
|
391
|
+
Raises:
|
|
392
|
+
ValueError:
|
|
393
|
+
1. `reasoning_effort` is provided, but is not a valid ReasoningEffort.
|
|
394
|
+
2. `reasoning_summary` is provided, but is not a valid ReasoningSummary.
|
|
395
|
+
'''
|
|
396
|
+
def set_response_schema(self, response_schema: ResponseSchema | None) -> None:
|
|
397
|
+
"""Sets the response schema for the OpenAI language model.
|
|
398
|
+
|
|
399
|
+
This method sets the response schema for the OpenAI language model. Any existing response schema will be
|
|
400
|
+
replaced.
|
|
401
|
+
|
|
402
|
+
Args:
|
|
403
|
+
response_schema (ResponseSchema | None): The response schema to be used.
|
|
404
|
+
"""
|