Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions BotSharp.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.6.33712.159
# Visual Studio Version 18
VisualStudioVersion = 18.0.11205.157 d18.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Infrastructure", "Infrastructure", "{E29DC6C4-5E57-48C5-BCB0-6B8F84782749}"
EndProject
Expand Down Expand Up @@ -149,8 +149,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.ExcelHandle
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.GiteeAI", "src\Plugins\BotSharp.Plugin.GiteeAI\BotSharp.Plugin.GiteeAI.csproj", "{50B57066-3267-1D10-0F72-D2F5CC494F2C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.ImageHandler", "src\Plugins\BotSharp.Plugin.ImageHandler\BotSharp.Plugin.ImageHandler.csproj", "{242F2D93-FCCE-4982-8075-F3052ECCA92C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.FuzzySharp", "src\Plugins\BotSharp.Plugin.FuzzySharp\BotSharp.Plugin.FuzzySharp.csproj", "{E7C243B9-E751-B3B4-8F16-95C76CA90D31}"
EndProject
Global
Expand Down Expand Up @@ -633,14 +631,6 @@ Global
{50B57066-3267-1D10-0F72-D2F5CC494F2C}.Release|Any CPU.Build.0 = Release|Any CPU
{50B57066-3267-1D10-0F72-D2F5CC494F2C}.Release|x64.ActiveCfg = Release|Any CPU
{50B57066-3267-1D10-0F72-D2F5CC494F2C}.Release|x64.Build.0 = Release|Any CPU
{242F2D93-FCCE-4982-8075-F3052ECCA92C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{242F2D93-FCCE-4982-8075-F3052ECCA92C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{242F2D93-FCCE-4982-8075-F3052ECCA92C}.Debug|x64.ActiveCfg = Debug|Any CPU
{242F2D93-FCCE-4982-8075-F3052ECCA92C}.Debug|x64.Build.0 = Debug|Any CPU
{242F2D93-FCCE-4982-8075-F3052ECCA92C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{242F2D93-FCCE-4982-8075-F3052ECCA92C}.Release|Any CPU.Build.0 = Release|Any CPU
{242F2D93-FCCE-4982-8075-F3052ECCA92C}.Release|x64.ActiveCfg = Release|Any CPU
{242F2D93-FCCE-4982-8075-F3052ECCA92C}.Release|x64.Build.0 = Release|Any CPU
{E7C243B9-E751-B3B4-8F16-95C76CA90D31}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E7C243B9-E751-B3B4-8F16-95C76CA90D31}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E7C243B9-E751-B3B4-8F16-95C76CA90D31}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -721,7 +711,6 @@ Global
{0428DEAA-E4FE-4259-A6D8-6EDD1A9D0702} = {51AFE054-AE99-497D-A593-69BAEFB5106F}
{FC63C875-E880-D8BB-B8B5-978AB7B62983} = {51AFE054-AE99-497D-A593-69BAEFB5106F}
{50B57066-3267-1D10-0F72-D2F5CC494F2C} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
{242F2D93-FCCE-4982-8075-F3052ECCA92C} = {51AFE054-AE99-497D-A593-69BAEFB5106F}
{E7C243B9-E751-B3B4-8F16-95C76CA90D31} = {51AFE054-AE99-497D-A593-69BAEFB5106F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
Expand Down
17 changes: 12 additions & 5 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="DeviceId" Version="6.9.0" />
<PackageVersion Include="DeviceId.Linux" Version="6.9.0" />
<PackageVersion Include="DeviceId.Mac" Version="6.9.0" />
<PackageVersion Include="DeviceId.Windows" Version="6.9.0" />
<PackageVersion Include="DeviceId.Windows.Wmi" Version="6.9.0" />
<PackageVersion Include="CsvHelper" Version="33.1.0" />
<PackageVersion Include="FuzzySharp" Version="2.0.2" />
<PackageVersion Include="EntityFramework" Version="6.4.4" />
Expand All @@ -27,8 +32,8 @@
<PackageVersion Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="8.14.0" />
<PackageVersion Include="System.Memory.Data" Version="8.0.0" />
<PackageVersion Include="System.Text.Json" Version="8.0.5" />
<PackageVersion Include="System.Reactive" Version="6.0.1" />
<PackageVersion Include="System.Text.Json" Version="9.0.11" />
<PackageVersion Include="System.Reactive" Version="6.0.1" />
<PackageVersion Include="Serilog.Sinks.Console" Version="6.1.1" />
<PackageVersion Include="Serilog.Extensions.Logging" Version="9.0.2" />
<PackageVersion Include="Serilog.Sinks.File" Version="7.0.0" />
Expand All @@ -48,7 +53,7 @@
<PackageVersion Include="Whisper.net.Runtime" Version="1.8.1" />
<PackageVersion Include="NCrontab" Version="3.3.3" />
<PackageVersion Include="Azure.AI.OpenAI" Version="2.5.0-beta.1" />
<PackageVersion Include="OpenAI" Version="2.6.0" />
<PackageVersion Include="OpenAI" Version="2.5.0" />
<PackageVersion Include="MailKit" Version="4.14.1" />
<PackageVersion Include="Microsoft.Data.Sqlite" Version="10.0.0" />
<PackageVersion Include="MySql.Data" Version="9.5.0" />
Expand Down Expand Up @@ -117,8 +122,10 @@
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="10.0.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="10.0.0" />
<PackageVersion Include="Shouldly" Version="4.3.0" />
<PackageVersion Include="ModelContextProtocol" Version="0.1.0-preview.11" />
<PackageVersion Include="ModelContextProtocol.AspNetCore" Version="0.1.0-preview.11" />
<PackageVersion Include="ModelContextProtocol.Core" Version="0.4.0-preview.3" />
<PackageVersion Include="ModelContextProtocol" Version="0.4.0-preview.3" />
<PackageVersion Include="ModelContextProtocol.AspNetCore" Version="0.4.0-preview.3" />
<PackageVersion Include="Langfuse.OpenTelemetry" Version="1.0.0" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="BotSharp.Core" Version="$(BotSharpVersion)" />
Expand Down
2 changes: 2 additions & 0 deletions src/BotSharp.AppHost/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"applicationUrl": "https://localhost:17248;http://localhost:15140",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPIRE_ALLOW_UNSECURED_TRANSPORT": "true",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21247",
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22140"
Expand All @@ -20,6 +21,7 @@
"applicationUrl": "http://localhost:15140",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPIRE_ALLOW_UNSECURED_TRANSPORT": "true",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19185",
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20069"
Expand Down
3 changes: 2 additions & 1 deletion src/BotSharp.ServiceDefaults/BotSharp.ServiceDefaults.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -20,6 +20,7 @@
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" />
<PackageReference Include="Langfuse.OpenTelemetry" />
</ItemGroup>

</Project>
49 changes: 26 additions & 23 deletions src/BotSharp.ServiceDefaults/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BotSharp.Langfuse;
using Langfuse.OpenTelemetry;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.Configuration;
Expand Down Expand Up @@ -100,10 +101,11 @@ public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicati
.AddService("apiservice", serviceVersion: "1.0.0")
)
.AddSource("BotSharp")
.AddSource("BotSharp.Server")
.AddSource("BotSharp.Abstraction.Diagnostics")
.AddSource("BotSharp.Core.Routing.Executor");

tracing.AddAspNetCoreInstrumentation()
.AddSource("BotSharp.Core.Routing.Executor")
.AddLangfuseExporter(builder.Configuration)
.AddAspNetCoreInstrumentation()
// Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package)
//.AddGrpcClientInstrumentation()
.AddHttpClientInstrumentation()
Expand All @@ -115,7 +117,7 @@ public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicati
// options.Headers = $"Authorization=Basic {base64EncodedAuth}";
//})
;


});

Expand All @@ -134,26 +136,27 @@ private static IHostApplicationBuilder AddOpenTelemetryExporters(this IHostAppli
{
builder.Services.Configure<OpenTelemetryLoggerOptions>(logging => logging.AddOtlpExporter());
builder.Services.ConfigureOpenTelemetryMeterProvider(metrics => metrics.AddOtlpExporter());
if (useLangfuse)
{
var publicKey = langfuseSection.GetValue<string>(nameof(LangfuseSettings.PublicKey)) ?? string.Empty;
var secretKey = langfuseSection.GetValue<string>(nameof(LangfuseSettings.SecretKey)) ?? string.Empty;
var host = langfuseSection.GetValue<string>(nameof(LangfuseSettings.Host)) ?? string.Empty;
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes($"{publicKey}:{secretKey}");
string base64EncodedAuth = Convert.ToBase64String(plainTextBytes);

builder.Services.ConfigureOpenTelemetryTracerProvider(tracing => tracing.AddOtlpExporter(options =>
{
options.Endpoint = new Uri(host);
options.Protocol = OtlpExportProtocol.HttpProtobuf;
options.Headers = $"Authorization=Basic {base64EncodedAuth}";
})
);
}
else
{
//if (useLangfuse)
//{
// var publicKey = langfuseSection.GetValue<string>(nameof(LangfuseSettings.PublicKey)) ?? string.Empty;
// var secretKey = langfuseSection.GetValue<string>(nameof(LangfuseSettings.SecretKey)) ?? string.Empty;
// var host = langfuseSection.GetValue<string>(nameof(LangfuseSettings.Host)) ?? string.Empty;
// var plainTextBytes = System.Text.Encoding.UTF8.GetBytes($"{publicKey}:{secretKey}");
// string base64EncodedAuth = Convert.ToBase64String(plainTextBytes);

// builder.Services.ConfigureOpenTelemetryTracerProvider(tracing => tracing.AddOtlpExporter(options =>
// {
// options.Endpoint = new Uri(host);
// options.Protocol = OtlpExportProtocol.HttpProtobuf;
// options.Headers = $"Authorization=Basic {base64EncodedAuth}";
// })
// );

//}
//else
//{
builder.Services.ConfigureOpenTelemetryTracerProvider(tracing => tracing.AddOtlpExporter());
}
//}
}

// Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
<PackageReference Include="Serilog.Sinks.Console" />
<PackageReference Include="Serilog.Sinks.File" />
<PackageReference Include="Rougamo.Fody" />
<PackageReference Include="ModelContextProtocol" />
<PackageReference Include="DeviceId" />
<PackageReference Include="DeviceId.Linux" />
<PackageReference Include="DeviceId.Mac" />
<PackageReference Include="DeviceId.Windows" />
<PackageReference Include="DeviceId.Windows.Wmi" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using System.Collections.Generic;
using BotSharp.Abstraction.Diagnostics.Telemetry;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;

namespace BotSharp.Abstraction.Diagnostics;

/// <summary>
/// Model diagnostics helper class that provides a set of methods to trace model activities with the OTel semantic conventions.
/// This class contains experimental features and may change in the future.
/// To enable these features, set one of the following switches to true:
/// `BotSharp.Experimental.GenAI.EnableOTelDiagnostics`
/// `BotSharp.Experimental.GenAI.EnableOTelDiagnosticsSensitive`
/// Or set the following environment variables to true:
/// `BOTSHARP_EXPERIMENTAL_GENAI_ENABLE_OTEL_DIAGNOSTICS`
/// `BOTSHARP_EXPERIMENTAL_GENAI_ENABLE_OTEL_DIAGNOSTICS_SENSITIVE`
/// </summary>
[ExcludeFromCodeCoverage]
public static class ActivityExtensions
public static class ActivityExtensions
{
private const string EnableDiagnosticsSwitch = "BotSharp.Experimental.GenAI.EnableOTelDiagnostics";
private const string EnableSensitiveEventsSwitch = "BotSharp.Experimental.GenAI.EnableOTelDiagnosticsSensitive";
private const string EnableDiagnosticsEnvVar = "BOTSHARP_EXPERIMENTAL_GENAI_ENABLE_OTEL_DIAGNOSTICS";
private const string EnableSensitiveEventsEnvVar = "BOTSHARP_EXPERIMENTAL_GENAI_ENABLE_OTEL_DIAGNOSTICS_SENSITIVE";

public static readonly bool s_enableDiagnostics = AppContextSwitchHelper.GetConfigValue(EnableDiagnosticsSwitch, EnableDiagnosticsEnvVar);
public static readonly bool s_enableSensitiveEvents = AppContextSwitchHelper.GetConfigValue(EnableSensitiveEventsSwitch, EnableSensitiveEventsEnvVar);


/// <summary>
/// Starts an activity with the appropriate tags for a kernel function execution.
/// </summary>
Expand All @@ -21,9 +36,9 @@ public static class ActivityExtensions
const string OperationName = "execute_tool";

return source.StartActivityWithTags($"{OperationName} {functionName}", [
new KeyValuePair<string, object?>("gen_ai.operation.name", OperationName),
new KeyValuePair<string, object?>("gen_ai.tool.name", functionName),
new KeyValuePair<string, object?>("gen_ai.tool.description", functionDescription)
new KeyValuePair<string, object?>(TelemetryConstants.ModelDiagnosticsTags.Operation, OperationName),
new KeyValuePair<string, object?>(TelemetryConstants.ModelDiagnosticsTags.ToolName, functionName),
new KeyValuePair<string, object?>(TelemetryConstants.ModelDiagnosticsTags.ToolDescription, functionDescription)
], ActivityKind.Internal);
}

Expand All @@ -42,8 +57,6 @@ public static Activity SetTags(this Activity activity, ReadOnlySpan<KeyValuePair
{
activity.SetTag(tag.Key, tag.Value);
}
;

return activity;
}

Expand All @@ -69,51 +82,4 @@ public static Activity SetError(this Activity activity, Exception exception)
activity.SetStatus(ActivityStatusCode.Error, exception.Message);
return activity;
}

public static async IAsyncEnumerable<TResult> RunWithActivityAsync<TResult>(
Func<Activity?> getActivity,
Func<IAsyncEnumerable<TResult>> operation,
[EnumeratorCancellation] CancellationToken cancellationToken)
{
using var activity = getActivity();

ConfiguredCancelableAsyncEnumerable<TResult> result;

try
{
result = operation().WithCancellation(cancellationToken).ConfigureAwait(false);
}
catch (Exception ex) when (activity is not null)
{
activity.SetError(ex);
throw;
}

var resultEnumerator = result.ConfigureAwait(false).GetAsyncEnumerator();

try
{
while (true)
{
try
{
if (!await resultEnumerator.MoveNextAsync())
{
break;
}
}
catch (Exception ex) when (activity is not null)
{
activity.SetError(ex);
throw;
}

yield return resultEnumerator.Current;
}
}
finally
{
await resultEnumerator.DisposeAsync();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using System.Diagnostics.CodeAnalysis;

namespace BotSharp.Abstraction.Diagnostics;
namespace BotSharp.Abstraction.Diagnostics;

/// <summary>
/// Helper class to get app context switch value
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace BotSharp.Abstraction.Diagnostics;

public class BotSharpOTelOptions
{
public const string DefaultName = "BotSharp.Server";

public string Name { get; set; } = DefaultName;

public string Version { get; set; } = "4.0.0";

public bool IsTelemetryEnabled { get; set; } = true;
}
Loading