-
Notifications
You must be signed in to change notification settings - Fork 431
/
Copy pathOpenAIAPI.cs
135 lines (120 loc) · 6.74 KB
/
OpenAIAPI.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System.Net.Http;
using OpenAI_API.Chat;
using OpenAI_API.Completions;
using OpenAI_API.Embedding;
using OpenAI_API.Files;
using OpenAI_API.Images;
using OpenAI_API.Models;
using OpenAI_API.Moderation;
using System.Xml.Linq;
namespace OpenAI_API
{
/// <summary>
/// Entry point to the OpenAPI API, handling auth and allowing access to the various API endpoints
/// </summary>
public class OpenAIAPI : IOpenAIAPI
{
/// <summary>
/// Base url for OpenAI
/// for OpenAI, should be "https://api.openai.com/{0}/{1}"
/// for Azure, should be "https://(your-resource-name.openai.azure.com/openai/deployments/(deployment-id)/{1}?api-version={0}"
/// </summary>
public string ApiUrlFormat { get; set; } = "https://api.openai.com/{0}/{1}";
/// <summary>
/// Version of the Rest Api
/// </summary>
public string ApiVersion { get; set; } = "v1";
/// <summary>
/// The API authentication information to use for API calls
/// </summary>
public APIAuthentication Auth { get; set; }
/// <summary>
/// Creates a new entry point to the OpenAPI API, handling auth and allowing access to the various API endpoints
/// </summary>
/// <param name="apiKeys">The API authentication information to use for API calls, or <see langword="null"/> to attempt to use the <see cref="APIAuthentication.Default"/>, potentially loading from environment vars or from a config file.</param>
public OpenAIAPI(APIAuthentication apiKeys = null)
{
this.Auth = apiKeys.ThisOrDefault();
Completions = new CompletionEndpoint(this);
Models = new ModelsEndpoint(this);
Files = new FilesEndpoint(this);
Embeddings = new EmbeddingEndpoint(this);
Chat = new ChatEndpoint(this);
Moderation = new ModerationEndpoint(this);
ImageGenerations = new ImageGenerationEndpoint(this);
}
/// <summary>
/// Creates a new entry point to the OpenAPI API, handling auth and allowing access to the various API endpoints
/// </summary>
/// <param name="httpClient">The HttpClient to use while calling OpenAI's API. By injecting it, you can customize its behavior.</param>
/// <param name="apiKeys">The API authentication information to use for API calls, or <see langword="null"/> to attempt to use the <see cref="APIAuthentication.Default"/>, potentially loading from environment vars or from a config file.</param>
public OpenAIAPI(HttpClient httpClient, APIAuthentication apiKeys = null)
{
this.Auth = apiKeys.ThisOrDefault();
Completions = new CompletionEndpoint(httpClient, this);
Models = new ModelsEndpoint(httpClient, this);
Files = new FilesEndpoint(httpClient, this);
Embeddings = new EmbeddingEndpoint(httpClient,this);
Chat = new ChatEndpoint(httpClient, this);
Moderation = new ModerationEndpoint(httpClient, this);
ImageGenerations = new ImageGenerationEndpoint(httpClient, this);
}
/// <summary>
/// Instantiates a version of the API for connecting to the Azure OpenAI endpoint instead of the main OpenAI endpoint.
/// </summary>
/// <param name="YourResourceName">The name of your Azure OpenAI Resource</param>
/// <param name="deploymentId">The name of your model deployment. You're required to first deploy a model before you can make calls.</param>
/// <param name="apiKey">The API authentication information to use for API calls, or <see langword="null"/> to attempt to use the <see cref="APIAuthentication.Default"/>, potentially loading from environment vars or from a config file. Currently this library only supports the api-key flow, not the AD-Flow.</param>
/// <returns></returns>
public static OpenAIAPI ForAzure(string YourResourceName, string deploymentId, APIAuthentication apiKey = null)
{
OpenAIAPI api = new OpenAIAPI(apiKey);
api.ApiVersion = "2022-12-01";
api.ApiUrlFormat = $"https://{YourResourceName}.openai.azure.com/openai/deployments/{deploymentId}/" + "{1}?api-version={0}";
return api;
}
/// <summary>
/// Instantiates a version of the API for connecting to the Azure OpenAI endpoint instead of the main OpenAI endpoint.
/// </summary>
/// <param name="httpClient">The HttpClient to use while calling OpenAI's API. By injecting it, you can customize its behavior.</param>
/// <param name="YourResourceName">The name of your Azure OpenAI Resource</param>
/// <param name="deploymentId">The name of your model deployment. You're required to first deploy a model before you can make calls.</param>
/// <param name="apiKey">The API authentication information to use for API calls, or <see langword="null"/> to attempt to use the <see cref="APIAuthentication.Default"/>, potentially loading from environment vars or from a config file. Currently this library only supports the api-key flow, not the AD-Flow.</param>
/// <returns></returns>
public static OpenAIAPI ForAzure(HttpClient httpClient, string YourResourceName, string deploymentId, APIAuthentication apiKey = null)
{
OpenAIAPI api = new OpenAIAPI(httpClient, apiKey);
api.ApiVersion = "2022-12-01";
api.ApiUrlFormat = $"https://{YourResourceName}.openai.azure.com/openai/deployments/{deploymentId}/" + "{1}?api-version={0}";
return api;
}
/// <summary>
/// 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).
/// </summary>
public CompletionEndpoint Completions { get; }
/// <summary>
/// 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.
/// </summary>
public EmbeddingEndpoint Embeddings { get; }
/// <summary>
/// Text generation in the form of chat messages. This interacts with the ChatGPT API.
/// </summary>
public ChatEndpoint Chat { get; }
/// <summary>
/// Classify text against the OpenAI Content Policy.
/// </summary>
public ModerationEndpoint Moderation { get; }
/// <summary>
/// The API endpoint for querying available Engines/models
/// </summary>
public ModelsEndpoint Models { get; }
/// <summary>
/// The API lets you do operations with files. You can upload, delete or retrieve files. Files can be used for fine-tuning, search, etc.
/// </summary>
public FilesEndpoint Files { get; }
/// <summary>
/// The API lets you do operations with images. You can Given a prompt and/or an input image, the model will generate a new image.
/// </summary>
public ImageGenerationEndpoint ImageGenerations { get; }
}
}