mirror of
https://github.com/ollama/ollama-python.git
synced 2026-07-21 09:05:23 -04:00
function calling #15
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @iplayfast on GitHub (Jan 29, 2024).
I keep seeing that openai has function calling [https://platform.openai.com/docs/api-reference/chat/create] (now called tooltypes) and some open source llm also support function calling.
This is done by having the models fine tuned to understand when they need to call a function. As we don't have that ability (as far as I know) maybe we could emulate it by adding a layer between ollama and the api, so the api can be added to.
So calling
ollama.tools() will return what tools are available.
ollama.addtool(name="string",prompt,function)
addtool would add to a list of tools the name and the prompt to recognize and the response it should have..
For instance to do an internet search
ollama.addtool("BrowseWeb","If the answer needs to access html pages return 'BrowseWeb:url'",BrowseWeb)
def BrowseWeb(url):
return httpx(url)
This is probably not the best solution, but I thought I would make it an issue to see if any discussion will come from it.
@jeremytregunna commented on GitHub (Feb 13, 2024):
One possible solution for you, that works well for me... is define a schema to represent your function's arguments, and pass that into the system message, get json formatted response using json mode in the API. Then treat the response you get (don't recommend streaming the json) as the input data you can then use to match to the function you want to invoke given the data.
It's not the same approach that OpenAI uses, but it's similar and does work.
@eliranwong commented on GitHub (Feb 23, 2024):
Function calling makes a big difference.
It would be perfect if ollama could support function calling
@eliranwong commented on GitHub (Mar 1, 2024):
An approach to run multiple functions with Ollama: https://github.com/eliranwong/freegenius#approach-to-run-function-calling-equivalent-features-offline-with-common-hardwares I like Ollama and will use ollama as the default engine for the project.
@jeremytregunna commented on GitHub (Mar 1, 2024):
I have been able to get function calling (openai style) to work with dolphin-mistral:8x7b-v2.7-q8_0, probably works with others too but probably not them all. Here's the prompt I send:
which causes output from the model like this (for illustrative purposes):
Hope that helps illustrate that at least with some models, you can do it directly with prompting and json mode.
@eliranwong commented on GitHub (Mar 1, 2024):
It is helpful, much appreciated
@eliranwong commented on GitHub (Mar 2, 2024):
This example works in some models. However, when I try to add more than 5 actions or above, the waiting time is getting terrible.
I have a project https://github.com/eliranwong/letmedoit uses ChatGPT by default for function calling among around 40 functions in one go, without an issue. I am working on how to use Ollama instead. So far, waiting time for a single prompt of 40 functions is not acceptable at all. I find a little hope to split the process in steps for generations.
@eliranwong commented on GitHub (Mar 12, 2024):
It seems this workaround works with better speed: https://github.com/eliranwong/freegenius#approach-to-run-function-calling-equivalent-features-offline-with-common-hardwares
I am testing with "phi", "mistral" and "llama2" via Ollama
... still in testing, though ...
@jquintanilla4 commented on GitHub (Apr 10, 2024):
i just used instructor. It's a bit of a pain to setup, but once you do. It works without issues everytime.
https://jxnl.github.io/instructor/
@lennartpollvogt commented on GitHub (Jun 2, 2024):
@jquintanilla4 I was inspired by instructor and tried to create a similar library just for Ollama. It is still missing some features like supporting async and I haven't tried if it works with images, but I am working on that and the documentation. Here is the link to the repo: ollama-instructor
Would be cool to have some feedback, if it would work for you projects as well
@elhumbertoz commented on GitHub (Jul 19, 2024):
Thank you,
I modify a litte:
context += `To respond to the users message, you have access to the following tools:
${JSON.stringify(functions)}
YOU MUST FOLLOW THESE INSTRUCTIONS CAREFULLY.
{
"tool_calls": [{
"name": "",
"arguments": <parameters for the selected tool, matching the tool's JSON schema
}]
}
3. Get the arguments for calling functions only from the context of this conversation. Don't concoct them; you will be able to ask the user for any missing information.
`
}
@SpudGunMan commented on GitHub (Sep 3, 2024):
https://medium.com/@meirgotroot/ollama-support-for-tool-calling-186bcfeb892f
https://ollama.com/blog/tool-support
@eliranwong commented on GitHub (Sep 5, 2024):
My previous struggles with ollama function calling was that it was not easy for ollama to handle 20+ tools in one go. However, I finally found a way to run multiple tools with ollama too. In case anyone interested https://github.com/eliranwong/freegenius/wiki/Multiple-Tools-in-One-Go
@ParthSareen commented on GitHub (Feb 13, 2025):
Closing this out: https://ollama.com/blog/functions-as-tools