Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tool parsing for ollama #1597

Merged
merged 4 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions cookbook/providers/ollama/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -18,3 +19,13 @@

# Print the response in the terminal
agent.print_response("What is the stock price of NVDA and TSLA")


Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another example with a more complex tool


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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's put this in agent_stream.py file

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 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
Expand Down
3 changes: 2 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 All @@ -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)
Expand Down
Loading