{
  "$schema": "./.schemas/page.schema.json",
  "title": "Model",
  "icon": "cube",
  "sidebar": false,
  "params": {
    "digest": {
      "example": "7df6b6e09427a769808717c0a93cadc4ae99ed4eb8bf5ca557c90846becea435",
      "pattern": "[0-9a-f]{64}"
    }
  },
  "title_from": {
    "ref": "ollama",
    "value": {
      "first": "name",
      "of": "tags.models",
      "where": {
        "path": "digest",
        "equals": "{param.digest}"
      }
    }
  },
  "sources": {
    "ollama": {
      "url": "http://localhost:11434",
      "every": "1m",
      "requests": {
        "ps": "/api/ps",
        "tags": "/api/tags"
      },
      "derive": [
        {
          "name": "found",
          "value": {
            "each": "tags.models",
            "where": {
              "path": "digest",
              "equals": "{param.digest}"
            },
            "limit": 1,
            "fields": {
              "name": "name",
              "caps": {
                "join": "$",
                "of": "capabilities",
                "default": ""
              },
              "size": "size",
              "modified_at": "modified_at",
              "capabilities": "capabilities",
              "context_length": "details.context_length",
              "parameter_size": "details.parameter_size",
              "quantization_level": "details.quantization_level",
              "family": "details.family"
            }
          }
        },
        {
          "name": "runs",
          "value": {
            "each": "ps.models",
            "where": {
              "path": "digest",
              "equals": "{param.digest}"
            },
            "limit": 1,
            "fields": {
              "name": "name",
              "digest": "digest",
              "short": {
                "path": "name",
                "match": "^(.*?)(?::latest)?$"
              },
              "size": "size",
              "size_vram": {
                "path": "size_vram",
                "default": 0
              },
              "context": {
                "path": "context_length",
                "default": 0
              },
              "cpu": {
                "divide": [
                  {
                    "subtract": [
                      "size",
                      {
                        "path": "size_vram",
                        "default": 0
                      }
                    ]
                  },
                  "size"
                ],
                "scale": 100,
                "round": 0
              },
              "gpu": {
                "subtract": [
                  100,
                  {
                    "divide": [
                      {
                        "subtract": [
                          "size",
                          {
                            "path": "size_vram",
                            "default": 0
                          }
                        ]
                      },
                      "size"
                    ],
                    "scale": 100,
                    "round": 0
                  }
                ]
              },
              "left": {
                "since": "expires_at",
                "scale": -1
              },
              "expires_at": "expires_at"
            }
          }
        },
        {
          "name": "running",
          "value": {
            "each": "runs",
            "fields": {
              "name": "name",
              "digest": "digest",
              "short": "short",
              "size": "size",
              "context": "context",
              "where": {
                "if": {
                  "path": "size_vram",
                  "equals": 0
                },
                "then": {
                  "template": "100% CPU"
                },
                "else": {
                  "if": {
                    "path": "size_vram",
                    "gte": {
                      "path": "size"
                    }
                  },
                  "then": {
                    "template": ""
                  },
                  "else": {
                    "template": "{cpu}%/{gpu}% CPU/GPU"
                  }
                }
              },
              "cpu": "cpu",
              "gpu": "gpu",
              "until": {
                "if": {
                  "path": "left",
                  "gt": 31536000
                },
                "then": {
                  "template": "stays loaded"
                },
                "else": {
                  "if": {
                    "path": "left",
                    "gt": 0
                  },
                  "then": {
                    "template": "unloads {expires_at|in}"
                  },
                  "else": {
                    "template": "unloading"
                  }
                }
              }
            }
          }
        },
        {
          "name": "name",
          "value": {
            "first": "name",
            "of": "found"
          }
        },
        {
          "name": "caps",
          "value": {
            "first": "caps",
            "of": "found"
          }
        },
        {
          "name": "found_count",
          "value": {
            "count": "found"
          }
        },
        {
          "name": "running_count",
          "value": {
            "count": "running"
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "model",
      "type": "facts",
      "items": [
        {
          "key": "In memory",
          "value": "—"
        },
        {
          "key": "Capabilities",
          "value": "—"
        },
        {
          "key": "Context",
          "value": "—"
        },
        {
          "key": "Parameters",
          "value": "—"
        },
        {
          "key": "Quantization",
          "value": "—"
        },
        {
          "key": "On disk",
          "value": "—"
        },
        {
          "key": "Family",
          "value": "—"
        },
        {
          "key": "Updated",
          "value": "—"
        }
      ],
      "source": {
        "ref": "ollama",
        "empty": {
          "when": {
            "path": "found_count",
            "equals": 0
          },
          "text": "This model isn't on this Ollama now"
        },
        "map": {
          "items[0].value": {
            "if": {
              "path": "running_count",
              "gt": 0
            },
            "then": {
              "if": {
                "path": "running[0].where",
                "equals": ""
              },
              "then": {
                "template": "{running[0].size|bytes} · 100% GPU · {running[0].until}"
              },
              "else": {
                "template": "{running[0].size|bytes} · {running[0].where} · {running[0].until}"
              }
            },
            "else": {
              "template": "Not loaded"
            }
          },
          "items[1].value": {
            "join": "$",
            "of": "found[0].capabilities",
            "separator": ", ",
            "default": "—"
          },
          "items[2].value": {
            "if": {
              "path": "running[0].context",
              "gt": 0
            },
            "then": {
              "template": "{running[0].context} loaded, up to {found[0].context_length}"
            },
            "else": {
              "if": {
                "path": "found[0].context_length",
                "exists": true
              },
              "then": {
                "template": "up to {found[0].context_length}"
              },
              "else": {
                "template": "—"
              }
            }
          },
          "items[3].value": {
            "path": "found[0].parameter_size",
            "default": "—"
          },
          "items[4].value": {
            "path": "found[0].quantization_level",
            "default": "—"
          },
          "items[5].value": {
            "template": "{found[0].size|bytes}"
          },
          "items[6].value": {
            "path": "found[0].family",
            "default": "—"
          },
          "items[7].value": {
            "template": "{found[0].modified_at|ago}"
          }
        }
      }
    },
    {
      "id": "controls",
      "type": "text",
      "label": "Model",
      "source": {
        "ref": "ollama",
        "when": {
          "path": "found_count",
          "gt": 0
        }
      },
      "actions_placement": "corner",
      "actions": [
        {
          "label": "Load",
          "icon": "play.fill",
          "path": "/api/generate",
          "body": {
            "model": "{answer.name}"
          },
          "timeout": 300,
          "when": {
            "all": [
              {
                "path": "running_count",
                "equals": 0
              },
              {
                "path": "caps",
                "not": "embedding"
              }
            ]
          }
        },
        {
          "label": "Load",
          "icon": "play.fill",
          "path": "/api/embed",
          "body": {
            "model": "{answer.name}",
            "input": []
          },
          "timeout": 300,
          "when": {
            "all": [
              {
                "path": "running_count",
                "equals": 0
              },
              {
                "path": "caps",
                "equals": "embedding"
              }
            ]
          }
        },
        {
          "label": "Unload",
          "icon": "eject",
          "path": "/api/generate",
          "body": {
            "model": "{answer.running[0].name}",
            "keep_alive": 0
          },
          "when": {
            "path": "running_count",
            "gt": 0
          }
        },
        "|",
        {
          "label": "Delete model",
          "icon": "trash",
          "method": "DELETE",
          "path": "/api/delete",
          "body": {
            "model": "{answer.name}"
          },
          "confirm": {
            "message": "Delete {answer.name} from this Ollama? Using it again means downloading it again.",
            "button": "Delete"
          }
        }
      ]
    }
  ]
}
