Skip to content

Commit

Permalink
v2.5.30
Browse files Browse the repository at this point in the history
  • Loading branch information
ashpreetbedi committed Nov 3, 2024
1 parent a7361e2 commit e0ce7fc
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 14 deletions.
6 changes: 5 additions & 1 deletion phi/agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,11 @@ def update_model(self) -> None:
agent_tools = self.get_tools()
if agent_tools is not None:
for tool in agent_tools:
if self.response_model is not None and self.structured_outputs:
if (
self.response_model is not None
and self.structured_outputs
and self.model.supports_structured_outputs
):
self.model.add_tool(tool, structured_outputs=True)
else:
self.model.add_tool(tool)
Expand Down
12 changes: 5 additions & 7 deletions phi/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,6 @@ def add_tool(self, tool: Union[Tool, Toolkit, Callable, Dict, Function], structu

# If the tool is a Tool or Dict, add it directly to the Model
if isinstance(tool, Tool) or isinstance(tool, Dict):
if structured_outputs:
tool.strict = True
if tool not in self.tools:
self.tools.append(tool)
logger.debug(f"Added tool {tool} to model.")
Expand All @@ -143,18 +141,18 @@ def add_tool(self, tool: Union[Tool, Toolkit, Callable, Dict, Function], structu
if isinstance(tool, Toolkit):
# For each function in the toolkit
for name, func in tool.functions.items():
if structured_outputs:
func.strict = True
# If the function does not exist in self.functions, add to self.tools
if name not in self.functions:
if structured_outputs and self.supports_structured_outputs:
func.strict = True
self.functions[name] = func
self.tools.append({"type": "function", "function": func.to_dict()})
logger.debug(f"Function {name} from {tool.name} added to model.")

elif isinstance(tool, Function):
if structured_outputs:
tool.strict = True
if tool.name not in self.functions:
if structured_outputs and self.supports_structured_outputs:
tool.strict = True
self.functions[tool.name] = tool
self.tools.append({"type": "function", "function": tool.to_dict()})
logger.debug(f"Function {tool.name} added to model.")
Expand All @@ -164,7 +162,7 @@ def add_tool(self, tool: Union[Tool, Toolkit, Callable, Dict, Function], structu
function_name = tool.__name__
if function_name not in self.functions:
func = Function.from_callable(tool)
if structured_outputs:
if structured_outputs and self.supports_structured_outputs:
func.strict = True
self.functions[func.name] = func
self.tools.append({"type": "function", "function": func.to_dict()})
Expand Down
4 changes: 3 additions & 1 deletion phi/model/google/gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ def _format_functions(self, params: Dict[str, Any]) -> Dict[str, Any]:
formatted_params[key] = value
return formatted_params

def add_tool(self, tool: Union["Tool", "Toolkit", Callable, dict, "Function"]) -> None:
def add_tool(
self, tool: Union["Tool", "Toolkit", Callable, dict, "Function"], structured_outputs: bool = False
) -> None:
"""
Adds tools to the model.
Expand Down
4 changes: 2 additions & 2 deletions phi/tools/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Function(BaseModel):
# To describe a function that accepts no parameters, provide the value {"type": "object", "properties": {}}.
parameters: Dict[str, Any] = {"type": "object", "properties": {}}
entrypoint: Optional[Callable] = None
strict: bool = False
strict: Optional[bool] = None

# If True, the arguments are sanitized before being passed to the function.
sanitize_arguments: bool = True
Expand All @@ -29,7 +29,7 @@ def from_callable(cls, c: Callable) -> "Function":
from inspect import getdoc
from phi.utils.json_schema import get_json_schema

parameters = {"type": "object", "properties": {}, "required": [], "additionalProperties": False}
parameters = {"type": "object", "properties": {}, "required": []}
try:
# logger.info(f"Getting type hints for {c}")
type_hints = get_type_hints(c)
Expand Down
11 changes: 9 additions & 2 deletions phi/utils/json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,19 @@ def get_json_schema_for_arg(t: Any) -> Optional[Any]:


def get_json_schema(type_hints: Dict[str, Any]) -> Dict[str, Any]:
json_schema: Dict[str, Any] = {"type": "object", "properties": {}, "required": [], "additionalProperties": False}
json_schema: Dict[str, Any] = {"type": "object", "properties": {}, "required": []}
for k, v in type_hints.items():
# logger.info(f"Parsing arg: {k} | {v}")
if k == "return":
continue
json_schema["required"].append(k)

# Check if type is Optional (Union with NoneType)
type_origin = get_origin(v)
type_args = get_args(v)
is_optional = type_origin is Union and len(type_args) == 2 and type(None) in type_args
if not is_optional:
json_schema["required"].append(k)

arg_json_schema = get_json_schema_for_arg(v)
if arg_json_schema is not None:
# logger.info(f"json_schema: {arg_json_schema}")
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "phidata"
version = "2.5.29"
version = "2.5.30"
description = "Build AI Agents with memory, knowledge and tools."
requires-python = ">=3.7"
readme = "README.md"
Expand Down

0 comments on commit e0ce7fc

Please sign in to comment.