From cb9cfe5610afb772a53a5623faf2ed39a6e62f8b Mon Sep 17 00:00:00 2001 From: Dirk Brand Date: Wed, 18 Dec 2024 11:33:24 +0200 Subject: [PATCH 1/4] Fix tool parsing for ollama --- cookbook/providers/ollama/agent.py | 11 +++++++++++ phi/model/ollama/chat.py | 5 +++++ phi/tools/function.py | 3 ++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/cookbook/providers/ollama/agent.py b/cookbook/providers/ollama/agent.py index 81621d443..6b0157bf6 100644 --- a/cookbook/providers/ollama/agent.py +++ b/cookbook/providers/ollama/agent.py @@ -2,6 +2,7 @@ from phi.agent import Agent, RunResponse # noqa from phi.model.ollama import Ollama +from phi.tools.crawl4ai_tools import Crawl4aiTools from phi.tools.yfinance import YFinanceTools agent = Agent( @@ -18,3 +19,13 @@ # Print the response in the terminal agent.print_response("What is the stock price of NVDA and TSLA") + + + +agent = Agent( + model=Ollama(id="llama3.1:8b"), + tools=[Crawl4aiTools(max_length=1000)], + show_tool_calls=True +) +agent.print_response("Summarize me the key points in bullet points of this: https://blog.google/products/gemini/google-gemini-deep-research/", + stream=True) diff --git a/phi/model/ollama/chat.py b/phi/model/ollama/chat.py index 4c5c809da..3b83b048c 100644 --- a/phi/model/ollama/chat.py +++ b/phi/model/ollama/chat.py @@ -134,6 +134,11 @@ def request_kwargs(self) -> Dict[str, Any]: request_params["keep_alive"] = self.keep_alive if self.tools is not None: request_params["tools"] = self.get_tools_for_api() + # Ensure types are valid strings + for tool in request_params["tools"]: + for prop, obj in tool["function"]["parameters"]["properties"].items(): + if type(obj["type"]) == list: + obj["type"] = obj["type"][0] if self.request_params is not None: request_params.update(self.request_params) return request_params diff --git a/phi/tools/function.py b/phi/tools/function.py index 89520833e..d67bd61f9 100644 --- a/phi/tools/function.py +++ b/phi/tools/function.py @@ -158,7 +158,6 @@ def process_entrypoint(self, strict: bool = False): # Get JSON schema for parameters only parameters = get_json_schema(type_hints=param_type_hints, strict=strict) - # If strict=True mark all fields as required # See: https://platform.openai.com/docs/guides/structured-outputs/supported-schemas#all-fields-must-be-required if strict: @@ -171,6 +170,8 @@ def process_entrypoint(self, strict: bool = False): if param.default == param.empty and name != "self" and name != "agent" ] + # Param type should not be a list + # logger.debug(f"JSON schema for {self.name}: {parameters}") except Exception as e: logger.warning(f"Could not parse args for {self.name}: {e}", exc_info=True) From a05b00eb98176973d9f57a2b9c40d87f3314642d Mon Sep 17 00:00:00 2001 From: Dirk Brand Date: Wed, 18 Dec 2024 11:42:41 +0200 Subject: [PATCH 2/4] Undo --- phi/tools/function.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/phi/tools/function.py b/phi/tools/function.py index d67bd61f9..a8174e147 100644 --- a/phi/tools/function.py +++ b/phi/tools/function.py @@ -170,8 +170,6 @@ def process_entrypoint(self, strict: bool = False): if param.default == param.empty and name != "self" and name != "agent" ] - # Param type should not be a list - # logger.debug(f"JSON schema for {self.name}: {parameters}") except Exception as e: logger.warning(f"Could not parse args for {self.name}: {e}", exc_info=True) From b7800799a3ec2d29a513b1b59bade29038db0162 Mon Sep 17 00:00:00 2001 From: Manthan Gupta Date: Wed, 18 Dec 2024 15:36:32 +0530 Subject: [PATCH 3/4] fix comments --- cookbook/providers/ollama/agent.py | 11 ----------- cookbook/providers/ollama/agent_stream.py | 8 ++++++++ 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/cookbook/providers/ollama/agent.py b/cookbook/providers/ollama/agent.py index 6b0157bf6..81621d443 100644 --- a/cookbook/providers/ollama/agent.py +++ b/cookbook/providers/ollama/agent.py @@ -2,7 +2,6 @@ from phi.agent import Agent, RunResponse # noqa from phi.model.ollama import Ollama -from phi.tools.crawl4ai_tools import Crawl4aiTools from phi.tools.yfinance import YFinanceTools agent = Agent( @@ -19,13 +18,3 @@ # Print the response in the terminal agent.print_response("What is the stock price of NVDA and TSLA") - - - -agent = Agent( - model=Ollama(id="llama3.1:8b"), - tools=[Crawl4aiTools(max_length=1000)], - show_tool_calls=True -) -agent.print_response("Summarize me the key points in bullet points of this: https://blog.google/products/gemini/google-gemini-deep-research/", - stream=True) diff --git a/cookbook/providers/ollama/agent_stream.py b/cookbook/providers/ollama/agent_stream.py index 13c8e1060..d39e0b46e 100644 --- a/cookbook/providers/ollama/agent_stream.py +++ b/cookbook/providers/ollama/agent_stream.py @@ -3,6 +3,7 @@ from typing import Iterator # noqa from phi.agent import Agent, RunResponse # noqa from phi.model.ollama import Ollama +from phi.tools.crawl4ai_tools import Crawl4aiTools from phi.tools.yfinance import YFinanceTools agent = Agent( @@ -20,3 +21,10 @@ # Print the response in the terminal agent.print_response("What are analyst recommendations for NVDA and TSLA", stream=True) + + +agent = Agent(model=Ollama(id="llama3.1:8b"), tools=[Crawl4aiTools(max_length=1000)], show_tool_calls=True) +agent.print_response( + "Summarize me the key points in bullet points of this: https://blog.google/products/gemini/google-gemini-deep-research/", + stream=True, +) From f844012b7fbb801e90948a88513e59e4e37b865f Mon Sep 17 00:00:00 2001 From: Manthan Gupta Date: Wed, 18 Dec 2024 15:38:45 +0530 Subject: [PATCH 4/4] fix validation --- phi/model/ollama/chat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phi/model/ollama/chat.py b/phi/model/ollama/chat.py index 3b83b048c..acc744454 100644 --- a/phi/model/ollama/chat.py +++ b/phi/model/ollama/chat.py @@ -137,7 +137,7 @@ def request_kwargs(self) -> Dict[str, Any]: # Ensure types are valid strings for tool in request_params["tools"]: for prop, obj in tool["function"]["parameters"]["properties"].items(): - if type(obj["type"]) == list: + if isinstance(obj["type"], list): obj["type"] = obj["type"][0] if self.request_params is not None: request_params.update(self.request_params)