Skip to content

Commit f4d9d9d

Browse files
committedJan 12, 2023
Update Engine to Model, deprecate the Search endpoint, get readme up to date, other minor fixes
1 parent 7d1d1ba commit f4d9d9d

13 files changed

+934
-858
lines changed
 

‎OpenAI_API/Completions/CompletionEndpoint.cs

+340-334
Large diffs are not rendered by default.

‎OpenAI_API/Completions/CompletionRequest.cs

+243-205
Large diffs are not rendered by default.

‎OpenAI_API/Completions/CompletionResult.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ public class CompletionResult
6767
public DateTime Created => DateTimeOffset.FromUnixTimeSeconds(CreatedUnixTime).DateTime;
6868

6969
/// <summary>
70-
/// Which model was used to generate this result. Be sure to check <see cref="Engine.ModelRevision"/> for the specific revision.
70+
/// Which model was used to generate this result.
7171
/// </summary>
7272
[JsonProperty("model")]
73-
public Engine Model { get; set; }
73+
public Model Model { get; set; }
7474

7575
/// <summary>
7676
/// The completions returned by the API. Depending on your request, there may be 1 or many choices.

‎OpenAI_API/Engine/Engine.cs

-120
This file was deleted.

‎OpenAI_API/Model/Model.cs

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Net.Http;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace OpenAI_API
9+
{
10+
/// <summary>
11+
/// Represents a language model
12+
/// </summary>
13+
public class Model
14+
{
15+
/// <summary>
16+
/// The id/name of the model
17+
/// </summary>
18+
[JsonProperty("id")]
19+
public string ModelID { get; set; }
20+
21+
/// <summary>
22+
/// The owner of this model. Generally "openai" is a generic OpenAI model, or the organization if a custom or finetuned model.
23+
/// </summary>
24+
[JsonProperty("owned_by")]
25+
public string OwnedBy { get; set; }
26+
27+
/// <summary>
28+
/// Allows an model to be implicitly cast to the string of its <see cref="ModelID"/>
29+
/// </summary>
30+
/// <param name="model">The <see cref="Model"/> to cast to a string.</param>
31+
public static implicit operator string(Model model)
32+
{
33+
return model?.ModelID;
34+
}
35+
36+
/// <summary>
37+
/// Allows a string to be implicitly cast as an <see cref="Model"/> with that <see cref="ModelID"/>
38+
/// </summary>
39+
/// <param name="name">The id/<see cref="ModelID"/> to use</param>
40+
public static implicit operator Model(string name)
41+
{
42+
return new Model(name);
43+
}
44+
45+
/// <summary>
46+
/// Represents an Model with the given id/<see cref="ModelID"/>
47+
/// </summary>
48+
/// <param name="name">The id/<see cref="ModelID"/> to use.
49+
/// If the <paramref name="name"/> contains a colon (as is the case in the API's <see cref="CompletionResult.Model"/> response),
50+
/// the part before the colon is treated as the id/<see cref="ModelID"/> and the following portion is considered the <see cref="ModelRevision"/>
51+
/// </param>
52+
public Model(string name)
53+
{
54+
this.ModelID = name;
55+
}
56+
57+
/// <summary>
58+
/// Represents a generic Model/model
59+
/// </summary>
60+
public Model()
61+
{
62+
63+
}
64+
65+
66+
67+
/// <summary>
68+
/// Capable of very simple tasks, usually the fastest model in the GPT-3 series, and lowest cost
69+
/// </summary>
70+
public static Model AdaText => new Model("text-ada-001") { OwnedBy = "openai" };
71+
72+
/// <summary>
73+
/// Capable of straightforward tasks, very fast, and lower cost.
74+
/// </summary>
75+
public static Model BabbageText => new Model("text-babbage-001") { OwnedBy = "openai" };
76+
77+
/// <summary>
78+
/// Very capable, but faster and lower cost than Davinci.
79+
/// </summary>
80+
public static Model CurieText => new Model("text-curie-001") { OwnedBy = "openai" };
81+
82+
/// <summary>
83+
/// Most capable GPT-3 model. Can do any task the other models can do, often with higher quality, longer output and better instruction-following. Also supports inserting completions within text.
84+
/// </summary>
85+
public static Model DavinciText => new Model("text-davinci-003") { OwnedBy = "openai" };
86+
87+
/// <summary>
88+
/// Almost as capable as Davinci Codex, but slightly faster. This speed advantage may make it preferable for real-time applications.
89+
/// </summary>
90+
public static Model CushmanCode => new Model("code-cushman-001") { OwnedBy = "openai" };
91+
92+
/// <summary>
93+
/// Most capable Codex model. Particularly good at translating natural language to code. In addition to completing code, also supports inserting completions within code.
94+
/// </summary>
95+
public static Model DavinciCode => new Model("code-davinci-002") { OwnedBy = "openai" };
96+
97+
/// <summary>
98+
/// OpenAI offers one second-generation embedding model for use with the embeddings API endpoint.
99+
/// </summary>
100+
public static Model AdaTextEmbedding => new Model("text-embedding-ada-002") { OwnedBy = "openai" };
101+
102+
103+
104+
/// <summary>
105+
/// Gets more details about this Model from the API, specifically properties such as <see cref="OwnedBy"/> and permissions.
106+
/// </summary>
107+
/// <param name="auth">API authentication in order to call the API endpoint. If not specified, attempts to use a default.</param>
108+
/// <returns>Asynchronously returns an Model with all relevant properties filled in</returns>
109+
public async Task<Model> RetrieveModelDetailsAsync(APIAuthentication auth = null)
110+
{
111+
return await ModelsEndpoint.RetrieveModelDetailsAsync(this.ModelID, auth);
112+
}
113+
}
114+
}

‎OpenAI_API/Engine/EnginesEndpoint.cs ‎OpenAI_API/Model/ModelsEndpoint.cs

+35-30
Original file line numberDiff line numberDiff line change
@@ -11,73 +11,77 @@
1111
namespace OpenAI_API
1212
{
1313
/// <summary>
14-
/// The API endpoint for querying available Engines/models
14+
/// The API endpoint for querying available models
1515
/// </summary>
16-
public class EnginesEndpoint
16+
public class ModelsEndpoint
1717
{
1818
OpenAIAPI Api;
1919

2020
/// <summary>
21-
/// Constructor of the api endpoint. Rather than instantiating this yourself, access it through an instance of <see cref="OpenAIAPI"/> as <see cref="OpenAIAPI.Engines"/>.
21+
/// Constructor of the api endpoint. Rather than instantiating this yourself, access it through an instance of <see cref="OpenAIAPI"/> as <see cref="OpenAIAPI.Models"/>.
2222
/// </summary>
2323
/// <param name="api"></param>
24-
internal EnginesEndpoint(OpenAIAPI api)
24+
internal ModelsEndpoint(OpenAIAPI api)
2525
{
2626
this.Api = api;
2727
}
2828

2929
/// <summary>
30-
/// List all engines via the API
30+
/// List all models via the API
3131
/// </summary>
32-
/// <returns>Asynchronously returns the list of all <see cref="Engine"/>s</returns>
33-
public Task<List<Engine>> GetEnginesAsync()
32+
/// <returns>Asynchronously returns the list of all <see cref="Model"/>s</returns>
33+
public Task<List<Model>> GetModelsAsync()
3434
{
35-
return GetEnginesAsync(Api?.Auth);
35+
return GetModelsAsync(Api?.Auth);
3636
}
3737

3838
/// <summary>
39-
/// Get details about a particular Engine from the API, specifically properties such as <see cref="Engine.Owner"/> and <see cref="Engine.Ready"/>
39+
/// Get details about a particular Model from the API, specifically properties such as <see cref="Model.Owner"/> and <see cref="Model.Ready"/>
4040
/// </summary>
41-
/// <param name="id">The id/name of the engine to get more details about</param>
42-
/// <returns>Asynchronously returns the <see cref="Engine"/> with all available properties</returns>
43-
public Task<Engine> RetrieveEngineDetailsAsync(string id)
41+
/// <param name="id">The id/name of the model to get more details about</param>
42+
/// <returns>Asynchronously returns the <see cref="Model"/> with all available properties</returns>
43+
public Task<Model> RetrieveModelDetailsAsync(string id)
4444
{
45-
return RetrieveEngineDetailsAsync(id, Api?.Auth);
45+
return RetrieveModelDetailsAsync(id, Api?.Auth);
4646
}
4747

4848
/// <summary>
49-
/// List all engines via the API
49+
/// List all models via the API
5050
/// </summary>
5151
/// <param name="auth">API authentication in order to call the API endpoint. If not specified, attempts to use a default.</param>
52-
/// <returns>Asynchronously returns the list of all <see cref="Engine"/>s</returns>
53-
public static async Task<List<Engine>> GetEnginesAsync(APIAuthentication auth = null)
52+
/// <returns>Asynchronously returns the list of all <see cref="Model"/>s</returns>
53+
public static async Task<List<Model>> GetModelsAsync(APIAuthentication auth = null)
5454
{
55-
HttpClient client = new HttpClient();
55+
if (auth.ThisOrDefault()?.ApiKey is null)
56+
{
57+
throw new AuthenticationException("You must provide API authentication. Please refer to https://github.com/OkGoDoIt/OpenAI-API-dotnet#authentication for details.");
58+
}
59+
HttpClient client = new HttpClient();
5660
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", auth.ThisOrDefault().ApiKey);
5761
client.DefaultRequestHeaders.Add("User-Agent", "okgodoit/dotnet_openai_api");
5862
if (!string.IsNullOrEmpty(auth.ThisOrDefault().OpenAIOrganization)) client.DefaultRequestHeaders.Add("OpenAI-Organization", auth.ThisOrDefault().OpenAIOrganization);
5963

60-
var response = await client.GetAsync(@"https://api.openai.com/v1/engines");
64+
var response = await client.GetAsync(@"https://api.openai.com/v1/models");
6165
string resultAsString = await response.Content.ReadAsStringAsync();
6266

6367
if (response.IsSuccessStatusCode)
6468
{
65-
var engines = JsonConvert.DeserializeObject<JsonHelperRoot>(resultAsString).data;
66-
return engines;
69+
var models = JsonConvert.DeserializeObject<JsonHelperRoot>(resultAsString).data;
70+
return models;
6771
}
6872
else
6973
{
70-
throw new HttpRequestException("Error calling OpenAi API to get list of engines. HTTP status code: " + response.StatusCode.ToString() + ". Content: " + resultAsString);
74+
throw new HttpRequestException("Error calling OpenAi API to get list of models. HTTP status code: " + response.StatusCode.ToString() + ". Content: " + resultAsString);
7175
}
7276
}
7377

7478
/// <summary>
75-
/// Get details about a particular Engine from the API, specifically properties such as <see cref="Engine.Owner"/> and <see cref="Engine.Ready"/>
79+
/// Get details about a particular Model from the API, specifically properties such as <see cref="Model.Owner"/> and <see cref="Model.Ready"/>
7680
/// </summary>
77-
/// <param name="id">The id/name of the engine to get more details about</param>
81+
/// <param name="id">The id/name of the model to get more details about</param>
7882
/// <param name="auth">API authentication in order to call the API endpoint. If not specified, attempts to use a default.</param>
79-
/// <returns>Asynchronously returns the <see cref="Engine"/> with all available properties</returns>
80-
public static async Task<Engine> RetrieveEngineDetailsAsync(string id, APIAuthentication auth = null)
83+
/// <returns>Asynchronously returns the <see cref="Model"/> with all available properties</returns>
84+
public static async Task<Model> RetrieveModelDetailsAsync(string id, APIAuthentication auth = null)
8185
{
8286
if (auth.ThisOrDefault()?.ApiKey is null)
8387
{
@@ -87,17 +91,18 @@ public static async Task<Engine> RetrieveEngineDetailsAsync(string id, APIAuthen
8791
HttpClient client = new HttpClient();
8892
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", auth.ThisOrDefault().ApiKey);
8993
client.DefaultRequestHeaders.Add("User-Agent", "okgodoit/dotnet_openai_api");
94+
if (!string.IsNullOrEmpty(auth.ThisOrDefault().OpenAIOrganization)) client.DefaultRequestHeaders.Add("OpenAI-Organization", auth.ThisOrDefault().OpenAIOrganization);
9095

91-
var response = await client.GetAsync(@"https://api.openai.com/v1/engines/" + id);
96+
var response = await client.GetAsync(@"https://api.openai.com/v1/models/" + id);
9297
if (response.IsSuccessStatusCode)
9398
{
9499
string resultAsString = await response.Content.ReadAsStringAsync();
95-
var engine = JsonConvert.DeserializeObject<Engine>(resultAsString);
96-
return engine;
100+
var model = JsonConvert.DeserializeObject<Model>(resultAsString);
101+
return model;
97102
}
98103
else
99104
{
100-
throw new HttpRequestException("Error calling OpenAi API to get engine details. HTTP status code: " + response.StatusCode.ToString());
105+
throw new HttpRequestException("Error calling OpenAi API to get model details. HTTP status code: " + response.StatusCode.ToString());
101106
}
102107
}
103108

@@ -107,7 +112,7 @@ public static async Task<Engine> RetrieveEngineDetailsAsync(string id, APIAuthen
107112
private class JsonHelperRoot
108113
{
109114
[JsonProperty("data")]
110-
public List<Engine> data { get; set; }
115+
public List<Model> data { get; set; }
111116
[JsonProperty("object")]
112117
public string obj { get; set; }
113118

0 commit comments

Comments
 (0)