openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.0.0-beta.12

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Custom/Chat/AssistantChatMessage.cs

131lines · modecode

1using System;
2using System.Collections.Generic;
3
4namespace 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")]
12public 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.AssertNotNull(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 public AssistantChatMessage(ChatFunctionCall functionCall)
78 : base(ChatMessageRole.Assistant)
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)
100 : base(ChatMessageRole.Assistant, chatCompletion?.Content)
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
109 Refusal = chatCompletion.Refusal;
110 FunctionCall = chatCompletion.FunctionCall;
111 foreach (ChatToolCall toolCall in chatCompletion.ToolCalls ?? [])
112 {
113 ToolCalls.Add(toolCall);
114 }
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")]
123 public string ParticipantName { get; set; }
124
125 // CUSTOM: Common initialization for input model collection property.
126 [CodeGenMember("ToolCalls")]
127 public IList<ChatToolCall> ToolCalls { get; } = new ChangeTrackingList<ChatToolCall>();
128
129 [Obsolete($"This property is obsolete. Please use {nameof(ToolCalls)} instead.")]
130 public ChatFunctionCall FunctionCall { get; set; }
131}