llama-index-llms-openai 0.3.4__py3-none-any.whl → 0.3.6__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.
- llama_index/llms/openai/base.py +24 -4
- llama_index/llms/openai/utils.py +13 -3
- {llama_index_llms_openai-0.3.4.dist-info → llama_index_llms_openai-0.3.6.dist-info}/METADATA +1 -1
- llama_index_llms_openai-0.3.6.dist-info/RECORD +6 -0
- llama_index_llms_openai-0.3.4.dist-info/RECORD +0 -6
- {llama_index_llms_openai-0.3.4.dist-info → llama_index_llms_openai-0.3.6.dist-info}/WHEEL +0 -0
llama_index/llms/openai/base.py
CHANGED
|
@@ -217,6 +217,10 @@ class OpenAI(FunctionCallingLLM):
|
|
|
217
217
|
default=False,
|
|
218
218
|
description="Whether to use strict mode for invoking tools/using schemas.",
|
|
219
219
|
)
|
|
220
|
+
supports_content_blocks: bool = Field(
|
|
221
|
+
default=True,
|
|
222
|
+
description="Whether the model supports content blocks in chat messages.",
|
|
223
|
+
)
|
|
220
224
|
|
|
221
225
|
_client: Optional[SyncOpenAI] = PrivateAttr()
|
|
222
226
|
_aclient: Optional[AsyncOpenAI] = PrivateAttr()
|
|
@@ -423,7 +427,11 @@ class OpenAI(FunctionCallingLLM):
|
|
|
423
427
|
@llm_retry_decorator
|
|
424
428
|
def _chat(self, messages: Sequence[ChatMessage], **kwargs: Any) -> ChatResponse:
|
|
425
429
|
client = self._get_client()
|
|
426
|
-
message_dicts = to_openai_message_dicts(
|
|
430
|
+
message_dicts = to_openai_message_dicts(
|
|
431
|
+
messages,
|
|
432
|
+
model=self.model,
|
|
433
|
+
supports_content_blocks=self.supports_content_blocks,
|
|
434
|
+
)
|
|
427
435
|
|
|
428
436
|
if self.reuse_client:
|
|
429
437
|
response = client.chat.completions.create(
|
|
@@ -458,7 +466,11 @@ class OpenAI(FunctionCallingLLM):
|
|
|
458
466
|
self, messages: Sequence[ChatMessage], **kwargs: Any
|
|
459
467
|
) -> ChatResponseGen:
|
|
460
468
|
client = self._get_client()
|
|
461
|
-
message_dicts = to_openai_message_dicts(
|
|
469
|
+
message_dicts = to_openai_message_dicts(
|
|
470
|
+
messages,
|
|
471
|
+
model=self.model,
|
|
472
|
+
supports_content_blocks=self.supports_content_blocks,
|
|
473
|
+
)
|
|
462
474
|
|
|
463
475
|
def gen() -> ChatResponseGen:
|
|
464
476
|
content = ""
|
|
@@ -668,7 +680,11 @@ class OpenAI(FunctionCallingLLM):
|
|
|
668
680
|
self, messages: Sequence[ChatMessage], **kwargs: Any
|
|
669
681
|
) -> ChatResponse:
|
|
670
682
|
aclient = self._get_aclient()
|
|
671
|
-
message_dicts = to_openai_message_dicts(
|
|
683
|
+
message_dicts = to_openai_message_dicts(
|
|
684
|
+
messages,
|
|
685
|
+
model=self.model,
|
|
686
|
+
supports_content_blocks=self.supports_content_blocks,
|
|
687
|
+
)
|
|
672
688
|
|
|
673
689
|
if self.reuse_client:
|
|
674
690
|
response = await aclient.chat.completions.create(
|
|
@@ -701,7 +717,11 @@ class OpenAI(FunctionCallingLLM):
|
|
|
701
717
|
self, messages: Sequence[ChatMessage], **kwargs: Any
|
|
702
718
|
) -> ChatResponseAsyncGen:
|
|
703
719
|
aclient = self._get_aclient()
|
|
704
|
-
message_dicts = to_openai_message_dicts(
|
|
720
|
+
message_dicts = to_openai_message_dicts(
|
|
721
|
+
messages,
|
|
722
|
+
model=self.model,
|
|
723
|
+
supports_content_blocks=self.supports_content_blocks,
|
|
724
|
+
)
|
|
705
725
|
|
|
706
726
|
async def gen() -> ChatResponseAsyncGen:
|
|
707
727
|
content = ""
|
llama_index/llms/openai/utils.py
CHANGED
|
@@ -253,14 +253,17 @@ def is_function_calling_model(model: str) -> bool:
|
|
|
253
253
|
|
|
254
254
|
|
|
255
255
|
def to_openai_message_dict(
|
|
256
|
-
message: ChatMessage,
|
|
256
|
+
message: ChatMessage,
|
|
257
|
+
drop_none: bool = False,
|
|
258
|
+
model: Optional[str] = None,
|
|
259
|
+
supports_content_blocks: bool = False,
|
|
257
260
|
) -> ChatCompletionMessageParam:
|
|
258
261
|
"""Convert a ChatMessage to an OpenAI message dict."""
|
|
259
262
|
content = []
|
|
260
263
|
content_txt = ""
|
|
261
264
|
for block in message.blocks:
|
|
262
265
|
if isinstance(block, TextBlock):
|
|
263
|
-
if message.role.value in ("assistant", "tool", "system"):
|
|
266
|
+
if message.role.value in ("assistant", "tool", "system") or not supports_content_blocks:
|
|
264
267
|
# Despite the docs say otherwise, when role is ASSISTANT, SYSTEM
|
|
265
268
|
# or TOOL, 'content' cannot be a list and must be string instead.
|
|
266
269
|
content_txt += block.text
|
|
@@ -291,6 +294,7 @@ def to_openai_message_dict(
|
|
|
291
294
|
"role": message.role.value,
|
|
292
295
|
"content": content_txt
|
|
293
296
|
if message.role.value in ("assistant", "tool", "system")
|
|
297
|
+
or not supports_content_blocks
|
|
294
298
|
else content,
|
|
295
299
|
}
|
|
296
300
|
|
|
@@ -317,10 +321,16 @@ def to_openai_message_dicts(
|
|
|
317
321
|
messages: Sequence[ChatMessage],
|
|
318
322
|
drop_none: bool = False,
|
|
319
323
|
model: Optional[str] = None,
|
|
324
|
+
supports_content_blocks: bool = False,
|
|
320
325
|
) -> List[ChatCompletionMessageParam]:
|
|
321
326
|
"""Convert generic messages to OpenAI message dicts."""
|
|
322
327
|
return [
|
|
323
|
-
to_openai_message_dict(
|
|
328
|
+
to_openai_message_dict(
|
|
329
|
+
message,
|
|
330
|
+
drop_none=drop_none,
|
|
331
|
+
model=model,
|
|
332
|
+
supports_content_blocks=supports_content_blocks,
|
|
333
|
+
)
|
|
324
334
|
for message in messages
|
|
325
335
|
]
|
|
326
336
|
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
llama_index/llms/openai/__init__.py,sha256=vm3cIBSGkBFlE77GyfyN0EhpJcnJZN95QMhPN53EkbE,148
|
|
2
|
+
llama_index/llms/openai/base.py,sha256=oyXczVpX-IF9MvTZ1xDY7OpiEmlywwiG1eTsyUGFrns,36195
|
|
3
|
+
llama_index/llms/openai/utils.py,sha256=aF2gP-Aef7rMUIYp9bG2Ke45pNszl_Jagga781Hul-A,18335
|
|
4
|
+
llama_index_llms_openai-0.3.6.dist-info/METADATA,sha256=ZzJAPLc3-rJCZG76WUO9uJuCLYlU14dLUQqNTWcnvAg,3320
|
|
5
|
+
llama_index_llms_openai-0.3.6.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
|
6
|
+
llama_index_llms_openai-0.3.6.dist-info/RECORD,,
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
llama_index/llms/openai/__init__.py,sha256=vm3cIBSGkBFlE77GyfyN0EhpJcnJZN95QMhPN53EkbE,148
|
|
2
|
-
llama_index/llms/openai/base.py,sha256=V__JjM8FSTuvQdeb-kqJOzIX3d1v9x4bbOGDxs-O56s,35637
|
|
3
|
-
llama_index/llms/openai/utils.py,sha256=cCmcpS2wMN7xTrq18UV012aC0_Fn3nHz6aA5bYypRkQ,18062
|
|
4
|
-
llama_index_llms_openai-0.3.4.dist-info/METADATA,sha256=36ZOZUGYVoJcjSaYHG2FkLugRtcYKfTM2M1AxycS9n8,3320
|
|
5
|
-
llama_index_llms_openai-0.3.4.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
|
6
|
-
llama_index_llms_openai-0.3.4.dist-info/RECORD,,
|
|
File without changes
|