openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.0.0-beta.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Custom/Chat/AssistantChatMessage.cs

110lines · 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 /// <summary> Initializes a new instance of <see cref="AssistantChatMessage"/>. </summary>
16 internal AssistantChatMessage()
17 {
18 }
19
20 // Assistant messages may present ONE OF:
21 // - Ordinary text content without tools or a function, in which case the content is required.
22 // - A list of tool calls, together with optional text content
23 // - A function call, together with optional text content
24
25 /// <summary>
26 /// Creates a new instance of <see cref="AssistantChatMessage"/> that represents ordinary text content and
27 /// does not feature tool or function calls.
28 /// </summary>
29 /// <param name="content"> The text content of the message. </param>
30 public AssistantChatMessage(string content)
31 {
32 Argument.AssertNotNull(content, nameof(content));
33
34 Role = "assistant";
35 Content = [ChatMessageContentPart.CreateTextMessageContentPart(content)];
36 ToolCalls = new ChangeTrackingList<ChatToolCall>();
37 }
38
39 /// <summary>
40 /// Creates a new instance of <see cref="AssistantChatMessage"/> that represents <c>tool_calls</c> that
41 /// were provided by the model.
42 /// </summary>
43 /// <param name="toolCalls"> The <c>tool_calls</c> made by the model. </param>
44 /// <param name="content"> Optional text content associated with the message. </param>
45 public AssistantChatMessage(IEnumerable<ChatToolCall> toolCalls, string content = null)
46 {
47 Argument.AssertNotNull(toolCalls, nameof(toolCalls));
48
49 Role = "assistant";
50 Content = (content == null)
51 ? new ChangeTrackingList<ChatMessageContentPart>()
52 : [ChatMessageContentPart.CreateTextMessageContentPart(content)];
53 ToolCalls = new List<ChatToolCall>(toolCalls);
54 }
55
56 /// <summary>
57 /// Creates a new instance of <see cref="AssistantChatMessage"/> that represents a <c>function_call</c>
58 /// (deprecated in favor of <c>tool_calls</c>) that was made by the model.
59 /// </summary>
60 /// <param name="functionCall"> The <c>function_call</c> made by the model. </param>
61 /// <param name="content"> Optional text content associated with the message. </param>
62 public AssistantChatMessage(ChatFunctionCall functionCall, string content = null)
63 {
64 Argument.AssertNotNull(functionCall, nameof(functionCall));
65
66 Role = "assistant";
67 Content = (content == null)
68 ? new ChangeTrackingList<ChatMessageContentPart>()
69 : [ChatMessageContentPart.CreateTextMessageContentPart(content)];
70 ToolCalls = new ChangeTrackingList<ChatToolCall>();
71 FunctionCall = functionCall;
72 }
73
74 /// <summary>
75 /// Creates a new instance of <see cref="AssistantChatMessage"/> from a <see cref="ChatCompletion"/> with
76 /// an <c>assistant</c> role response.
77 /// </summary>
78 /// <remarks>
79 /// This constructor will copy the <c>content</c>, <c>tool_calls</c>, and <c>function_call</c> from a chat
80 /// completion response into a new <c>assistant</c> role request message.
81 /// </remarks>
82 /// <param name="chatCompletion">
83 /// The <see cref="ChatCompletion"/> from which the conversation history request message should be created.
84 /// </param>
85 /// <exception cref="ArgumentException">
86 /// The <c>role</c> of the provided chat completion response was not <see cref="ChatMessageRole.Assistant"/>.
87 /// </exception>
88 public AssistantChatMessage(ChatCompletion chatCompletion)
89 {
90 Argument.AssertNotNull(chatCompletion, nameof(chatCompletion));
91
92 if (chatCompletion.Role != ChatMessageRole.Assistant)
93 {
94 throw new NotSupportedException($"Cannot instantiate an {nameof(AssistantChatMessage)} from a {nameof(ChatCompletion)} with role: {chatCompletion.Role}.");
95 }
96
97 Role = "assistant";
98 Content = (IList<ChatMessageContentPart>)chatCompletion.Content;
99 ToolCalls = (IList<ChatToolCall>)chatCompletion.ToolCalls;
100 FunctionCall = chatCompletion.FunctionCall;
101 }
102
103 // CUSTOM: Renamed.
104 /// <summary>
105 /// An optional <c>name</c> associated with the assistant message. This is typically defined with a <c>system</c>
106 /// message and is used to differentiate between multiple participants of the same role.
107 /// </summary>
108 [CodeGenMember("Name")]
109 public string ParticipantName { get; init; }
110}