@@ -29,8 +29,9 @@ Run LLM prompts straight from your shell. Command line access to the ghost in th
2929[ Functions] ( #functions ) :
3030[ Api] ( #api-functions ) -
3131[ Helper] ( #helper-functions ) -
32- [ Generate] ( #generate-functions ) -
33- [ Chat] ( #chat-functions ) -
32+ [ Generate] ( #generate-functions ) -
33+ [ Chat] ( #chat-functions ) -
34+ [ Tools] ( #tools-functions ) -
3435[ Model] ( #model-functions ) -
3536[ Ollama] ( #ollama-functions ) -
3637[ Lib] ( #lib-functions ) -
@@ -173,6 +174,125 @@ Examples:
173174OLLAMA_LIB_DEBUG=1 ollama_generate gpt-oss:20b "Three words about debugging"
174175```
175176
177+ ### Howto Use Tools
178+
179+ The Tool System allows you to define custom tools that a model can use to perform actions.
180+
181+ ** Step 1: Add a Tool**
182+
183+ First, you need to define a tool. A tool consists of a name, a command to execute, and a JSON definition that describes the tool to the model.
184+
185+ For example, let's create a tool that gets the current weather:
186+
187+ ``` bash
188+ # The command for our tool will be a function that takes a JSON object as input
189+ weather_tool () {
190+ local location
191+ location=" $( printf ' %s' " $1 " | jq -r ' .location' ) "
192+ # In a real scenario, you would call a weather API here
193+ printf ' {"temperature": "72F", "conditions": "Sunny"}'
194+ }
195+
196+ # The JSON definition for the model
197+ weather_definition=' {
198+ "name": "get_weather",
199+ "description": "Get the current weather in a given location",
200+ "parameters": {
201+ "type": "object",
202+ "properties": {
203+ "location": {
204+ "type": "string",
205+ "description": "The city and state, e.g. San Francisco, CA"
206+ }
207+ },
208+ "required": ["location"]
209+ }
210+ }'
211+
212+ # Add the tool
213+ ollama_tools_add " get_weather" " weather_tool" " $weather_definition "
214+ ```
215+
216+ ** Step 2: View Tools**
217+
218+ You can see all registered tools with ` ollama_tools ` :
219+
220+ ``` bash
221+ ollama_tools
222+ # get_weather weather_tool
223+ ```
224+
225+ ** Step 3: Send a Tool Request**
226+
227+ You can send a request to a model that supports tool calling. You can use either ` ollama_generate ` or ` ollama_chat ` .
228+
229+ Using ` ollama_chat ` :
230+ ``` bash
231+ ollama_messages_add " user" " What is the weather in San Francisco?"
232+ response=" $( ollama_chat " gpt-4-turbo" ) "
233+ ```
234+
235+ ** Step 4: Consume the Tool Response**
236+
237+ If the model decides to use a tool, the response will contain a ` tool_calls ` section. You can check for this with ` ollama_tools_is_call ` .
238+
239+ ``` bash
240+ if ollama_tools_is_call " $response " ; then
241+ echo " Tool call detected!"
242+ fi
243+ ```
244+
245+ The response will look something like this:
246+ ``` json
247+ {
248+ "model" : " gpt-4-turbo" ,
249+ "created_at" : " ..." ,
250+ "message" : {
251+ "role" : " assistant" ,
252+ "content" : " " ,
253+ "tool_calls" : [
254+ {
255+ "id" : " call_1234" ,
256+ "type" : " function" ,
257+ "function" : {
258+ "name" : " get_weather" ,
259+ "arguments" : " {\n \" location\" : \" San Francisco, CA\"\n }"
260+ }
261+ }
262+ ]
263+ },
264+ ...
265+ }
266+ ```
267+
268+ ** Step 5: Run the Tool**
269+
270+ Now you need to extract the tool call information and run the tool.
271+
272+ ``` bash
273+ tool_name=" $( printf ' %s' " $response " | jq -r ' .message.tool_calls[0].function.name' ) "
274+ tool_args=" $( printf ' %s' " $response " | jq -r ' .message.tool_calls[0].function.arguments' ) "
275+ tool_call_id=" $( printf ' %s' " $response " | jq -r ' .message.tool_calls[0].id' ) "
276+
277+ tool_result=" $( ollama_tools_run " $tool_name " " $tool_args " ) "
278+ ```
279+
280+ ** Step 6: Add Tool Response to Message List (for chat)**
281+
282+ Finally, if you are in a chat session, you need to add the tool's output back into the message list so the model can use it to generate a user-facing response.
283+
284+ ``` bash
285+ # Create a JSON object with the tool_call_id and the result
286+ tool_response_json=" $( jq -c -n --arg tool_call_id " $tool_call_id " --arg result " $tool_result " ' {tool_call_id: $tool_call_id, result: $result}' ) "
287+
288+ # Add the tool response to the messages
289+ ollama_messages_add " tool" " $tool_response_json "
290+
291+ # Now, call the chat again to get the final response
292+ final_response=" $( ollama_chat " gpt-4-turbo" ) "
293+ echo " $final_response "
294+ ```
295+
176296## Demos
177297
178298See the ** [ demos] ( demos ) ** directory for all demo scripts
@@ -245,6 +365,17 @@ To run all demos and save output to Markdown files: [demos/run.demos.sh](demos/r
245365| ` ollama_messages_count ` <br />` omco ` | Count of messages in chat context | ` ollama_messages_count ` | number of messages to ` stdout ` | ` 0 ` /` 1 ` |
246366| ` ollama_messages_clear ` <br />` omc ` | Clear all messages in chat context | ` ollama_messages_clear ` | none | ` 0 ` /` 1 ` |
247367
368+ ### Tools Functions
369+
370+ | Function<br />Alias | About | Usage | Output | Return |
371+ | ----------------------------------| -------------------------------------| --------------------------------------------------------------------| ----------------------------| ---------------------------------------------|
372+ | ` ollama_tools_add ` <br />` ota ` | Add a tool | ` ollama_tools_add "name" "command" "json_definition" ` | none | ` 0 ` /` 1 ` |
373+ | ` ollama_tools ` <br />` oto ` | View all tools | ` ollama_tools ` | list of tools to ` stdout ` | ` 0 ` /` 1 ` |
374+ | ` ollama_tools_count ` <br />` otco ` | Get count of tools | ` ollama_tools_count ` | number of tools to ` stdout ` | ` 0 ` /` 1 ` |
375+ | ` ollama_tools_clear ` <br />` otc ` | Remove all tools | ` ollama_tools_clear ` | none | ` 0 ` /` 1 ` |
376+ | ` ollama_tools_is_call ` <br />` otic ` | Does the response have a tool call? | ` ollama_tools_is_call "json_response" ` | none | ` 0 ` if it has a tool call, ` 1 ` otherwise |
377+ | ` ollama_tools_run ` <br />` otr ` | Run a tool | ` ollama_tools_run "tool_name" "arguments_json" ` | result of tool to ` stdout ` | ` 0 ` /` 1 ` |
378+
248379### Model Functions
249380
250381| Function<br />Alias | About | Usage | Output | Return |
0 commit comments