0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

SemanticKernelの監視の使用

Posted at

監視は、サービスを保護するだけでなく、ビジネスの定量化にも強力なサポートを提供します。もちろん、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 # デモをより反応的にするために非常に迅速にポーリング
    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"
}

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

図

(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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?