SemanticKernelの監視の使用


監視は、サービスを保護するだけでなく、ビジネスの定量化にも強力なサポートを提供します。もちろん、SemanticKernelでの監視も例外ではありません。また、実際の金銭的価値のあるTokenの定量化監視も提供できます。

プロジェクトで使用されるNugetパッケージは以下の通りです：

```xml
<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監視プラグインの追加：

```typescript
var meterProvider = Sdk.CreateMeterProviderBuilder()
    .AddMeter("Microsoft.SemanticKernel*")
    .AddPrometheusHttpListener(options => options.UriPrefixes = new string[] { "http://localhost:9465/" })
    .Build();
```

次に、Prometheusをダウンロードし、prometheus.yml設定を以下のように変更します：

```typescript
scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]
  - job_name: "AI"
    scrape_interval: 1s # デモをより反応的にするために非常に迅速にポーリング
    static_configs:
      - targets: ["localhost:9465"]
```

次に、Grafanaをダウンロードし、データソースとしてPrometheusを設定し、それぞれの表示を設定します：

左上角の線形グラフのjsonは以下の通りです:

```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は以下の通りです:

```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は以下の通りです:
```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は以下の通りです:
```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"
}
```

結果は以下の図に示されます：

![図](https://mmbiz.qpic.cn/sz_mmbiz_png/4CAA63nvHRAoUIZub5yC5BRcnUCA4flpFk8ZRnHLvUF27gOyqoNmck8aBicwz84pTkZnBT5PhStA8zt6ZgjmhHQ/640?wx_fmt=png&from=appmsg)

(Translated by GPT)

元のリンク：https://mp.weixin.qq.com/s?__biz=MzA3NDM1MzIyMQ==&mid=2247488087&idx=1&sn=f84bb66f681a47b063c5c4a831898c4d&chksm=9f004d7da877c46b217cc9f602822a38970b1e21e451e3ef9c39faec4f842a5ffc5148c77a9f&token=1106494811&lang=zh_CN#rd&wt.mc_id=MVP_325642