kagent-adk 0.6.7__py3-none-any.whl → 0.6.9__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of kagent-adk might be problematic. Click here for more details.

@@ -0,0 +1,206 @@
1
+ # Copyright 2025 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """
16
+ module containing utilities for conversion betwen A2A Part and Google GenAI Part
17
+ """
18
+
19
+ from __future__ import annotations
20
+
21
+ import base64
22
+ import json
23
+ import logging
24
+ from typing import Optional
25
+
26
+ from a2a import types as a2a_types
27
+ from google.genai import types as genai_types
28
+
29
+ from kagent.core.a2a import (
30
+ A2A_DATA_PART_METADATA_TYPE_CODE_EXECUTION_RESULT,
31
+ A2A_DATA_PART_METADATA_TYPE_EXECUTABLE_CODE,
32
+ A2A_DATA_PART_METADATA_TYPE_FUNCTION_CALL,
33
+ A2A_DATA_PART_METADATA_TYPE_FUNCTION_RESPONSE,
34
+ A2A_DATA_PART_METADATA_TYPE_KEY,
35
+ get_kagent_metadata_key,
36
+ )
37
+
38
+ logger = logging.getLogger("kagent_adk." + __name__)
39
+
40
+
41
+ def convert_a2a_part_to_genai_part(
42
+ a2a_part: a2a_types.Part,
43
+ ) -> Optional[genai_types.Part]:
44
+ """Convert an A2A Part to a Google GenAI Part."""
45
+ part = a2a_part.root
46
+ if isinstance(part, a2a_types.TextPart):
47
+ return genai_types.Part(text=part.text)
48
+
49
+ if isinstance(part, a2a_types.FilePart):
50
+ if isinstance(part.file, a2a_types.FileWithUri):
51
+ return genai_types.Part(
52
+ file_data=genai_types.FileData(file_uri=part.file.uri, mime_type=part.file.mime_type)
53
+ )
54
+
55
+ elif isinstance(part.file, a2a_types.FileWithBytes):
56
+ return genai_types.Part(
57
+ inline_data=genai_types.Blob(
58
+ data=base64.b64decode(part.file.bytes),
59
+ mime_type=part.file.mime_type,
60
+ )
61
+ )
62
+ else:
63
+ logger.warning(
64
+ "Cannot convert unsupported file type: %s for A2A part: %s",
65
+ type(part.file),
66
+ a2a_part,
67
+ )
68
+ return None
69
+
70
+ if isinstance(part, a2a_types.DataPart):
71
+ # Conver the Data Part to funcall and function reponse.
72
+ # This is mainly for converting human in the loop and auth request and
73
+ # response.
74
+ # TODO once A2A defined how to suervice such information, migrate below
75
+ # logic accordinlgy
76
+ if part.metadata and get_kagent_metadata_key(A2A_DATA_PART_METADATA_TYPE_KEY) in part.metadata:
77
+ if (
78
+ part.metadata[get_kagent_metadata_key(A2A_DATA_PART_METADATA_TYPE_KEY)]
79
+ == A2A_DATA_PART_METADATA_TYPE_FUNCTION_CALL
80
+ ):
81
+ return genai_types.Part(function_call=genai_types.FunctionCall.model_validate(part.data, by_alias=True))
82
+ if (
83
+ part.metadata[get_kagent_metadata_key(A2A_DATA_PART_METADATA_TYPE_KEY)]
84
+ == A2A_DATA_PART_METADATA_TYPE_FUNCTION_RESPONSE
85
+ ):
86
+ return genai_types.Part(
87
+ function_response=genai_types.FunctionResponse.model_validate(part.data, by_alias=True)
88
+ )
89
+ if (
90
+ part.metadata[get_kagent_metadata_key(A2A_DATA_PART_METADATA_TYPE_KEY)]
91
+ == A2A_DATA_PART_METADATA_TYPE_CODE_EXECUTION_RESULT
92
+ ):
93
+ return genai_types.Part(
94
+ code_execution_result=genai_types.CodeExecutionResult.model_validate(part.data, by_alias=True)
95
+ )
96
+ if (
97
+ part.metadata[get_kagent_metadata_key(A2A_DATA_PART_METADATA_TYPE_KEY)]
98
+ == A2A_DATA_PART_METADATA_TYPE_EXECUTABLE_CODE
99
+ ):
100
+ return genai_types.Part(
101
+ executable_code=genai_types.ExecutableCode.model_validate(part.data, by_alias=True)
102
+ )
103
+ return genai_types.Part(text=json.dumps(part.data))
104
+
105
+ logger.warning(
106
+ "Cannot convert unsupported part type: %s for A2A part: %s",
107
+ type(part),
108
+ a2a_part,
109
+ )
110
+ return None
111
+
112
+
113
+ def convert_genai_part_to_a2a_part(
114
+ part: genai_types.Part,
115
+ ) -> Optional[a2a_types.Part]:
116
+ """Convert a Google GenAI Part to an A2A Part."""
117
+
118
+ if part.text:
119
+ a2a_part = a2a_types.TextPart(text=part.text)
120
+ if part.thought is not None:
121
+ a2a_part.metadata = {get_kagent_metadata_key("thought"): part.thought}
122
+ return a2a_types.Part(root=a2a_part)
123
+
124
+ if part.file_data:
125
+ return a2a_types.Part(
126
+ root=a2a_types.FilePart(
127
+ file=a2a_types.FileWithUri(
128
+ uri=part.file_data.file_uri,
129
+ mime_type=part.file_data.mime_type,
130
+ )
131
+ )
132
+ )
133
+
134
+ if part.inline_data:
135
+ a2a_part = a2a_types.FilePart(
136
+ file=a2a_types.FileWithBytes(
137
+ bytes=base64.b64encode(part.inline_data.data).decode("utf-8"),
138
+ mime_type=part.inline_data.mime_type,
139
+ )
140
+ )
141
+
142
+ if part.video_metadata:
143
+ a2a_part.metadata = {
144
+ get_kagent_metadata_key("video_metadata"): part.video_metadata.model_dump(
145
+ by_alias=True, exclude_none=True
146
+ )
147
+ }
148
+
149
+ return a2a_types.Part(root=a2a_part)
150
+
151
+ # Conver the funcall and function reponse to A2A DataPart.
152
+ # This is mainly for converting human in the loop and auth request and
153
+ # response.
154
+ # TODO once A2A defined how to suervice such information, migrate below
155
+ # logic accordinlgy
156
+ if part.function_call:
157
+ return a2a_types.Part(
158
+ root=a2a_types.DataPart(
159
+ data=part.function_call.model_dump(by_alias=True, exclude_none=True),
160
+ metadata={
161
+ get_kagent_metadata_key(A2A_DATA_PART_METADATA_TYPE_KEY): A2A_DATA_PART_METADATA_TYPE_FUNCTION_CALL
162
+ },
163
+ )
164
+ )
165
+
166
+ if part.function_response:
167
+ return a2a_types.Part(
168
+ root=a2a_types.DataPart(
169
+ data=part.function_response.model_dump(by_alias=True, exclude_none=True),
170
+ metadata={
171
+ get_kagent_metadata_key(
172
+ A2A_DATA_PART_METADATA_TYPE_KEY
173
+ ): A2A_DATA_PART_METADATA_TYPE_FUNCTION_RESPONSE
174
+ },
175
+ )
176
+ )
177
+
178
+ if part.code_execution_result:
179
+ return a2a_types.Part(
180
+ root=a2a_types.DataPart(
181
+ data=part.code_execution_result.model_dump(by_alias=True, exclude_none=True),
182
+ metadata={
183
+ get_kagent_metadata_key(
184
+ A2A_DATA_PART_METADATA_TYPE_KEY
185
+ ): A2A_DATA_PART_METADATA_TYPE_CODE_EXECUTION_RESULT
186
+ },
187
+ )
188
+ )
189
+
190
+ if part.executable_code:
191
+ return a2a_types.Part(
192
+ root=a2a_types.DataPart(
193
+ data=part.executable_code.model_dump(by_alias=True, exclude_none=True),
194
+ metadata={
195
+ get_kagent_metadata_key(
196
+ A2A_DATA_PART_METADATA_TYPE_KEY
197
+ ): A2A_DATA_PART_METADATA_TYPE_EXECUTABLE_CODE
198
+ },
199
+ )
200
+ )
201
+
202
+ logger.warning(
203
+ "Cannot convert unsupported part for Google GenAI part: %s",
204
+ part,
205
+ )
206
+ return None
@@ -0,0 +1,33 @@
1
+ from typing import Any
2
+
3
+ from a2a.server.agent_execution import RequestContext
4
+ from google.adk.runners import RunConfig
5
+ from google.genai import types as genai_types
6
+
7
+ from .part_converter import convert_a2a_part_to_genai_part
8
+
9
+
10
+ def _get_user_id(request: RequestContext) -> str:
11
+ # Get user from call context if available (auth is enabled on a2a server)
12
+ if request.call_context and request.call_context.user and request.call_context.user.user_name:
13
+ return request.call_context.user.user_name
14
+
15
+ # Get user from context id
16
+ return f"A2A_USER_{request.context_id}"
17
+
18
+
19
+ def convert_a2a_request_to_adk_run_args(
20
+ request: RequestContext,
21
+ ) -> dict[str, Any]:
22
+ if not request.message:
23
+ raise ValueError("Request message cannot be None")
24
+
25
+ return {
26
+ "user_id": _get_user_id(request),
27
+ "session_id": request.context_id,
28
+ "new_message": genai_types.Content(
29
+ role="user",
30
+ parts=[convert_a2a_part_to_genai_part(part) for part in request.message.parts],
31
+ ),
32
+ "run_config": RunConfig(),
33
+ }
@@ -0,0 +1,3 @@
1
+ from ._openai import AzureOpenAI, OpenAI
2
+
3
+ __all__ = ["OpenAI", "AzureOpenAI"]
@@ -0,0 +1,393 @@
1
+ from __future__ import annotations
2
+
3
+ import base64
4
+ import json
5
+ import os
6
+ from functools import cached_property
7
+ from typing import TYPE_CHECKING, Any, AsyncGenerator, Iterable, Literal, Optional
8
+
9
+ from google.adk.models import BaseLlm
10
+ from google.adk.models.llm_response import LlmResponse
11
+ from google.genai import types
12
+ from openai import AsyncAzureOpenAI, AsyncOpenAI
13
+ from openai.types.chat import (
14
+ ChatCompletion,
15
+ ChatCompletionAssistantMessageParam,
16
+ ChatCompletionContentPartImageParam,
17
+ ChatCompletionContentPartTextParam,
18
+ ChatCompletionMessageParam,
19
+ ChatCompletionSystemMessageParam,
20
+ ChatCompletionToolMessageParam,
21
+ ChatCompletionToolParam,
22
+ ChatCompletionUserMessageParam,
23
+ )
24
+ from openai.types.chat.chat_completion_message_tool_call_param import (
25
+ ChatCompletionMessageToolCallParam,
26
+ )
27
+ from openai.types.chat.chat_completion_message_tool_call_param import (
28
+ Function as ToolCallFunction,
29
+ )
30
+ from openai.types.shared_params import FunctionDefinition
31
+ from pydantic import Field
32
+
33
+ if TYPE_CHECKING:
34
+ from google.adk.models.llm_request import LlmRequest
35
+
36
+
37
+ def _convert_role_to_openai(role: Optional[str]) -> str:
38
+ """Convert google.genai role to OpenAI role."""
39
+ if role in ["model", "assistant"]:
40
+ return "assistant"
41
+ elif role == "system":
42
+ return "system"
43
+ else:
44
+ return "user"
45
+
46
+
47
+ def _convert_content_to_openai_messages(
48
+ contents: list[types.Content], system_instruction: Optional[str] = None
49
+ ) -> list[ChatCompletionMessageParam]:
50
+ """Convert google.genai Content list to OpenAI messages format."""
51
+ messages: list[ChatCompletionMessageParam] = []
52
+
53
+ # Add system message if provided
54
+ if system_instruction:
55
+ system_message: ChatCompletionSystemMessageParam = {"role": "system", "content": system_instruction}
56
+ messages.append(system_message)
57
+
58
+ # Track tool calls to ensure proper flow
59
+ pending_tool_calls = set()
60
+
61
+ for content in contents:
62
+ role = _convert_role_to_openai(content.role)
63
+
64
+ # Separate different types of parts
65
+ text_parts = []
66
+ function_calls = []
67
+ function_responses = []
68
+ image_parts = []
69
+
70
+ for part in content.parts or []:
71
+ if part.text:
72
+ text_parts.append(part.text)
73
+ elif part.function_call:
74
+ function_calls.append(part)
75
+ elif part.function_response:
76
+ function_responses.append(part)
77
+ elif part.inline_data and part.inline_data.mime_type and part.inline_data.mime_type.startswith("image"):
78
+ if part.inline_data.data:
79
+ image_data = base64.b64encode(part.inline_data.data).decode()
80
+ image_part: ChatCompletionContentPartImageParam = {
81
+ "type": "image_url",
82
+ "image_url": {"url": f"data:{part.inline_data.mime_type};base64,{image_data}"},
83
+ }
84
+ image_parts.append(image_part)
85
+
86
+ # Handle function responses first (they should be tool messages)
87
+ for func_response in function_responses:
88
+ tool_call_id = func_response.function_response.id or "call_1"
89
+ if tool_call_id in pending_tool_calls:
90
+ tool_message: ChatCompletionToolMessageParam = {
91
+ "role": "tool",
92
+ "tool_call_id": tool_call_id,
93
+ "content": str(func_response.function_response.response.get("result", ""))
94
+ if func_response.function_response.response
95
+ else "",
96
+ }
97
+ messages.append(tool_message)
98
+ pending_tool_calls.discard(tool_call_id)
99
+
100
+ # Handle function calls (assistant messages with tool_calls)
101
+ if function_calls:
102
+ tool_calls = []
103
+ for func_call in function_calls:
104
+ tool_call_function: ToolCallFunction = {
105
+ "name": func_call.function_call.name or "",
106
+ "arguments": str(func_call.function_call.args) if func_call.function_call.args else "{}",
107
+ }
108
+ tool_call_id = func_call.function_call.id or "call_1"
109
+ tool_call: ChatCompletionMessageToolCallParam = {
110
+ "id": tool_call_id,
111
+ "type": "function",
112
+ "function": tool_call_function,
113
+ }
114
+ tool_calls.append(tool_call)
115
+ pending_tool_calls.add(tool_call_id)
116
+
117
+ # Create assistant message with tool calls
118
+ text_content = "\n".join(text_parts) if text_parts else None
119
+ assistant_message: ChatCompletionAssistantMessageParam = {
120
+ "role": "assistant",
121
+ "content": text_content,
122
+ "tool_calls": tool_calls,
123
+ }
124
+ messages.append(assistant_message)
125
+
126
+ # Handle regular text/image messages (only if no function calls)
127
+ elif text_parts or image_parts:
128
+ if role == "user":
129
+ if image_parts and text_parts:
130
+ # Multi-modal content
131
+ text_part: ChatCompletionContentPartTextParam = {"type": "text", "text": "\n".join(text_parts)}
132
+ content_parts = [text_part] + image_parts
133
+ user_message: ChatCompletionUserMessageParam = {"role": "user", "content": content_parts}
134
+ elif image_parts:
135
+ # Image only
136
+ user_message: ChatCompletionUserMessageParam = {"role": "user", "content": image_parts}
137
+ else:
138
+ # Text only
139
+ user_message: ChatCompletionUserMessageParam = {"role": "user", "content": "\n".join(text_parts)}
140
+ messages.append(user_message)
141
+ elif role == "assistant":
142
+ # Assistant messages with text (no tool calls)
143
+ assistant_message: ChatCompletionAssistantMessageParam = {
144
+ "role": "assistant",
145
+ "content": "\n".join(text_parts),
146
+ }
147
+ messages.append(assistant_message)
148
+
149
+ return messages
150
+
151
+
152
+ def _update_type_string(value_dict: dict[str, Any]):
153
+ """Updates 'type' field to expected JSON schema format."""
154
+ if "type" in value_dict:
155
+ value_dict["type"] = value_dict["type"].lower()
156
+
157
+ if "items" in value_dict:
158
+ # 'type' field could exist for items as well, this would be the case if
159
+ # items represent primitive types.
160
+ _update_type_string(value_dict["items"])
161
+
162
+ if "properties" in value_dict["items"]:
163
+ # There could be properties as well on the items, especially if the items
164
+ # are complex object themselves. We recursively traverse each individual
165
+ # property as well and fix the "type" value.
166
+ for _, value in value_dict["items"]["properties"].items():
167
+ _update_type_string(value)
168
+
169
+ if "properties" in value_dict:
170
+ # Handle nested properties
171
+ for _, value in value_dict["properties"].items():
172
+ _update_type_string(value)
173
+
174
+
175
+ def _convert_tools_to_openai(tools: list[types.Tool]) -> list[ChatCompletionToolParam]:
176
+ """Convert google.genai Tools to OpenAI tools format."""
177
+ openai_tools: list[ChatCompletionToolParam] = []
178
+
179
+ for tool in tools:
180
+ if tool.function_declarations:
181
+ for func_decl in tool.function_declarations:
182
+ # Build function definition
183
+ function_def: FunctionDefinition = {
184
+ "name": func_decl.name or "",
185
+ "description": func_decl.description or "",
186
+ }
187
+
188
+ # Always include parameters field, even if empty
189
+ properties = {}
190
+ required = []
191
+
192
+ if func_decl.parameters:
193
+ if func_decl.parameters.properties:
194
+ for prop_name, prop_schema in func_decl.parameters.properties.items():
195
+ value_dict = prop_schema.model_dump(exclude_none=True)
196
+ _update_type_string(value_dict)
197
+ properties[prop_name] = value_dict
198
+
199
+ if func_decl.parameters.required:
200
+ required = func_decl.parameters.required
201
+
202
+ function_def["parameters"] = {"type": "object", "properties": properties, "required": required}
203
+
204
+ # Create the tool param
205
+ openai_tool: ChatCompletionToolParam = {"type": "function", "function": function_def}
206
+ openai_tools.append(openai_tool)
207
+
208
+ return openai_tools
209
+
210
+
211
+ def _convert_openai_response_to_llm_response(response: ChatCompletion) -> LlmResponse:
212
+ """Convert OpenAI response to LlmResponse."""
213
+ choice = response.choices[0]
214
+ message = choice.message
215
+
216
+ parts = []
217
+
218
+ # Handle text content
219
+ if message.content:
220
+ parts.append(types.Part.from_text(text=message.content))
221
+
222
+ # Handle function calls
223
+ if hasattr(message, "tool_calls") and message.tool_calls:
224
+ for tool_call in message.tool_calls:
225
+ if tool_call.type == "function":
226
+ try:
227
+ args = json.loads(tool_call.function.arguments) if tool_call.function.arguments else {}
228
+ except json.JSONDecodeError:
229
+ args = {}
230
+
231
+ part = types.Part.from_function_call(name=tool_call.function.name, args=args)
232
+ if part.function_call:
233
+ part.function_call.id = tool_call.id
234
+ parts.append(part)
235
+
236
+ content = types.Content(role="model", parts=parts)
237
+
238
+ # Handle usage metadata
239
+ usage_metadata = None
240
+ if hasattr(response, "usage") and response.usage:
241
+ usage_metadata = types.GenerateContentResponseUsageMetadata(
242
+ prompt_token_count=response.usage.prompt_tokens,
243
+ candidates_token_count=response.usage.completion_tokens,
244
+ total_token_count=response.usage.total_tokens,
245
+ )
246
+
247
+ # Handle finish reason
248
+ finish_reason = types.FinishReason.STOP
249
+ if choice.finish_reason == "length":
250
+ finish_reason = types.FinishReason.MAX_TOKENS
251
+ elif choice.finish_reason == "content_filter":
252
+ finish_reason = types.FinishReason.SAFETY
253
+
254
+ return LlmResponse(content=content, usage_metadata=usage_metadata, finish_reason=finish_reason)
255
+
256
+
257
+ class BaseOpenAI(BaseLlm):
258
+ """Base class for OpenAI-compatible models."""
259
+
260
+ model: str
261
+ base_url: Optional[str] = None
262
+ api_key: Optional[str] = Field(default=None, exclude=True)
263
+ max_tokens: Optional[int] = None
264
+ temperature: Optional[float] = None
265
+
266
+ @classmethod
267
+ def supported_models(cls) -> list[str]:
268
+ """Returns a list of supported models in regex for LlmRegistry."""
269
+ return [r"gpt-.*", r"o1-.*"]
270
+
271
+ @cached_property
272
+ def _client(self) -> AsyncOpenAI:
273
+ """Get the OpenAI client."""
274
+ kwargs = {}
275
+ if self.base_url:
276
+ kwargs["base_url"] = self.base_url
277
+ if self.api_key:
278
+ kwargs["api_key"] = self.api_key
279
+
280
+ return AsyncOpenAI(**kwargs)
281
+
282
+ async def generate_content_async(
283
+ self, llm_request: LlmRequest, stream: bool = False
284
+ ) -> AsyncGenerator[LlmResponse, None]:
285
+ """Generate content using OpenAI API."""
286
+
287
+ # Convert messages
288
+ system_instruction = None
289
+ if llm_request.config and llm_request.config.system_instruction:
290
+ if isinstance(llm_request.config.system_instruction, str):
291
+ system_instruction = llm_request.config.system_instruction
292
+ elif hasattr(llm_request.config.system_instruction, "parts"):
293
+ # Handle Content type system instruction
294
+ text_parts = []
295
+ parts = getattr(llm_request.config.system_instruction, "parts", [])
296
+ if parts:
297
+ for part in parts:
298
+ if hasattr(part, "text") and part.text:
299
+ text_parts.append(part.text)
300
+ system_instruction = "\n".join(text_parts)
301
+
302
+ messages = _convert_content_to_openai_messages(llm_request.contents, system_instruction)
303
+
304
+ # Prepare request parameters
305
+ kwargs = {
306
+ "model": llm_request.model or self.model,
307
+ "messages": messages,
308
+ }
309
+
310
+ if self.max_tokens:
311
+ kwargs["max_tokens"] = self.max_tokens
312
+ if self.temperature is not None:
313
+ kwargs["temperature"] = self.temperature
314
+
315
+ # Handle tools
316
+ if llm_request.config and llm_request.config.tools:
317
+ # Filter to only google.genai.types.Tool objects
318
+ genai_tools = []
319
+ for tool in llm_request.config.tools:
320
+ if hasattr(tool, "function_declarations"):
321
+ genai_tools.append(tool)
322
+
323
+ if genai_tools:
324
+ openai_tools = _convert_tools_to_openai(genai_tools)
325
+ if openai_tools:
326
+ kwargs["tools"] = openai_tools
327
+ kwargs["tool_choice"] = "auto"
328
+
329
+ try:
330
+ if stream:
331
+ # Handle streaming
332
+ async for chunk in await self._client.chat.completions.create(stream=True, **kwargs):
333
+ if chunk.choices and chunk.choices[0].delta:
334
+ delta = chunk.choices[0].delta
335
+ if delta.content:
336
+ content = types.Content(role="model", parts=[types.Part.from_text(text=delta.content)])
337
+ yield LlmResponse(
338
+ content=content, partial=True, turn_complete=chunk.choices[0].finish_reason is not None
339
+ )
340
+ else:
341
+ # Handle non-streaming
342
+ response = await self._client.chat.completions.create(stream=False, **kwargs)
343
+ yield _convert_openai_response_to_llm_response(response)
344
+
345
+ except Exception as e:
346
+ yield LlmResponse(error_code="API_ERROR", error_message=str(e))
347
+
348
+
349
+ class OpenAI(BaseOpenAI):
350
+ """OpenAI model implementation."""
351
+
352
+ type: Literal["openai"]
353
+
354
+ @cached_property
355
+ def _client(self) -> AsyncOpenAI:
356
+ """Get the OpenAI client."""
357
+ kwargs = {}
358
+ if self.base_url:
359
+ kwargs["base_url"] = self.base_url
360
+ if self.api_key:
361
+ kwargs["api_key"] = self.api_key
362
+ elif "OPENAI_API_KEY" in os.environ:
363
+ kwargs["api_key"] = os.environ["OPENAI_API_KEY"]
364
+
365
+ return AsyncOpenAI(**kwargs)
366
+
367
+
368
+ class AzureOpenAI(BaseOpenAI):
369
+ """Azure OpenAI model implementation."""
370
+
371
+ type: Literal["azure_openai"]
372
+ api_version: Optional[str] = None
373
+ azure_endpoint: Optional[str] = None
374
+ azure_deployment: Optional[str] = None
375
+
376
+ @cached_property
377
+ def _client(self) -> AsyncAzureOpenAI:
378
+ """Get the Azure OpenAI client."""
379
+ api_version = self.api_version or os.environ.get("AZURE_OPENAI_API_VERSION", "2024-02-15-preview")
380
+ azure_endpoint = self.azure_endpoint or os.environ.get("AZURE_OPENAI_ENDPOINT")
381
+ api_key = self.api_key or os.environ.get("AZURE_OPENAI_API_KEY")
382
+
383
+ if not azure_endpoint:
384
+ raise ValueError(
385
+ "Azure endpoint must be provided either via azure_endpoint parameter or AZURE_OPENAI_ENDPOINT environment variable"
386
+ )
387
+
388
+ if not api_key:
389
+ raise ValueError(
390
+ "API key must be provided either via api_key parameter or AZURE_OPENAI_API_KEY environment variable"
391
+ )
392
+
393
+ return AsyncAzureOpenAI(api_version=api_version, azure_endpoint=azure_endpoint, api_key=api_key)