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 · modeblame

9f9f2936Jose Arriaga Maldonado2 years ago1using 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{
2ab1a942Jose Arriaga Maldonado1 years ago14// CUSTOM: Made internal.
15internal AssistantChatMessage()
16{
17}
18
3467b535Travis Wilson1 years ago19/// <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>
26public AssistantChatMessage(IEnumerable<ChatMessageContentPart> contentParts)
27: base(ChatMessageRole.Assistant, contentParts)
9f9f2936Jose Arriaga Maldonado2 years ago28{
3467b535Travis Wilson1 years ago29Argument.AssertNotNullOrEmpty(contentParts, nameof(contentParts));
9f9f2936Jose Arriaga Maldonado2 years ago30}
31
3467b535Travis Wilson1 years ago32/// <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>
39public AssistantChatMessage(params ChatMessageContentPart[] contentParts)
40: base(ChatMessageRole.Assistant, contentParts)
41{
42Argument.AssertNotNullOrEmpty(contentParts, nameof(contentParts));
43}
9f9f2936Jose Arriaga Maldonado2 years ago44
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>
50public AssistantChatMessage(string content)
3467b535Travis Wilson1 years ago51: base(ChatMessageRole.Assistant, content)
9f9f2936Jose Arriaga Maldonado2 years ago52{
53Argument.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 ago61public AssistantChatMessage(IEnumerable<ChatToolCall> toolCalls)
62: base(ChatMessageRole.Assistant)
9f9f2936Jose Arriaga Maldonado2 years ago63{
64Argument.AssertNotNull(toolCalls, nameof(toolCalls));
65
3467b535Travis Wilson1 years ago66foreach (ChatToolCall toolCall in toolCalls)
67{
68ToolCalls.Add(toolCall);
69}
9f9f2936Jose Arriaga Maldonado2 years ago70}
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 ago77public AssistantChatMessage(ChatFunctionCall functionCall)
78: base(ChatMessageRole.Assistant)
9f9f2936Jose Arriaga Maldonado2 years ago79{
80Argument.AssertNotNull(functionCall, nameof(functionCall));
81
82FunctionCall = 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>
99public AssistantChatMessage(ChatCompletion chatCompletion)
3467b535Travis Wilson1 years ago100: base(ChatMessageRole.Assistant, chatCompletion?.Content)
9f9f2936Jose Arriaga Maldonado2 years ago101{
102Argument.AssertNotNull(chatCompletion, nameof(chatCompletion));
103
104if (chatCompletion.Role != ChatMessageRole.Assistant)
105{
106throw new NotSupportedException($"Cannot instantiate an {nameof(AssistantChatMessage)} from a {nameof(ChatCompletion)} with role: {chatCompletion.Role}.");
107}
108
3467b535Travis Wilson1 years ago109Refusal = chatCompletion.Refusal;
9f9f2936Jose Arriaga Maldonado2 years ago110FunctionCall = chatCompletion.FunctionCall;
3467b535Travis Wilson1 years ago111foreach (ChatToolCall toolCall in chatCompletion.ToolCalls ?? [])
112{
113ToolCalls.Add(toolCall);
114}
9f9f2936Jose Arriaga Maldonado2 years ago115}
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 ago123public string ParticipantName { get; set; }
3467b535Travis Wilson1 years ago124
125// CUSTOM: Common initialization for input model collection property.
126[CodeGenMember("ToolCalls")]
127public IList<ChatToolCall> ToolCalls { get; } = new ChangeTrackingList<ChatToolCall>();
128
2ab1a942Jose Arriaga Maldonado1 years ago129[Obsolete($"This property is obsolete. Please use {nameof(ToolCalls)} instead.")]
130public ChatFunctionCall FunctionCall { get; set; }
9f9f2936Jose Arriaga Maldonado2 years ago131}