openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/customize-openairesponsescontext-attribute

Branches

Tags

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

Clone

HTTPS

Download ZIP

OpenAI.Responses/src/Custom/Items/Message/MessageRole.Serialization.cs

36lines · modecode

1using System;
2
3namespace OpenAI.Responses;
4
5internal static partial class MessageRoleExtensions
6{
7 public static string ToSerialString(this MessageRole value) => value switch
8 {
9 MessageRole.Assistant => InternalResponsesMessageRole.Assistant.ToString(),
10 MessageRole.Developer => InternalResponsesMessageRole.Developer.ToString(),
11 MessageRole.System => InternalResponsesMessageRole.System.ToString(),
12 MessageRole.User => InternalResponsesMessageRole.User.ToString(),
13 _ => throw new ArgumentOutOfRangeException(nameof(value), value, "Unknown MessageRole value."),
14 };
15
16 public static MessageRole ToMessageRole(this string value)
17 {
18 if (StringComparer.OrdinalIgnoreCase.Equals(value, InternalResponsesMessageRole.Assistant.ToString()))
19 {
20 return MessageRole.Assistant;
21 }
22 if (StringComparer.OrdinalIgnoreCase.Equals(value, InternalResponsesMessageRole.Developer.ToString()))
23 {
24 return MessageRole.Developer;
25 }
26 if (StringComparer.OrdinalIgnoreCase.Equals(value, InternalResponsesMessageRole.System.ToString()))
27 {
28 return MessageRole.System;
29 }
30 if (StringComparer.OrdinalIgnoreCase.Equals(value, InternalResponsesMessageRole.User.ToString()))
31 {
32 return MessageRole.User;
33 }
34 return MessageRole.Unknown;
35 }
36}
37