-
Notifications
You must be signed in to change notification settings - Fork 431
/
Copy pathOpenAIAPI.cs
111 lines (97 loc) · 4.93 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
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.Net.Http;
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>
/// Optionally provide an IHttpClientFactory to create the client to send requests.
/// </summary>
public IHttpClientFactory HttpClientFactory { 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);
ImageEdit= new ImageEditEndpoint(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>
/// 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 ICompletionEndpoint 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 IEmbeddingEndpoint Embeddings { get; }
/// <summary>
/// Text generation in the form of chat messages. This interacts with the ChatGPT API.
/// </summary>
public IChatEndpoint Chat { get; }
/// <summary>
/// Classify text against the OpenAI Content Policy.
/// </summary>
public IModerationEndpoint Moderation { get; }
/// <summary>
/// The API endpoint for querying available Engines/models
/// </summary>
public IModelsEndpoint 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 IFilesEndpoint Files { get; }
/// <summary>
/// The API lets you do operations with images. Given a prompt and/or an input image, the model will generate a new image.
/// </summary>
public IImageGenerationEndpoint ImageGenerations { get; }
/// <summary>
/// The API lets you do operations with images. Given a prompt and an input image, the model will edit a new image.
/// </summary>
public IImageEditEndpoint ImageEdit { get; }
}
}