Skip to content

Commit

Permalink
.Net: Add MistralAI to the AppInsights sample (#6301)
Browse files Browse the repository at this point in the history
### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
  • Loading branch information
markwallace-microsoft committed May 16, 2024
1 parent fdf35d8 commit 9b0dde5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
34 changes: 33 additions & 1 deletion dotnet/samples/Demos/TelemetryWithAppInsights/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.Google;
using Microsoft.SemanticKernel.Connectors.HuggingFace;
using Microsoft.SemanticKernel.Connectors.MistralAI;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel.Services;
using OpenTelemetry;
Expand Down Expand Up @@ -84,6 +85,8 @@ public static async Task Main()
await RunGoogleAIChatAsync(kernel);
Console.WriteLine();
await RunHuggingFaceChatAsync(kernel);
Console.WriteLine();
await RunMistralAIChatAsync(kernel);
}

Console.WriteLine();
Expand Down Expand Up @@ -115,6 +118,7 @@ public static async Task Main()
private const string AzureOpenAIServiceKey = "AzureOpenAI";
private const string GoogleAIGeminiServiceKey = "GoogleAIGemini";
private const string HuggingFaceServiceKey = "HuggingFace";
private const string MistralAIServiceKey = "MistralAI";

#region chat completion
private static async Task RunAzureOpenAIChatAsync(Kernel kernel)
Expand Down Expand Up @@ -170,6 +174,24 @@ private static async Task RunHuggingFaceChatAsync(Kernel kernel)
}
}

private static async Task RunMistralAIChatAsync(Kernel kernel)
{
Console.WriteLine("============= MistralAI Chat Completion =============");

using var activity = s_activitySource.StartActivity(MistralAIServiceKey);
SetTargetService(kernel, MistralAIServiceKey);

try
{
await RunChatAsync(kernel);
}
catch (Exception ex)
{
activity?.SetStatus(ActivityStatusCode.Error, ex.Message);
Console.WriteLine($"Error: {ex.Message}");
}
}

private static async Task RunChatAsync(Kernel kernel)
{
// Using non-streaming to get the poem.
Expand Down Expand Up @@ -243,7 +265,12 @@ private static Kernel GetKernel(ILoggerFactory loggerFactory)
model: TestConfiguration.HuggingFace.ModelId,
endpoint: new Uri("https://api-inference.huggingface.co"),
apiKey: TestConfiguration.HuggingFace.ApiKey,
serviceId: HuggingFaceServiceKey);
serviceId: HuggingFaceServiceKey)
.AddMistralChatCompletion(
modelId: TestConfiguration.MistralAI.ChatModelId,
apiKey: TestConfiguration.MistralAI.ApiKey,
serviceId: MistralAIServiceKey
);

builder.Services.AddSingleton<IAIServiceSelector>(new AIServiceSelector());
builder.Plugins.AddFromPromptDirectory(Path.Combine(folder, "WriterPlugin"));
Expand Down Expand Up @@ -305,6 +332,11 @@ private sealed class AIServiceSelector : IAIServiceSelector
{
Temperature = 0,
},
MistralAIServiceKey => new MistralAIPromptExecutionSettings()
{
Temperature = 0,
ToolCallBehavior = MistralAIToolCallBehavior.AutoInvokeKernelFunctions
},
_ => null,
};

Expand Down
3 changes: 3 additions & 0 deletions dotnet/samples/Demos/TelemetryWithAppInsights/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ dotnet user-secrets set "GoogleAI:ApiKey" "..."
dotnet user-secrets set "HuggingFace:ModelId" "..."
dotnet user-secrets set "HuggingFace:ApiKey" "..."
dotnet user-secrets set "MistralAI:ChatModelId" "mistral-large-latest"
dotnet user-secrets set "MistralAI:ApiKey" "..."
dotnet user-secrets set "ApplicationInsights:ConnectionString" "..."
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Connectors\Connectors.MistralAI\Connectors.MistralAI.csproj" />
<ProjectReference Include="..\..\..\src\Connectors\Connectors.OpenAI\Connectors.OpenAI.csproj" />
<ProjectReference Include="..\..\..\src\Connectors\Connectors.Google\Connectors.Google.csproj" />
<ProjectReference
Include="..\..\..\src\Connectors\Connectors.HuggingFace\Connectors.HuggingFace.csproj" />
<ProjectReference Include="..\..\..\src\Connectors\Connectors.HuggingFace\Connectors.HuggingFace.csproj" />
<ProjectReference Include="..\..\..\src\Planners\Planners.Handlebars\Planners.Handlebars.csproj" />
<ProjectReference Include="..\..\..\src\SemanticKernel.Core\SemanticKernel.Core.csproj" />
<ProjectReference Include="..\..\..\src\Plugins\Plugins.Core\Plugins.Core.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public static void Initialize(IConfigurationRoot configRoot)

public static HuggingFaceConfig HuggingFace => LoadSection<HuggingFaceConfig>();

public static MistralAIConfig MistralAI => LoadSection<MistralAIConfig>();

private static T LoadSection<T>([CallerMemberName] string? caller = null)
{
if (s_instance is null)
Expand Down Expand Up @@ -78,5 +80,11 @@ public class HuggingFaceConfig
public string EmbeddingModelId { get; set; }
}

public class MistralAIConfig
{
public string ApiKey { get; set; }
public string ChatModelId { get; set; }
}

#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor.
}

0 comments on commit 9b0dde5

Please sign in to comment.