• SemanticKernel中的监控
  • 发布于 1个月前
  • 167 热度
    0 评论
  • 怪性酷
  • 1 粉丝 27 篇博客
  •   
监控,为服务保驾护航,也为业务量化提供有力支持,当然在SemanticKernel中的监控也不例外,同时还能为真金白银的Token提供量化监控。
下面是项目中使用的Nuget包:
<ItemGroup>
  <PackageReference Include="Microsoft.SemanticKernel" Version="1.7.1" />
  <PackageReference Include="NAudio" Version="2.2.1" />
  <PackageReference Include="OpenTelemetry" Version="1.8.1" />
  <PackageReference Include="OpenTelemetry.Exporter.Prometheus.HttpListener" Version="1.8.0-rc.1" />
  <PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.8.1" />
</ItemGroup>
添加PrometheusHttpListener监控插件:
 var meterProvider = Sdk.CreateMeterProviderBuilder()
     .AddMeter("Microsoft.SemanticKernel*")
     .AddPrometheusHttpListener(options => options.UriPrefixes = new string[] { "http://localhost:9465/" })
     .Build();
接下来就是下载Prometheus,修改prometheus.yml配置如下:
scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]      
  - job_name: "AI"
    scrape_interval: 1s # poll very quickly for a more responsive demo
    static_configs:
      - targets: ["localhost:9465"]
然后再下载Grafana,配置数据源为Prometheus,然后分别配置展示:
左上角线型图json如下:
{
  "datasource": {
    "uid": "b493cf27-810d-4b67-aa35-aa7da3af9454",
    "type": "prometheus"
  },
  "fieldConfig": {
    "defaults": {
      "custom": {
        "drawStyle": "line",
        "lineInterpolation": "linear",
        "barAlignment": 0,
        "lineWidth": 1,
        "fillOpacity": 0,
        "gradientMode": "none",
        "spanNulls": false,
        "insertNulls": false,
        "showPoints": "auto",
        "pointSize": 5,
        "stacking": {
          "mode": "none",
          "group": "A"
        },
        "axisPlacement": "auto",
        "axisLabel": "",
        "axisColorMode": "text",
        "axisBorderShow": false,
        "scaleDistribution": {
          "type": "linear"
        },
        "axisCenteredZero": false,
        "hideFrom": {
          "tooltip": false,
          "viz": false,
          "legend": false
        },
        "thresholdsStyle": {
          "mode": "off"
        }
      },
      "unitScale": true,
      "color": {
        "mode": "palette-classic"
      },
      "mappings": [],
      "thresholds": {
        "mode": "absolute",
        "steps": [
          {
            "color": "green",
            "value": null
          },
          {
            "color": "red",
            "value": 80
          }
        ]
      }
    },
    "overrides": []
  },
  "gridPos": {
    "h": 8,
    "w": 19,
    "x": 0,
    "y": 0
  },
  "id": 2,
  "options": {
    "tooltip": {
      "mode": "single",
      "sort": "none"
    },
    "legend": {
      "showLegend": true,
      "displayMode": "list",
      "placement": "bottom",
      "calcs": []
    }
  },
  "targets": [
    {
      "datasource": {
        "type": "prometheus",
        "uid": "b493cf27-810d-4b67-aa35-aa7da3af9454"
      },
      "disableTextWrap": false,
      "editorMode": "code",
      "expr": "semantic_kernel_connectors_openai_tokens_prompt_total{instance=\"localhost:9465\"}",
      "fullMetaSearch": false,
      "includeNullMetadata": true,
      "instant": false,
      "interval": "",
      "legendFormat": "总Token数",
      "range": true,
      "refId": "A",
      "useBackend": false
    }
  ],
  "title": "Tokens",
  "type": "timeseries"
}
右上角饼型图json如下:
{
  "datasource": {
    "type": "prometheus",
    "uid": "b493cf27-810d-4b67-aa35-aa7da3af9454"
  },
  "fieldConfig": {
    "defaults": {
      "custom": {
        "hideFrom": {
          "tooltip": false,
          "viz": false,
          "legend": false
        }
      },
      "unitScale": true,
      "color": {
        "mode": "palette-classic"
      },
      "mappings": []
    },
    "overrides": []
  },
  "gridPos": {
    "h": 8,
    "w": 5,
    "x": 19,
    "y": 0
  },
  "id": 4,
  "options": {
    "reduceOptions": {
      "values": false,
      "calcs": [
        "lastNotNull"
      ],
      "fields": ""
    },
    "pieType": "pie",
    "tooltip": {
      "mode": "single",
      "sort": "none"
    },
    "legend": {
      "showLegend": true,
      "displayMode": "list",
      "placement": "bottom"
    }
  },
  "targets": [
    {
      "datasource": {
        "type": "prometheus",
        "uid": "b493cf27-810d-4b67-aa35-aa7da3af9454"
      },
      "disableTextWrap": false,
      "editorMode": "builder",
      "expr": "semantic_kernel_connectors_openai_tokens_prompt_total{instance=\"localhost:9465\"}",
      "fullMetaSearch": false,
      "hide": false,
      "includeNullMetadata": false,
      "instant": false,
      "legendFormat": "提示Token数",
      "range": true,
      "refId": "Prompt",
      "useBackend": false
    },
    {
      "datasource": {
        "type": "prometheus",
        "uid": "b493cf27-810d-4b67-aa35-aa7da3af9454"
      },
      "disableTextWrap": false,
      "editorMode": "builder",
      "expr": "semantic_kernel_connectors_openai_tokens_completion_total{instance=\"localhost:9465\"}",
      "fullMetaSearch": false,
      "hide": false,
      "includeNullMetadata": true,
      "instant": false,
      "legendFormat": "回复Token数",
      "range": true,
      "refId": "Completion",
      "useBackend": false
    }
  ],
  "title": "Tokens Rate",
  "type": "piechart"
}
左下角线型图json如下:
{
  "datasource": {
    "type": "prometheus",
    "uid": "b493cf27-810d-4b67-aa35-aa7da3af9454"
  },
  "fieldConfig": {
    "defaults": {
      "custom": {
        "drawStyle": "line",
        "lineInterpolation": "linear",
        "barAlignment": 0,
        "lineWidth": 1,
        "fillOpacity": 0,
        "gradientMode": "none",
        "spanNulls": false,
        "insertNulls": false,
        "showPoints": "auto",
        "pointSize": 5,
        "stacking": {
          "mode": "none",
          "group": "A"
        },
        "axisPlacement": "auto",
        "axisLabel": "",
        "axisColorMode": "text",
        "axisBorderShow": false,
        "scaleDistribution": {
          "type": "linear"
        },
        "axisCenteredZero": false,
        "hideFrom": {
          "tooltip": false,
          "viz": false,
          "legend": false
        },
        "thresholdsStyle": {
          "mode": "off"
        }
      },
      "unitScale": true,
      "color": {
        "mode": "palette-classic"
      },
      "mappings": [],
      "thresholds": {
        "mode": "absolute",
        "steps": [
          {
            "color": "green",
            "value": null
          },
          {
            "color": "red",
            "value": 80
          }
        ]
      }
    },
    "overrides": []
  },
  "gridPos": {
    "h": 8,
    "w": 12,
    "x": 0,
    "y": 8
  },
  "id": 1,
  "options": {
    "tooltip": {
      "mode": "single",
      "sort": "none"
    },
    "legend": {
      "showLegend": true,
      "displayMode": "list",
      "placement": "bottom",
      "calcs": []
    }
  },
  "targets": [
    {
      "datasource": {
        "type": "prometheus",
        "uid": "b493cf27-810d-4b67-aa35-aa7da3af9454"
      },
      "disableTextWrap": false,
      "editorMode": "builder",
      "expr": "semantic_kernel_connectors_openai_tokens_total{instance=\"localhost:9465\"}",
      "fullMetaSearch": false,
      "includeNullMetadata": true,
      "instant": false,
      "legendFormat": "提示Toekn数",
      "range": true,
      "refId": "A",
      "useBackend": false
    }
  ],
  "title": "Prompt Tokens",
  "type": "timeseries"
}
右下角线型图json如下:
{
  "datasource": {
    "type": "prometheus",
    "uid": "b493cf27-810d-4b67-aa35-aa7da3af9454"
  },
  "fieldConfig": {
    "defaults": {
      "custom": {
        "drawStyle": "line",
        "lineInterpolation": "linear",
        "barAlignment": 0,
        "lineWidth": 1,
        "fillOpacity": 0,
        "gradientMode": "none",
        "spanNulls": false,
        "insertNulls": false,
        "showPoints": "auto",
        "pointSize": 5,
        "stacking": {
          "mode": "none",
          "group": "A"
        },
        "axisPlacement": "auto",
        "axisLabel": "",
        "axisColorMode": "text",
        "axisBorderShow": false,
        "scaleDistribution": {
          "type": "linear"
        },
        "axisCenteredZero": false,
        "hideFrom": {
          "tooltip": false,
          "viz": false,
          "legend": false
        },
        "thresholdsStyle": {
          "mode": "off"
        }
      },
      "unitScale": true,
      "color": {
        "mode": "palette-classic"
      },
      "mappings": [],
      "thresholds": {
        "mode": "absolute",
        "steps": [
          {
            "color": "green",
            "value": null
          },
          {
            "color": "red",
            "value": 80
          }
        ]
      }
    },
    "overrides": []
  },
  "gridPos": {
    "h": 8,
    "w": 12,
    "x": 12,
    "y": 8
  },
  "id": 3,
  "options": {
    "tooltip": {
      "mode": "single",
      "sort": "none"
    },
    "legend": {
      "showLegend": true,
      "displayMode": "list",
      "placement": "bottom",
      "calcs": []
    }
  },
  "targets": [
    {
      "datasource": {
        "type": "prometheus",
        "uid": "b493cf27-810d-4b67-aa35-aa7da3af9454"
      },
      "disableTextWrap": false,
      "editorMode": "builder",
      "expr": "semantic_kernel_connectors_openai_tokens_completion_total{instance=\"localhost:9465\"}",
      "fullMetaSearch": false,
      "includeNullMetadata": true,
      "instant": false,
      "legendFormat": "回复Token数",
      "range": true,
      "refId": "A",
      "useBackend": false
    }
  ],
  "title": "Completion Tokens",
  "type": "timeseries"
}

最后结果如下图:

用户评论