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
7 changes: 7 additions & 0 deletions all.sln
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Workflow.Abstractions.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.Testcontainers.Xunit", "src\Dapr.Testcontainers.Xunit\Dapr.Testcontainers.Xunit.csproj", "{2D6EB9E0-C5BF-4BA4-B69F-0D2B5A0E36D5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapr.IntegrationTest.AI", "test\Dapr.IntegrationTest.AI\Dapr.IntegrationTest.AI.csproj", "{AD9F25C7-7BBD-459A-B3EF-1BE75C25E80A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -609,6 +611,10 @@ Global
{2D6EB9E0-C5BF-4BA4-B69F-0D2B5A0E36D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2D6EB9E0-C5BF-4BA4-B69F-0D2B5A0E36D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2D6EB9E0-C5BF-4BA4-B69F-0D2B5A0E36D5}.Release|Any CPU.Build.0 = Release|Any CPU
{AD9F25C7-7BBD-459A-B3EF-1BE75C25E80A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AD9F25C7-7BBD-459A-B3EF-1BE75C25E80A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AD9F25C7-7BBD-459A-B3EF-1BE75C25E80A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AD9F25C7-7BBD-459A-B3EF-1BE75C25E80A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -721,6 +727,7 @@ Global
{E958E875-8DDE-4B25-BE3A-C0760EC89376} = {8462B106-175A-423A-BA94-BE0D39D0BD8E}
{38AAD849-B59C-4011-B309-3E9F291E9B9F} = {0AF0FE8D-C234-4F04-8514-32206ACE01BD}
{2D6EB9E0-C5BF-4BA4-B69F-0D2B5A0E36D5} = {27C5D71D-0721-4221-9286-B94AB07B58CF}
{AD9F25C7-7BBD-459A-B3EF-1BE75C25E80A} = {8462B106-175A-423A-BA94-BE0D39D0BD8E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {65220BF2-EAE1-4CB2-AA58-EBE80768CB40}
Expand Down
15 changes: 14 additions & 1 deletion src/Dapr.AI/Conversation/CalledToolFunction.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
namespace Dapr.AI.Conversation;
// ------------------------------------------------------------------------
// Copyright 2026 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------

namespace Dapr.AI.Conversation;

/// <summary>
/// Documents a tool call by a function within the context of a conversation message.
Expand Down
51 changes: 51 additions & 0 deletions src/Dapr.AI/Conversation/CompletionUsage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// ------------------------------------------------------------------------
// Copyright 2026 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------

using Dapr.Client.Autogen.Grpc.v1;

namespace Dapr.AI.Conversation;

/// <summary>
/// The breakdown of tokens used in a conversation.
/// </summary>
/// <param name="CompletionTokens">Number of tokens in the generated completion.</param>
/// <param name="PromptTokens">The number of tokes in the prompt.</param>
/// <param name="TotalTokens">The total number of tokens used in the request (prompt and completion).</param>
public sealed record CompletionUsage(ulong CompletionTokens, ulong PromptTokens, ulong TotalTokens)
{
/// <summary>
/// Breakdown of tokens used in completion.
/// </summary>
public UsageCompletionTokensDetails? CompletionTokensDetails { get; init; }

/// <summary>
/// Breakdown of tokens used in the prompt.
/// </summary>
public UsagePromptTokensDetails? PromptTokensDetails { get; init; }

/// <summary>
/// Creates an instance of <see cref="CompletionUsage"/> from the prototype value.
/// </summary>
/// <param name="proto">The gRPC response from the runtime to parse.</param>
/// <returns>A new instance of <see cref="CompletionUsage"/>.</returns>
internal static CompletionUsage? FromProto(ConversationResultAlpha2CompletionUsage? proto) =>
proto is null
? null
: new(proto.CompletionTokens, proto.PromptTokens, proto.TotalTokens)
{
CompletionTokensDetails = UsageCompletionTokensDetails.FromProto(proto.CompletionTokensDetails),
PromptTokensDetails = UsagePromptTokensDetails.FromProto(proto.PromptTokensDetails)
};
}


10 changes: 10 additions & 0 deletions src/Dapr.AI/Conversation/ConversationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,14 @@ public sealed record ConversationOptions(string ConversationComponentId)
/// - 'auto' is the default if tools are present.
/// </remarks>
public ToolChoice? ToolChoice { get; init; }

/// <summary>
/// Retention policy for the prompt cache.
/// </summary>
public TimeSpan? PromptCacheRetention { get; init; }

/// <summary>
/// The JSON schema used to coerce the response into a specific format.
/// </summary>
public Struct? ResponseFormat { get; init; }
}
17 changes: 16 additions & 1 deletion src/Dapr.AI/Conversation/ConversationProtoUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ public static Autogenerated.ConversationRequestAlpha2 CreateConversationInputReq
request.Temperature = options.Temperature.Value;
}

if (options.PromptCacheRetention is not null)
{
request.PromptCacheRetention = options.PromptCacheRetention.Value.ToDuration();
}

if (options.ResponseFormat is not null)
{
request.ResponseFormat = options.ResponseFormat;
}

if (options.Tools.Count > 0)
{
var tools = options.Tools.Select(tool =>
Expand Down Expand Up @@ -163,7 +173,12 @@ public static ConversationResponse ToDomain(this Autogenerated.ConversationRespo
return new ConversationResultChoice(didParseReason ? parsedFinishReason : null,
c.Index, resultMessage);
}).ToList();
return new ConversationResponseResult(choices);

return new ConversationResponseResult(choices)
{
Model = !string.IsNullOrWhiteSpace(convoResult.Model) ? convoResult.Model : null,
Usage = convoResult.Usage is not null ? CompletionUsage.FromProto(convoResult.Usage) : null
};
}).ToList();

return new ConversationResponse(conversationResults, contextId);
Expand Down
28 changes: 26 additions & 2 deletions src/Dapr.AI/Conversation/ConversationResponseResult.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
namespace Dapr.AI.Conversation;
// ------------------------------------------------------------------------
// Copyright 2026 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------

namespace Dapr.AI.Conversation;

/// <summary>
/// The result in a conversation for a given input.
/// </summary>
/// <param name="Choices">The resulting choices from the conversation.</param>
public sealed record ConversationResponseResult(IReadOnlyList<ConversationResultChoice> Choices);
public sealed record ConversationResponseResult(IReadOnlyList<ConversationResultChoice> Choices)
{
/// <summary>
/// The model used for the conversation.
/// </summary>
public string? Model { get; init; }

/// <summary>
/// Usage statistics for the completion request.
/// </summary>
public CompletionUsage? Usage { get; init; }
}
15 changes: 14 additions & 1 deletion src/Dapr.AI/Conversation/ConversationResultChoice.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
using Dapr.AI.Conversation.Tools;
// ------------------------------------------------------------------------
// Copyright 2026 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------

using Dapr.AI.Conversation.Tools;

namespace Dapr.AI.Conversation;

Expand Down
15 changes: 14 additions & 1 deletion src/Dapr.AI/Conversation/ConversationRoles/AssistantMessage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
using System.Text.Json.Serialization;
// ------------------------------------------------------------------------
// Copyright 2026 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------

using System.Text.Json.Serialization;
using Dapr.Common.JsonConverters;

namespace Dapr.AI.Conversation.ConversationRoles;
Expand Down
15 changes: 14 additions & 1 deletion src/Dapr.AI/Conversation/ConversationRoles/DeveloperMessage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
using System.Text.Json.Serialization;
// ------------------------------------------------------------------------
// Copyright 2026 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------

using System.Text.Json.Serialization;
using Dapr.Common.JsonConverters;

namespace Dapr.AI.Conversation.ConversationRoles;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
using System.Text.Json.Serialization;
// ------------------------------------------------------------------------
// Copyright 2026 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------

using System.Text.Json.Serialization;

namespace Dapr.AI.Conversation.ConversationRoles;

Expand Down
15 changes: 14 additions & 1 deletion src/Dapr.AI/Conversation/ConversationRoles/MessageContent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
namespace Dapr.AI.Conversation.ConversationRoles;
// ------------------------------------------------------------------------
// Copyright 2026 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------

namespace Dapr.AI.Conversation.ConversationRoles;

/// <summary>
/// The content of a message.
Expand Down
15 changes: 14 additions & 1 deletion src/Dapr.AI/Conversation/ConversationRoles/MessageRole.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
using System.Runtime.Serialization;
// ------------------------------------------------------------------------
// Copyright 2026 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------

using System.Runtime.Serialization;

namespace Dapr.AI.Conversation.ConversationRoles;

Expand Down
15 changes: 14 additions & 1 deletion src/Dapr.AI/Conversation/ConversationRoles/SystemMessage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
using System.Text.Json.Serialization;
// ------------------------------------------------------------------------
// Copyright 2026 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------

using System.Text.Json.Serialization;
using Dapr.Common.JsonConverters;

namespace Dapr.AI.Conversation.ConversationRoles;
Expand Down
15 changes: 14 additions & 1 deletion src/Dapr.AI/Conversation/ConversationRoles/ToolMessage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
using System.Text.Json.Serialization;
// ------------------------------------------------------------------------
// Copyright 2026 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------

using System.Text.Json.Serialization;
using Dapr.Common.JsonConverters;

namespace Dapr.AI.Conversation.ConversationRoles;
Expand Down
15 changes: 14 additions & 1 deletion src/Dapr.AI/Conversation/ConversationRoles/UserMessage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
using System.Text.Json.Serialization;
// ------------------------------------------------------------------------
// Copyright 2026 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------

using System.Text.Json.Serialization;
using Dapr.Common.JsonConverters;

namespace Dapr.AI.Conversation.ConversationRoles;
Expand Down
12 changes: 8 additions & 4 deletions src/Dapr.AI/Conversation/DaprConversationGrpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ namespace Dapr.AI.Conversation;
/// <param name="client">The Dapr client.</param>
/// <param name="httpClient">The HTTP client used by the client for calling the Dapr runtime.</param>
/// <param name="daprApiToken">An optional token required to send requests to the Dapr sidecar.</param>
[Experimental("DAPR_CONVERSATION", UrlFormat = "https://docs.dapr.io/developing-applications/building-blocks/conversation/conversation-overview/")]
internal sealed class DaprConversationGrpcClient(Autogenerated.Dapr.DaprClient client, HttpClient httpClient, string? daprApiToken = null) : DaprConversationClient(client, httpClient, daprApiToken: daprApiToken)
{
[Experimental("DAPR_CONVERSATION",
UrlFormat = "https://docs.dapr.io/developing-applications/building-blocks/conversation/conversation-overview/")]
internal sealed class DaprConversationGrpcClient(
Autogenerated.Dapr.DaprClient client,
HttpClient httpClient,
string? daprApiToken = null) : DaprConversationClient(client, httpClient, daprApiToken: daprApiToken)
{
/// <inheritdoc />
public override async Task<ConversationResponse> ConverseAsync(IReadOnlyList<ConversationInput> inputs,
ConversationOptions options, CancellationToken cancellationToken = default)
Expand All @@ -42,7 +46,7 @@ public override async Task<ConversationResponse> ConverseAsync(IReadOnlyList<Con
var response = result.ToDomain();
return response;
}

/// <inheritdoc />
protected override void Dispose(bool disposing)
{
Expand Down
15 changes: 14 additions & 1 deletion src/Dapr.AI/Conversation/Extensions/ProtobufHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
using System.Collections;
// ------------------------------------------------------------------------
// Copyright 2026 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------

using System.Collections;
using System.Text.Json;
using Google.Protobuf.Collections;
using Google.Protobuf.WellKnownTypes;
Expand Down
Loading
Loading