SemanticKernel之使用监控
监控,为服务保驾护航,也为业务量化提供有力支持,当然在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"
}
最后结果如下图:
文章来源微信公众号
想要更快更方便的了解相关知识,可以关注微信公众号
分类:
.net
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2019-02-27 WHERE 子句操作符
2019-02-27 不同的数据库查询行数的方式