openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
OpenAI/src/Custom/Chat/Messages/AssistantChatMessage.cs
203lines · modecode
| 1 | using Microsoft.TypeSpec.Generator.Customizations; |
| 2 | using System; |
| 3 | using System.Collections.Generic; |
| 4 | using System.Diagnostics.CodeAnalysis; |
| 5 | |
| 6 | namespace OpenAI.Chat; |
| 7 | |
| 8 | /// <summary> |
| 9 | /// Represents a chat message of the <c>assistant</c> role as supplied to a chat completion request. As assistant |
| 10 | /// messages are originated by the model on responses, <see cref="AssistantChatMessage"/> instances typically |
| 11 | /// represent chat history or example interactions to guide model behavior. |
| 12 | /// </summary> |
| 13 | [CodeGenType("ChatCompletionRequestAssistantMessage")] |
| 14 | [CodeGenVisibility(nameof(AssistantChatMessage), CodeGenVisibility.Internal)] |
| 15 | public partial class AssistantChatMessage : ChatMessage |
| 16 | { |
| 17 | /// <summary> |
| 18 | /// Creates a new instance of <see cref="AssistantChatMessage"/> using a collection of content items. |
| 19 | /// For <c>assistant</c> messages, this can be one or more of type <c>text</c> or exactly one of type <c>refusal</c>. |
| 20 | /// </summary> |
| 21 | /// <param name="contentParts"> |
| 22 | /// The collection of content items associated with the message. |
| 23 | /// </param> |
| 24 | public AssistantChatMessage(IEnumerable<ChatMessageContentPart> contentParts) |
| 25 | : this( |
| 26 | content: new ChatMessageContent(contentParts), |
| 27 | role: ChatMessageRole.Assistant, |
| 28 | patch: default, |
| 29 | refusal: null, |
| 30 | participantName: null, |
| 31 | toolCalls: null, |
| 32 | functionCall: null, |
| 33 | outputAudioReference: null) |
| 34 | { |
| 35 | Argument.AssertNotNullOrEmpty(contentParts, nameof(contentParts)); |
| 36 | } |
| 37 | |
| 38 | /// <summary> |
| 39 | /// Creates a new instance of <see cref="AssistantChatMessage"/> using a collection of content items. |
| 40 | /// For <c>assistant</c> messages, this can be one or more of type <c>text</c> or exactly one of type <c>refusal</c>. |
| 41 | /// </summary> |
| 42 | /// <param name="contentParts"> |
| 43 | /// The collection of text and image content items associated with the message. |
| 44 | /// </param> |
| 45 | public AssistantChatMessage(params ChatMessageContentPart[] contentParts) |
| 46 | : this( |
| 47 | content: new ChatMessageContent(contentParts), |
| 48 | role: ChatMessageRole.Assistant, |
| 49 | patch: default, |
| 50 | refusal: null, |
| 51 | participantName: null, |
| 52 | toolCalls: null, |
| 53 | functionCall: null, |
| 54 | outputAudioReference: null) |
| 55 | { |
| 56 | Argument.AssertNotNullOrEmpty(contentParts, nameof(contentParts)); |
| 57 | } |
| 58 | |
| 59 | /// <summary> |
| 60 | /// Creates a new instance of <see cref="AssistantChatMessage"/> that represents ordinary text content and |
| 61 | /// does not feature tool or function calls. |
| 62 | /// </summary> |
| 63 | /// <param name="content"> The text content of the message. </param> |
| 64 | public AssistantChatMessage(string content) |
| 65 | : this( |
| 66 | content: new ChatMessageContent([content]), |
| 67 | role: ChatMessageRole.Assistant, |
| 68 | patch: default, |
| 69 | refusal: null, |
| 70 | participantName: null, |
| 71 | toolCalls: null, |
| 72 | functionCall: null, |
| 73 | outputAudioReference: null) |
| 74 | { |
| 75 | Argument.AssertNotNull(content, nameof(content)); |
| 76 | } |
| 77 | |
| 78 | /// <summary> |
| 79 | /// Creates a new instance of <see cref="AssistantChatMessage"/> that represents <c>tool_calls</c> that |
| 80 | /// were provided by the model. |
| 81 | /// </summary> |
| 82 | /// <param name="toolCalls"> The <c>tool_calls</c> made by the model. </param> |
| 83 | public AssistantChatMessage(IEnumerable<ChatToolCall> toolCalls) |
| 84 | : this( |
| 85 | content: null, |
| 86 | role: ChatMessageRole.Assistant, |
| 87 | patch: default, |
| 88 | refusal: null, |
| 89 | participantName: null, |
| 90 | toolCalls: null, |
| 91 | functionCall: null, |
| 92 | outputAudioReference: null) |
| 93 | { |
| 94 | Argument.AssertNotNullOrEmpty(toolCalls, nameof(toolCalls)); |
| 95 | |
| 96 | foreach (ChatToolCall toolCall in toolCalls) |
| 97 | { |
| 98 | ToolCalls.Add(toolCall); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /// <summary> |
| 103 | /// Creates a new instance of <see cref="AssistantChatMessage"/> that represents a <c>function_call</c> |
| 104 | /// (deprecated in favor of <c>tool_calls</c>) that was made by the model. |
| 105 | /// </summary> |
| 106 | /// <param name="functionCall"> The <c>function_call</c> made by the model. </param> |
| 107 | [Obsolete($"This constructor is obsolete. Please use the constructor that takes an IEnumerable<ChatToolCall> parameter instead.")] |
| 108 | public AssistantChatMessage(ChatFunctionCall functionCall) |
| 109 | : this( |
| 110 | content: null, |
| 111 | role: ChatMessageRole.Assistant, |
| 112 | patch: default, |
| 113 | refusal: null, |
| 114 | participantName: null, |
| 115 | toolCalls: null, |
| 116 | functionCall: functionCall, |
| 117 | outputAudioReference: null) |
| 118 | { |
| 119 | Argument.AssertNotNull(functionCall, nameof(functionCall)); |
| 120 | } |
| 121 | |
| 122 | /// <summary> |
| 123 | /// Creates a new instance of <see cref="AssistantChatMessage"/> that represents a prior response from the model |
| 124 | /// that included audio with a correlation ID. |
| 125 | /// </summary> |
| 126 | /// <param name="outputAudioReference"> The <c>audio</c> reference with an <c>id</c>, produced by the model. </param> |
| 127 | [Experimental("OPENAI001")] |
| 128 | public AssistantChatMessage(ChatOutputAudioReference outputAudioReference) |
| 129 | : this( |
| 130 | content: null, |
| 131 | role: ChatMessageRole.Assistant, |
| 132 | patch: default, |
| 133 | refusal: null, |
| 134 | participantName: null, |
| 135 | toolCalls: null, |
| 136 | functionCall: null, |
| 137 | outputAudioReference: outputAudioReference) |
| 138 | { |
| 139 | Argument.AssertNotNull(outputAudioReference, nameof(outputAudioReference)); |
| 140 | } |
| 141 | |
| 142 | /// <summary> |
| 143 | /// Creates a new instance of <see cref="AssistantChatMessage"/> from a <see cref="ChatCompletion"/> with |
| 144 | /// an <c>assistant</c> role response. |
| 145 | /// </summary> |
| 146 | /// <remarks> |
| 147 | /// This constructor will copy the <c>content</c>, <c>tool_calls</c>, and <c>function_call</c> from a chat |
| 148 | /// completion response into a new <c>assistant</c> role request message. |
| 149 | /// </remarks> |
| 150 | /// <param name="chatCompletion"> |
| 151 | /// The <see cref="ChatCompletion"/> from which the conversation history request message should be created. |
| 152 | /// </param> |
| 153 | /// <exception cref="ArgumentException"> |
| 154 | /// The <c>role</c> of the provided chat completion response was not <see cref="ChatMessageRole.Assistant"/>. |
| 155 | /// </exception> |
| 156 | public AssistantChatMessage(ChatCompletion chatCompletion) |
| 157 | : this( |
| 158 | content: chatCompletion?.Content, |
| 159 | role: ChatMessageRole.Assistant, |
| 160 | patch: default, |
| 161 | refusal: chatCompletion?.Refusal, |
| 162 | participantName: null, |
| 163 | toolCalls: null, |
| 164 | functionCall: chatCompletion?.FunctionCall, |
| 165 | outputAudioReference: chatCompletion?.OutputAudio is not null |
| 166 | ? new(chatCompletion.OutputAudio.Id) |
| 167 | : null) |
| 168 | { |
| 169 | Argument.AssertNotNull(chatCompletion, nameof(chatCompletion)); |
| 170 | |
| 171 | if (chatCompletion.Role != ChatMessageRole.Assistant) |
| 172 | { |
| 173 | throw new NotSupportedException($"Cannot instantiate an {nameof(AssistantChatMessage)} from a {nameof(ChatCompletion)} with role: {chatCompletion.Role}."); |
| 174 | } |
| 175 | |
| 176 | foreach (ChatToolCall toolCall in chatCompletion.ToolCalls ?? []) |
| 177 | { |
| 178 | ToolCalls.Add(toolCall); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // CUSTOM: Renamed. |
| 183 | /// <summary> |
| 184 | /// An optional <c>name</c> associated with the assistant message. This is typically defined with a <c>system</c> |
| 185 | /// message and is used to differentiate between multiple participants of the same role. |
| 186 | /// </summary> |
| 187 | [CodeGenMember("Name")] |
| 188 | public string ParticipantName { get; set; } |
| 189 | |
| 190 | // CUSTOM: Common initialization for input model collection property. |
| 191 | [CodeGenMember("ToolCalls")] |
| 192 | public IList<ChatToolCall> ToolCalls { get; } |
| 193 | |
| 194 | [Obsolete($"This property is obsolete. Please use {nameof(ToolCalls)} instead.")] |
| 195 | public ChatFunctionCall FunctionCall { get; set; } |
| 196 | |
| 197 | // CUSTOM: |
| 198 | // - Added Experimental attribute. |
| 199 | // - Renamed. |
| 200 | [Experimental("OPENAI001")] |
| 201 | [CodeGenMember("Audio")] |
| 202 | public ChatOutputAudioReference OutputAudioReference { get; set; } |
| 203 | } |