From 77fe8f57cf04b8235527efe756b418c57d98abd1 Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Fri, 26 Jul 2024 22:12:52 -0700 Subject: [PATCH] docs(ollama.md): add ollama tool calling to docs --- docs/my-website/docs/providers/ollama.md | 117 ++++++++++++++++++ ...odel_prices_and_context_window_backup.json | 10 ++ litellm/proxy/_new_secret_config.yaml | 2 +- model_prices_and_context_window.json | 10 ++ 4 files changed, 138 insertions(+), 1 deletion(-) diff --git a/docs/my-website/docs/providers/ollama.md b/docs/my-website/docs/providers/ollama.md index c1c8fc57c83..63b79fe3aa3 100644 --- a/docs/my-website/docs/providers/ollama.md +++ b/docs/my-website/docs/providers/ollama.md @@ -1,3 +1,6 @@ +import Tabs from '@theme/Tabs'; +import TabItem from '@theme/TabItem'; + # Ollama LiteLLM supports all models from [Ollama](https://github.com/ollama/ollama) @@ -84,6 +87,120 @@ response = completion( ) ``` +## Example Usage - Tool Calling + +To use ollama tool calling, pass `tools=[{..}]` to `litellm.completion()` + + + + +```python +from litellm import completion +import litellm + +## [OPTIONAL] REGISTER MODEL - not all ollama models support function calling, litellm defaults to json mode tool calls if native tool calling not supported. + +# litellm.register_model(model_cost={ +# "ollama_chat/llama3.1": { +# "supports_function_calling": true +# }, +# }) + +tools = [ + { + "type": "function", + "function": { + "name": "get_current_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA", + }, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, + }, + "required": ["location"], + }, + } + } +] + +messages = [{"role": "user", "content": "What's the weather like in Boston today?"}] + + +response = completion( + model="ollama_chat/llama3.1", + messages=messages, + tools=tools +) +``` + + + + +1. Setup config.yaml + +```yaml +model_list: + - model_name: "llama3.1" + litellm_params: + model: "ollama_chat/llama3.1" + model_info: + supports_function_calling: true +``` + +2. Start proxy + +```bash +litellm --config /path/to/config.yaml +``` + +3. Test it! + +```bash +curl -X POST 'http://0.0.0.0:4000/chat/completions' \ +-H 'Content-Type: application/json' \ +-H 'Authorization: Bearer sk-1234' \ +-d '{ + "model": "llama3.1", + "messages": [ + { + "role": "user", + "content": "What'\''s the weather like in Boston today?" + } + ], + "tools": [ + { + "type": "function", + "function": { + "name": "get_current_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA" + }, + "unit": { + "type": "string", + "enum": ["celsius", "fahrenheit"] + } + }, + "required": ["location"] + } + } + } + ], + "tool_choice": "auto", + "stream": true +}' +``` + + + ## Using ollama `api/chat` In order to send ollama requests to `POST /api/chat` on your ollama server, set the model prefix to `ollama_chat` diff --git a/litellm/model_prices_and_context_window_backup.json b/litellm/model_prices_and_context_window_backup.json index d4985bffd4c..2689d0566e4 100644 --- a/litellm/model_prices_and_context_window_backup.json +++ b/litellm/model_prices_and_context_window_backup.json @@ -3956,6 +3956,16 @@ "litellm_provider": "ollama", "mode": "chat" }, + "ollama/llama3.1": { + "max_tokens": 8192, + "max_input_tokens": 8192, + "max_output_tokens": 8192, + "input_cost_per_token": 0.0, + "output_cost_per_token": 0.0, + "litellm_provider": "ollama", + "mode": "chat", + "supports_function_calling": true + }, "ollama/mistral": { "max_tokens": 8192, "max_input_tokens": 8192, diff --git a/litellm/proxy/_new_secret_config.yaml b/litellm/proxy/_new_secret_config.yaml index 240c3d436bc..34bf7d89a30 100644 --- a/litellm/proxy/_new_secret_config.yaml +++ b/litellm/proxy/_new_secret_config.yaml @@ -1,5 +1,5 @@ model_list: - - model_name: "mistral" + - model_name: "llama3.1" litellm_params: model: "ollama_chat/llama3.1" model_info: diff --git a/model_prices_and_context_window.json b/model_prices_and_context_window.json index d4985bffd4c..2689d0566e4 100644 --- a/model_prices_and_context_window.json +++ b/model_prices_and_context_window.json @@ -3956,6 +3956,16 @@ "litellm_provider": "ollama", "mode": "chat" }, + "ollama/llama3.1": { + "max_tokens": 8192, + "max_input_tokens": 8192, + "max_output_tokens": 8192, + "input_cost_per_token": 0.0, + "output_cost_per_token": 0.0, + "litellm_provider": "ollama", + "mode": "chat", + "supports_function_calling": true + }, "ollama/mistral": { "max_tokens": 8192, "max_input_tokens": 8192,