Skip to content

Commit

Permalink
merged in changes from master
Browse files Browse the repository at this point in the history
  • Loading branch information
OkGoDoIt committed Feb 3, 2023
2 parents 864263d + f3c2b75 commit 030891a
Show file tree
Hide file tree
Showing 23 changed files with 2,109 additions and 1,051 deletions.
4 changes: 2 additions & 2 deletions OpenAI_API.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30309.148
# Visual Studio Version 17
VisualStudioVersion = 17.2.32616.157
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenAI_API", "OpenAI_API\OpenAI_API.csproj", "{99C80D3E-3F0F-4ACC-900D-7AAE6230A780}"
EndProject
Expand Down
73 changes: 34 additions & 39 deletions OpenAI_API/APIAuthentication.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;

namespace OpenAI_API
{
Expand All @@ -17,10 +12,10 @@ public class APIAuthentication
/// The API key, required to access the API endpoint.
/// </summary>
public string ApiKey { get; set; }
/// <summary>
/// The Organization ID to count API requests against. This can be found at https://beta.openai.com/account/org-settings.
/// </summary>
public string OpenAIOrganization { get; set; }
/// <summary>
/// The Organization ID to count API requests against. This can be found at https://beta.openai.com/account/org-settings.
/// </summary>
public string OpenAIOrganization { get; set; }

/// <summary>
/// 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"/>
Expand All @@ -41,18 +36,18 @@ public APIAuthentication(string apiKey)
}


/// <summary>
/// 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.
/// </summary>
/// <param name="apiKey">The API key, required to access the API endpoint.</param>
/// <param name="openAIOrganization">The Organization ID to count API requests against. This can be found at https://beta.openai.com/account/org-settings.</param>
public APIAuthentication(string apiKey, string openAIOrganization)
{
this.ApiKey = apiKey;
/// <summary>
/// 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.
/// </summary>
/// <param name="apiKey">The API key, required to access the API endpoint.</param>
/// <param name="openAIOrganization">The Organization ID to count API requests against. This can be found at https://beta.openai.com/account/org-settings.</param>
public APIAuthentication(string apiKey, string openAIOrganization)
{
this.ApiKey = apiKey;
this.OpenAIOrganization = openAIOrganization;
}
}

private static APIAuthentication cachedDefault = null;
private static APIAuthentication cachedDefault = null;

/// <summary>
/// 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)"/>
Expand All @@ -79,25 +74,25 @@ public static APIAuthentication Default
}
}

/// <summary>
/// Attempts to load api key from environment variables, as "OPENAI_KEY" or "OPENAI_API_KEY". Also loads org if from "OPENAI_ORGANIZATION" if present.
/// </summary>
/// <returns>Returns the loaded <see cref="APIAuthentication"/> any api keys were found, or <see langword="null"/> if there were no matching environment vars.</returns>
public static APIAuthentication LoadFromEnv()
/// <summary>
/// Attempts to load api key from environment variables, as "OPENAI_KEY" or "OPENAI_API_KEY". Also loads org if from "OPENAI_ORGANIZATION" if present.
/// </summary>
/// <returns>Returns the loaded <see cref="APIAuthentication"/> any api keys were found, or <see langword="null"/> if there were no matching environment vars.</returns>
public static APIAuthentication LoadFromEnv()
{
string key = Environment.GetEnvironmentVariable("OPENAI_KEY");
string key = Environment.GetEnvironmentVariable("OPENAI_KEY");

if (string.IsNullOrEmpty(key))
{
key = Environment.GetEnvironmentVariable("OPENAI_API_KEY");
key = Environment.GetEnvironmentVariable("OPENAI_API_KEY");

if (string.IsNullOrEmpty(key))
return null;
return null;
}

string org = Environment.GetEnvironmentVariable("OPENAI_ORGANIZATION");
string org = Environment.GetEnvironmentVariable("OPENAI_ORGANIZATION");

return new APIAuthentication(key, org);
return new APIAuthentication(key, org);
}

/// <summary>
Expand Down Expand Up @@ -128,16 +123,16 @@ public static APIAuthentication LoadFromPath(string directory = null, string fil
{
switch (parts[0].ToUpper())
{
case "OPENAI_KEY":
key = parts[1].Trim();
break;
case "OPENAI_API_KEY":
key = parts[1].Trim();
break;
case "OPENAI_ORGANIZATION":
org = parts[1].Trim();
break;
default:
case "OPENAI_KEY":
key = parts[1].Trim();
break;
case "OPENAI_API_KEY":
key = parts[1].Trim();
break;
case "OPENAI_ORGANIZATION":
org = parts[1].Trim();
break;
default:
break;
}
}
Expand Down
58 changes: 58 additions & 0 deletions OpenAI_API/ApiResultBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Newtonsoft.Json;
using System;

namespace OpenAI_API
{
/// <summary>
/// Represents a result from calling the OpenAI API, with all the common metadata returned from every endpoint
/// </summary>
abstract public class ApiResultBase
{

/// The time when the result was generated
[JsonIgnore]
public DateTime Created => DateTimeOffset.FromUnixTimeSeconds(CreatedUnixTime).DateTime;

/// <summary>
/// The time when the result was generated in unix epoch format
/// </summary>
[JsonProperty("created")]
public int CreatedUnixTime { get; set; }

/// <summary>
/// Which model was used to generate this result.
/// </summary>
[JsonProperty("model")]
public Model Model { get; set; }

/// <summary>
/// Object type, ie: text_completion, file, fine-tune, list, etc
/// </summary>
[JsonProperty("object")]
public string Object { get; set; }

/// <summary>
/// The organization associated with the API request, as reported by the API.
/// </summary>
[JsonIgnore]
public string Organization { get; internal set; }

/// <summary>
/// The server-side processing time as reported by the API. This can be useful for debugging where a delay occurs.
/// </summary>
[JsonIgnore]
public TimeSpan ProcessingTime { get; internal set; }

/// <summary>
/// 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.
/// </summary>
[JsonIgnore]
public string RequestId { get; internal set; }

/// <summary>
/// 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.
/// </summary>
[JsonIgnore]
public string OpenaiVersion { get; internal set; }
}
}
Loading

0 comments on commit 030891a

Please sign in to comment.