openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Chat/AssistantChatMessage.cs
131lines · modeblame
9f9f2936Jose Arriaga Maldonado2 years ago | 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 | { | |
2ab1a942Jose Arriaga Maldonado1 years ago | 14 | // CUSTOM: Made internal. |
| 15 | internal AssistantChatMessage() | |
| 16 | { | |
| 17 | } | |
| 18 | | |
3467b535Travis Wilson1 years ago | 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) | |
9f9f2936Jose Arriaga Maldonado2 years ago | 28 | { |
3467b535Travis Wilson1 years ago | 29 | Argument.AssertNotNullOrEmpty(contentParts, nameof(contentParts)); |
9f9f2936Jose Arriaga Maldonado2 years ago | 30 | } |
| 31 | | |
3467b535Travis Wilson1 years ago | 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 | } | |
9f9f2936Jose Arriaga Maldonado2 years ago | 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) | |
3467b535Travis Wilson1 years ago | 51 | : base(ChatMessageRole.Assistant, content) |
9f9f2936Jose Arriaga Maldonado2 years ago | 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> | |
2ab1a942Jose Arriaga Maldonado1 years ago | 61 | public AssistantChatMessage(IEnumerable<ChatToolCall> toolCalls) |
| 62 | : base(ChatMessageRole.Assistant) | |
9f9f2936Jose Arriaga Maldonado2 years ago | 63 | { |
| 64 | Argument.AssertNotNull(toolCalls, nameof(toolCalls)); | |
| 65 | | |
3467b535Travis Wilson1 years ago | 66 | foreach (ChatToolCall toolCall in toolCalls) |
| 67 | { | |
| 68 | ToolCalls.Add(toolCall); | |
| 69 | } | |
9f9f2936Jose Arriaga Maldonado2 years ago | 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> | |
2ab1a942Jose Arriaga Maldonado1 years ago | 77 | public AssistantChatMessage(ChatFunctionCall functionCall) |
| 78 | : base(ChatMessageRole.Assistant) | |
9f9f2936Jose Arriaga Maldonado2 years ago | 79 | { |
| 80 | Argument.AssertNotNull(functionCall, nameof(functionCall)); | |
| 81 | | |
| 82 | FunctionCall = functionCall; | |
| 83 | } | |
| 84 | | |
| 85 | /// <summary> | |
| 86 | /// Creates a new instance of <see cref="AssistantChatMessage"/> from a <see cref="ChatCompletion"/> with | |
| 87 | /// an <c>assistant</c> role response. | |
| 88 | /// </summary> | |
| 89 | /// <remarks> | |
| 90 | /// This constructor will copy the <c>content</c>, <c>tool_calls</c>, and <c>function_call</c> from a chat | |
| 91 | /// completion response into a new <c>assistant</c> role request message. | |
| 92 | /// </remarks> | |
| 93 | /// <param name="chatCompletion"> | |
| 94 | /// The <see cref="ChatCompletion"/> from which the conversation history request message should be created. | |
| 95 | /// </param> | |
| 96 | /// <exception cref="ArgumentException"> | |
| 97 | /// The <c>role</c> of the provided chat completion response was not <see cref="ChatMessageRole.Assistant"/>. | |
| 98 | /// </exception> | |
| 99 | public AssistantChatMessage(ChatCompletion chatCompletion) | |
3467b535Travis Wilson1 years ago | 100 | : base(ChatMessageRole.Assistant, chatCompletion?.Content) |
9f9f2936Jose Arriaga Maldonado2 years ago | 101 | { |
| 102 | Argument.AssertNotNull(chatCompletion, nameof(chatCompletion)); | |
| 103 | | |
| 104 | if (chatCompletion.Role != ChatMessageRole.Assistant) | |
| 105 | { | |
| 106 | throw new NotSupportedException($"Cannot instantiate an {nameof(AssistantChatMessage)} from a {nameof(ChatCompletion)} with role: {chatCompletion.Role}."); | |
| 107 | } | |
| 108 | | |
3467b535Travis Wilson1 years ago | 109 | Refusal = chatCompletion.Refusal; |
9f9f2936Jose Arriaga Maldonado2 years ago | 110 | FunctionCall = chatCompletion.FunctionCall; |
3467b535Travis Wilson1 years ago | 111 | foreach (ChatToolCall toolCall in chatCompletion.ToolCalls ?? []) |
| 112 | { | |
| 113 | ToolCalls.Add(toolCall); | |
| 114 | } | |
9f9f2936Jose Arriaga Maldonado2 years ago | 115 | } |
| 116 | | |
| 117 | // CUSTOM: Renamed. | |
| 118 | /// <summary> | |
| 119 | /// An optional <c>name</c> associated with the assistant message. This is typically defined with a <c>system</c> | |
| 120 | /// message and is used to differentiate between multiple participants of the same role. | |
| 121 | /// </summary> | |
| 122 | [CodeGenMember("Name")] | |
58f93c8dShivangiReja2 years ago | 123 | public string ParticipantName { get; set; } |
3467b535Travis Wilson1 years ago | 124 | |
| 125 | // CUSTOM: Common initialization for input model collection property. | |
| 126 | [CodeGenMember("ToolCalls")] | |
| 127 | public IList<ChatToolCall> ToolCalls { get; } = new ChangeTrackingList<ChatToolCall>(); | |
| 128 | | |
2ab1a942Jose Arriaga Maldonado1 years ago | 129 | [Obsolete($"This property is obsolete. Please use {nameof(ToolCalls)} instead.")] |
| 130 | public ChatFunctionCall FunctionCall { get; set; } | |
9f9f2936Jose Arriaga Maldonado2 years ago | 131 | } |