pepperoni21 / ollama-rs

A Rust library allowing to interact with the Ollama API.
MIT License
542 stars 81 forks source link

Ollama Function Calling #51

Closed andthattoo closed 5 months ago

andthattoo commented 6 months ago
erhant commented 6 months ago

We should also add docs about this

andthattoo commented 6 months ago

Did some manual testing with different models also added a few more tests to tests/function_calling.rs.

An example run with finance tool + nous hermes 2:

> What are the current risk factors to $APPL?
[2024-05-20T00:01:32Z INFO  ollama_rs::generation::functions] Function call: To provide you with an analysis of the current risk factors for Apple Inc. (AAPL), I will use the "Stock Scraper" tool from the provided functions. This tool allows me to scrape stock information from Google Finance, including potential risk factors. Please bear with me as I gather this data.

    <tool_call>
    {"arguments": {"exchange": "NASDAQ", "ticker": "AAPL"}, "name": "Stock Scraper"}
    </tool_call>

[2024-05-20T00:01:33Z INFO  ollama_rs::generation::functions] Parsing done
<tool_response>
{"Market cap":"2.91T USD","Avg Volume":"63.51M","Dividend yield":"0.53%","CEO":"Tim Cook","Primary exchange":"NASDAQ","Founded":"Apr 1, 1976","Year range":"$164.08 - $199.62","Previous close":"$189.84","Website":"apple.com","P/E ratio":"29.53","Headquarters":"Cupertino, CaliforniaUnited States","Employees":"161,000","Day range":"$189.18 - $190.81"}
</tool_response>

I tried to stick to the original design of ollama-rs as much as possible, and keep the recursive-looping agentic functionality excluded from this repo. Both send_function_call and send_function_call_with_history handle function calling, with the latter storing a history of tool calls.

Function calling is crucial for agentic frameworks such as ReAct (Reason + Action), but it would introduce unnecessary overhead to ollama-rs. I would probably implement it in langchain-rust or a crewAI-like Rust doppelganger. One can use responses from function calls to "Reason" over the observations done through "tools" implemented within the library.

andthattoo commented 6 months ago

@pepperoni21 my bad, added cargo fmt -all + cargo clippy to build. Should be ok now

pepperoni21 commented 6 months ago

Looks very good! I'll run the tests when I'm home 👍

erhant commented 5 months ago

@pepperoni21 any estimates on when we can merge this?