openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Chat/Messages/ChatMessage.cs
173lines · modecode
| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | |
| 4 | namespace OpenAI.Chat; |
| 5 | |
| 6 | /// <summary> |
| 7 | /// A common, base representation of a message provided as input into a chat completion request. |
| 8 | /// </summary> |
| 9 | /// <remarks> |
| 10 | /// <list type="table"> |
| 11 | /// <listheader> |
| 12 | /// <type>Type -</type> |
| 13 | /// <role>Role -</role> |
| 14 | /// <note>Description</note> |
| 15 | /// </listheader> |
| 16 | /// <item> |
| 17 | /// <type><see cref="SystemChatMessage"/> -</type> |
| 18 | /// <role><c>system</c> -</role> |
| 19 | /// <note>Instructions to the model that guide the behavior of future <c>assistant</c> messages.</note> |
| 20 | /// </item> |
| 21 | /// <item> |
| 22 | /// <type><see cref="UserChatMessage"/> -</type> |
| 23 | /// <role><c>user</c> -</role> |
| 24 | /// <note>Input messages from the caller, typically paired with <c>assistant</c> messages in a conversation.</note> |
| 25 | /// </item> |
| 26 | /// <item> |
| 27 | /// <type><see cref="AssistantChatMessage"/> -</type> |
| 28 | /// <role><c>assistant</c> -</role> |
| 29 | /// <note> |
| 30 | /// Output messages from the model with responses to the <c>user</c> or calls to tools or functions that are |
| 31 | /// needed to continue the logical conversation. |
| 32 | /// </note> |
| 33 | /// </item> |
| 34 | /// <item> |
| 35 | /// <type><see cref="ToolChatMessage"/> -</type> |
| 36 | /// <role><c>tool</c> -</role> |
| 37 | /// <note> |
| 38 | /// Resolution information for a <see cref="ChatToolCall"/> in an earlier |
| 39 | /// <see cref="AssistantChatMessage"/> that was made against a supplied |
| 40 | /// <see cref="ChatTool"/>. |
| 41 | /// </note> |
| 42 | /// </item> |
| 43 | /// <item> |
| 44 | /// <type><see cref="FunctionChatMessage"/> -</type> |
| 45 | /// <role><c>function</c> -</role> |
| 46 | /// <note> |
| 47 | /// Resolution information for a <see cref="ChatFunctionCall"/> in an earlier |
| 48 | /// <see cref="AssistantChatMessage"/> that was made against a supplied |
| 49 | /// <see cref="ChatFunction"/>. Note that <c>functions</c> are deprecated in favor of |
| 50 | /// <c>tool_calls</c>. |
| 51 | /// </note> |
| 52 | /// </item> |
| 53 | /// </list> |
| 54 | /// </remarks> |
| 55 | [CodeGenModel("ChatCompletionRequestMessage")] |
| 56 | public partial class ChatMessage |
| 57 | { |
| 58 | /// <summary> |
| 59 | /// The content associated with the message. The interpretation of this content will vary depending on the message type. |
| 60 | /// </summary> |
| 61 | public ChatMessageContent Content { get; } = new ChatMessageContent(); |
| 62 | |
| 63 | // CUSTOM: Changed type from string to ChatMessageRole. |
| 64 | [CodeGenMember("Role")] |
| 65 | internal ChatMessageRole Role { get; set; } |
| 66 | |
| 67 | // CUSTOM: Made internal. |
| 68 | internal ChatMessage() |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | internal ChatMessage(ChatMessageRole role) |
| 73 | { |
| 74 | Role = role; |
| 75 | } |
| 76 | |
| 77 | internal ChatMessage(ChatMessageRole role, IEnumerable<ChatMessageContentPart> contentParts) : this(role) |
| 78 | { |
| 79 | if (contentParts != null) |
| 80 | { |
| 81 | foreach (ChatMessageContentPart contentPart in contentParts) |
| 82 | { |
| 83 | Content.Add(contentPart); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | internal ChatMessage(ChatMessageRole role, string content = null) : this(role) |
| 89 | { |
| 90 | if (content != null) |
| 91 | { |
| 92 | Content.Add(ChatMessageContentPart.CreateTextPart(content)); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | #region SystemChatMessage |
| 97 | /// <inheritdoc cref="SystemChatMessage(string)"/> |
| 98 | public static SystemChatMessage CreateSystemMessage(string content) => new(content); |
| 99 | |
| 100 | /// <inheritdoc cref="SystemChatMessage(IEnumerable{ChatMessageContentPart})"/> |
| 101 | public static SystemChatMessage CreateSystemMessage(IEnumerable<ChatMessageContentPart> contentParts) => new(contentParts); |
| 102 | |
| 103 | /// <inheritdoc cref="SystemChatMessage(ChatMessageContentPart[])"/> |
| 104 | public static SystemChatMessage CreateSystemMessage(params ChatMessageContentPart[] contentParts) => new(contentParts); |
| 105 | #endregion |
| 106 | |
| 107 | #region DeveloperChatMessage |
| 108 | /// <inheritdoc cref="DeveloperChatMessage(string)"/> |
| 109 | public static DeveloperChatMessage CreateDeveloperMessage(string content) => new(content); |
| 110 | |
| 111 | /// <inheritdoc cref="DeveloperChatMessage(IEnumerable{ChatMessageContentPart})"/> |
| 112 | public static DeveloperChatMessage CreateDeveloperMessage(IEnumerable<ChatMessageContentPart> contentParts) => new(contentParts); |
| 113 | |
| 114 | /// <inheritdoc cref="DeveloperChatMessage(ChatMessageContentPart[])"/> |
| 115 | public static DeveloperChatMessage CreateDeveloperMessage(params ChatMessageContentPart[] contentParts) => new(contentParts); |
| 116 | #endregion |
| 117 | |
| 118 | #region UserChatMessage |
| 119 | /// <inheritdoc cref="UserChatMessage(string)"/> |
| 120 | public static UserChatMessage CreateUserMessage(string content) => new(content); |
| 121 | |
| 122 | /// <inheritdoc cref="UserChatMessage(IEnumerable{ChatMessageContentPart})"/> |
| 123 | public static UserChatMessage CreateUserMessage(IEnumerable<ChatMessageContentPart> contentParts) => new(contentParts); |
| 124 | |
| 125 | /// <inheritdoc cref="UserChatMessage(ChatMessageContentPart[])"/> |
| 126 | public static UserChatMessage CreateUserMessage(params ChatMessageContentPart[] contentParts) => new(contentParts); |
| 127 | #endregion |
| 128 | |
| 129 | #region AssistantChatMessage |
| 130 | /// <inheritdoc cref="AssistantChatMessage(string)"/> |
| 131 | public static AssistantChatMessage CreateAssistantMessage(string content) => new(content); |
| 132 | |
| 133 | /// <inheritdoc cref="AssistantChatMessage(IEnumerable{ChatMessageContentPart})"/> |
| 134 | public static AssistantChatMessage CreateAssistantMessage(IEnumerable<ChatMessageContentPart> contentParts) => new(contentParts); |
| 135 | |
| 136 | /// <inheritdoc cref="AssistantChatMessage(ChatMessageContentPart[])"/> |
| 137 | public static AssistantChatMessage CreateAssistantMessage(params ChatMessageContentPart[] contentParts) => new(contentParts); |
| 138 | |
| 139 | /// <inheritdoc cref="AssistantChatMessage(IEnumerable{ChatToolCall})"/> |
| 140 | public static AssistantChatMessage CreateAssistantMessage(IEnumerable<ChatToolCall> toolCalls) => new(toolCalls); |
| 141 | |
| 142 | /// <inheritdoc cref="AssistantChatMessage(ChatFunctionCall)"/> |
| 143 | public static AssistantChatMessage CreateAssistantMessage(ChatFunctionCall functionCall) => new(functionCall); |
| 144 | |
| 145 | /// <inheritdoc cref="AssistantChatMessage(ChatCompletion)"/> |
| 146 | public static AssistantChatMessage CreateAssistantMessage(ChatCompletion chatCompletion) => new(chatCompletion); |
| 147 | |
| 148 | /// <inheritdoc cref="AssistantChatMessage(ChatOutputAudioReference)"/> |
| 149 | public static AssistantChatMessage CreateAssistantMessage(ChatOutputAudioReference outputAudioReference) => new(outputAudioReference); |
| 150 | |
| 151 | #endregion |
| 152 | |
| 153 | #region ToolChatMessage |
| 154 | /// <inheritdoc cref="ToolChatMessage(string, string)"/> |
| 155 | public static ToolChatMessage CreateToolMessage(string toolCallId, string content) => new(toolCallId, content); |
| 156 | |
| 157 | /// <inheritdoc cref="ToolChatMessage(string, IEnumerable{ChatMessageContentPart})"/> |
| 158 | public static ToolChatMessage CreateToolMessage(string toolCallId, IEnumerable<ChatMessageContentPart> contentParts) => new(toolCallId, contentParts); |
| 159 | |
| 160 | /// <inheritdoc cref="ToolChatMessage(string, ChatMessageContentPart[])"/> |
| 161 | public static ToolChatMessage CreateToolMessage(string toolCallId, params ChatMessageContentPart[] contentParts) => new(toolCallId, contentParts); |
| 162 | #endregion |
| 163 | |
| 164 | #region FunctionChatMessage |
| 165 | /// <inheritdoc cref="FunctionChatMessage(string, string)"/> |
| 166 | [Obsolete($"This method is obsolete. Please use {nameof(CreateToolMessage)} instead.")] |
| 167 | public static FunctionChatMessage CreateFunctionMessage(string functionName, string content) => new(functionName, content); |
| 168 | #endregion |
| 169 | |
| 170 | /// <summary> Creates a new instance of <see cref="UserChatMessage"/>. </summary> |
| 171 | /// <param name="content"> The text content of the <see cref="UserChatMessage"/>. </param> |
| 172 | public static implicit operator ChatMessage(string content) => new UserChatMessage(content); |
| 173 | } |
| 174 | |