llama-index-llms-openai 0.3.44__py3-none-any.whl → 0.4.0__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.
@@ -904,12 +904,15 @@ class OpenAI(FunctionCallingLLM):
904
904
  chat_history: Optional[List[ChatMessage]] = None,
905
905
  verbose: bool = False,
906
906
  allow_parallel_tool_calls: bool = False,
907
- tool_choice: Union[str, dict] = "auto",
907
+ tool_required: bool = False,
908
+ tool_choice: Optional[Union[str, dict]] = None,
908
909
  strict: Optional[bool] = None,
909
910
  **kwargs: Any,
910
911
  ) -> Dict[str, Any]:
911
912
  """Predict and call the tool."""
912
- tool_specs = [tool.metadata.to_openai_tool(skip_length_check=True) for tool in tools]
913
+ tool_specs = [
914
+ tool.metadata.to_openai_tool(skip_length_check=True) for tool in tools
915
+ ]
913
916
 
914
917
  # if strict is passed in, use, else default to the class-level attribute, else default to True`
915
918
  if strict is not None:
@@ -934,7 +937,9 @@ class OpenAI(FunctionCallingLLM):
934
937
  return {
935
938
  "messages": messages,
936
939
  "tools": tool_specs or None,
937
- "tool_choice": resolve_tool_choice(tool_choice) if tool_specs else None,
940
+ "tool_choice": resolve_tool_choice(tool_choice, tool_required)
941
+ if tool_specs
942
+ else None,
938
943
  **kwargs,
939
944
  }
940
945
 
@@ -570,7 +570,12 @@ class OpenAIResponses(FunctionCallingLLM):
570
570
  elif isinstance(event, ResponseImageGenCallPartialImageEvent):
571
571
  # Partial image
572
572
  if event.partial_image_b64:
573
- blocks.append(ImageBlock(image=base64.b64decode(event.partial_image_b64), detail=f"id_{event.partial_image_index}"))
573
+ blocks.append(
574
+ ImageBlock(
575
+ image=base64.b64decode(event.partial_image_b64),
576
+ detail=f"id_{event.partial_image_index}",
577
+ )
578
+ )
574
579
  elif isinstance(event, ResponseFunctionCallArgumentsDeltaEvent):
575
580
  # Function call arguments are being streamed
576
581
  if current_tool_call is not None:
@@ -815,7 +820,8 @@ class OpenAIResponses(FunctionCallingLLM):
815
820
  user_msg: Optional[Union[str, ChatMessage]] = None,
816
821
  chat_history: Optional[List[ChatMessage]] = None,
817
822
  allow_parallel_tool_calls: bool = True,
818
- tool_choice: Union[str, dict] = "auto",
823
+ tool_required: bool = False,
824
+ tool_choice: Optional[Union[str, dict]] = None,
819
825
  verbose: bool = False,
820
826
  strict: Optional[bool] = None,
821
827
  **kwargs: Any,
@@ -848,7 +854,9 @@ class OpenAIResponses(FunctionCallingLLM):
848
854
  return {
849
855
  "messages": messages,
850
856
  "tools": tool_specs or None,
851
- "tool_choice": resolve_tool_choice(tool_choice) if tool_specs else None,
857
+ "tool_choice": resolve_tool_choice(tool_choice, tool_required)
858
+ if tool_specs
859
+ else None,
852
860
  "parallel_tool_calls": allow_parallel_tool_calls,
853
861
  **kwargs,
854
862
  }
@@ -860,9 +868,9 @@ class OpenAIResponses(FunctionCallingLLM):
860
868
  **kwargs: Any,
861
869
  ) -> List[ToolSelection]:
862
870
  """Predict and call the tool."""
863
- tool_calls: List[
864
- ResponseFunctionToolCall
865
- ] = response.message.additional_kwargs.get("tool_calls", [])
871
+ tool_calls: List[ResponseFunctionToolCall] = (
872
+ response.message.additional_kwargs.get("tool_calls", [])
873
+ )
866
874
 
867
875
  if len(tool_calls) < 1:
868
876
  if error_on_no_tool_call:
@@ -754,13 +754,19 @@ def validate_openai_api_key(api_key: Optional[str] = None) -> None:
754
754
  raise ValueError(MISSING_API_KEY_ERROR_MESSAGE)
755
755
 
756
756
 
757
- def resolve_tool_choice(tool_choice: Union[str, dict] = "auto") -> Union[str, dict]:
757
+ def resolve_tool_choice(
758
+ tool_choice: Optional[Union[str, dict]], tool_required: bool = False
759
+ ) -> Union[str, dict]:
758
760
  """
759
761
  Resolve tool choice.
760
762
 
761
763
  If tool_choice is a function name string, return the appropriate dict.
762
764
  """
763
- if isinstance(tool_choice, str) and tool_choice not in ["none", "auto", "required"]:
765
+ if tool_choice is None:
766
+ tool_choice = "required" if tool_required else "auto"
767
+ if isinstance(tool_choice, dict):
768
+ return tool_choice
769
+ if tool_choice not in ["none", "auto", "required"]:
764
770
  return {"type": "function", "function": {"name": tool_choice}}
765
771
 
766
772
  return tool_choice
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: llama-index-llms-openai
3
- Version: 0.3.44
3
+ Version: 0.4.0
4
4
  Summary: llama-index llms openai integration
5
5
  Author: llama-index
6
6
  License-Expression: MIT
@@ -0,0 +1,9 @@
1
+ llama_index/llms/openai/__init__.py,sha256=8nmgixeXifQ4eVSgtCic54WxXqrrpXQPL4rhACWCSFs,229
2
+ llama_index/llms/openai/base.py,sha256=C32WIR4La7l3FLw9-V88Tdb57HlNlFiCkcJUACbhrFk,38748
3
+ llama_index/llms/openai/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ llama_index/llms/openai/responses.py,sha256=Dmp7Tj-SzrzOcqGtWSBAFn-kwT44giXJSYxAXx233Ao,35944
5
+ llama_index/llms/openai/utils.py,sha256=0xO-_6FPZRFCbW5JeqJDCMzLx3WN2_vVeAApWG74oA4,28071
6
+ llama_index_llms_openai-0.4.0.dist-info/METADATA,sha256=t11u_EkGEhXb78ioRIF-DC3EiLNPXgGjUh8Xp_2fM6Y,3039
7
+ llama_index_llms_openai-0.4.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
8
+ llama_index_llms_openai-0.4.0.dist-info/licenses/LICENSE,sha256=JPQLUZD9rKvCTdu192Nk0V5PAwklIg6jANii3UmTyMs,1065
9
+ llama_index_llms_openai-0.4.0.dist-info/RECORD,,
@@ -1,9 +0,0 @@
1
- llama_index/llms/openai/__init__.py,sha256=8nmgixeXifQ4eVSgtCic54WxXqrrpXQPL4rhACWCSFs,229
2
- llama_index/llms/openai/base.py,sha256=2ojLUicnyDwVXO8-_kis93Yibrc8tQoUthSsY7tSyn8,38642
3
- llama_index/llms/openai/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- llama_index/llms/openai/responses.py,sha256=IVB2ZW8AZopUqKzaeJBsA36glKa9z6bh9lSBdT_KqGE,35749
5
- llama_index/llms/openai/utils.py,sha256=K8LQ6ykuU-TPzIJQLvZ869q5aGvXSM9HAJIX8GQfM_s,27913
6
- llama_index_llms_openai-0.3.44.dist-info/METADATA,sha256=pK9skvohtMh4L5JpWFDWht1gOSF3ikgnLJLsARZi1SI,3040
7
- llama_index_llms_openai-0.3.44.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
8
- llama_index_llms_openai-0.3.44.dist-info/licenses/LICENSE,sha256=JPQLUZD9rKvCTdu192Nk0V5PAwklIg6jANii3UmTyMs,1065
9
- llama_index_llms_openai-0.3.44.dist-info/RECORD,,