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/Assistants/AssistantResponseFormat.cs

107lines · modecode

1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4
5namespace OpenAI.Assistants;
6
7/// <summary>
8/// Specifies the format that the model must output. Compatible with GPT-4o, GPT-4 Turbo, and all GPT-3.5 Turbo models since gpt-3.5-turbo-1106.
9/// </summary>
10/// <remarks>
11/// <para>
12/// Setting to { "type": "json_object" } enables JSON mode, which guarantees the message the model generates is valid JSON.
13/// </para>
14/// <para>
15/// Important: when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message.Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request.Also note that the message content may be partially cut off if finish_reason= "length", which indicates the generation exceeded max_tokens or the conversation exceeded the max context length.
16/// </para>
17/// </remarks>
18[CodeGenModel("AssistantResponseFormat")]
19public partial class AssistantResponseFormat
20{
21 private readonly string _plainTextValue;
22 private readonly string _objectType;
23 private readonly IDictionary<string, BinaryData> _serializedAdditionalRawData;
24
25 private const string AutoValue = "auto";
26 private const string TextValue = "text";
27 private const string JsonObjectValue = "json_object";
28
29 /// <summary>
30 /// Default option. Specifies that the model should automatically determine whether it should respond with
31 /// plain text or another format.
32 /// </summary>
33 public static AssistantResponseFormat Auto { get; }
34 = new(plainTextValue: AutoValue, objectType: null, serializedAdditionalRawData: null);
35
36 /// <summary>
37 /// Specifies that the model should respond with plain text.
38 /// </summary>
39 public static AssistantResponseFormat Text { get; }
40 = new(plainTextValue: null, objectType: TextValue, serializedAdditionalRawData: null);
41
42 /// <summary>
43 /// Specifies that the model should reply with a structured JSON object, enabling JSON mode.
44 /// </summary>
45 /// <remarks>
46 /// **Important:** when using JSON mode, you **must** also instruct the model to produce JSON yourself via a
47 /// system or user message. Without this, the model may generate an unending stream of whitespace until the
48 /// generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that
49 /// the message content may be partially cut off if `finish_reason="length"`, which indicates the generation
50 /// exceeded `max_tokens` or the conversation exceeded the max context length.
51 /// </remarks>
52 public static AssistantResponseFormat JsonObject { get; }
53 = new(plainTextValue: null, objectType: JsonObjectValue, serializedAdditionalRawData: null);
54
55 /// <summary>
56 /// Creates a new instance of <see cref="AssistantResponseFormat"/> for mocking.
57 /// </summary>
58 protected AssistantResponseFormat()
59 { }
60
61 internal AssistantResponseFormat(string plainTextValue, string objectType, IDictionary<string, BinaryData> serializedAdditionalRawData)
62 {
63 _plainTextValue = plainTextValue;
64 _objectType = objectType;
65 _serializedAdditionalRawData = serializedAdditionalRawData ?? new ChangeTrackingDictionary<string, BinaryData>();
66 }
67
68 /// <inheritdoc />
69 public static bool operator ==(AssistantResponseFormat left, AssistantResponseFormat right) => left.Equals(right);
70 /// <inheritdoc />
71 public static bool operator !=(AssistantResponseFormat left, AssistantResponseFormat right) => !left.Equals(right);
72
73 /// <inheritdoc />
74 public static implicit operator AssistantResponseFormat(string value)
75 {
76 if (string.Equals(value, AutoValue, StringComparison.InvariantCultureIgnoreCase))
77 {
78 return Auto;
79 }
80 if (string.Equals(value, TextValue, StringComparison.InvariantCultureIgnoreCase))
81 {
82 return Text;
83 }
84 if (string.Equals(value, JsonObjectValue, StringComparison.InvariantCultureIgnoreCase))
85 {
86 return JsonObject;
87 }
88 else
89 {
90 return new(plainTextValue: null, objectType: value, serializedAdditionalRawData: null);
91 }
92 }
93
94 /// <inheritdoc />
95 [EditorBrowsable(EditorBrowsableState.Never)]
96 public override bool Equals(object obj) => obj is AssistantResponseFormat other && Equals(other);
97 /// <inheritdoc />
98 public bool Equals(AssistantResponseFormat other)
99 => string.Equals(_plainTextValue, other?._plainTextValue, StringComparison.InvariantCultureIgnoreCase)
100 && string.Equals(_objectType, other?._objectType, StringComparison.InvariantCultureIgnoreCase);
101
102 /// <inheritdoc />
103 [EditorBrowsable(EditorBrowsableState.Never)]
104 public override int GetHashCode() => $"{_plainTextValue}/{_objectType}".GetHashCode();
105 /// <inheritdoc />
106 public override string ToString() => _plainTextValue ?? _objectType;
107}
108