Skip to content

Commit

Permalink
Fix tool parsing for ollama (#1597)
Browse files Browse the repository at this point in the history
## Description

Ollama requires a specific formatting for JSONschema tools where
parameter types can only be a string, never a list of strings (which is
actually valid JSONschema). To compensate for this we ensure optional
parameters where the type is `['string', 'null']` is transformed to just
`'string'` to be compatible.
This should not have impact, because we provide a list of "required"
parameters with function definitions.

Fixes #1584 

---------

Co-authored-by: Manthan Gupta <[email protected]>
  • Loading branch information
dirkbrnd and manthanguptaa authored Dec 18, 2024
1 parent eae8290 commit 96ecee2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
8 changes: 8 additions & 0 deletions cookbook/providers/ollama/agent_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
)
5 changes: 5 additions & 0 deletions phi/model/ollama/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 isinstance(obj["type"], list):
obj["type"] = obj["type"][0]
if self.request_params is not None:
request_params.update(self.request_params)
return request_params
Expand Down
1 change: 0 additions & 1 deletion phi/tools/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 96ecee2

Please sign in to comment.