WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit a257e03

Browse files
authored
Renaming Dapr.AI.DotnetExtensions to Dapr.AI.Microsoft.Extensions (#1621)
* Renamed new project as Dapr.AI.Microsoft.Extensions for clarity from Dapr.AI.DotnetExtensions * Fixed deserialization bug with badly formed tool JSON arguments * Updated demo to include tool-calling example (though it doesn't do anything as the echo conversation component doesn't actively do anything with tools) * Added documentation for the new capability --------- Signed-off-by: Whit Waldo <[email protected]>
1 parent 7cc5355 commit a257e03

File tree

8 files changed

+173
-13
lines changed

8 files changed

+173
-13
lines changed

all.sln

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Common", "examples\Hosting\
195195
EndProject
196196
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.AI.A2A", "src\Dapr.AI.A2A\Dapr.AI.A2A.csproj", "{AE9804A8-906C-4C3B-B2A8-41F4D3269C19}"
197197
EndProject
198-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.AI.DotnetExtensions", "src\Dapr.AI.DotnetExtensions\Dapr.AI.DotnetExtensions.csproj", "{CA680984-0D3A-40E5-88FD-E2A125C6A126}"
198+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.AI.Microsoft.Extensions", "src\Dapr.AI.Microsoft.Extensions\Dapr.AI.Microsoft.Extensions.csproj", "{CA680984-0D3A-40E5-88FD-E2A125C6A126}"
199199
EndProject
200-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNetExtensionsDemo", "examples\AI\DotNetExtensionsDemo\DotNetExtensionsDemo.csproj", "{EDEEEAED-1856-4408-BAA6-A83E1B497B2A}"
200+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MicrosoftAiExtensionsDemo", "examples\AI\MicrosoftAiExtensionsDemo\MicrosoftAiExtensionsDemo.csproj", "{EDEEEAED-1856-4408-BAA6-A83E1B497B2A}"
201201
EndProject
202202
Global
203203
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
---
2+
type: docs
3+
title: "How to: Using Microsoft's AI extensions with Dapr's .NET Conversation SDK"
4+
linkTitle: "How to: Use Microsoft's AI extensions with Dapr"
5+
weight: 500200
6+
description: Learn how to create and use Dapr with Microsoft's AI extensions
7+
---
8+
9+
## Prerequisites
10+
- [.NET 8](https://dotnet.microsoft.com/download/dotnet/8.0), or [.NET 9](https://dotnet.microsoft.com/download/dotnet/9.0) installed
11+
- [Dapr CLI](https://docs.dapr.io/getting-started/install-dapr-cli/)
12+
- [Initialized Dapr environment](https://docs.dapr.io/getting-started/install-dapr-selfhost)
13+
14+
## Installation
15+
16+
To get started with this SDK, install both the [Dapr.AI](https://www.nuget.org/packages/Dapr.AI) and
17+
[Dapr.AI.Microsoft.Extensions](https://www.nuget.org/packages/Dapr.AI.Microsoft.Extensions) packages from NuGet:
18+
```sh
19+
dotnet add package Dapr.AI
20+
dotnet add package Dapr.AI.Microsoft.Extensions
21+
```
22+
23+
The `DaprChatClient` is a Dapr-based implementation of the `IChatClient` interface provided in the
24+
`Microsoft.Extensions.AI.Abstractions` package using Dapr's [conversation building block]({{ ref conversation-overview.md }}). It allows
25+
developers to build against the types provided by Microsoft's abstraction while providing the greatest conformity to the
26+
Dapr conversation building block available. As both approaches adopt OpenAI's API approach, these are expected to increasingly
27+
converge over time.
28+
29+
{{% alert title="Dapr Conversation Building Block" color="primary" %}}
30+
31+
Do note that Dapr's conversation building block is still in an alpha state, meaning that the shape of the API
32+
is likely to change future releases. It's the intent of this SDK package to provide an API that's aligned with
33+
Microsoft's AI extensions that also maps to and conforms with the Dapr API, but the names of types and properties
34+
may change from one release to the next, so please be aware of this possibility when using this SDK.
35+
36+
{{% /alert %}}
37+
38+
## About Microsoft.Extensions.AI
39+
The `Dapr.AI.Microsoft.Extensions` package implements the `Microsoft.Extensions.AI` abstractions, providing a unified API for
40+
AI services in .NET applications. `Microsoft.Extensions.AI` is designed to offer a consistent programming model across
41+
different AI providers and scenarios. For detailed information about `Microsoft.Extensions.AI`, refer to the
42+
[official documentation](https://learn.microsoft.com/en-us/dotnet/ai/microsoft-extensions-ai).
43+
44+
{{% alert title="Limited Support" color="warning" %}}
45+
46+
Note that Microsoft's AI extension provide many more properties and methods than Dapr's conversation building block currently
47+
supports. This package will only map those properties that have Dapr support and will ignore the others, so just because
48+
it's available in the Microsoft.Extensions.AI package doesn't mean it's supported by Dapr. Rely on this documentation
49+
and the exposed XML documentation in the package to understand what is and isn't supported.
50+
51+
{{% /alert %}}
52+
53+
## Service Registration
54+
The `DaprChatClient` can be registered with the dependency injection container using several extension methods. First,
55+
ensure that you reigster the `DaprConversationClient` that's part of the `Dapr.AI` package from NuGet:
56+
57+
```csharp
58+
services.AddDaprConversationClient();
59+
```
60+
61+
Then register the `DaprChatClient` with your conversation component name:
62+
63+
```csharp
64+
services.AddDaprChatClient("my-conversation-component");
65+
```
66+
67+
### Configuration Options
68+
You can confiugre the `DaprChatClient` using the `DaprChatClientOptions` though the current implementation only
69+
provides configuration for the component name itself. This is expected to change in future releases.
70+
71+
```csharp
72+
services.AddDaprChatClient("my-conversation-component", options =>
73+
{
74+
// Configure additional options here
75+
});
76+
```
77+
78+
You can also configure the service lifetime (this defaults to `ServiceLifetime.Scoped`):
79+
80+
```csharp
81+
services.AddDaprChatClient("my-conversation-component", ServiceLifetime.Singleton);
82+
```
83+
84+
## Usage
85+
Once registered, you can inject and use `IChatClient` in your services:
86+
87+
```csharp
88+
public class ChatService(IChatClient chatClient)
89+
{
90+
public async Task<IReadOnlyList<string>> GetResponseAsync(string message)
91+
{
92+
var response = await chatClient.GetResponseAsync([
93+
new ChatMessage(ChatRole.User,
94+
"Please write me a poem in iambic pentameter about the joys of using Dapr to develop distributed applications with .NET")
95+
]);
96+
97+
return response.Messages.Select(msg => msg.Text).ToList();
98+
}
99+
}
100+
```
101+
102+
### Streaming Conversations
103+
The `DaprChatClient` does not yet support streaming responses and use of the corresponding `GetStreamingResponseAsync`
104+
methods will throw a `NotImplemenetedException`. This is expected to change in a future release once the Dapr runtime
105+
supports this functionality.
106+
107+
### Tool Integration
108+
The client supports function calling through the `Microsoft.Extensions.AI` tool integration. Tools registered with the
109+
conversation will be automatically available to the large language model.
110+
111+
```csharp
112+
string GetCurrentWeather() => Random.Shared.NextDouble() > 0.5 ? "It's sunny today!" : "It's raining today!";
113+
var toolChatOptions = new ChatOptions { Tools = [AIFunctionFactory.Create(GetCurrentWeather, "weather")] };
114+
var toolResponse = await chatClient.GetResponseAsync("What's the weather like today?", toolChatOptions);
115+
foreach (var toolResp in toolResponse.Messages)
116+
{
117+
Console.WriteLine(toolResp);
118+
}
119+
```
120+
121+
## Error Handling
122+
The `DaprChatClient` integrates with Dapr's error handling and will throw appropriate exceptions when issues occur.
123+
124+
## Configuration and Metadata
125+
The underlying Dapr conversation component can be configured with metadata and parameters through the Dapr conversation
126+
building block configuration. The `DaprChatClient` will respect these settings when making calls to the conversation component.
127+
128+
## Best Practices
129+
130+
1. **Service Lifetime**: Use `ServiceLifetime.Scoped` or `ServiceLifetime.Singleton` for the `DaprChatClient` registration to avoid creating multiple instances unnecessarily.
131+
132+
2. **Error Handling**: Always wrap calls in appropriate try-catch blocks to handle both Dapr-specific and general exceptions.
133+
134+
3. **Resource Management**: The `DaprChatClient` properly implements `IDisposable` through its base classes, so resources are automatically managed when using dependency injection.
135+
136+
4. **Configuration**: Configure your Dapr conversation component properly to ensure optimal performance and reliability.
137+
138+
## Related Links
139+
140+
- [Dapr Conversation Building Block]({{ ref conversation-overview.md }})
141+
- [Microsoft.Extensions.AI Documentation](https://learn.microsoft.com/en-us/dotnet/ai/microsoft-extensions-ai)
142+
- [Dapr .NET Conversation SDK]({{% ref dotnet-ai-conversation-howto.md %}})

examples/AI/DotNetExtensionsDemo/DotNetExtensionsDemo.csproj renamed to examples/AI/MicrosoftAiExtensionsDemo/MicrosoftAiExtensionsDemo.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<ProjectReference Include="..\..\..\src\Dapr.AI.DotnetExtensions\Dapr.AI.DotnetExtensions.csproj" />
10+
<ProjectReference Include="..\..\..\src\Dapr.AI.Microsoft.Extensions\Dapr.AI.Microsoft.Extensions.csproj" />
1111
</ItemGroup>
1212

1313
</Project>

examples/AI/DotNetExtensionsDemo/Program.cs renamed to examples/AI/MicrosoftAiExtensionsDemo/Program.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
// See https://aka.ms/new-console-template for more information
2-
3-
using Dapr.AI.Conversation.Extensions;
4-
using Dapr.AI.DotnetExtensions;
1+
using Dapr.AI.Conversation.Extensions;
2+
using Dapr.AI.Microsoft.Extensions;
53
using Microsoft.Extensions.AI;
64
using Microsoft.Extensions.DependencyInjection;
75
using Microsoft.Extensions.Hosting;
@@ -22,6 +20,7 @@
2220
using var scope = app.Services.CreateScope();
2321
var chatClient = scope.ServiceProvider.GetRequiredService<IChatClient>();
2422

23+
// Vanilla message/resposne
2524
var response = await chatClient.GetResponseAsync([
2625
new ChatMessage(ChatRole.User,
2726
"Please write me a poem in iambic pentameter about the joys of using Dapr to develop distributed applications with .NET")
@@ -31,3 +30,12 @@
3130
{
3231
Console.WriteLine(r.Text);
3332
}
33+
34+
// Tool-based support - This just demonstrates how to do it, but Dapr's echo conversation component doesn't use tools
35+
string GetCurrentWeather() => Random.Shared.NextDouble() > 0.5 ? "It's sunny today!" : "It's raining today!";
36+
var toolChatOptions = new ChatOptions { Tools = [AIFunctionFactory.Create(GetCurrentWeather, "weather")] };
37+
var toolResponse = await chatClient.GetResponseAsync("What's the weather like today?", toolChatOptions);
38+
foreach (var toolResp in toolResponse.Messages)
39+
{
40+
Console.WriteLine(toolResp);
41+
}

src/Dapr.AI.DotnetExtensions/Dapr.AI.DotnetExtensions.csproj renamed to src/Dapr.AI.Microsoft.Extensions/Dapr.AI.Microsoft.Extensions.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<ImplicitUsings>enable</ImplicitUsings>
55
<Nullable>enable</Nullable>
6-
<PackageId>Dapr.AI.DotnetExtensions</PackageId>
6+
<PackageId>Dapr.AI.Microsoft.Extensions</PackageId>
77
<Title>Dapr .NET AI Implementation</Title>
88
<Description>Dapr AI implementation for performing operations with the .NET AI extensions.</Description>
99
<VersionSuffix>alpha</VersionSuffix>

src/Dapr.AI.DotnetExtensions/DaprChatClient.cs renamed to src/Dapr.AI.Microsoft.Extensions/DaprChatClient.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using Microsoft.Extensions.Options;
99
using Type = System.Type;
1010

11-
namespace Dapr.AI.DotnetExtensions;
11+
namespace Dapr.AI.Microsoft.Extensions;
1212

1313
/// <summary>
1414
/// Provides a concrete implementation of <see cref="IChatClient"/> that uses the Dapr Conversation component.
@@ -129,10 +129,20 @@ private static ChatResponse MapToResponse(ConversationResponse response) =>
129129
{
130130
if (toolCall is CalledToolFunction calledToolFunction)
131131
{
132+
var jsonArgs = new Dictionary<string, object?>();
133+
try
134+
{
135+
jsonArgs = JsonSerializer.Deserialize<Dictionary<string, object?>>(
136+
calledToolFunction.JsonArguments);
137+
}
138+
catch
139+
{
140+
// Ignored - either it's not set, badly formed or something else is wrong
141+
}
142+
132143
content.Add(new FunctionCallContent(calledToolFunction.Id ?? string.Empty,
133144
calledToolFunction.Name,
134-
JsonSerializer.Deserialize<Dictionary<string, object?>>(
135-
calledToolFunction.JsonArguments)));
145+
jsonArgs));
136146
}
137147
}
138148

src/Dapr.AI.DotnetExtensions/DaprChatClientExtensions.cs renamed to src/Dapr.AI.Microsoft.Extensions/DaprChatClientExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using Microsoft.Extensions.DependencyInjection.Extensions;
66
using Microsoft.Extensions.Options;
77

8-
namespace Dapr.AI.DotnetExtensions;
8+
namespace Dapr.AI.Microsoft.Extensions;
99

1010
/// <summary>
1111
/// Contains extension methods for an <see cref="IServiceCollection"/> for registering <see cref="DaprChatClient"/>.

src/Dapr.AI.DotnetExtensions/DaprChatClientOptions.cs renamed to src/Dapr.AI.Microsoft.Extensions/DaprChatClientOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Dapr.AI.DotnetExtensions;
1+
namespace Dapr.AI.Microsoft.Extensions;
22

33
/// <summary>
44
/// Provides the required operation for configuring the <see cref="DaprChatClient"/>.

0 commit comments

Comments
 (0)