diff --git a/litellm/llms/prompt_templates/factory.py b/litellm/llms/prompt_templates/factory.py index 7864d5ebc8..e359d36f4c 100644 --- a/litellm/llms/prompt_templates/factory.py +++ b/litellm/llms/prompt_templates/factory.py @@ -135,7 +135,7 @@ def convert_to_ollama_image(openai_image_url: str): def ollama_pt( - model, messages + model, messages ): # https://github.com/ollama/ollama/blob/af4cf55884ac54b9e637cd71dadfe9b7a5685877/docs/modelfile.md#template if "instruct" in model: prompt = custom_prompt( @@ -178,12 +178,27 @@ def ollama_pt( content = message.get("content", "") if "tool_calls" in message: + tool_calls = [] + for call in message["tool_calls"]: - function_name = call["function"]["name"] + call_id: str = call["id"] + function_name: str = call["function"]["name"] arguments = json.loads(call["function"]["arguments"]) - prompt += f"### FunctionCall ({call["id"]}):\nFunctionName: {function_name}\nArguments: {json.dumps(arguments)}\n\n" + + tool_calls.append({ + "id": call_id, + "type": "function", + "function": { + "name": function_name, + "arguments": arguments + } + }) + + prompt += f"### Assistant:\nTool Calls: {json.dumps(tool_calls, indent=2)}\n\n" + elif "tool_call_id" in message: - prompt += f"### FunctionCall Result ({message["tool_call_id"]}):\n{message["content"]}\n\n" + prompt += f"### User:\n{message["content"]}\n\n" + elif content: prompt += f"### {role.capitalize()}:\n{content}\n\n"