Skip to content

Commit dafb02e

Browse files
committed
feat: backwards-compatible constructors
1 parent d23797c commit dafb02e

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

OpenAI_API/OpenAIAPI.cs

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,49 @@ public class OpenAIAPI: IOpenAI
2020
/// <summary>The HTTP client configured with the authentication headers.</summary>
2121
internal HttpClient Client { get; }
2222

23+
private OpenAIAPI()
24+
{
25+
this.Completions = new CompletionEndpoint(this);
26+
this.Models = new ModelsEndpoint(this);
27+
this.Files = new FilesEndpoint(this);
28+
this.Embeddings = new EmbeddingEndpoint(this);
29+
}
30+
2331
/// <summary>
2432
/// Creates a new entry point to the OpenAPI API, handling auth and allowing access to the various API endpoints
2533
/// </summary>
2634
/// <param name="httpClient">The HTTP client configured with the authentication headers.</param>
27-
public OpenAIAPI(
28-
HttpClient httpClient)
29-
{
30-
this.Client = httpClient;
31-
this.Completions = new CompletionEndpoint(this);
32-
this.Models = new ModelsEndpoint(this);
33-
this.Files = new FilesEndpoint(this);
34-
this.Embeddings = new EmbeddingEndpoint(this);
35-
}
35+
public OpenAIAPI(HttpClient httpClient) : this() =>
36+
this.Client = httpClient;
3637

37-
/// <summary>
38-
/// Text generation is the core function of the API. You give the API a prompt, and it generates a completion. The way you “program” the API to do a task is by simply describing the task in plain english or providing a few written examples. This simple approach works for a wide range of use cases, including summarization, translation, grammar correction, question answering, chatbots, composing emails, and much more (see the prompt library for inspiration).
39-
/// </summary>
40-
public CompletionEndpoint Completions { get; }
38+
/// <summary>
39+
/// Creates a new entry point to the OpenAPI API, handling auth and allowing access to the various API endpoints
40+
/// </summary>
41+
/// <param name="auth">The authentication details for the API</param>
42+
[Obsolete("""
43+
This constructor will generate a new HTTP client every time it is called.
44+
Do not use this in scenarios where multiple instances of the API are required.
45+
This is provided for backwards compatibility, use .NET Dependency Injection instead.
46+
""", false)]
47+
public OpenAIAPI(APIAuthentication auth) :
48+
this(EndpointBase.ConfigureClient(new HttpClient(), auth)) { }
49+
50+
/// <summary>
51+
/// Creates a new entry point to the OpenAPI API, handling auth and allowing access to the various API endpoints
52+
/// </summary>
53+
/// <param name="key">The authentication key for the API</param>
54+
[Obsolete("""
55+
This constructor will generate a new HTTP client every time it is called.
56+
Do not use this in scenarios where multiple instances of the API are required.
57+
This is provided for backwards compatibility, use .NET Dependency Injection instead.
58+
""", false)]
59+
public OpenAIAPI(string key) :
60+
this(EndpointBase.ConfigureClient(new HttpClient(), new APIAuthentication(key))) { }
61+
62+
/// <summary>
63+
/// Text generation is the core function of the API. You give the API a prompt, and it generates a completion. The way you “program” the API to do a task is by simply describing the task in plain english or providing a few written examples. This simple approach works for a wide range of use cases, including summarization, translation, grammar correction, question answering, chatbots, composing emails, and much more (see the prompt library for inspiration).
64+
/// </summary>
65+
public CompletionEndpoint Completions { get; }
4166

4267
/// <summary>
4368
/// The API lets you transform text into a vector (list) of floating point numbers. The distance between two vectors measures their relatedness. Small distances suggest high relatedness and large distances suggest low relatedness.

0 commit comments

Comments
 (0)