Skip to content

Commit 137c10b

Browse files
committed
Update models including gpt-4-turbo and dalle-3
1 parent fb551b7 commit 137c10b

15 files changed

+445
-110
lines changed

OpenAI_API/Chat/ChatEndpoint.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class ChatEndpoint : EndpointBase, IChatEndpoint
1616
/// <summary>
1717
/// This allows you to set default parameters for every request, for example to set a default temperature or max tokens. For every request, if you do not have a parameter set on the request but do have it set here as a default, the request will automatically pick up the default value.
1818
/// </summary>
19-
public ChatRequest DefaultChatRequestArgs { get; set; } = new ChatRequest() { Model = Model.ChatGPTTurbo };
19+
public ChatRequest DefaultChatRequestArgs { get; set; } = new ChatRequest() { Model = Model.DefaultChatModel };
2020

2121
/// <summary>
2222
/// The name of the endpoint, which is the final path segment in the API URL. For example, "completions".
@@ -33,7 +33,7 @@ internal ChatEndpoint(OpenAIAPI api) : base(api) { }
3333
/// Creates an ongoing chat which can easily encapsulate the conversation. This is the simplest way to use the Chat endpoint.
3434
/// </summary>
3535
/// <param name="defaultChatRequestArgs">Allows setting the parameters to use when calling the ChatGPT API. Can be useful for setting temperature, presence_penalty, and more. See <see href="https://platform.openai.com/docs/api-reference/chat/create">OpenAI documentation for a list of possible parameters to tweak.</see></param>
36-
/// <returns>A <see cref="Conversation"/> which encapulates a back and forth chat betwen a user and an assistant.</returns>
36+
/// <returns>A <see cref="Conversation"/> which encapsulates a back and forth chat between a user and an assistant.</returns>
3737
public Conversation CreateConversation(ChatRequest defaultChatRequestArgs = null)
3838
{
3939
return new Conversation(this, defaultChatRequestArgs: defaultChatRequestArgs ?? DefaultChatRequestArgs);

OpenAI_API/Chat/ChatRequest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class ChatRequest
4343
public int? NumChoicesPerMessage { get; set; }
4444

4545
/// <summary>
46-
/// Specifies where the results should stream and be returned at one time. Do not set this yourself, use the appropriate methods on <see cref="CompletionEndpoint"/> instead.
46+
/// Specifies where the results should stream and be returned at one time. Do not set this yourself, use the appropriate methods on <see cref="OpenAI_API.Completions.CompletionEndpoint"/> instead.
4747
/// </summary>
4848
[JsonProperty("stream")]
4949
public bool Stream { get; internal set; } = false;

OpenAI_API/Chat/Conversation.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ public OpenAI_API.Models.Model Model
4949
/// Creates a new conversation with ChatGPT chat
5050
/// </summary>
5151
/// <param name="endpoint">A reference to the API endpoint, needed for API requests. Generally should be <see cref="OpenAIAPI.Chat"/>.</param>
52-
/// <param name="model">Optionally specify the model to use for ChatGPT requests. If not specified, used <paramref name="defaultChatRequestArgs"/>.Model or falls back to <see cref="OpenAI_API.Models.Model.ChatGPTTurbo"/></param>
52+
/// <param name="model">Optionally specify the model to use for ChatGPT requests. If not specified, used <paramref name="defaultChatRequestArgs"/>.Model or falls back to <see cref="OpenAI_API.Models.Model.DefaultChatModel"/></param>
5353
/// <param name="defaultChatRequestArgs">Allows setting the parameters to use when calling the ChatGPT API. Can be useful for setting temperature, presence_penalty, and more. See <see href="https://platform.openai.com/docs/api-reference/chat/create">OpenAI documentation for a list of possible parameters to tweak.</see></param>
5454
public Conversation(ChatEndpoint endpoint, OpenAI_API.Models.Model model = null, ChatRequest defaultChatRequestArgs = null)
5555
{
5656
RequestParameters = new ChatRequest(defaultChatRequestArgs);
5757
if (model != null)
5858
RequestParameters.Model = model;
5959
if (RequestParameters.Model == null)
60-
RequestParameters.Model = Models.Model.ChatGPTTurbo;
60+
RequestParameters.Model = Models.Model.DefaultChatModel;
6161

6262
_Messages = new List<ChatMessage>();
6363
_endpoint = endpoint;
@@ -66,9 +66,9 @@ public Conversation(ChatEndpoint endpoint, OpenAI_API.Models.Model model = null,
6666
}
6767

6868
/// <summary>
69-
/// A list of messages exchanged so far. Do not modify this list directly. Instead, use <see cref="AppendMessage(ChatMessage)"/>, <see cref="AppendUserInput(string)"/>, <see cref="AppendSystemMessage(string)"/>, or <see cref="AppendExampleChatbotOutput(string)"/>.
69+
/// A list of messages exchanged so far. To append to this list, use <see cref="AppendMessage(ChatMessage)"/>, <see cref="AppendUserInput(string)"/>, <see cref="AppendSystemMessage(string)"/>, or <see cref="AppendExampleChatbotOutput(string)"/>.
7070
/// </summary>
71-
public IReadOnlyList<ChatMessage> Messages { get => _Messages; }
71+
public IList<ChatMessage> Messages { get => _Messages; }
7272
private List<ChatMessage> _Messages;
7373

7474
/// <summary>

OpenAI_API/Chat/IChatEndpoint.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public interface IChatEndpoint
1919
/// Creates an ongoing chat which can easily encapsulate the conversation. This is the simplest way to use the Chat endpoint.
2020
/// </summary>
2121
/// <param name="defaultChatRequestArgs">Allows setting the parameters to use when calling the ChatGPT API. Can be useful for setting temperature, presence_penalty, and more. See <see href="https://platform.openai.com/docs/api-reference/chat/create">OpenAI documentation for a list of possible parameters to tweak.</see></param>
22-
/// <returns>A <see cref="Conversation"/> which encapulates a back and forth chat betwen a user and an assistant.</returns>
22+
/// <returns>A <see cref="Conversation"/> which encapsulates a back and forth chat between a user and an assistant.</returns>
2323
Conversation CreateConversation(ChatRequest defaultChatRequestArgs = null);
2424

2525

OpenAI_API/Completions/CompletionRequest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class CompletionRequest
1313
/// ID of the model to use. You can use <see cref="ModelsEndpoint.GetModelsAsync()"/> to see all of your available models, or use a standard model like <see cref="Model.DavinciText"/>.
1414
/// </summary>
1515
[JsonProperty("model")]
16-
public string Model { get; set; } = OpenAI_API.Models.Model.DavinciText;
16+
public string Model { get; set; } = OpenAI_API.Models.Model.DefaultModel;
1717

1818
/// <summary>
1919
/// This is only used for serializing the request into JSON, do not use it directly.

OpenAI_API/Images/IImageGenerationEndpoint.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Threading.Tasks;
2+
using OpenAI_API.Models;
23

34
namespace OpenAI_API.Images
45
{
@@ -18,7 +19,8 @@ public interface IImageGenerationEndpoint
1819
/// Ask the API to Creates an image given a prompt.
1920
/// </summary>
2021
/// <param name="input">A text description of the desired image(s)</param>
22+
/// <param name="model">The model to use for generating the image. Defaults to <see cref="OpenAI_API.Models.Model.DALLE2"/>.</param>
2123
/// <returns>Asynchronously returns the image result. Look in its <see cref="Data.Url"/> </returns>
22-
Task<ImageResult> CreateImageAsync(string input);
24+
Task<ImageResult> CreateImageAsync(string input, Model model = null);
2325
}
2426
}

OpenAI_API/Images/ImageGenerationEndpoint.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ internal ImageGenerationEndpoint(OpenAIAPI api) : base(api) { }
2626
/// Ask the API to Creates an image given a prompt.
2727
/// </summary>
2828
/// <param name="input">A text description of the desired image(s)</param>
29+
/// <param name="model">The model to use for generating the image. Defaults to <see cref="OpenAI_API.Models.Model.DALLE2"/>.</param>
2930
/// <returns>Asynchronously returns the image result. Look in its <see cref="Data.Url"/> </returns>
30-
public async Task<ImageResult> CreateImageAsync(string input)
31+
public async Task<ImageResult> CreateImageAsync(string input, Model model = null)
3132
{
32-
ImageGenerationRequest req = new ImageGenerationRequest(prompt: input);
33+
ImageGenerationRequest req = new ImageGenerationRequest(prompt: input, model: model);
3334
return await CreateImageAsync(req);
3435
}
3536

OpenAI_API/Images/ImageGenerationRequest.cs

+107-7
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,40 @@
77

88
namespace OpenAI_API.Images
99
{
10-
/// <summary>
10+
/// <summary>
1111
/// Represents a request to the Images API. Mostly matches the parameters in <see href="https://platform.openai.com/docs/api-reference/images/create">the OpenAI docs</see>, although some have been renamed or expanded into single/multiple properties for ease of use.
1212
/// </summary>
13-
public class ImageGenerationRequest
14-
{
13+
public class ImageGenerationRequest
14+
{
15+
private int? numOfImages = 1;
16+
private ImageSize size = ImageSize._1024;
17+
private string quality = "standard";
18+
1519
/// <summary>
1620
/// A text description of the desired image(s). The maximum length is 1000 characters.
1721
/// </summary>
1822
[JsonProperty("prompt")]
1923
public string Prompt { get; set; }
2024

2125
/// <summary>
22-
/// How many different choices to request for each prompt. Defaults to 1.
26+
/// How many different choices to request for each prompt. Defaults to 1. Only for DALL-E 2. For DALL-E 3, only 1 is allowed.
2327
/// </summary>
2428
[JsonProperty("n")]
25-
public int? NumOfImages { get; set; } = 1;
29+
public int? NumOfImages
30+
{
31+
get
32+
{
33+
if (this.Model == OpenAI_API.Models.Model.DALLE3 && numOfImages != 1)
34+
throw new ArgumentException("For DALL-E 3, only 1 NumOfImages is allowed.");
35+
return numOfImages;
36+
}
37+
set => numOfImages = value;
38+
}
39+
/// <summary>
40+
/// The model to use for this request. Defaults to DALL-E 2.
41+
/// </summary>
42+
[JsonProperty("model")]
43+
public string Model { get; set; } = OpenAI_API.Models.Model.DALLE2;
2644

2745
/// <summary>
2846
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Optional.
@@ -31,10 +49,51 @@ public class ImageGenerationRequest
3149
public string User { get; set; }
3250

3351
/// <summary>
34-
/// The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024. Defauls to 1024x1024
52+
/// The size of the generated images. Defaults to 1024x1024.
3553
/// </summary>
3654
[JsonProperty("size"), JsonConverter(typeof(ImageSize.ImageSizeJsonConverter))]
37-
public ImageSize Size { get; set; }
55+
public ImageSize Size
56+
{
57+
get
58+
{
59+
if (this.Model == OpenAI_API.Models.Model.DALLE3 && (this.size == ImageSize._256 || this.size == ImageSize._512))
60+
throw new ArgumentException("For DALL-E 3, only 1024x1024, 1024x1792, or 1792x1024 is allowed.");
61+
if (this.Model == OpenAI_API.Models.Model.DALLE2 && (this.size == ImageSize._1792x1024 || this.size == ImageSize._1024x1792))
62+
throw new ArgumentException("For DALL-E 2, only 256x256, 512x512, or 1024x1024 is allowed.");
63+
return size;
64+
}
65+
set => size = value;
66+
}
67+
68+
/// <summary>
69+
/// By default, images are generated at `standard` quality, but when using DALL·E 3 you can set quality to `hd` for enhanced detail. Square, standard quality images are the fastest to generate.
70+
/// </summary>
71+
[JsonProperty("quality", NullValueHandling=NullValueHandling.Ignore)]
72+
public string Quality
73+
{
74+
get
75+
{
76+
if (this.Model == OpenAI_API.Models.Model.DALLE2 && this.quality == "hd")
77+
throw new ArgumentException("For DALL-E 2, hd quality is not available.");
78+
if (this.Model == OpenAI_API.Models.Model.DALLE3 && this.quality == "standard")
79+
return null;
80+
return quality;
81+
}
82+
set
83+
{
84+
switch (value.ToLower().Trim())
85+
{
86+
case "standard":
87+
quality="standard";
88+
break;
89+
case "hd":
90+
quality = "hd";
91+
break;
92+
default:
93+
throw new ArgumentException("Quality must be either 'standard' or 'hd'.");
94+
}
95+
}
96+
}
3897

3998
/// <summary>
4099
/// The format in which the generated images are returned. Must be one of url or b64_json. Defaults to Url.
@@ -50,6 +109,47 @@ public ImageGenerationRequest()
50109

51110
}
52111

112+
/// <summary>
113+
/// Creates a new <see cref="ImageGenerationRequest"/> with the specified parameters
114+
/// </summary>
115+
/// <param name="prompt">A text description of the desired image(s). The maximum length is 1000 characters.</param>
116+
/// <param name="model">The model to use for this request. Defaults to DALL-E 2.</param>
117+
/// <param name="size">The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024.</param>
118+
/// <param name="quality">By default, images are generated at `standard` quality, but when using DALL·E 3 you can set quality to `hd` for enhanced detail.</param>
119+
/// <param name="user">A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.</param>
120+
/// <param name="responseFormat">The format in which the generated images are returned. Must be one of url or b64_json.</param>
121+
public ImageGenerationRequest(
122+
string prompt,
123+
Model model,
124+
ImageSize size = null,
125+
string quality = "standard",
126+
string user = null,
127+
ImageResponseFormat responseFormat = null)
128+
{
129+
this.Prompt = prompt;
130+
this.Model = model ?? OpenAI_API.Models.Model.DALLE2;
131+
this.Quality = quality ?? "standard";
132+
this.User = user;
133+
this.Size = size ?? ImageSize._1024;
134+
this.ResponseFormat = responseFormat ?? ImageResponseFormat.Url;
135+
136+
// check for incompatible parameters
137+
if (this.Model == OpenAI_API.Models.Model.DALLE3)
138+
{
139+
if (this.Size == ImageSize._256 || this.Size == ImageSize._512)
140+
throw new ArgumentException("For DALL-E 3, only sizes 1024x1024, 1024x1792, or 1792x1024 are allowed.");
141+
if (this.quality != "standard" && this.quality != "hd")
142+
throw new ArgumentException("Quality must be one of 'standard' or 'hd'");
143+
}
144+
else
145+
{
146+
if (this.Size == ImageSize._1792x1024 || this.Size == ImageSize._1024x1792)
147+
throw new ArgumentException("For DALL-E 2, only sizes 256x256, 512x512, or 1024x1024 are allowed.");
148+
if (this.quality != "standard")
149+
throw new ArgumentException("For DALL-E 2, only 'standard' quality is available");
150+
}
151+
}
152+
53153
/// <summary>
54154
/// Creates a new <see cref="ImageGenerationRequest"/> with the specified parameters
55155
/// </summary>

OpenAI_API/Images/ImageSize.cs

+45-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Newtonsoft.Json;
2+
using OpenAI_API.Models;
23
using System;
34
using System.Collections.Generic;
45
using System.Text;
@@ -10,23 +11,33 @@ namespace OpenAI_API.Images
1011
/// </summary>
1112
public class ImageSize
1213
{
13-
private ImageSize(string value) { Value = value; }
14+
internal ImageSize(string value) { Value = value; }
1415

1516
private string Value { get; set; }
1617

1718
/// <summary>
18-
/// Requests an image that is 256x256
19+
/// Only for DALL-E 2. Requests an image that is 256x256
1920
/// </summary>
2021
public static ImageSize _256 { get { return new ImageSize("256x256"); } }
2122
/// <summary>
22-
/// Requests an image that is 512x512
23+
/// Only for DALL-E 2. Requests an image that is 512x512
2324
/// </summary>
2425
public static ImageSize _512 { get { return new ImageSize("512x512"); } }
2526
/// <summary>
26-
/// Requests and image that is 1024x1024
27+
/// Works with both DALL-E 2 and 3. Requests and image that is 1024x1024.
2728
/// </summary>
2829
public static ImageSize _1024 { get { return new ImageSize("1024x1024"); } }
2930

31+
/// <summary>
32+
/// Only for DALL-E 3. Requests a tall image that is 1024x1792.
33+
/// </summary>
34+
public static ImageSize _1024x1792 { get { return new ImageSize("1024x1792"); } }
35+
36+
/// <summary>
37+
/// Only for DALL-E 3. Requests a wide image that is 1792x1024.
38+
/// </summary>
39+
public static ImageSize _1792x1024 { get { return new ImageSize("1792x1024"); } }
40+
3041
/// <summary>
3142
/// Gets the string value for this size to pass to the API
3243
/// </summary>
@@ -36,7 +47,37 @@ public override string ToString()
3647
return Value;
3748
}
3849

50+
/// <summary>
51+
/// Returns true is the string value of the sizes match
52+
/// </summary>
53+
/// <param name="obj">The other object to compare to</param>
54+
/// <returns>True is the sizes are the same</returns>
55+
public override bool Equals(object obj)
56+
{
57+
if (obj is null)
58+
return false;
59+
else if (obj is ImageSize)
60+
return this.Value.Equals(((ImageSize)obj).Value);
61+
else if (obj is string)
62+
return this.Value.Equals((string)obj);
63+
else
64+
return false;
65+
}
66+
67+
/// <inheritdoc/>
68+
public override int GetHashCode()
69+
{
70+
return Value.GetHashCode();
71+
}
3972

73+
public static bool operator ==(ImageSize a, ImageSize b)
74+
{
75+
return a.Equals(b);
76+
}
77+
public static bool operator !=(ImageSize a, ImageSize b)
78+
{
79+
return !a.Equals(b);
80+
}
4081

4182
/// <summary>
4283
/// Gets the string value for this size to pass to the API

0 commit comments

Comments
 (0)