openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Chat/AssistantChatMessage.cs
132lines · modecode
| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | |
| 4 | namespace OpenAI.Chat; |
| 5 | |
| 6 | /// <summary> |
| 7 | /// Represents a chat message of the <c>assistant</c> role as supplied to a chat completion request. As assistant |
| 8 | /// messages are originated by the model on responses, <see cref="AssistantChatMessage"/> instances typically |
| 9 | /// represent chat history or example interactions to guide model behavior. |
| 10 | /// </summary> |
| 11 | [CodeGenModel("ChatCompletionRequestAssistantMessage")] |
| 12 | public partial class AssistantChatMessage : ChatMessage |
| 13 | { |
| 14 | // CUSTOM: Made internal. |
| 15 | internal AssistantChatMessage() |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | /// <summary> |
| 20 | /// Creates a new instance of <see cref="AssistantChatMessage"/> using a collection of content items. |
| 21 | /// For <c>assistant</c> messages, this can be one or more of type <c>text</c> or exactly one of type <c>refusal</c>. |
| 22 | /// </summary> |
| 23 | /// <param name="contentParts"> |
| 24 | /// The collection of content items associated with the message. |
| 25 | /// </param> |
| 26 | public AssistantChatMessage(IEnumerable<ChatMessageContentPart> contentParts) |
| 27 | : base(ChatMessageRole.Assistant, contentParts) |
| 28 | { |
| 29 | Argument.AssertNotNullOrEmpty(contentParts, nameof(contentParts)); |
| 30 | } |
| 31 | |
| 32 | /// <summary> |
| 33 | /// Creates a new instance of <see cref="AssistantChatMessage"/> using a collection of content items. |
| 34 | /// For <c>assistant</c> messages, this can be one or more of type <c>text</c> or exactly one of type <c>refusal</c>. |
| 35 | /// </summary> |
| 36 | /// <param name="contentParts"> |
| 37 | /// The collection of text and image content items associated with the message. |
| 38 | /// </param> |
| 39 | public AssistantChatMessage(params ChatMessageContentPart[] contentParts) |
| 40 | : base(ChatMessageRole.Assistant, contentParts) |
| 41 | { |
| 42 | Argument.AssertNotNullOrEmpty(contentParts, nameof(contentParts)); |
| 43 | } |
| 44 | |
| 45 | /// <summary> |
| 46 | /// Creates a new instance of <see cref="AssistantChatMessage"/> that represents ordinary text content and |
| 47 | /// does not feature tool or function calls. |
| 48 | /// </summary> |
| 49 | /// <param name="content"> The text content of the message. </param> |
| 50 | public AssistantChatMessage(string content) |
| 51 | : base(ChatMessageRole.Assistant, content) |
| 52 | { |
| 53 | Argument.AssertNotNull(content, nameof(content)); |
| 54 | } |
| 55 | |
| 56 | /// <summary> |
| 57 | /// Creates a new instance of <see cref="AssistantChatMessage"/> that represents <c>tool_calls</c> that |
| 58 | /// were provided by the model. |
| 59 | /// </summary> |
| 60 | /// <param name="toolCalls"> The <c>tool_calls</c> made by the model. </param> |
| 61 | public AssistantChatMessage(IEnumerable<ChatToolCall> toolCalls) |
| 62 | : base(ChatMessageRole.Assistant) |
| 63 | { |
| 64 | Argument.AssertNotNullOrEmpty(toolCalls, nameof(toolCalls)); |
| 65 | |
| 66 | foreach (ChatToolCall toolCall in toolCalls) |
| 67 | { |
| 68 | ToolCalls.Add(toolCall); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /// <summary> |
| 73 | /// Creates a new instance of <see cref="AssistantChatMessage"/> that represents a <c>function_call</c> |
| 74 | /// (deprecated in favor of <c>tool_calls</c>) that was made by the model. |
| 75 | /// </summary> |
| 76 | /// <param name="functionCall"> The <c>function_call</c> made by the model. </param> |
| 77 | [Obsolete($"This constructor is obsolete. Please use the constructor that takes an IEnumerable<ChatToolCall> parameter instead.")] |
| 78 | public AssistantChatMessage(ChatFunctionCall functionCall) |
| 79 | : base(ChatMessageRole.Assistant) |
| 80 | { |
| 81 | Argument.AssertNotNull(functionCall, nameof(functionCall)); |
| 82 | |
| 83 | FunctionCall = functionCall; |
| 84 | } |
| 85 | |
| 86 | /// <summary> |
| 87 | /// Creates a new instance of <see cref="AssistantChatMessage"/> from a <see cref="ChatCompletion"/> with |
| 88 | /// an <c>assistant</c> role response. |
| 89 | /// </summary> |
| 90 | /// <remarks> |
| 91 | /// This constructor will copy the <c>content</c>, <c>tool_calls</c>, and <c>function_call</c> from a chat |
| 92 | /// completion response into a new <c>assistant</c> role request message. |
| 93 | /// </remarks> |
| 94 | /// <param name="chatCompletion"> |
| 95 | /// The <see cref="ChatCompletion"/> from which the conversation history request message should be created. |
| 96 | /// </param> |
| 97 | /// <exception cref="ArgumentException"> |
| 98 | /// The <c>role</c> of the provided chat completion response was not <see cref="ChatMessageRole.Assistant"/>. |
| 99 | /// </exception> |
| 100 | public AssistantChatMessage(ChatCompletion chatCompletion) |
| 101 | : base(ChatMessageRole.Assistant, chatCompletion?.Content) |
| 102 | { |
| 103 | Argument.AssertNotNull(chatCompletion, nameof(chatCompletion)); |
| 104 | |
| 105 | if (chatCompletion.Role != ChatMessageRole.Assistant) |
| 106 | { |
| 107 | throw new NotSupportedException($"Cannot instantiate an {nameof(AssistantChatMessage)} from a {nameof(ChatCompletion)} with role: {chatCompletion.Role}."); |
| 108 | } |
| 109 | |
| 110 | Refusal = chatCompletion.Refusal; |
| 111 | FunctionCall = chatCompletion.FunctionCall; |
| 112 | foreach (ChatToolCall toolCall in chatCompletion.ToolCalls ?? []) |
| 113 | { |
| 114 | ToolCalls.Add(toolCall); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // CUSTOM: Renamed. |
| 119 | /// <summary> |
| 120 | /// An optional <c>name</c> associated with the assistant message. This is typically defined with a <c>system</c> |
| 121 | /// message and is used to differentiate between multiple participants of the same role. |
| 122 | /// </summary> |
| 123 | [CodeGenMember("Name")] |
| 124 | public string ParticipantName { get; set; } |
| 125 | |
| 126 | // CUSTOM: Common initialization for input model collection property. |
| 127 | [CodeGenMember("ToolCalls")] |
| 128 | public IList<ChatToolCall> ToolCalls { get; } = new ChangeTrackingList<ChatToolCall>(); |
| 129 | |
| 130 | [Obsolete($"This property is obsolete. Please use {nameof(ToolCalls)} instead.")] |
| 131 | public ChatFunctionCall FunctionCall { get; set; } |
| 132 | } |