Skip to content

Commit 7d8f04d

Browse files
authored
Merge branch 'master' into unused-usings-add-properties
2 parents 63befd4 + b49a217 commit 7d8f04d

23 files changed

+2023
-1072
lines changed

OpenAI_API.sln

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.30309.148
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.2.32616.157
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenAI_API", "OpenAI_API\OpenAI_API.csproj", "{99C80D3E-3F0F-4ACC-900D-7AAE6230A780}"
77
EndProject

OpenAI_API/APIAuthentication.cs

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Dynamic;
42
using System.IO;
5-
using System.Linq;
6-
using System.Runtime.CompilerServices;
7-
using System.Text;
83

94
namespace OpenAI_API
105
{
@@ -17,10 +12,10 @@ public class APIAuthentication
1712
/// The API key, required to access the API endpoint.
1813
/// </summary>
1914
public string ApiKey { get; set; }
20-
/// <summary>
21-
/// The Organization ID to count API requests against. This can be found at https://beta.openai.com/account/org-settings.
22-
/// </summary>
23-
public string OpenAIOrganization { get; set; }
15+
/// <summary>
16+
/// The Organization ID to count API requests against. This can be found at https://beta.openai.com/account/org-settings.
17+
/// </summary>
18+
public string OpenAIOrganization { get; set; }
2419

2520
/// <summary>
2621
/// Allows implicit casting from a string, so that a simple string API key can be provided in place of an instance of <see cref="APIAuthentication"/>
@@ -41,18 +36,18 @@ public APIAuthentication(string apiKey)
4136
}
4237

4338

44-
/// <summary>
45-
/// Instantiates a new Authentication object with the given <paramref name="apiKey"/>, which may be <see langword="null"/>. For users who belong to multiple organizations, you can specify which organization is used. Usage from these API requests will count against the specified organization's subscription quota.
46-
/// </summary>
47-
/// <param name="apiKey">The API key, required to access the API endpoint.</param>
48-
/// <param name="openAIOrganization">The Organization ID to count API requests against. This can be found at https://beta.openai.com/account/org-settings.</param>
49-
public APIAuthentication(string apiKey, string openAIOrganization)
50-
{
51-
this.ApiKey = apiKey;
39+
/// <summary>
40+
/// Instantiates a new Authentication object with the given <paramref name="apiKey"/>, which may be <see langword="null"/>. For users who belong to multiple organizations, you can specify which organization is used. Usage from these API requests will count against the specified organization's subscription quota.
41+
/// </summary>
42+
/// <param name="apiKey">The API key, required to access the API endpoint.</param>
43+
/// <param name="openAIOrganization">The Organization ID to count API requests against. This can be found at https://beta.openai.com/account/org-settings.</param>
44+
public APIAuthentication(string apiKey, string openAIOrganization)
45+
{
46+
this.ApiKey = apiKey;
5247
this.OpenAIOrganization = openAIOrganization;
53-
}
48+
}
5449

55-
private static APIAuthentication cachedDefault = null;
50+
private static APIAuthentication cachedDefault = null;
5651

5752
/// <summary>
5853
/// The default authentication to use when no other auth is specified. This can be set manually, or automatically loaded via environment variables or a config file. <seealso cref="LoadFromEnv"/><seealso cref="LoadFromPath(string, string, bool)"/>
@@ -79,25 +74,25 @@ public static APIAuthentication Default
7974
}
8075
}
8176

82-
/// <summary>
83-
/// Attempts to load api key from environment variables, as "OPENAI_KEY" or "OPENAI_API_KEY". Also loads org if from "OPENAI_ORGANIZATION" if present.
84-
/// </summary>
85-
/// <returns>Returns the loaded <see cref="APIAuthentication"/> any api keys were found, or <see langword="null"/> if there were no matching environment vars.</returns>
86-
public static APIAuthentication LoadFromEnv()
77+
/// <summary>
78+
/// Attempts to load api key from environment variables, as "OPENAI_KEY" or "OPENAI_API_KEY". Also loads org if from "OPENAI_ORGANIZATION" if present.
79+
/// </summary>
80+
/// <returns>Returns the loaded <see cref="APIAuthentication"/> any api keys were found, or <see langword="null"/> if there were no matching environment vars.</returns>
81+
public static APIAuthentication LoadFromEnv()
8782
{
88-
string key = Environment.GetEnvironmentVariable("OPENAI_KEY");
83+
string key = Environment.GetEnvironmentVariable("OPENAI_KEY");
8984

9085
if (string.IsNullOrEmpty(key))
9186
{
92-
key = Environment.GetEnvironmentVariable("OPENAI_API_KEY");
87+
key = Environment.GetEnvironmentVariable("OPENAI_API_KEY");
9388

9489
if (string.IsNullOrEmpty(key))
95-
return null;
90+
return null;
9691
}
9792

98-
string org = Environment.GetEnvironmentVariable("OPENAI_ORGANIZATION");
93+
string org = Environment.GetEnvironmentVariable("OPENAI_ORGANIZATION");
9994

100-
return new APIAuthentication(key, org);
95+
return new APIAuthentication(key, org);
10196
}
10297

10398
/// <summary>
@@ -128,16 +123,16 @@ public static APIAuthentication LoadFromPath(string directory = null, string fil
128123
{
129124
switch (parts[0].ToUpper())
130125
{
131-
case "OPENAI_KEY":
132-
key = parts[1].Trim();
133-
break;
134-
case "OPENAI_API_KEY":
135-
key = parts[1].Trim();
136-
break;
137-
case "OPENAI_ORGANIZATION":
138-
org = parts[1].Trim();
139-
break;
140-
default:
126+
case "OPENAI_KEY":
127+
key = parts[1].Trim();
128+
break;
129+
case "OPENAI_API_KEY":
130+
key = parts[1].Trim();
131+
break;
132+
case "OPENAI_ORGANIZATION":
133+
org = parts[1].Trim();
134+
break;
135+
default:
141136
break;
142137
}
143138
}

OpenAI_API/ApiResultBase.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
4+
namespace OpenAI_API
5+
{
6+
/// <summary>
7+
/// Represents a result from calling the OpenAI API, with all the common metadata returned from every endpoint
8+
/// </summary>
9+
abstract public class ApiResultBase
10+
{
11+
12+
/// The time when the result was generated
13+
[JsonIgnore]
14+
public DateTime Created => DateTimeOffset.FromUnixTimeSeconds(CreatedUnixTime).DateTime;
15+
16+
/// <summary>
17+
/// The time when the result was generated in unix epoch format
18+
/// </summary>
19+
[JsonProperty("created")]
20+
public int CreatedUnixTime { get; set; }
21+
22+
/// <summary>
23+
/// Which model was used to generate this result.
24+
/// </summary>
25+
[JsonProperty("model")]
26+
public Model Model { get; set; }
27+
28+
/// <summary>
29+
/// Object type, ie: text_completion, file, fine-tune, list, etc
30+
/// </summary>
31+
[JsonProperty("object")]
32+
public string Object { get; set; }
33+
34+
/// <summary>
35+
/// The organization associated with the API request, as reported by the API.
36+
/// </summary>
37+
[JsonIgnore]
38+
public string Organization { get; internal set; }
39+
40+
/// <summary>
41+
/// The server-side processing time as reported by the API. This can be useful for debugging where a delay occurs.
42+
/// </summary>
43+
[JsonIgnore]
44+
public TimeSpan ProcessingTime { get; internal set; }
45+
46+
/// <summary>
47+
/// The request id of this API call, as reported in the response headers. This may be useful for troubleshooting or when contacting OpenAI support in reference to a specific request.
48+
/// </summary>
49+
[JsonIgnore]
50+
public string RequestId { get; internal set; }
51+
52+
/// <summary>
53+
/// The Openai-Version used to generate this response, as reported in the response headers. This may be useful for troubleshooting or when contacting OpenAI support in reference to a specific request.
54+
/// </summary>
55+
[JsonIgnore]
56+
public string OpenaiVersion { get; internal set; }
57+
}
58+
}

0 commit comments

Comments
 (0)