llama-index-llms-openai 0.3.44__tar.gz → 0.4.1__tar.gz

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.
@@ -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.1
4
4
  Summary: llama-index llms openai integration
5
5
  Author: llama-index
6
6
  License-Expression: MIT
@@ -179,6 +179,7 @@ class OpenAI(FunctionCallingLLM):
179
179
  )
180
180
  max_tokens: Optional[int] = Field(
181
181
  description="The maximum number of tokens to generate.",
182
+ default=None,
182
183
  gt=0,
183
184
  )
184
185
  logprobs: Optional[bool] = Field(
@@ -215,9 +216,13 @@ class OpenAI(FunctionCallingLLM):
215
216
  ),
216
217
  )
217
218
 
218
- api_key: str = Field(default=None, description="The OpenAI API key.")
219
- api_base: str = Field(description="The base URL for OpenAI API.")
220
- api_version: str = Field(description="The API version for OpenAI API.")
219
+ api_key: Optional[str] = Field(default=None, description="The OpenAI API key.")
220
+ api_base: Optional[str] = Field(
221
+ default=None, description="The base URL for OpenAI API."
222
+ )
223
+ api_version: Optional[str] = Field(
224
+ default=None, description="The API version for OpenAI API."
225
+ )
221
226
  strict: bool = Field(
222
227
  default=False,
223
228
  description="Whether to use strict mode for invoking tools/using schemas.",
@@ -904,12 +909,15 @@ class OpenAI(FunctionCallingLLM):
904
909
  chat_history: Optional[List[ChatMessage]] = None,
905
910
  verbose: bool = False,
906
911
  allow_parallel_tool_calls: bool = False,
907
- tool_choice: Union[str, dict] = "auto",
912
+ tool_required: bool = False,
913
+ tool_choice: Optional[Union[str, dict]] = None,
908
914
  strict: Optional[bool] = None,
909
915
  **kwargs: Any,
910
916
  ) -> Dict[str, Any]:
911
917
  """Predict and call the tool."""
912
- tool_specs = [tool.metadata.to_openai_tool(skip_length_check=True) for tool in tools]
918
+ tool_specs = [
919
+ tool.metadata.to_openai_tool(skip_length_check=True) for tool in tools
920
+ ]
913
921
 
914
922
  # if strict is passed in, use, else default to the class-level attribute, else default to True`
915
923
  if strict is not None:
@@ -934,7 +942,9 @@ class OpenAI(FunctionCallingLLM):
934
942
  return {
935
943
  "messages": messages,
936
944
  "tools": tool_specs or None,
937
- "tool_choice": resolve_tool_choice(tool_choice) if tool_specs else None,
945
+ "tool_choice": resolve_tool_choice(tool_choice, tool_required)
946
+ if tool_specs
947
+ else None,
938
948
  **kwargs,
939
949
  }
940
950
 
@@ -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
@@ -12,7 +12,7 @@ dev = [
12
12
  "pytest==7.2.1",
13
13
  "pytest-asyncio==0.21.0",
14
14
  "pytest-mock==3.11.1",
15
- "ruff==0.0.292",
15
+ "ruff==0.11.11",
16
16
  "types-Deprecated>=0.1.0",
17
17
  "types-PyYAML>=6.0.12.12,<7",
18
18
  "types-protobuf>=4.24.0.4,<5",
@@ -27,7 +27,7 @@ dev = [
27
27
 
28
28
  [project]
29
29
  name = "llama-index-llms-openai"
30
- version = "0.3.44"
30
+ version = "0.4.1"
31
31
  description = "llama-index llms openai integration"
32
32
  authors = [{name = "llama-index"}]
33
33
  requires-python = ">=3.9,<4.0"