#!/usr/bin/env python3
"""
Creates an Ollama Modelfile that uses the huihui abliterated weights
with a proper tool-calling template for qwen3-coder.
Run on Gaming PC: python3 create-modelfile.py
Then: ollama create qwen3-coder-abliterated -f Modelfile
"""

modelfile = r'''FROM huihui_ai/qwen3-coder-abliterated:30b-a3b-instruct-q3_K_M

PARAMETER temperature 0.7
PARAMETER top_p 0.8
PARAMETER top_k 20
PARAMETER repeat_penalty 1.05
PARAMETER num_ctx 65536

TEMPLATE """
{{- /* Extract system message and other messages */ -}}
{{- $system_message := "" -}}
{{- $loop_messages := .Messages -}}
{{- if and .Messages (gt (len .Messages) 0) (eq (index .Messages 0).Role "system") -}}
{{- $system_message = (index .Messages 0).Content -}}
{{- $loop_messages = slice .Messages 1 -}}
{{- end -}}

{{- /* Handle tools if they exist */ -}}
{{- $has_tools := and .Tools (gt (len .Tools) 0) -}}

{{- /* System message */ -}}
{{- if $system_message -}}
<|im_start|>system
{{ $system_message }}
{{- else if $has_tools -}}
<|im_start|>system
You are a helpful AI assistant that can interact with a computer to solve tasks.
{{- end -}}

{{- /* Add XML-style tools section if tools exist */ -}}
{{- if $has_tools -}}

You have access to the following functions:

<tools>
{{- range .Tools -}}
{{- $tool := . -}}
{{- if .Function -}}
{{- $tool = .Function -}}
{{- end }}
<function>
<name>{{ $tool.Name }}</name>
<description>{{ $tool.Description }}</description>
<parameters>
{{- if $tool.Parameters -}}
{{- if $tool.Parameters.properties -}}
{{- range $param_name, $param_fields := $tool.Parameters.properties }}
<parameter>
<name>{{ $param_name }}</name>
{{- if $param_fields.type }}
<type>{{ $param_fields.type }}</type>
{{- end }}
{{- if $param_fields.description }}
<description>{{ $param_fields.description }}</description>
{{- end }}
{{- if $param_fields.enum }}
<enum>[{{ range $i, $val := $param_fields.enum }}{{ if gt $i 0 }}, {{ end }}`{{ $val }}`{{ end }}]</enum>
{{- end }}
</parameter>
{{- end -}}
{{- end -}}
{{- if $tool.Parameters.required }}
<required>[{{ range $i, $req := $tool.Parameters.required }}{{ if gt $i 0 }}, {{ end }}`{{ $req }}`{{ end }}]</required>
{{- end -}}
{{- end }}
</parameters>
</function>
{{- end }}
</tools>

If you choose to call a function ONLY reply in the following format with NO suffix:

<tool_call>
<function=example_function_name>
<parameter=example_parameter_1>
value_1
</parameter>
<parameter=example_parameter_2>
This is the value for the second parameter
that can span
multiple lines
</parameter>
</function>
</tool_call>

<IMPORTANT>
Reminder:
- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags
- Required parameters MUST be specified
- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after
- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls
</IMPORTANT>
{{- end -}}

{{- /* Close system message if it was opened */ -}}
{{- if or $system_message $has_tools -}}
<|im_end|>
{{- end -}}

{{- /* Process conversation messages */ -}}
{{- $in_tool_block := false -}}
{{- range $i, $message := $loop_messages -}}
{{- if eq $message.Role "tool" -}}
{{- if not $in_tool_block -}}
<|im_start|>user
{{- $in_tool_block = true -}}
{{- end }}
<tool_response>
{{ $message.Content }}
</tool_response>
{{- else if eq $message.Role "assistant" -}}
{{- if $in_tool_block -}}
<|im_end|>
{{- $in_tool_block = false -}}
{{- end }}
<|im_start|>assistant
{{- if $message.Content }}
{{ $message.Content }}
{{- end }}
{{- range $message.ToolCalls -}}
{{- $tool_call := . -}}
{{- if .Function -}}
{{- $tool_call = .Function -}}
{{- end }}

<tool_call>
<function={{ $tool_call.Name }}>
{{- if $tool_call.Arguments -}}
{{- range $args_name, $args_value := $tool_call.Arguments }}
<parameter={{ $args_name }}>
{{ $args_value }}
</parameter>
{{- end -}}
{{- end }}
</function>
</tool_call>
{{- end }}
<|im_end|>
{{- else -}}
{{- if $in_tool_block -}}
<|im_end|>
{{- $in_tool_block = false -}}
{{- end }}
<|im_start|>{{ $message.Role }}
{{ $message.Content }}<|im_end|>
{{- end -}}
{{- end -}}

<|im_start|>assistant
"""
'''

with open("Modelfile", "w", encoding="utf-8") as f:
    f.write(modelfile)

print("Modelfile created successfully!")
print("Now run: ollama create qwen3-coder-abliterated -f Modelfile")
