复制模型 (POST /api/copy)

复制模型接口可以快速创建现有模型的副本,适用于创建备份或测试版本。

基本用法

curl http://localhost:11434/api/copy -d '{
  "source": "llama3.2",
  "destination": "llama3.2-backup"
}'

成功时返回 200 状态码,无响应体。

请求参数

参数类型必需说明
sourcestring源模型名称
destinationstring目标模型名称

代码示例

Python

import requests

def copy_model(source, destination):
    response = requests.post(
        "http://localhost:11434/api/copy",
        json={
            "source": source,
            "destination": destination
        }
    )
    
    if response.status_code == 200:
        print(f"模型已复制: {source} -> {destination}")
    else:
        print(f"复制失败: {response.json()}")

copy_model("llama3.2", "llama3.2-backup")

批量复制

def backup_models(suffix="-backup"):
    models = requests.get("http://localhost:11434/api/tags").json()["models"]
    
    for model in models:
        source = model["name"]
        if not source.endswith(suffix):
            destination = source.split(":")[0] + suffix
            copy_model(source, destination)

backup_models()

JavaScript

async function copyModel(source, destination) {
    const response = await fetch('http://localhost:11434/api/copy', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ source, destination })
    });
    
    if (response.ok) {
        console.log(`模型已复制: ${source} -> ${destination}`);
    } else {
        const error = await response.json();
        console.error(`复制失败:`, error);
    }
}

await copyModel('llama3.2', 'llama3.2-backup');

Go

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "net/http"
)

type CopyRequest struct {
    Source      string `json:"source"`
    Destination string `json:"destination"`
}

func copyModel(source, destination string) error {
    req := CopyRequest{
        Source:      source,
        Destination: destination,
    }
    
    body, _ := json.Marshal(req)
    resp, err := http.Post(
        "http://localhost:11434/api/copy",
        "application/json",
        bytes.NewReader(body),
    )
    if err != nil {
        return err
    }
    defer resp.Body.Close()
    
    if resp.StatusCode == 200 {
        fmt.Printf("模型已复制: %s -> %s\n", source, destination)
    }
    
    return nil
}

func main() {
    copyModel("llama3.2", "llama3.2-backup")
}

实际应用

创建测试版本

def create_test_version(model_name):
    test_name = f"{model_name}-test"
    copy_model(model_name, test_name)
    
    response = requests.post(
        "http://localhost:11434/api/create",
        json={
            "name": test_name,
            "modelfile": f"""
FROM {model_name}
SYSTEM 你是一个测试助手,用于测试新的系统提示。
PARAMETER temperature 0.5
"""
        }
    )
    
    print(f"测试版本已创建: {test_name}")

create_test_version("llama3.2")

版本管理

import datetime

def create_version(model_name, version_note=""):
    date_str = datetime.datetime.now().strftime("%Y%m%d")
    version_name = f"{model_name}-v{date_str}"
    
    copy_model(model_name, version_name)
    
    print(f"版本已创建: {version_name}")
    if version_note:
        print(f"备注: {version_note}")

create_version("llama3.2", "修改系统提示前的备份")

注意事项

  1. 源模型必须存在:否则返回 404 错误
  2. 目标名称不能重复:如果目标模型已存在,会被覆盖
  3. 共享模型文件:复制只是创建新的引用,不占用额外磁盘空间
  4. 修改互不影响:修改复制的模型不会影响原模型