|
| 1 | +// ------------------------------------------------------------------------ |
| 2 | +// Copyright 2025 The Dapr Authors |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +// See the License for the specific language governing permissions and |
| 11 | +// limitations under the License. |
| 12 | +// ------------------------------------------------------------------------ |
| 13 | + |
| 14 | +using Dapr.AI.Conversation.ConversationRoles; |
| 15 | +using Dapr.AI.Conversation.Extensions; |
| 16 | +using Dapr.AI.Conversation.Tools; |
| 17 | +using Dapr.Common.Extensions; |
| 18 | +using Google.Protobuf.WellKnownTypes; |
| 19 | +using Autogenerated = Dapr.Client.Autogen.Grpc.v1; |
| 20 | + |
| 21 | +namespace Dapr.AI.Conversation; |
| 22 | + |
| 23 | +/// <summary> |
| 24 | +/// Prototype utilities used for mapping domain to protobuf types for the Conversation functionality. |
| 25 | +/// </summary> |
| 26 | +public static class ConversationProtoUtilities |
| 27 | +{ |
| 28 | + /// <summary> |
| 29 | + /// Converts an <see cref="IEnumerable{MessageContent}"/> into its protobuf equivalent. |
| 30 | + /// </summary> |
| 31 | + /// <param name="contents">The contents to map.</param> |
| 32 | + /// <returns></returns> |
| 33 | + public static IEnumerable<Autogenerated.ConversationMessageContent> ToProtoContents( |
| 34 | + this IEnumerable<MessageContent> contents) => |
| 35 | + contents.Select(c => new Autogenerated.ConversationMessageContent { Text = c.Text }); |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Converts an <see cref="IReadOnlyList{ConversationInput}"/> into its protobuf equivalent. |
| 39 | + /// </summary> |
| 40 | + /// <param name="inputs">The contents to map.</param> |
| 41 | + /// <returns></returns> |
| 42 | + private static IEnumerable<Autogenerated.ConversationInputAlpha2> ToProto(this IReadOnlyList<ConversationInput> inputs) => |
| 43 | + inputs.Select(input => |
| 44 | + { |
| 45 | + var messages = input.Messages.Select(message => message.ToProto()).ToRepeatedField(); |
| 46 | + var output = new Autogenerated.ConversationInputAlpha2(); |
| 47 | + output.Messages.AddRange(messages); |
| 48 | + |
| 49 | + if (input.ScrubPII is not null) |
| 50 | + { |
| 51 | + output.ScrubPii = input.ScrubPII.Value; |
| 52 | + } |
| 53 | + |
| 54 | + return output; |
| 55 | + }); |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Creates an <see cref="Autogenerated.ConversationRequestAlpha2"/> from the provided inputs and options. |
| 59 | + /// </summary> |
| 60 | + /// <param name="inputs">The conversation inputs.</param> |
| 61 | + /// <param name="options">The conversation options.</param> |
| 62 | + /// <returns></returns> |
| 63 | + /// <exception cref="NotSupportedException"></exception> |
| 64 | + public static Autogenerated.ConversationRequestAlpha2 CreateConversationInputRequest(IReadOnlyList<ConversationInput> inputs, |
| 65 | + ConversationOptions options) |
| 66 | + { |
| 67 | + var request = new Autogenerated.ConversationRequestAlpha2 |
| 68 | + { |
| 69 | + Name = options.ConversationComponentId, |
| 70 | + ContextId = options.ContextId, |
| 71 | + ToolChoice = options.ToolChoice?.ToString(), |
| 72 | + }; |
| 73 | + request.Inputs.AddRange(inputs.ToProto()); |
| 74 | + |
| 75 | + foreach (var p in options.Parameters) |
| 76 | + { |
| 77 | + request.Parameters.Add(p.Key, p.Value); |
| 78 | + } |
| 79 | + |
| 80 | + foreach (var m in options.Metadata) |
| 81 | + { |
| 82 | + request.Metadata.Add(m.Key, m.Value); |
| 83 | + } |
| 84 | + |
| 85 | + if (options.ScrubPII is not null) |
| 86 | + { |
| 87 | + request.ScrubPii = options.ScrubPII.Value; |
| 88 | + } |
| 89 | + |
| 90 | + if (options.Temperature is not null) |
| 91 | + { |
| 92 | + request.Temperature = options.Temperature.Value; |
| 93 | + } |
| 94 | + |
| 95 | + if (options.Tools.Count > 0) |
| 96 | + { |
| 97 | + var tools = options.Tools.Select(tool => |
| 98 | + { |
| 99 | + switch (tool) |
| 100 | + { |
| 101 | + case ToolFunction toolF: |
| 102 | + { |
| 103 | + var toolFunction = new Autogenerated.ConversationToolsFunction |
| 104 | + { |
| 105 | + Name = toolF.Name, Description = toolF.Description, |
| 106 | + }; |
| 107 | + |
| 108 | + var parametersStruct = new Struct(); |
| 109 | + foreach (var (k, v) in toolF.Parameters) |
| 110 | + { |
| 111 | + parametersStruct.Fields[k] = ProtobufHelpers.ToValue(v); |
| 112 | + } |
| 113 | + |
| 114 | + toolFunction.Parameters = parametersStruct; |
| 115 | + |
| 116 | + return new Autogenerated.ConversationTools { Function = toolFunction }; |
| 117 | + } |
| 118 | + default: |
| 119 | + throw new NotSupportedException($"Unsupported tool type: {tool.GetType().FullName}"); |
| 120 | + } |
| 121 | + }).ToRepeatedField(); |
| 122 | + request.Tools.AddRange(tools); |
| 123 | + } |
| 124 | + |
| 125 | + return request; |
| 126 | + } |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Maps the <see cref="Autogenerated.ConversationResponseAlpha2"/> to a <see cref="ConversationResponse"/>. |
| 130 | + /// </summary> |
| 131 | + /// <param name="result">The result from the conversation API to parse.</param> |
| 132 | + /// <returns></returns> |
| 133 | + public static ConversationResponse ToDomain(this Autogenerated.ConversationResponseAlpha2 result) |
| 134 | + { |
| 135 | + string? contextId = result.ContextId; |
| 136 | + var conversationResults = result.Outputs.Select(convoResult => |
| 137 | + { |
| 138 | + var choices = convoResult.Choices.Select(c => |
| 139 | + { |
| 140 | + var didParseReason = c.FinishReason.TryParseEnumMember<FinishReason>(out var parsedFinishReason); |
| 141 | + var resultMessage = new ResultMessage(c.Message.Content) |
| 142 | + { |
| 143 | + ToolCalls = c.Message.ToolCalls |
| 144 | + .Select(ToolCallBase (tc) => |
| 145 | + new CalledToolFunction(tc.Function.Name, tc.Function.Arguments) |
| 146 | + { |
| 147 | + Id = tc.Id |
| 148 | + }) |
| 149 | + .ToList() |
| 150 | + }; |
| 151 | + |
| 152 | + return new ConversationResultChoice(didParseReason ? parsedFinishReason : null, |
| 153 | + c.Index, resultMessage); |
| 154 | + }).ToList(); |
| 155 | + return new ConversationResponseResult(choices); |
| 156 | + }).ToList(); |
| 157 | + |
| 158 | + return new ConversationResponse(conversationResults, contextId); |
| 159 | + } |
| 160 | + |
| 161 | + /// <summary> |
| 162 | + /// Converts an <see cref="IConversationMessage"/> into its protobuf equivalent. |
| 163 | + /// </summary> |
| 164 | + /// <param name="message">The message to convert.</param> |
| 165 | + /// <returns></returns> |
| 166 | + /// <exception cref="ArgumentException"></exception> |
| 167 | + /// <exception cref="NotImplementedException"></exception> |
| 168 | + private static Autogenerated.ConversationMessage ToProto(this IConversationMessage message) |
| 169 | + { |
| 170 | + var messageContents = message.Content |
| 171 | + .Select(msg => new Autogenerated.ConversationMessageContent { Text = msg.Text }) |
| 172 | + .ToList() |
| 173 | + .ToRepeatedField(); |
| 174 | + |
| 175 | + switch (message) |
| 176 | + { |
| 177 | + case DeveloperMessage devMessage: |
| 178 | + { |
| 179 | + var output = new Autogenerated.ConversationMessageOfDeveloper { Name = devMessage.Name }; |
| 180 | + output.Content.AddRange(messageContents); |
| 181 | + |
| 182 | + return new Autogenerated.ConversationMessage { OfDeveloper = output }; |
| 183 | + } |
| 184 | + case UserMessage userMessage: |
| 185 | + { |
| 186 | + var output = new Autogenerated.ConversationMessageOfUser { Name = userMessage.Name }; |
| 187 | + output.Content.AddRange(messageContents); |
| 188 | + |
| 189 | + return new Autogenerated.ConversationMessage { OfUser = output }; |
| 190 | + } |
| 191 | + case AssistantMessage assistantMessage: |
| 192 | + { |
| 193 | + var output = new Autogenerated.ConversationMessageOfAssistant { Name = assistantMessage.Name }; |
| 194 | + output.Content.AddRange(messageContents); |
| 195 | + |
| 196 | + var toolContent = assistantMessage.ToolCalls.Select(toolCall => |
| 197 | + { |
| 198 | + if (toolCall is CalledToolFunction funcToolCall) |
| 199 | + { |
| 200 | + return new Autogenerated.ConversationToolCalls |
| 201 | + { |
| 202 | + Id = funcToolCall.Id, |
| 203 | + Function = new Autogenerated.ConversationToolCallsOfFunction |
| 204 | + { |
| 205 | + Name = funcToolCall.Name, Arguments = funcToolCall.JsonArguments |
| 206 | + } |
| 207 | + }; |
| 208 | + } |
| 209 | + |
| 210 | + throw new ArgumentException($"Unrecognized tool call type for identifier '{toolCall.Id}'"); |
| 211 | + }); |
| 212 | + output.ToolCalls.AddRange(toolContent); |
| 213 | + |
| 214 | + return new Autogenerated.ConversationMessage { OfAssistant = output }; |
| 215 | + } |
| 216 | + case SystemMessage systemMessage: |
| 217 | + { |
| 218 | + var output = new Autogenerated.ConversationMessageOfSystem { Name = systemMessage.Name }; |
| 219 | + output.Content.AddRange(messageContents); |
| 220 | + |
| 221 | + return new Autogenerated.ConversationMessage { OfSystem = output }; |
| 222 | + } |
| 223 | + case ToolMessage toolMessage: |
| 224 | + { |
| 225 | + var output = new Autogenerated.ConversationMessageOfTool |
| 226 | + { |
| 227 | + Name = toolMessage.Name, ToolId = toolMessage.Id |
| 228 | + }; |
| 229 | + output.Content.AddRange(messageContents); |
| 230 | + |
| 231 | + return new Autogenerated.ConversationMessage { OfTool = output }; |
| 232 | + } |
| 233 | + default: |
| 234 | + throw new NotImplementedException("Message type not recognized."); |
| 235 | + } |
| 236 | + } |
| 237 | +} |
0 commit comments