Skip to content

ollama: Add tool call support #29563

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

Merged
merged 23 commits into from
May 5, 2025
Merged

ollama: Add tool call support #29563

merged 23 commits into from
May 5, 2025

Conversation

tidely
Copy link
Contributor

@tidely tidely commented Apr 28, 2025

The goal of this PR is to support tool calls using ollama. A lot of the serialization work was done in #15803 however the abstraction over language models always disables tools.

Changelog:

  • Use serde_json::Value inside OllamaFunctionCall just as it's used in OllamaFunctionCall. This fixes deserialization of ollama tool calls.
  • Added deserialization tests using json from official ollama api docs.
  • Fetch model capabilities during model enumeration from ollama provider
  • Added supports_tools setting to manually configure if a model supports tools

TODO:

  • Fix tool call serialization/deserialization
  • Fetch model capabilities from ollama api
  • Add tests for parsing model capabilities
  • Documentation for supports_tools field for ollama language model config
  • Convert between generic language model types
  • Pass tools to ollama

Release Notes:

  • N/A

@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Apr 28, 2025
@tidely tidely changed the title ollama: Tool calling using ollama ollama: Support tool calls (by extension agent) Apr 28, 2025
@tidely tidely changed the title ollama: Support tool calls (by extension agent) ollama: Support tool calls (by extension, the agent) Apr 28, 2025
@maxdeviant maxdeviant changed the title ollama: Support tool calls (by extension, the agent) ollama: Add tool call support Apr 28, 2025
@tidely
Copy link
Contributor Author

tidely commented Apr 29, 2025

The best way to allow tool calls on specific models is likely to have the user explicitly allow tools on specific models in settings.json under the ollama provider. However this means ollama wouldn't work with the agent crate out of the box, rather a user must enable it by allowing tools calls manually.

@lj3954
Copy link
Contributor

lj3954 commented Apr 29, 2025

The show model information API should have what you're looking for. You'll just have to call it on each model.

@tidely
Copy link
Contributor Author

tidely commented Apr 29, 2025

The show model information API should have what you're looking for. You'll just have to call it on each model.

Unfortunately I believe the show endpoint does not contain a field for whether the model supports tools. Atleast not one which would be consistent across different model families :(

@lj3954
Copy link
Contributor

lj3954 commented Apr 29, 2025

Does it not? The capabilities key contains "tools" on llama models which have tool support. Are there models where this doesn't apply?

@tidely
Copy link
Contributor Author

tidely commented Apr 29, 2025

I just checked with a bunch of different model families are it seems I was wrong! Thank you @lj3954, I'll work on something to make use of it.

tidely added 2 commits May 2, 2025 11:42
Value implements ToString, so we dont need to pass it through serde_json::to_string. This saves us from handling an error that can never occur since serde_json::json! always returns a valid Value
@tidely
Copy link
Contributor Author

tidely commented May 4, 2025

@mgsloan Hey! I saw you recently worked on #29885. I looked at creating a similar map_to_completion_events function for the ollama provider, but it doesn't seem to be working as I hoped for. Would you like to take a look at this PR's implementation and see if there's anything you could point out at a quick glance? Otherwise I'll schedule myself a deep dive into the agent crate. Thank you in advance!

@as-cii as-cii marked this pull request as ready for review May 5, 2025 17:40
@as-cii as-cii enabled auto-merge (squash) May 5, 2025 17:40
@nathansobo
Copy link
Contributor

Thanks so much for getting this started! Very helpful ❤️

@as-cii
Copy link
Member

as-cii commented May 5, 2025

Really nice work here, @tidely! We made some small tweaks to your pull request, but it was already working great for the most part!

@as-cii as-cii merged commit 769ec59 into zed-industries:main May 5, 2025
19 checks passed
JosephTLyons pushed a commit that referenced this pull request May 5, 2025
The goal of this PR is to support tool calls using ollama. A lot of the
serialization work was done in
#15803 however the abstraction
over language models always disables tools.

## Changelog:

- Use `serde_json::Value` inside `OllamaFunctionCall` just as it's used
in `OllamaFunctionCall`. This fixes deserialization of ollama tool
calls.
- Added deserialization tests using json from official ollama api docs.
- Fetch model capabilities during model enumeration from ollama provider
- Added `supports_tools` setting to manually configure if a model
supports tools

## TODO:

- [x] Fix tool call serialization/deserialization
- [x] Fetch model capabilities from ollama api
- [x] Add tests for parsing model capabilities 
- [ ] Documentation for `supports_tools` field for ollama language model
config
- [ ] Convert between generic language model types
- [x] Pass tools to ollama

Release Notes:

- N/A

---------

Co-authored-by: Antonio Scandurra <[email protected]>
Co-authored-by: Nathan Sobo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed The user has signed the Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants