openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.2.0-beta.1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/Custom/Chat/Messages/AssistantChatMessage.cs

153lines · modecode

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