監控,為服務保駕護航,也為業務量化提供有力支持,當然在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": {
"draw style": "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
},
"thresholds style": {
"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": {
"draw style": "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
},
"thresholds style": {
"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": {
"draw style": "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
},
"thresholds style": {
"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"
}
最後結果如下圖: