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.
Files changed (140) hide show
  1. gllm_inference/__init__.pyi +0 -0
  2. gllm_inference/builder/__init__.pyi +6 -0
  3. gllm_inference/builder/_build_invoker.pyi +28 -0
  4. gllm_inference/builder/build_em_invoker.pyi +130 -0
  5. gllm_inference/builder/build_lm_invoker.pyi +222 -0
  6. gllm_inference/builder/build_lm_request_processor.pyi +88 -0
  7. gllm_inference/builder/build_output_parser.pyi +29 -0
  8. gllm_inference/catalog/__init__.pyi +4 -0
  9. gllm_inference/catalog/catalog.pyi +121 -0
  10. gllm_inference/catalog/lm_request_processor_catalog.pyi +112 -0
  11. gllm_inference/catalog/prompt_builder_catalog.pyi +82 -0
  12. gllm_inference/constants.pyi +12 -0
  13. gllm_inference/em_invoker/__init__.pyi +12 -0
  14. gllm_inference/em_invoker/azure_openai_em_invoker.pyi +88 -0
  15. gllm_inference/em_invoker/bedrock_em_invoker.pyi +118 -0
  16. gllm_inference/em_invoker/cohere_em_invoker.pyi +127 -0
  17. gllm_inference/em_invoker/em_invoker.pyi +90 -0
  18. gllm_inference/em_invoker/google_em_invoker.pyi +129 -0
  19. gllm_inference/em_invoker/jina_em_invoker.pyi +103 -0
  20. gllm_inference/em_invoker/langchain/__init__.pyi +3 -0
  21. gllm_inference/em_invoker/langchain/em_invoker_embeddings.pyi +84 -0
  22. gllm_inference/em_invoker/langchain_em_invoker.pyi +46 -0
  23. gllm_inference/em_invoker/openai_compatible_em_invoker.pyi +41 -0
  24. gllm_inference/em_invoker/openai_em_invoker.pyi +118 -0
  25. gllm_inference/em_invoker/schema/__init__.pyi +0 -0
  26. gllm_inference/em_invoker/schema/bedrock.pyi +29 -0
  27. gllm_inference/em_invoker/schema/cohere.pyi +20 -0
  28. gllm_inference/em_invoker/schema/google.pyi +9 -0
  29. gllm_inference/em_invoker/schema/jina.pyi +29 -0
  30. gllm_inference/em_invoker/schema/langchain.pyi +5 -0
  31. gllm_inference/em_invoker/schema/openai.pyi +7 -0
  32. gllm_inference/em_invoker/schema/openai_compatible.pyi +7 -0
  33. gllm_inference/em_invoker/schema/twelvelabs.pyi +17 -0
  34. gllm_inference/em_invoker/schema/voyage.pyi +15 -0
  35. gllm_inference/em_invoker/twelevelabs_em_invoker.pyi +101 -0
  36. gllm_inference/em_invoker/voyage_em_invoker.pyi +104 -0
  37. gllm_inference/exceptions/__init__.pyi +4 -0
  38. gllm_inference/exceptions/error_parser.pyi +41 -0
  39. gllm_inference/exceptions/exceptions.pyi +132 -0
  40. gllm_inference/exceptions/provider_error_map.pyi +24 -0
  41. gllm_inference/lm_invoker/__init__.pyi +15 -0
  42. gllm_inference/lm_invoker/anthropic_lm_invoker.pyi +320 -0
  43. gllm_inference/lm_invoker/azure_openai_lm_invoker.pyi +237 -0
  44. gllm_inference/lm_invoker/batch/__init__.pyi +3 -0
  45. gllm_inference/lm_invoker/batch/batch_operations.pyi +128 -0
  46. gllm_inference/lm_invoker/bedrock_lm_invoker.pyi +212 -0
  47. gllm_inference/lm_invoker/datasaur_lm_invoker.pyi +157 -0
  48. gllm_inference/lm_invoker/google_lm_invoker.pyi +421 -0
  49. gllm_inference/lm_invoker/langchain_lm_invoker.pyi +239 -0
  50. gllm_inference/lm_invoker/litellm_lm_invoker.pyi +224 -0
  51. gllm_inference/lm_invoker/lm_invoker.pyi +183 -0
  52. gllm_inference/lm_invoker/openai_chat_completions_lm_invoker.pyi +252 -0
  53. gllm_inference/lm_invoker/openai_compatible_lm_invoker.pyi +52 -0
  54. gllm_inference/lm_invoker/openai_lm_invoker.pyi +437 -0
  55. gllm_inference/lm_invoker/portkey_lm_invoker.pyi +296 -0
  56. gllm_inference/lm_invoker/schema/__init__.pyi +0 -0
  57. gllm_inference/lm_invoker/schema/anthropic.pyi +56 -0
  58. gllm_inference/lm_invoker/schema/bedrock.pyi +53 -0
  59. gllm_inference/lm_invoker/schema/datasaur.pyi +14 -0
  60. gllm_inference/lm_invoker/schema/google.pyi +36 -0
  61. gllm_inference/lm_invoker/schema/langchain.pyi +23 -0
  62. gllm_inference/lm_invoker/schema/openai.pyi +109 -0
  63. gllm_inference/lm_invoker/schema/openai_chat_completions.pyi +62 -0
  64. gllm_inference/lm_invoker/schema/portkey.pyi +31 -0
  65. gllm_inference/lm_invoker/schema/xai.pyi +31 -0
  66. gllm_inference/lm_invoker/sea_lion_lm_invoker.pyi +48 -0
  67. gllm_inference/lm_invoker/xai_lm_invoker.pyi +252 -0
  68. gllm_inference/model/__init__.pyi +13 -0
  69. gllm_inference/model/em/__init__.pyi +0 -0
  70. gllm_inference/model/em/cohere_em.pyi +17 -0
  71. gllm_inference/model/em/google_em.pyi +16 -0
  72. gllm_inference/model/em/jina_em.pyi +22 -0
  73. gllm_inference/model/em/openai_em.pyi +15 -0
  74. gllm_inference/model/em/twelvelabs_em.pyi +13 -0
  75. gllm_inference/model/em/voyage_em.pyi +20 -0
  76. gllm_inference/model/lm/__init__.pyi +0 -0
  77. gllm_inference/model/lm/anthropic_lm.pyi +22 -0
  78. gllm_inference/model/lm/google_lm.pyi +18 -0
  79. gllm_inference/model/lm/openai_lm.pyi +27 -0
  80. gllm_inference/model/lm/sea_lion_lm.pyi +16 -0
  81. gllm_inference/model/lm/xai_lm.pyi +19 -0
  82. gllm_inference/output_parser/__init__.pyi +3 -0
  83. gllm_inference/output_parser/json_output_parser.pyi +60 -0
  84. gllm_inference/output_parser/output_parser.pyi +27 -0
  85. gllm_inference/prompt_builder/__init__.pyi +3 -0
  86. gllm_inference/prompt_builder/format_strategy/__init__.pyi +4 -0
  87. gllm_inference/prompt_builder/format_strategy/format_strategy.pyi +55 -0
  88. gllm_inference/prompt_builder/format_strategy/jinja_format_strategy.pyi +45 -0
  89. gllm_inference/prompt_builder/format_strategy/string_format_strategy.pyi +20 -0
  90. gllm_inference/prompt_builder/prompt_builder.pyi +73 -0
  91. gllm_inference/prompt_formatter/__init__.pyi +7 -0
  92. gllm_inference/prompt_formatter/agnostic_prompt_formatter.pyi +49 -0
  93. gllm_inference/prompt_formatter/huggingface_prompt_formatter.pyi +55 -0
  94. gllm_inference/prompt_formatter/llama_prompt_formatter.pyi +59 -0
  95. gllm_inference/prompt_formatter/mistral_prompt_formatter.pyi +53 -0
  96. gllm_inference/prompt_formatter/openai_prompt_formatter.pyi +35 -0
  97. gllm_inference/prompt_formatter/prompt_formatter.pyi +30 -0
  98. gllm_inference/realtime_chat/__init__.pyi +3 -0
  99. gllm_inference/realtime_chat/google_realtime_chat.pyi +205 -0
  100. gllm_inference/realtime_chat/input_streamer/__init__.pyi +4 -0
  101. gllm_inference/realtime_chat/input_streamer/input_streamer.pyi +36 -0
  102. gllm_inference/realtime_chat/input_streamer/keyboard_input_streamer.pyi +27 -0
  103. gllm_inference/realtime_chat/input_streamer/linux_mic_input_streamer.pyi +36 -0
  104. gllm_inference/realtime_chat/output_streamer/__init__.pyi +4 -0
  105. gllm_inference/realtime_chat/output_streamer/console_output_streamer.pyi +21 -0
  106. gllm_inference/realtime_chat/output_streamer/linux_speaker_output_streamer.pyi +42 -0
  107. gllm_inference/realtime_chat/output_streamer/output_streamer.pyi +33 -0
  108. gllm_inference/realtime_chat/realtime_chat.pyi +28 -0
  109. gllm_inference/request_processor/__init__.pyi +4 -0
  110. gllm_inference/request_processor/lm_request_processor.pyi +101 -0
  111. gllm_inference/request_processor/uses_lm_mixin.pyi +130 -0
  112. gllm_inference/schema/__init__.pyi +19 -0
  113. gllm_inference/schema/activity.pyi +64 -0
  114. gllm_inference/schema/attachment.pyi +102 -0
  115. gllm_inference/schema/code_exec_result.pyi +14 -0
  116. gllm_inference/schema/config.pyi +15 -0
  117. gllm_inference/schema/enums.pyi +82 -0
  118. gllm_inference/schema/events.pyi +105 -0
  119. gllm_inference/schema/formatter.pyi +31 -0
  120. gllm_inference/schema/lm_input.pyi +4 -0
  121. gllm_inference/schema/lm_output.pyi +266 -0
  122. gllm_inference/schema/mcp.pyi +31 -0
  123. gllm_inference/schema/message.pyi +52 -0
  124. gllm_inference/schema/model_id.pyi +176 -0
  125. gllm_inference/schema/reasoning.pyi +15 -0
  126. gllm_inference/schema/token_usage.pyi +75 -0
  127. gllm_inference/schema/tool_call.pyi +14 -0
  128. gllm_inference/schema/tool_result.pyi +11 -0
  129. gllm_inference/schema/type_alias.pyi +11 -0
  130. gllm_inference/utils/__init__.pyi +5 -0
  131. gllm_inference/utils/io_utils.pyi +26 -0
  132. gllm_inference/utils/langchain.pyi +30 -0
  133. gllm_inference/utils/validation.pyi +15 -0
  134. gllm_inference.build/.gitignore +1 -0
  135. gllm_inference.cpython-313-darwin.so +0 -0
  136. gllm_inference.pyi +156 -0
  137. gllm_inference_binary-0.5.65.dist-info/METADATA +138 -0
  138. gllm_inference_binary-0.5.65.dist-info/RECORD +140 -0
  139. gllm_inference_binary-0.5.65.dist-info/WHEEL +5 -0
  140. gllm_inference_binary-0.5.65.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,437 @@
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 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 langchain_core.tools import Tool as LangChainTool
10
+ from openai import AsyncStream as AsyncStream
11
+ from openai.types.responses import Response as Response, ResponseFunctionWebSearch as ResponseFunctionWebSearch, ResponseOutputItem as ResponseOutputItem
12
+ from openai.types.responses.response_output_item import McpCall as McpCall, McpListTools as McpListTools
13
+ from typing import Any
14
+
15
+ SUPPORTED_ATTACHMENTS: Incomplete
16
+ STREAM_DATA_START_TYPE_MAP: Incomplete
17
+ STREAM_DATA_END_TYPE_MAP: Incomplete
18
+ STREAM_DATA_CONTENT_TYPE_MAP: Incomplete
19
+
20
+ class OpenAILMInvoker(BaseLMInvoker):
21
+ '''A language model invoker to interact with OpenAI language models.
22
+
23
+ This class provides support for OpenAI\'s Responses API schema, which is recommended by OpenAI as the preferred API
24
+ to use whenever possible. Use this class unless you have a specific reason to use the Chat Completions API instead.
25
+ The Chat Completions API schema is supported through the `OpenAIChatCompletionsLMInvoker` class.
26
+
27
+ Attributes:
28
+ model_id (str): The model ID of the language model.
29
+ model_provider (str): The provider of the language model.
30
+ model_name (str): The name of the language model.
31
+ client_kwargs (dict[str, Any]): The keyword arguments for the OpenAI client.
32
+ default_hyperparameters (dict[str, Any]): Default hyperparameters for invoking the model.
33
+ tools (list[Tool]): The list of tools provided to the model to enable tool calling.
34
+ response_schema (ResponseSchema | None): The schema of the response. If provided, the model will output a
35
+ structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema dictionary.
36
+ output_analytics (bool): Whether to output the invocation analytics.
37
+ retry_config (RetryConfig): The retry configuration for the language model.
38
+ reasoning_effort (ReasoningEffort | None): The reasoning effort for reasoning models. Not allowed
39
+ for non-reasoning models. If None, the model will perform medium reasoning effort.
40
+ reasoning_summary (ReasoningSummary | None): The reasoning summary level for reasoning models. Not allowed
41
+ for non-reasoning models. If None, no summary will be generated.
42
+ image_generation (bool): Whether to enable image generation.
43
+ mcp_servers (list[MCPServer]): The list of MCP servers to enable MCP tool calling.
44
+ code_interpreter (bool): Whether to enable the code interpreter.
45
+ web_search (bool): Whether to enable the web search.
46
+
47
+ Basic usage:
48
+ The `OpenAILMInvoker` can be used as follows:
49
+ ```python
50
+ lm_invoker = OpenAILMInvoker(model_name="gpt-5-nano")
51
+ result = await lm_invoker.invoke("Hi there!")
52
+ ```
53
+
54
+ OpenAI compatible endpoints:
55
+ The `OpenAILMInvoker` can also be used to interact with endpoints that are compatible with
56
+ OpenAI\'s Responses API schema. This includes but are not limited to:
57
+ 1. SGLang (https://github.com/sgl-project/sglang)
58
+ Please note that the supported features and capabilities may vary between different endpoints and
59
+ language models. Using features that are not supported by the endpoint will result in an error.
60
+
61
+ This customization can be done by setting the `base_url` parameter to the base URL of the endpoint:
62
+ ```python
63
+ lm_invoker = OpenAILMInvoker(
64
+ model_name="<model-name>",
65
+ api_key="<your-api-key>",
66
+ base_url="<https://base-url>",
67
+ )
68
+ result = await lm_invoker.invoke("Hi there!")
69
+ ```
70
+
71
+ Input types:
72
+ The `OpenAILMInvoker` supports the following input types: text, document, and image.
73
+ Non-text inputs can be passed as an `Attachment` object with the `user` role.
74
+
75
+ Usage example:
76
+ ```python
77
+ text = "What animal is in this image?"
78
+ image = Attachment.from_path("path/to/local/image.png")
79
+ result = await lm_invoker.invoke([text, image])
80
+ ```
81
+
82
+ Text output:
83
+ The `OpenAILMInvoker` generates text outputs by default.
84
+ Text outputs are stored in the `outputs` attribute of the `LMOutput` object and can be accessed
85
+ via the `texts` (all text outputs) or `text` (first text output) properties.
86
+
87
+ Output example:
88
+ ```python
89
+ LMOutput(outputs=[LMOutputItem(type="text", output="Hello, there!")])
90
+ ```
91
+
92
+ Structured output:
93
+ The `OpenAILMInvoker` can be configured to generate structured outputs.
94
+ This feature can be enabled by providing a schema to the `response_schema` parameter.
95
+
96
+ Structured outputs are stored in the `outputs` attribute of the `LMOutput` object and can be accessed
97
+ via the `structureds` (all structured outputs) or `structured` (first structured output) properties.
98
+
99
+ The schema must either be one of the following:
100
+ 1. A Pydantic BaseModel class
101
+ The structured output will be a Pydantic model.
102
+ 2. A JSON schema dictionary
103
+ JSON dictionary schema must be compatible with Pydantic\'s JSON schema, especially for complex schemas.
104
+ Thus, it is recommended to create the JSON schema using Pydantic\'s `model_json_schema` method.
105
+ The structured output will be a dictionary.
106
+
107
+ Usage example:
108
+ ```python
109
+ class Animal(BaseModel):
110
+ name: str
111
+ color: str
112
+
113
+ json_schema = Animal.model_json_schema()
114
+
115
+ lm_invoker = OpenAILMInvoker(..., response_schema=Animal) # Using Pydantic BaseModel class
116
+ lm_invoker = OpenAILMInvoker(..., response_schema=json_schema) # Using JSON schema dictionary
117
+ ```
118
+
119
+ Output example:
120
+ ```python
121
+ # Using Pydantic BaseModel class outputs a Pydantic model
122
+ LMOutput(outputs=[LMOutputItem(type="structured", output=Animal(name="dog", color="white"))])
123
+
124
+ # Using JSON schema dictionary outputs a dictionary
125
+ LMOutput(outputs=[LMOutputItem(type="structured", output={"name": "dog", "color": "white"})])
126
+ ```
127
+
128
+ When structured output is enabled, streaming is disabled.
129
+
130
+ Image generation:
131
+ The `OpenAILMInvoker` can be configured to generate images.
132
+ This feature can be enabled by setting the `image_generation` parameter to `True`.
133
+
134
+ Image outputs are stored in the `outputs` attribute of the `LMOutput` object and can be accessed
135
+ via the `attachments` property.
136
+
137
+ Usage example:
138
+ ```python
139
+ lm_invoker = OpenAILMInvoker(..., image_generation=True)
140
+ result = await lm_invoker.invoke("Create a picture...")
141
+ result.attachments[0].write_to_file("path/to/local/image.png")
142
+ ```
143
+
144
+ Output example:
145
+ ```python
146
+ LMOutput(
147
+ outputs=[
148
+ LMOutputItem(
149
+ type="attachment",
150
+ output=Attachment(filename="image.png", mime_type="image/png", data=b"..."),
151
+ ),
152
+ ],
153
+ )
154
+ ```
155
+
156
+ When image generation is enabled, streaming is disabled.
157
+ Image generation is only available for certain models.
158
+
159
+ Tool calling:
160
+ The `OpenAILMInvoker` can be configured to call tools to perform certain tasks.
161
+ This feature can be enabled by providing a list of `Tool` objects to the `tools` parameter.
162
+
163
+ Tool calls outputs are stored in the `outputs` attribute of the `LMOutput` object and
164
+ can be accessed via the `tool_calls` property.
165
+
166
+ Usage example:
167
+ ```python
168
+ lm_invoker = OpenAILMInvoker(..., tools=[tool_1, tool_2])
169
+ ```
170
+
171
+ Output example:
172
+ ```python
173
+ LMOutput(
174
+ outputs=[
175
+ LMOutputItem(type="text", output="I\'m using tools..."),
176
+ LMOutputItem(type="tool_call", output=ToolCall(id="123", name="tool_1", args={"key": "value"})),
177
+ LMOutputItem(type="tool_call", output=ToolCall(id="456", name="tool_2", args={"key": "value"})),
178
+ ]
179
+ )
180
+ ```
181
+
182
+ MCP tool calling:
183
+ The `OpenAILMInvoker` can be configured to call MCP tools to perform certain tasks.
184
+ This feature can be enabled by providing a list of MCP servers to the `mcp_servers` parameter.
185
+
186
+ MCP calls outputs are stored in the `outputs` attribute of the `LMOutput` object and
187
+ can be accessed via the `mcp_calls` property.
188
+
189
+ Usage example:
190
+ ```python
191
+ from gllm_inference.schema import MCPServer
192
+
193
+ mcp_server_1 = MCPServer(url="https://mcp_server_1.com", name="mcp_server_1")
194
+ lm_invoker = OpenAILMInvoker(..., mcp_servers=[mcp_server_1])
195
+ ```
196
+
197
+ Output example:
198
+ ```python
199
+ LMOutput(
200
+ outputs=[
201
+ LMOutputItem(type="text", output="I\'m using MCP tools..."),
202
+ LMOutputItem(
203
+ type="mcp_call",
204
+ output=MCPCall(
205
+ id="123",
206
+ server_name="mcp_server_1",
207
+ tool_name="mcp_tool_1",
208
+ args={"key": "value"},
209
+ output="The result is 10."
210
+ ),
211
+ ),
212
+ ],
213
+ )
214
+ ```
215
+
216
+ Streaming output example:
217
+ ```python
218
+ {"type": "activity", "value": {"type": "mcp_list_tools", ...}, ...}
219
+ {"type": "activity", "value": {"type": "mcp_call", ...}, ...}
220
+ {"type": "response", "value": "The result ", ...}
221
+ {"type": "response", "value": "is 10.", ...}
222
+ ```
223
+ Note: By default, the activity token will be streamed with the legacy `EventType.DATA` event type.
224
+ To use the new simplified streamed event format, set the `simplify_events` parameter to `True` during
225
+ LM invoker initialization. The legacy event format support will be removed in v0.6.
226
+
227
+ Reasoning:
228
+ The `OpenAILMInvoker` performs step-by-step reasoning before generating a response when reasoning
229
+ models are used, such as GPT-5 models and o-series models.
230
+
231
+ The reasoning effort can be set via the `reasoning_effort` parameter, which guides the models on the amount
232
+ of reasoning tokens to generate. Available options include `minimal`, `low`, `medium`, and `high`.
233
+
234
+ While the raw reasoning tokens are not available, the summary of the reasoning tokens can still be generated.
235
+ This can be done by passing the desired summary level via the `reasoning_summary` parameter.
236
+ Available options include `auto` and `detailed`.
237
+
238
+ Reasoning summaries are stored in the `outputs` attribute of the `LMOutput` object
239
+ and can be accessed via the `thinkings` property.
240
+
241
+ Usage example:
242
+ ```python
243
+ lm_invoker = OpenAILMInvoker(..., reasoning_effort="high", reasoning_summary="detailed")
244
+ ```
245
+
246
+ Output example:
247
+ ```python
248
+ LMOutput(
249
+ outputs=[
250
+ LMOutputItem(type="thinking", output=Reasoning(type="thinking", reasoning="I\'m thinking...", ...)),
251
+ LMOutputItem(type="text", output="Golden retriever is a good dog breed."),
252
+ ]
253
+ )
254
+ ```
255
+
256
+ Streaming output example:
257
+ ```python
258
+ {"type": "thinking_start", "value": "", ...}
259
+ {"type": "thinking", "value": "I\'m ", ...}
260
+ {"type": "thinking", "value": "thinking...", ...}
261
+ {"type": "thinking_end", "value": "", ...}
262
+ {"type": "response", "value": "Golden retriever ", ...}
263
+ {"type": "response", "value": "is a good dog breed.", ...}
264
+ ```
265
+ Note: By default, the thinking token will be streamed with the legacy `EventType.DATA` event type.
266
+ To use the new simplified streamed event format, set the `simplify_events` parameter to `True` during
267
+ LM invoker initialization. The legacy event format support will be removed in v0.6.
268
+
269
+ Reasoning summary is not compatible with tool calling.
270
+
271
+ Code interpreter:
272
+ The `OpenAILMInvoker` can be configured to write and run Python code in a sandboxed environment.
273
+ This is useful for solving complex problems in domains like data analysis, coding, and math.
274
+ This feature can be enabled by setting the `code_interpreter` parameter to `True`.
275
+
276
+ When code interpreter is enabled, it is highly recommended to instruct the model to use the "python tool"
277
+ in the system message, as "python tool" is the term recognized by the model to refer to the code interpreter.
278
+
279
+ Code execution results are stored in the `outputs` attribute of the `LMOutput` object and
280
+ can be accessed via the `code_exec_results` property.
281
+
282
+ Usage example:
283
+ ```python
284
+ lm_invoker = OpenAILMInvoker(..., code_interpreter=True)
285
+ messages = [
286
+ Message.system("You are a data analyst. Use the python tool to generate a file."]),
287
+ Message.user("Show an histogram of the following data: [1, 2, 1, 4, 1, 2, 4, 2, 3, 1]"),
288
+ ]
289
+ result = await lm_invoker.invoke(messages)
290
+ ```
291
+
292
+ Output example:
293
+ ```python
294
+ LMOutput(
295
+ outputs=[
296
+ LMOutputItem(type="text", output="The histogram is attached."),
297
+ LMOutputItem(
298
+ type="code_exec_result",
299
+ output=CodeExecResult(
300
+ id="123",
301
+ code="import matplotlib.pyplot as plt...",
302
+ output=[Attachment(data=b"...", mime_type="image/png")],
303
+ ),
304
+ ),
305
+ ],
306
+ )
307
+ ```
308
+
309
+ Streaming output example:
310
+ ```python
311
+ {"type": "code_start", "value": ""}\', ...}
312
+ {"type": "code", "value": "import matplotlib"}\', ...}
313
+ {"type": "code", "value": ".pyplot as plt..."}\', ...}
314
+ {"type": "code_end", "value": ""}\', ...}
315
+ {"type": "response", "value": "The histogram ", ...}
316
+ {"type": "response", "value": "is attached.", ...}
317
+ ```
318
+ Note: By default, the code 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
+ Web Search:
323
+ The `OpenAILMInvoker` can be configured to search the web for relevant information.
324
+ This feature can be enabled by setting the `web_search` parameter to `True`.
325
+
326
+ Web search citations are stored in the `outputs` attribute of the `LMOutput` object and
327
+ can be accessed via the `citations` property.
328
+
329
+ Usage example:
330
+ ```python
331
+ lm_invoker = OpenAILMInvoker(..., web_search=True)
332
+ ```
333
+
334
+ Output example:
335
+ ```python
336
+ LMOutput(
337
+ outputs=[
338
+ LMOutputItem(type="citation", output=Chunk(id="123", content="...", metadata={...}, score=None)),
339
+ LMOutputItem(type="text", output="According to recent reports... ([Source](https://example.com))."),
340
+ ],
341
+ )
342
+ ```
343
+
344
+ Streaming output example:
345
+ ```python
346
+ {"type": "activity", "value": {"query": "search query"}, ...}
347
+ {"type": "response", "value": "According to recent ", ...}
348
+ {"type": "response", "value": "reports... ([Source](https://example.com)).", ...}
349
+ ```
350
+ Note: By default, the activity token will be streamed with the legacy `EventType.DATA` event type.
351
+ To use the new simplified streamed event format, set the `simplify_events` parameter to `True` during
352
+ LM invoker initialization. The legacy event format support will be removed in v0.6.
353
+
354
+ Analytics tracking:
355
+ The `OpenAILMInvoker` can be configured to output additional information about the invocation.
356
+ This feature can be enabled by setting the `output_analytics` parameter to `True`.
357
+
358
+ When enabled, the following attributes will be stored in the output:
359
+ 1. `token_usage`: The token usage.
360
+ 2. `duration`: The duration in seconds.
361
+ 3. `finish_details`: The details about how the generation finished.
362
+
363
+ Output example:
364
+ ```python
365
+ LMOutput(
366
+ outputs=[...],
367
+ token_usage=TokenUsage(input_tokens=100, output_tokens=50),
368
+ duration=0.729,
369
+ finish_details={"stop_reason": "end_turn"},
370
+ )
371
+ ```
372
+
373
+ Retry and timeout:
374
+ The `OpenAILMInvoker` supports retry and timeout configuration.
375
+ By default, the max retries is set to 0 and the timeout is set to 30.0 seconds.
376
+ They can be customized by providing a custom `RetryConfig` object to the `retry_config` parameter.
377
+
378
+ Retry config examples:
379
+ ```python
380
+ retry_config = RetryConfig(max_retries=0, timeout=None) # No retry, no timeout
381
+ retry_config = RetryConfig(max_retries=5, timeout=10.0) # 5 max retries, 10.0 seconds timeout
382
+ ```
383
+
384
+ Usage example:
385
+ ```python
386
+ lm_invoker = OpenAILMInvoker(..., retry_config=retry_config)
387
+ ```
388
+ '''
389
+ client_kwargs: Incomplete
390
+ def __init__(self, model_name: str, api_key: str | None = None, base_url: str = ..., model_kwargs: dict[str, Any] | None = None, default_hyperparameters: dict[str, Any] | None = None, tools: list[Tool | LangChainTool] | None = None, response_schema: ResponseSchema | None = None, output_analytics: bool = False, retry_config: RetryConfig | None = None, reasoning_effort: ReasoningEffort | None = None, reasoning_summary: ReasoningSummary | None = None, image_generation: bool = False, mcp_servers: list[MCPServer] | None = None, code_interpreter: bool = False, web_search: bool = False, simplify_events: bool = False) -> None:
391
+ '''Initializes a new instance of the OpenAILMInvoker class.
392
+
393
+ Args:
394
+ model_name (str): The name of the OpenAI model.
395
+ api_key (str | None, optional): The API key for authenticating with OpenAI. Defaults to None, in which
396
+ case the `OPENAI_API_KEY` environment variable will be used. If the endpoint does not require an
397
+ API key, a dummy value can be passed (e.g. "<empty>").
398
+ base_url (str, optional): The base URL of a custom endpoint that is compatible with OpenAI\'s
399
+ Responses API schema. Defaults to OpenAI\'s default URL.
400
+ model_kwargs (dict[str, Any] | None, optional): Additional model parameters. Defaults to None.
401
+ default_hyperparameters (dict[str, Any] | None, optional): Default hyperparameters for invoking the model.
402
+ Defaults to None.
403
+ tools (list[Tool | LangChainTool] | None, optional): Tools provided to the model to enable tool calling.
404
+ Defaults to None, in which case an empty list is used.
405
+ response_schema (ResponseSchema | None, optional): The schema of the response. If provided, the model will
406
+ output a structured response as defined by the schema. Supports both Pydantic BaseModel and JSON schema
407
+ dictionary. Defaults to None.
408
+ output_analytics (bool, optional): Whether to output the invocation analytics. Defaults to False.
409
+ retry_config (RetryConfig | None, optional): The retry configuration for the language model.
410
+ Defaults to None, in which case a default config with no retry and 30.0 seconds timeout will be used.
411
+ reasoning_effort (ReasoningEffort | None, optional): The reasoning effort for reasoning models. Not allowed
412
+ for non-reasoning models. If None, the model will perform medium reasoning effort. Defaults to None.
413
+ reasoning_summary (ReasoningSummary | None, optional): The reasoning summary level for reasoning models.
414
+ Not allowed for non-reasoning models. If None, no summary will be generated. Defaults to None.
415
+ image_generation (bool, optional): Whether to enable image generation. Defaults to False.
416
+ mcp_servers (list[MCPServer] | None, optional): The MCP servers containing tools to be accessed by the
417
+ language model. Defaults to None.
418
+ code_interpreter (bool, optional): Whether to enable the code interpreter. Defaults to False.
419
+ web_search (bool, optional): Whether to enable the web search. Defaults to False.
420
+ simplify_events (bool, optional): Temporary parameter to control the streamed events format.
421
+ When True, uses the simplified events format. When False, uses the legacy events format for
422
+ backward compatibility. Will be removed in v0.6. Defaults to False.
423
+
424
+ Raises:
425
+ ValueError:
426
+ 1. `reasoning_effort` is provided, but is not a valid ReasoningEffort.
427
+ 2. `reasoning_summary` is provided, but is not a valid ReasoningSummary.
428
+ '''
429
+ def set_response_schema(self, response_schema: ResponseSchema | None) -> None:
430
+ """Sets the response schema for the OpenAI language model.
431
+
432
+ This method sets the response schema for the OpenAI language model. Any existing response schema will be
433
+ replaced.
434
+
435
+ Args:
436
+ response_schema (ResponseSchema | None): The response schema to be used.
437
+ """